1 | <?php |
||
15 | class OAuthMiddleware |
||
16 | { |
||
17 | /** |
||
18 | * @var AccessToken|null |
||
19 | */ |
||
20 | protected $accessToken; |
||
21 | |||
22 | /** |
||
23 | * @var GrantTypeInterface |
||
24 | */ |
||
25 | protected $grantType; |
||
26 | |||
27 | /** |
||
28 | * @var RefreshTokenGrantTypeInterface |
||
29 | */ |
||
30 | protected $refreshTokenGrantType; |
||
31 | |||
32 | /** |
||
33 | * @var ClientInterface |
||
34 | */ |
||
35 | protected $client; |
||
36 | |||
37 | /** |
||
38 | * Create a new Oauth2 subscriber. |
||
39 | * |
||
40 | * @param ClientInterface $client |
||
41 | * @param GrantTypeInterface $grantType |
||
42 | * @param RefreshTokenGrantTypeInterface $refreshTokenGrantType |
||
43 | */ |
||
44 | 10 | public function __construct( |
|
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 6 | public function onBefore() |
|
77 | |||
78 | 6 | public function onFailure($limit) |
|
79 | { |
||
80 | 6 | $calls = 0; |
|
81 | |||
82 | return function (callable $handler) use (&$calls, $limit) { |
||
83 | return function (RequestInterface $request, array $options) use ($handler, &$calls, $limit) { |
||
84 | /* @var PromiseInterface */ |
||
85 | 6 | $promise = $handler($request, $options); |
|
86 | |||
87 | 6 | return $promise->then( |
|
88 | 6 | function (ResponseInterface $response) use ($request, $options, &$calls, $limit) { |
|
89 | if ( |
||
90 | 6 | $response->getStatusCode() == 401 && |
|
91 | 6 | isset($options['auth']) && |
|
92 | 6 | 'oauth2' == $options['auth'] && |
|
93 | 6 | $this->grantType instanceof GrantTypeInterface && |
|
94 | 3 | $this->grantType->getConfigByName(GrantTypeBase::CONFIG_TOKEN_URL) != $request->getUri()->getPath() |
|
95 | 6 | ) { |
|
96 | 3 | ++$calls; |
|
97 | 3 | if ($calls > $limit) { |
|
98 | 1 | return $response; |
|
99 | } |
||
100 | |||
101 | 3 | if ($token = $this->getAccessToken()) { |
|
102 | 3 | $response = $this->client->send($request->withHeader('Authorization', 'Bearer '.$token->getToken()), $options); |
|
103 | 3 | } |
|
104 | 3 | } |
|
105 | |||
106 | 6 | return $response; |
|
107 | } |
||
108 | 6 | ); |
|
109 | 6 | }; |
|
110 | 6 | }; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get a new access token. |
||
115 | * |
||
116 | * @return AccessToken|null |
||
117 | */ |
||
118 | 4 | protected function acquireAccessToken() |
|
135 | |||
136 | /** |
||
137 | * Get the access token. |
||
138 | * |
||
139 | * @return AccessToken|null Oauth2 access token |
||
140 | */ |
||
141 | 6 | public function getAccessToken() |
|
142 | { |
||
143 | 6 | if (!($this->accessToken instanceof AccessToken) || $this->accessToken->isExpired()) { |
|
144 | 4 | $this->acquireAccessToken(); |
|
145 | 4 | } |
|
146 | |||
147 | 6 | return $this->accessToken; |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Get the refresh token. |
||
152 | * |
||
153 | * @return AccessToken|null |
||
154 | */ |
||
155 | 4 | public function getRefreshToken() |
|
163 | |||
164 | /** |
||
165 | * Set the access token. |
||
166 | * |
||
167 | * @param AccessToken|string $accessToken |
||
168 | * @param string $type |
||
169 | * @param int $expires |
||
170 | * |
||
171 | * @return self |
||
172 | */ |
||
173 | 7 | public function setAccessToken($accessToken, $type = null, $expires = null) |
|
192 | |||
193 | /** |
||
194 | * Set the refresh token. |
||
195 | * |
||
196 | * @param AccessToken|string $refreshToken The refresh token |
||
197 | * |
||
198 | * @return self |
||
199 | */ |
||
200 | 3 | public function setRefreshToken($refreshToken) |
|
220 | } |
||
221 |