|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stevenmaguire\OAuth2\Client\Tool; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
6
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Psr\Http\Message\RequestInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
|
|
11
|
|
|
trait ProviderRedirectTrait |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Maximum number of times to follow provider initiated redirects |
|
15
|
|
|
* |
|
16
|
|
|
* @var integer |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $redirectLimit = 2; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Retrieves current redirect limit. |
|
22
|
|
|
* |
|
23
|
|
|
* @return integer |
|
24
|
|
|
*/ |
|
25
|
4 |
|
public function getRedirectLimit() |
|
26
|
|
|
{ |
|
27
|
4 |
|
return $this->redirectLimit; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Sends a request instance and returns a response instance. |
|
32
|
|
|
* |
|
33
|
|
|
* @param RequestInterface $request |
|
34
|
|
|
* @return ResponseInterface |
|
35
|
|
|
*/ |
|
36
|
8 |
|
protected function sendRequest(RequestInterface $request) |
|
37
|
|
|
{ |
|
38
|
8 |
|
$attempts = 0; |
|
39
|
|
|
|
|
40
|
|
|
$requestOptions = [ |
|
41
|
|
|
'allow_redirects' => false |
|
42
|
8 |
|
]; |
|
43
|
|
|
|
|
44
|
8 |
|
$isRedirect = function (ResponseInterface $response) { |
|
45
|
6 |
|
$statusCode = $response->getStatusCode(); |
|
46
|
|
|
|
|
47
|
6 |
|
return $statusCode > 300 && $statusCode < 400 && $response->hasHeader('Location'); |
|
48
|
8 |
|
}; |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
8 |
|
while ($attempts < $this->redirectLimit) { |
|
52
|
8 |
|
$attempts++; |
|
53
|
8 |
|
$response = $this->getHttpClient()->send($request, $requestOptions); |
|
|
|
|
|
|
54
|
6 |
|
$statusCode = $response->getStatusCode(); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
6 |
|
if ($isRedirect($response)) { |
|
57
|
2 |
|
$redirectUrl = new Uri($response->getHeader('Location')[0]); |
|
58
|
2 |
|
$request = $request->withUri($redirectUrl); |
|
59
|
2 |
|
} else { |
|
60
|
4 |
|
break; |
|
61
|
|
|
} |
|
62
|
2 |
|
} |
|
63
|
8 |
|
} catch (BadResponseException $e) { |
|
64
|
2 |
|
$response = $e->getResponse(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
8 |
|
return $response; |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Updates the redirect limit. |
|
72
|
|
|
* |
|
73
|
|
|
* @param integer $limit |
|
74
|
|
|
* @return League\OAuth2\Client\Provider\AbstractProvider |
|
75
|
|
|
* @throws InvalidArgumentException |
|
76
|
|
|
*/ |
|
77
|
10 |
|
public function setRedirectLimit($limit) |
|
78
|
|
|
{ |
|
79
|
10 |
|
if (!is_numeric($limit)) { |
|
80
|
4 |
|
throw new InvalidArgumentException('setRedirectLimit function only accepts numeric values.'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
6 |
|
$this->redirectLimit = $limit; |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
6 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.