@@ -15,7 +15,7 @@ |
||
15 | 15 | * |
16 | 16 | * @link https://tools.ietf.org/html/rfc6749#section-10.12 |
17 | 17 | */ |
18 | -interface CSRFToken{ |
|
18 | +interface CSRFToken { |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * Checks whether the CSRF state was set and verifies against the last known state. |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | * Implements an abstract OAuth2 provider with all methods required by the OAuth2Interface. |
28 | 28 | * It also implements the ClientCredentials, CSRFToken and TokenRefresh interfaces in favor over traits. |
29 | 29 | */ |
30 | -abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{ |
|
30 | +abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface { |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * Specifies the authentication method: |
@@ -78,11 +78,11 @@ discard block |
||
78 | 78 | 'type' => 'web_server', |
79 | 79 | ]); |
80 | 80 | |
81 | - if(!empty($scopes)){ |
|
81 | + if (!empty($scopes)) { |
|
82 | 82 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
83 | 83 | } |
84 | 84 | |
85 | - if($this instanceof CSRFToken){ |
|
85 | + if ($this instanceof CSRFToken) { |
|
86 | 86 | $params = $this->setState($params); |
87 | 87 | } |
88 | 88 | |
@@ -102,19 +102,19 @@ discard block |
||
102 | 102 | protected function parseTokenResponse(ResponseInterface $response):AccessToken{ |
103 | 103 | $data = json_decode(decompress_content($response), true); // silly amazon... |
104 | 104 | |
105 | - if(!is_array($data)){ |
|
105 | + if (!is_array($data)) { |
|
106 | 106 | throw new ProviderException('unable to parse token response'); |
107 | 107 | } |
108 | 108 | |
109 | - foreach(['error_description', 'error'] as $field){ |
|
109 | + foreach (['error_description', 'error'] as $field) { |
|
110 | 110 | |
111 | - if(isset($data[$field])){ |
|
111 | + if (isset($data[$field])) { |
|
112 | 112 | throw new ProviderException('error retrieving access token: "'.$data[$field].'"'); |
113 | 113 | } |
114 | 114 | |
115 | 115 | } |
116 | 116 | |
117 | - if(!isset($data['access_token'])){ |
|
117 | + if (!isset($data['access_token'])) { |
|
118 | 118 | throw new ProviderException('token missing'); |
119 | 119 | } |
120 | 120 | |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | */ |
138 | 138 | public function getAccessToken(string $code, string $state = null):AccessToken{ |
139 | 139 | |
140 | - if($this instanceof CSRFToken){ |
|
140 | + if ($this instanceof CSRFToken) { |
|
141 | 141 | $this->checkState($state); |
142 | 142 | } |
143 | 143 | |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | ->withHeader('Accept-Encoding', 'identity') |
156 | 156 | ->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738))); |
157 | 157 | |
158 | - foreach($this->authHeaders as $header => $value){ |
|
158 | + foreach ($this->authHeaders as $header => $value) { |
|
159 | 159 | $request = $request->withHeader($header, $value); |
160 | 160 | } |
161 | 161 | |
@@ -171,11 +171,11 @@ discard block |
||
171 | 171 | */ |
172 | 172 | public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{ |
173 | 173 | |
174 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){ |
|
174 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER) { |
|
175 | 175 | return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken); |
176 | 176 | } |
177 | 177 | |
178 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){ |
|
178 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY) { |
|
179 | 179 | $uri = merge_query((string)$request->getUri(), [$this->authMethodQuery => $token->accessToken]); |
180 | 180 | |
181 | 181 | return $request->withUri($this->uriFactory->createUri($uri)); |
@@ -196,13 +196,13 @@ discard block |
||
196 | 196 | */ |
197 | 197 | public function getClientCredentialsToken(array $scopes = null):AccessToken{ |
198 | 198 | |
199 | - if(!$this instanceof ClientCredentials){ |
|
199 | + if (!$this instanceof ClientCredentials) { |
|
200 | 200 | throw new ProviderException('client credentials token not supported'); |
201 | 201 | } |
202 | 202 | |
203 | 203 | $params = ['grant_type' => 'client_credentials']; |
204 | 204 | |
205 | - if($scopes !== null){ |
|
205 | + if ($scopes !== null) { |
|
206 | 206 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
207 | 207 | } |
208 | 208 | |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | ->withBody($this->streamFactory->createStream(http_build_query($params, '', '&', PHP_QUERY_RFC1738))) |
215 | 215 | ; |
216 | 216 | |
217 | - foreach($this->authHeaders as $header => $value){ |
|
217 | + foreach ($this->authHeaders as $header => $value) { |
|
218 | 218 | $request = $request->withAddedHeader($header, $value); |
219 | 219 | } |
220 | 220 | |
@@ -237,17 +237,17 @@ discard block |
||
237 | 237 | */ |
238 | 238 | public function refreshAccessToken(AccessToken $token = null):AccessToken{ |
239 | 239 | |
240 | - if(!$this instanceof TokenRefresh){ |
|
240 | + if (!$this instanceof TokenRefresh) { |
|
241 | 241 | throw new ProviderException('token refresh not supported'); |
242 | 242 | } |
243 | 243 | |
244 | - if($token === null){ |
|
244 | + if ($token === null) { |
|
245 | 245 | $token = $this->storage->getAccessToken($this->serviceName); |
246 | 246 | } |
247 | 247 | |
248 | 248 | $refreshToken = $token->refreshToken; |
249 | 249 | |
250 | - if(empty($refreshToken)){ |
|
250 | + if (empty($refreshToken)) { |
|
251 | 251 | throw new ProviderException( |
252 | 252 | sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires)) |
253 | 253 | ); |
@@ -268,13 +268,13 @@ discard block |
||
268 | 268 | ->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738))) |
269 | 269 | ; |
270 | 270 | |
271 | - foreach($this->authHeaders as $header => $value){ |
|
271 | + foreach ($this->authHeaders as $header => $value) { |
|
272 | 272 | $request = $request->withAddedHeader($header, $value); |
273 | 273 | } |
274 | 274 | |
275 | 275 | $newToken = $this->parseTokenResponse($this->http->sendRequest($request)); |
276 | 276 | |
277 | - if(empty($newToken->refreshToken)){ |
|
277 | + if (empty($newToken->refreshToken)) { |
|
278 | 278 | $newToken->refreshToken = $refreshToken; |
279 | 279 | } |
280 | 280 | |
@@ -297,17 +297,17 @@ discard block |
||
297 | 297 | */ |
298 | 298 | public function checkState(string $state = null):void{ |
299 | 299 | |
300 | - if(!$this instanceof CSRFToken){ |
|
300 | + if (!$this instanceof CSRFToken) { |
|
301 | 301 | throw new ProviderException('CSRF protection not supported'); |
302 | 302 | } |
303 | 303 | |
304 | - if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){ |
|
304 | + if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) { |
|
305 | 305 | throw new ProviderException('invalid state for '.$this->serviceName); |
306 | 306 | } |
307 | 307 | |
308 | 308 | $knownState = $this->storage->getCSRFState($this->serviceName); |
309 | 309 | |
310 | - if(!hash_equals($knownState, $state)){ |
|
310 | + if (!hash_equals($knownState, $state)) { |
|
311 | 311 | throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state); |
312 | 312 | } |
313 | 313 | |
@@ -327,11 +327,11 @@ discard block |
||
327 | 327 | */ |
328 | 328 | public function setState(array $params):array{ |
329 | 329 | |
330 | - if(!$this instanceof CSRFToken){ |
|
330 | + if (!$this instanceof CSRFToken) { |
|
331 | 331 | throw new ProviderException('CSRF protection not supported'); |
332 | 332 | } |
333 | 333 | |
334 | - if(!isset($params['state'])){ |
|
334 | + if (!isset($params['state'])) { |
|
335 | 335 | $params['state'] = sha1(random_bytes(256)); |
336 | 336 | } |
337 | 337 |