@@ -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: |
@@ -84,11 +84,11 @@ discard block |
||
| 84 | 84 | 'type' => 'web_server', |
| 85 | 85 | ]); |
| 86 | 86 | |
| 87 | - if(!empty($scopes)){ |
|
| 87 | + if (!empty($scopes)) { |
|
| 88 | 88 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - if($this instanceof CSRFToken){ |
|
| 91 | + if ($this instanceof CSRFToken) { |
|
| 92 | 92 | $params = $this->setState($params); |
| 93 | 93 | } |
| 94 | 94 | |
@@ -110,19 +110,19 @@ discard block |
||
| 110 | 110 | // silly amazon sends compressed data... |
| 111 | 111 | $data = json_decode(decompress_content($response), true, 512, JSON_THROW_ON_ERROR); |
| 112 | 112 | |
| 113 | - if(!is_array($data)){ |
|
| 113 | + if (!is_array($data)) { |
|
| 114 | 114 | throw new ProviderException('unable to parse token response'); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - foreach(['error_description', 'error'] as $field){ |
|
| 117 | + foreach (['error_description', 'error'] as $field) { |
|
| 118 | 118 | |
| 119 | - if(isset($data[$field])){ |
|
| 119 | + if (isset($data[$field])) { |
|
| 120 | 120 | throw new ProviderException('error retrieving access token: "'.$data[$field].'"'); |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - if(!isset($data['access_token'])){ |
|
| 125 | + if (!isset($data['access_token'])) { |
|
| 126 | 126 | throw new ProviderException('token missing'); |
| 127 | 127 | } |
| 128 | 128 | |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | */ |
| 146 | 146 | public function getAccessToken(string $code, string $state = null):AccessToken{ |
| 147 | 147 | |
| 148 | - if($this instanceof CSRFToken){ |
|
| 148 | + if ($this instanceof CSRFToken) { |
|
| 149 | 149 | $this->checkState($state); |
| 150 | 150 | } |
| 151 | 151 | |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | ->withHeader('Accept-Encoding', 'identity') |
| 164 | 164 | ->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738))); |
| 165 | 165 | |
| 166 | - foreach($this->authHeaders as $header => $value){ |
|
| 166 | + foreach ($this->authHeaders as $header => $value) { |
|
| 167 | 167 | $request = $request->withHeader($header, $value); |
| 168 | 168 | } |
| 169 | 169 | |
@@ -179,11 +179,11 @@ discard block |
||
| 179 | 179 | */ |
| 180 | 180 | public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{ |
| 181 | 181 | |
| 182 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){ |
|
| 182 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER) { |
|
| 183 | 183 | return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken); |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){ |
|
| 186 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY) { |
|
| 187 | 187 | $uri = merge_query((string)$request->getUri(), [$this->authMethodQuery => $token->accessToken]); |
| 188 | 188 | |
| 189 | 189 | return $request->withUri($this->uriFactory->createUri($uri)); |
@@ -204,13 +204,13 @@ discard block |
||
| 204 | 204 | */ |
| 205 | 205 | public function getClientCredentialsToken(array $scopes = null):AccessToken{ |
| 206 | 206 | |
| 207 | - if(!$this instanceof ClientCredentials){ |
|
| 207 | + if (!$this instanceof ClientCredentials) { |
|
| 208 | 208 | throw new ProviderException('client credentials token not supported'); |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | $params = ['grant_type' => 'client_credentials']; |
| 212 | 212 | |
| 213 | - if($scopes !== null){ |
|
| 213 | + if ($scopes !== null) { |
|
| 214 | 214 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
| 215 | 215 | } |
| 216 | 216 | |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | ->withBody($this->streamFactory->createStream(http_build_query($params, '', '&', PHP_QUERY_RFC1738))) |
| 223 | 223 | ; |
| 224 | 224 | |
| 225 | - foreach($this->authHeaders as $header => $value){ |
|
| 225 | + foreach ($this->authHeaders as $header => $value) { |
|
| 226 | 226 | $request = $request->withAddedHeader($header, $value); |
| 227 | 227 | } |
| 228 | 228 | |
@@ -245,17 +245,17 @@ discard block |
||
| 245 | 245 | */ |
| 246 | 246 | public function refreshAccessToken(AccessToken $token = null):AccessToken{ |
| 247 | 247 | |
| 248 | - if(!$this instanceof TokenRefresh){ |
|
| 248 | + if (!$this instanceof TokenRefresh) { |
|
| 249 | 249 | throw new ProviderException('token refresh not supported'); |
| 250 | 250 | } |
| 251 | 251 | |
| 252 | - if($token === null){ |
|
| 252 | + if ($token === null) { |
|
| 253 | 253 | $token = $this->storage->getAccessToken($this->serviceName); |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | $refreshToken = $token->refreshToken; |
| 257 | 257 | |
| 258 | - if(empty($refreshToken)){ |
|
| 258 | + if (empty($refreshToken)) { |
|
| 259 | 259 | throw new ProviderException( |
| 260 | 260 | sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires)) |
| 261 | 261 | ); |
@@ -276,13 +276,13 @@ discard block |
||
| 276 | 276 | ->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738))) |
| 277 | 277 | ; |
| 278 | 278 | |
| 279 | - foreach($this->authHeaders as $header => $value){ |
|
| 279 | + foreach ($this->authHeaders as $header => $value) { |
|
| 280 | 280 | $request = $request->withAddedHeader($header, $value); |
| 281 | 281 | } |
| 282 | 282 | |
| 283 | 283 | $newToken = $this->parseTokenResponse($this->http->sendRequest($request)); |
| 284 | 284 | |
| 285 | - if(empty($newToken->refreshToken)){ |
|
| 285 | + if (empty($newToken->refreshToken)) { |
|
| 286 | 286 | $newToken->refreshToken = $refreshToken; |
| 287 | 287 | } |
| 288 | 288 | |
@@ -305,17 +305,17 @@ discard block |
||
| 305 | 305 | */ |
| 306 | 306 | public function checkState(string $state = null):void{ |
| 307 | 307 | |
| 308 | - if(!$this instanceof CSRFToken){ |
|
| 308 | + if (!$this instanceof CSRFToken) { |
|
| 309 | 309 | throw new ProviderException('CSRF protection not supported'); |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){ |
|
| 312 | + if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) { |
|
| 313 | 313 | throw new ProviderException('invalid state for '.$this->serviceName); |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | $knownState = $this->storage->getCSRFState($this->serviceName); |
| 317 | 317 | |
| 318 | - if(!hash_equals($knownState, $state)){ |
|
| 318 | + if (!hash_equals($knownState, $state)) { |
|
| 319 | 319 | throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state); |
| 320 | 320 | } |
| 321 | 321 | |
@@ -335,11 +335,11 @@ discard block |
||
| 335 | 335 | */ |
| 336 | 336 | public function setState(array $params):array{ |
| 337 | 337 | |
| 338 | - if(!$this instanceof CSRFToken){ |
|
| 338 | + if (!$this instanceof CSRFToken) { |
|
| 339 | 339 | throw new ProviderException('CSRF protection not supported'); |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | - if(!isset($params['state'])){ |
|
| 342 | + if (!isset($params['state'])) { |
|
| 343 | 343 | $params['state'] = sha1(random_bytes(256)); |
| 344 | 344 | } |
| 345 | 345 | |