@@ -16,6 +16,6 @@ |
||
| 16 | 16 | /** |
| 17 | 17 | * This class holds all settings related to the OAuth provider as well as the default HTTP client. |
| 18 | 18 | */ |
| 19 | -class OAuthOptions extends SettingsContainerAbstract{ |
|
| 19 | +class OAuthOptions extends SettingsContainerAbstract { |
|
| 20 | 20 | use OAuthOptionsTrait, HTTPOptionsTrait; |
| 21 | 21 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * Implements an abstract OAuth2 provider with all methods required by the OAuth2Interface. |
| 35 | 35 | * It also implements the ClientCredentials, CSRFToken and TokenRefresh interfaces in favor over traits. |
| 36 | 36 | */ |
| 37 | -abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{ |
|
| 37 | +abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface { |
|
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * Specifies the authentication method: |
@@ -91,11 +91,11 @@ discard block |
||
| 91 | 91 | 'type' => 'web_server', |
| 92 | 92 | ]); |
| 93 | 93 | |
| 94 | - if(!empty($scopes)){ |
|
| 94 | + if (!empty($scopes)) { |
|
| 95 | 95 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - if($this instanceof CSRFToken){ |
|
| 98 | + if ($this instanceof CSRFToken) { |
|
| 99 | 99 | $params = $this->setState($params); |
| 100 | 100 | } |
| 101 | 101 | |
@@ -114,19 +114,19 @@ discard block |
||
| 114 | 114 | // silly amazon sends compressed data... |
| 115 | 115 | $data = json_decode(MessageUtil::decompress($response), true, 512, JSON_THROW_ON_ERROR); |
| 116 | 116 | |
| 117 | - if(!is_array($data)){ |
|
| 117 | + if (!is_array($data)) { |
|
| 118 | 118 | throw new ProviderException('unable to parse token response'); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - foreach(['error_description', 'error'] as $field){ |
|
| 121 | + foreach (['error_description', 'error'] as $field) { |
|
| 122 | 122 | |
| 123 | - if(isset($data[$field])){ |
|
| 123 | + if (isset($data[$field])) { |
|
| 124 | 124 | throw new ProviderException('error retrieving access token: "'.$data[$field].'"'); |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | - if(!isset($data['access_token'])){ |
|
| 129 | + if (!isset($data['access_token'])) { |
|
| 130 | 130 | throw new ProviderException('token missing'); |
| 131 | 131 | } |
| 132 | 132 | |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | */ |
| 149 | 149 | public function getAccessToken(string $code, string $state = null):AccessToken{ |
| 150 | 150 | |
| 151 | - if($this instanceof CSRFToken){ |
|
| 151 | + if ($this instanceof CSRFToken) { |
|
| 152 | 152 | $this->checkState($state); |
| 153 | 153 | } |
| 154 | 154 | |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | ->withHeader('Accept-Encoding', 'identity') |
| 167 | 167 | ->withBody($this->streamFactory->createStream(QueryUtil::build($body, PHP_QUERY_RFC1738))); |
| 168 | 168 | |
| 169 | - foreach($this->authHeaders as $header => $value){ |
|
| 169 | + foreach ($this->authHeaders as $header => $value) { |
|
| 170 | 170 | $request = $request->withHeader($header, $value); |
| 171 | 171 | } |
| 172 | 172 | |
@@ -182,11 +182,11 @@ discard block |
||
| 182 | 182 | */ |
| 183 | 183 | public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{ |
| 184 | 184 | |
| 185 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){ |
|
| 185 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER) { |
|
| 186 | 186 | return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken); |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){ |
|
| 189 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY) { |
|
| 190 | 190 | $uri = QueryUtil::merge((string)$request->getUri(), [$this->authMethodQuery => $token->accessToken]); |
| 191 | 191 | |
| 192 | 192 | 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(QueryUtil::build($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 | |
@@ -242,17 +242,17 @@ discard block |
||
| 242 | 242 | */ |
| 243 | 243 | public function refreshAccessToken(AccessToken $token = null):AccessToken{ |
| 244 | 244 | |
| 245 | - if(!$this instanceof TokenRefresh){ |
|
| 245 | + if (!$this instanceof TokenRefresh) { |
|
| 246 | 246 | throw new ProviderException('token refresh not supported'); |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | - if($token === null){ |
|
| 249 | + if ($token === null) { |
|
| 250 | 250 | $token = $this->storage->getAccessToken($this->serviceName); |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | $refreshToken = $token->refreshToken; |
| 254 | 254 | |
| 255 | - if(empty($refreshToken)){ |
|
| 255 | + if (empty($refreshToken)) { |
|
| 256 | 256 | throw new ProviderException( |
| 257 | 257 | sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires)) |
| 258 | 258 | ); |
@@ -273,13 +273,13 @@ discard block |
||
| 273 | 273 | ->withBody($this->streamFactory->createStream(QueryUtil::build($body, PHP_QUERY_RFC1738))) |
| 274 | 274 | ; |
| 275 | 275 | |
| 276 | - foreach($this->authHeaders as $header => $value){ |
|
| 276 | + foreach ($this->authHeaders as $header => $value) { |
|
| 277 | 277 | $request = $request->withAddedHeader($header, $value); |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | $newToken = $this->parseTokenResponse($this->http->sendRequest($request)); |
| 281 | 281 | |
| 282 | - if(empty($newToken->refreshToken)){ |
|
| 282 | + if (empty($newToken->refreshToken)) { |
|
| 283 | 283 | $newToken->refreshToken = $refreshToken; |
| 284 | 284 | } |
| 285 | 285 | |
@@ -299,17 +299,17 @@ discard block |
||
| 299 | 299 | */ |
| 300 | 300 | public function checkState(string $state = null):void{ |
| 301 | 301 | |
| 302 | - if(!$this instanceof CSRFToken){ |
|
| 302 | + if (!$this instanceof CSRFToken) { |
|
| 303 | 303 | throw new ProviderException('CSRF protection not supported'); |
| 304 | 304 | } |
| 305 | 305 | |
| 306 | - if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){ |
|
| 306 | + if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) { |
|
| 307 | 307 | throw new ProviderException('invalid state for '.$this->serviceName); |
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | $knownState = $this->storage->getCSRFState($this->serviceName); |
| 311 | 311 | |
| 312 | - if(!hash_equals($knownState, $state)){ |
|
| 312 | + if (!hash_equals($knownState, $state)) { |
|
| 313 | 313 | throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state); |
| 314 | 314 | } |
| 315 | 315 | |
@@ -326,11 +326,11 @@ discard block |
||
| 326 | 326 | */ |
| 327 | 327 | public function setState(array $params):array{ |
| 328 | 328 | |
| 329 | - if(!$this instanceof CSRFToken){ |
|
| 329 | + if (!$this instanceof CSRFToken) { |
|
| 330 | 330 | throw new ProviderException('CSRF protection not supported'); |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | - if(!isset($params['state'])){ |
|
| 333 | + if (!isset($params['state'])) { |
|
| 334 | 334 | $params['state'] = sha1(random_bytes(256)); |
| 335 | 335 | } |
| 336 | 336 | |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | * @property string $serviceName |
| 43 | 43 | * @property string|null $userRevokeURL |
| 44 | 44 | */ |
| 45 | -abstract class OAuthProvider implements OAuthInterface{ |
|
| 45 | +abstract class OAuthProvider implements OAuthInterface { |
|
| 46 | 46 | use LoggerAwareTrait; |
| 47 | 47 | |
| 48 | 48 | protected const ALLOWED_PROPERTIES = [ |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | OAuthStorageInterface $storage, |
| 138 | 138 | OAuthOptions|SettingsContainerInterface $options, |
| 139 | 139 | LoggerInterface $logger = null |
| 140 | - ){ |
|
| 140 | + ) { |
|
| 141 | 141 | $this->http = $http; |
| 142 | 142 | $this->storage = $storage; |
| 143 | 143 | $this->options = $options; |
@@ -158,7 +158,7 @@ discard block |
||
| 158 | 158 | */ |
| 159 | 159 | public function __get(string $name):mixed{ |
| 160 | 160 | |
| 161 | - if(in_array($name, $this::ALLOWED_PROPERTIES, true)){ |
|
| 161 | + if (in_array($name, $this::ALLOWED_PROPERTIES, true)) { |
|
| 162 | 162 | return $this->{$name}; |
| 163 | 163 | } |
| 164 | 164 | |
@@ -234,28 +234,28 @@ discard block |
||
| 234 | 234 | $request = $this->requestFactory |
| 235 | 235 | ->createRequest($method ?? 'GET', QueryUtil::merge($this->getRequestTarget($path), $params ?? [])); |
| 236 | 236 | |
| 237 | - foreach(array_merge($this->apiHeaders, $headers ?? []) as $header => $value){ |
|
| 237 | + foreach (array_merge($this->apiHeaders, $headers ?? []) as $header => $value) { |
|
| 238 | 238 | $request = $request->withAddedHeader($header, $value); |
| 239 | 239 | } |
| 240 | 240 | |
| 241 | - if($request->hasHeader('content-type')){ |
|
| 241 | + if ($request->hasHeader('content-type')) { |
|
| 242 | 242 | $contentType = strtolower($request->getHeaderLine('content-type')); |
| 243 | 243 | |
| 244 | - if(is_array($body)){ |
|
| 245 | - if($contentType === 'application/x-www-form-urlencoded'){ |
|
| 244 | + if (is_array($body)) { |
|
| 245 | + if ($contentType === 'application/x-www-form-urlencoded') { |
|
| 246 | 246 | $body = $this->streamFactory->createStream(QueryUtil::build($body, PHP_QUERY_RFC1738)); |
| 247 | 247 | } |
| 248 | - elseif(in_array($contentType, ['application/json', 'application/vnd.api+json'])){ |
|
| 248 | + elseif (in_array($contentType, ['application/json', 'application/vnd.api+json'])) { |
|
| 249 | 249 | $body = $this->streamFactory->createStream(json_encode($body)); |
| 250 | 250 | } |
| 251 | 251 | } |
| 252 | - elseif(is_string($body)){ |
|
| 252 | + elseif (is_string($body)) { |
|
| 253 | 253 | // we don't check if the given string matches the content type - this is the implementor's responsibility |
| 254 | 254 | $body = $this->streamFactory->createStream($body); |
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - if($body instanceof StreamInterface){ |
|
| 258 | + if ($body instanceof StreamInterface) { |
|
| 259 | 259 | $request = $request |
| 260 | 260 | ->withBody($body) |
| 261 | 261 | ->withHeader('Content-length', (string)$body->getSize()) |
@@ -277,18 +277,18 @@ discard block |
||
| 277 | 277 | protected function getRequestTarget(string $uri):string{ |
| 278 | 278 | $parsedURL = QueryUtil::parseUrl($uri); |
| 279 | 279 | |
| 280 | - if(!isset($parsedURL['path'])){ |
|
| 280 | + if (!isset($parsedURL['path'])) { |
|
| 281 | 281 | throw new ProviderException('invalid path'); |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | // for some reason we were given a host name |
| 285 | - if(isset($parsedURL['host'])){ |
|
| 285 | + if (isset($parsedURL['host'])) { |
|
| 286 | 286 | $api = QueryUtil::parseUrl($this->apiURL); |
| 287 | 287 | $host = $api['host'] ?? null; |
| 288 | 288 | |
| 289 | 289 | // back out if it doesn't match |
| 290 | - if($parsedURL['host'] !== $host){ |
|
| 291 | - throw new ProviderException(sprintf('given host (%s) does not match provider (%s)', $parsedURL['host'] , $host)); |
|
| 290 | + if ($parsedURL['host'] !== $host) { |
|
| 291 | + throw new ProviderException(sprintf('given host (%s) does not match provider (%s)', $parsedURL['host'], $host)); |
|
| 292 | 292 | } |
| 293 | 293 | |
| 294 | 294 | // we explicitly ignore any existing parameters here |
@@ -305,15 +305,15 @@ discard block |
||
| 305 | 305 | public function sendRequest(RequestInterface $request):ResponseInterface{ |
| 306 | 306 | |
| 307 | 307 | // get authorization only if we request the provider API |
| 308 | - if(str_starts_with((string)$request->getUri(), $this->apiURL)){ |
|
| 308 | + if (str_starts_with((string)$request->getUri(), $this->apiURL)) { |
|
| 309 | 309 | $token = $this->storage->getAccessToken($this->serviceName); |
| 310 | 310 | |
| 311 | 311 | // attempt to refresh an expired token |
| 312 | - if( |
|
| 312 | + if ( |
|
| 313 | 313 | $this instanceof TokenRefresh |
| 314 | 314 | && $this->options->tokenAutoRefresh |
| 315 | 315 | && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN) |
| 316 | - ){ |
|
| 316 | + ) { |
|
| 317 | 317 | $token = $this->refreshAccessToken($token); |
| 318 | 318 | } |
| 319 | 319 | |
@@ -244,12 +244,10 @@ |
||
| 244 | 244 | if(is_array($body)){ |
| 245 | 245 | if($contentType === 'application/x-www-form-urlencoded'){ |
| 246 | 246 | $body = $this->streamFactory->createStream(QueryUtil::build($body, PHP_QUERY_RFC1738)); |
| 247 | - } |
|
| 248 | - elseif(in_array($contentType, ['application/json', 'application/vnd.api+json'])){ |
|
| 247 | + } elseif(in_array($contentType, ['application/json', 'application/vnd.api+json'])){ |
|
| 249 | 248 | $body = $this->streamFactory->createStream(json_encode($body)); |
| 250 | 249 | } |
| 251 | - } |
|
| 252 | - elseif(is_string($body)){ |
|
| 250 | + } elseif(is_string($body)){ |
|
| 253 | 251 | // we don't check if the given string matches the content type - this is the implementor's responsibility |
| 254 | 252 | $body = $this->streamFactory->createStream($body); |
| 255 | 253 | } |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | /** |
| 29 | 29 | * Implements an abstract OAuth1 provider with all methods required by the OAuth1Interface. |
| 30 | 30 | */ |
| 31 | -abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{ |
|
| 31 | +abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface { |
|
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * The request OAuth1 token URL |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | ->withHeader('Content-Length', '0') // tumblr requires a content-length header set |
| 68 | 68 | ; |
| 69 | 69 | |
| 70 | - foreach($this->authHeaders as $header => $value){ |
|
| 70 | + foreach ($this->authHeaders as $header => $value) { |
|
| 71 | 71 | $request = $request->withAddedHeader($header, $value); |
| 72 | 72 | } |
| 73 | 73 | |
@@ -84,20 +84,20 @@ discard block |
||
| 84 | 84 | protected function parseTokenResponse(ResponseInterface $response, bool $checkCallbackConfirmed = null):AccessToken{ |
| 85 | 85 | $data = QueryUtil::parse(MessageUtil::decompress($response)); |
| 86 | 86 | |
| 87 | - if(empty($data)){ |
|
| 87 | + if (empty($data)) { |
|
| 88 | 88 | throw new ProviderException('unable to parse token response'); |
| 89 | 89 | } |
| 90 | - elseif(isset($data['error'])){ |
|
| 90 | + elseif (isset($data['error'])) { |
|
| 91 | 91 | throw new ProviderException('error retrieving access token: '.$data['error']); |
| 92 | 92 | } |
| 93 | - elseif(!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])){ |
|
| 93 | + elseif (!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])) { |
|
| 94 | 94 | throw new ProviderException('invalid token'); |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - if( |
|
| 97 | + if ( |
|
| 98 | 98 | $checkCallbackConfirmed |
| 99 | 99 | && (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') |
| 100 | - ){ |
|
| 100 | + ) { |
|
| 101 | 101 | throw new ProviderException('oauth callback unconfirmed'); |
| 102 | 102 | } |
| 103 | 103 | |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | protected function getSignature(string $url, array $params, string $method, string $accessTokenSecret = null):string{ |
| 134 | 134 | $parsed = QueryUtil::parseUrl($url); |
| 135 | 135 | |
| 136 | - if(!isset($parsed['host']) || !isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)){ |
|
| 136 | + if (!isset($parsed['host']) || !isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) { |
|
| 137 | 137 | throw new ProviderException('getSignature: invalid url'); |
| 138 | 138 | } |
| 139 | 139 | |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | ]); |
| 151 | 151 | |
| 152 | 152 | // https://tools.ietf.org/html/rfc5849#section-3.4.2 |
| 153 | - $key = array_map('rawurlencode', [ |
|
| 153 | + $key = array_map('rawurlencode', [ |
|
| 154 | 154 | $this->options->secret, |
| 155 | 155 | $accessTokenSecret ?? '' |
| 156 | 156 | ]); |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | $token->accessTokenSecret |
| 198 | 198 | ); |
| 199 | 199 | |
| 200 | - if(isset($query['oauth_session_handle'])){ |
|
| 200 | + if (isset($query['oauth_session_handle'])) { |
|
| 201 | 201 | $parameters['oauth_session_handle'] = $query['oauth_session_handle']; // @codeCoverageIgnore |
| 202 | 202 | } |
| 203 | 203 | |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | /** |
| 20 | 20 | * Implements ab anstract OAuth storage adapter |
| 21 | 21 | */ |
| 22 | -abstract class OAuthStorageAbstract implements OAuthStorageInterface{ |
|
| 22 | +abstract class OAuthStorageAbstract implements OAuthStorageInterface { |
|
| 23 | 23 | use LoggerAwareTrait; |
| 24 | 24 | |
| 25 | 25 | protected OAuthOptions|SettingsContainerInterface $options; |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | /** |
| 28 | 28 | * OAuthStorageAbstract constructor. |
| 29 | 29 | */ |
| 30 | - public function __construct(OAuthOptions|SettingsContainerInterface $options = null, LoggerInterface $logger = null){ |
|
| 30 | + public function __construct(OAuthOptions|SettingsContainerInterface $options = null, LoggerInterface $logger = null) { |
|
| 31 | 31 | $this->options = $options ?? new OAuthOptions; |
| 32 | 32 | $this->logger = $logger ?? new NullLogger; |
| 33 | 33 | } |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | */ |
| 48 | 48 | public function fromStorage(mixed $data):AccessToken{ |
| 49 | 49 | |
| 50 | - if(!is_string($data)){ |
|
| 50 | + if (!is_string($data)) { |
|
| 51 | 51 | throw new OAuthStorageException('invalid data'); |
| 52 | 52 | } |
| 53 | 53 | |