@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | /** |
| 24 | 24 | * Implements an abstract OAuth1 provider with all methods required by the OAuth1Interface. |
| 25 | 25 | */ |
| 26 | -abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{ |
|
| 26 | +abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface { |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * The request OAuth1 token URL |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | ->withHeader('Content-Length', '0') // tumblr requires a content-length header set |
| 63 | 63 | ; |
| 64 | 64 | |
| 65 | - foreach($this->authHeaders as $header => $value){ |
|
| 65 | + foreach ($this->authHeaders as $header => $value) { |
|
| 66 | 66 | $request = $request->withAddedHeader($header, $value); |
| 67 | 67 | } |
| 68 | 68 | |
@@ -83,20 +83,20 @@ discard block |
||
| 83 | 83 | protected function parseTokenResponse(ResponseInterface $response, bool $checkCallbackConfirmed = null):AccessToken{ |
| 84 | 84 | $data = Query::parse(decompress_content($response)); |
| 85 | 85 | |
| 86 | - if(!$data || !is_array($data)){ |
|
| 86 | + if (!$data || !is_array($data)) { |
|
| 87 | 87 | throw new ProviderException('unable to parse token response'); |
| 88 | 88 | } |
| 89 | - elseif(isset($data['error'])){ |
|
| 89 | + elseif (isset($data['error'])) { |
|
| 90 | 90 | throw new ProviderException('error retrieving access token: '.$data['error']); |
| 91 | 91 | } |
| 92 | - elseif(!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])){ |
|
| 92 | + elseif (!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])) { |
|
| 93 | 93 | throw new ProviderException('invalid token'); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - if( |
|
| 96 | + if ( |
|
| 97 | 97 | $checkCallbackConfirmed |
| 98 | 98 | && (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') |
| 99 | - ){ |
|
| 99 | + ) { |
|
| 100 | 100 | throw new ProviderException('oauth callback unconfirmed'); |
| 101 | 101 | } |
| 102 | 102 | |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | protected function getSignature(string $url, array $params, string $method, string $accessTokenSecret = null):string{ |
| 147 | 147 | $parseURL = parse_url($url); |
| 148 | 148 | |
| 149 | - if(!isset($parseURL['host']) || !isset($parseURL['scheme']) || !in_array($parseURL['scheme'], ['http', 'https'], true)){ |
|
| 149 | + if (!isset($parseURL['host']) || !isset($parseURL['scheme']) || !in_array($parseURL['scheme'], ['http', 'https'], true)) { |
|
| 150 | 150 | throw new ProviderException('getSignature: invalid url'); |
| 151 | 151 | } |
| 152 | 152 | |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | $token->accessTokenSecret |
| 207 | 207 | ); |
| 208 | 208 | |
| 209 | - if(isset($query['oauth_session_handle'])){ |
|
| 209 | + if (isset($query['oauth_session_handle'])) { |
|
| 210 | 210 | $parameters['oauth_session_handle'] = $query['oauth_session_handle']; // @codeCoverageIgnore |
| 211 | 211 | } |
| 212 | 212 | |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | * Implements an abstract OAuth2 provider with all methods required by the OAuth2Interface. |
| 29 | 29 | * It also implements the ClientCredentials, CSRFToken and TokenRefresh interfaces in favor over traits. |
| 30 | 30 | */ |
| 31 | -abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{ |
|
| 31 | +abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface { |
|
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * Specifies the authentication method: |
@@ -85,11 +85,11 @@ discard block |
||
| 85 | 85 | 'type' => 'web_server', |
| 86 | 86 | ]); |
| 87 | 87 | |
| 88 | - if(!empty($scopes)){ |
|
| 88 | + if (!empty($scopes)) { |
|
| 89 | 89 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - if($this instanceof CSRFToken){ |
|
| 92 | + if ($this instanceof CSRFToken) { |
|
| 93 | 93 | $params = $this->setState($params); |
| 94 | 94 | } |
| 95 | 95 | |
@@ -111,19 +111,19 @@ discard block |
||
| 111 | 111 | // silly amazon sends compressed data... |
| 112 | 112 | $data = json_decode(decompress_content($response), true, 512, JSON_THROW_ON_ERROR); |
| 113 | 113 | |
| 114 | - if(!is_array($data)){ |
|
| 114 | + if (!is_array($data)) { |
|
| 115 | 115 | throw new ProviderException('unable to parse token response'); |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | - foreach(['error_description', 'error'] as $field){ |
|
| 118 | + foreach (['error_description', 'error'] as $field) { |
|
| 119 | 119 | |
| 120 | - if(isset($data[$field])){ |
|
| 120 | + if (isset($data[$field])) { |
|
| 121 | 121 | throw new ProviderException('error retrieving access token: "'.$data[$field].'"'); |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - if(!isset($data['access_token'])){ |
|
| 126 | + if (!isset($data['access_token'])) { |
|
| 127 | 127 | throw new ProviderException('token missing'); |
| 128 | 128 | } |
| 129 | 129 | |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | */ |
| 147 | 147 | public function getAccessToken(string $code, string $state = null):AccessToken{ |
| 148 | 148 | |
| 149 | - if($this instanceof CSRFToken){ |
|
| 149 | + if ($this instanceof CSRFToken) { |
|
| 150 | 150 | $this->checkState($state); |
| 151 | 151 | } |
| 152 | 152 | |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | ->withHeader('Accept-Encoding', 'identity') |
| 165 | 165 | ->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738))); |
| 166 | 166 | |
| 167 | - foreach($this->authHeaders as $header => $value){ |
|
| 167 | + foreach ($this->authHeaders as $header => $value) { |
|
| 168 | 168 | $request = $request->withHeader($header, $value); |
| 169 | 169 | } |
| 170 | 170 | |
@@ -180,11 +180,11 @@ discard block |
||
| 180 | 180 | */ |
| 181 | 181 | public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{ |
| 182 | 182 | |
| 183 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){ |
|
| 183 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER) { |
|
| 184 | 184 | return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){ |
|
| 187 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY) { |
|
| 188 | 188 | $uri = Query::merge((string)$request->getUri(), [$this->authMethodQuery => $token->accessToken]); |
| 189 | 189 | |
| 190 | 190 | return $request->withUri($this->uriFactory->createUri($uri)); |
@@ -205,13 +205,13 @@ discard block |
||
| 205 | 205 | */ |
| 206 | 206 | public function getClientCredentialsToken(array $scopes = null):AccessToken{ |
| 207 | 207 | |
| 208 | - if(!$this instanceof ClientCredentials){ |
|
| 208 | + if (!$this instanceof ClientCredentials) { |
|
| 209 | 209 | throw new ProviderException('client credentials token not supported'); |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | $params = ['grant_type' => 'client_credentials']; |
| 213 | 213 | |
| 214 | - if($scopes !== null){ |
|
| 214 | + if ($scopes !== null) { |
|
| 215 | 215 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
| 216 | 216 | } |
| 217 | 217 | |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | ->withBody($this->streamFactory->createStream(http_build_query($params, '', '&', PHP_QUERY_RFC1738))) |
| 224 | 224 | ; |
| 225 | 225 | |
| 226 | - foreach($this->authHeaders as $header => $value){ |
|
| 226 | + foreach ($this->authHeaders as $header => $value) { |
|
| 227 | 227 | $request = $request->withAddedHeader($header, $value); |
| 228 | 228 | } |
| 229 | 229 | |
@@ -246,17 +246,17 @@ discard block |
||
| 246 | 246 | */ |
| 247 | 247 | public function refreshAccessToken(AccessToken $token = null):AccessToken{ |
| 248 | 248 | |
| 249 | - if(!$this instanceof TokenRefresh){ |
|
| 249 | + if (!$this instanceof TokenRefresh) { |
|
| 250 | 250 | throw new ProviderException('token refresh not supported'); |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | - if($token === null){ |
|
| 253 | + if ($token === null) { |
|
| 254 | 254 | $token = $this->storage->getAccessToken($this->serviceName); |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | $refreshToken = $token->refreshToken; |
| 258 | 258 | |
| 259 | - if(empty($refreshToken)){ |
|
| 259 | + if (empty($refreshToken)) { |
|
| 260 | 260 | throw new ProviderException( |
| 261 | 261 | sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires)) |
| 262 | 262 | ); |
@@ -277,13 +277,13 @@ discard block |
||
| 277 | 277 | ->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738))) |
| 278 | 278 | ; |
| 279 | 279 | |
| 280 | - foreach($this->authHeaders as $header => $value){ |
|
| 280 | + foreach ($this->authHeaders as $header => $value) { |
|
| 281 | 281 | $request = $request->withAddedHeader($header, $value); |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | $newToken = $this->parseTokenResponse($this->http->sendRequest($request)); |
| 285 | 285 | |
| 286 | - if(empty($newToken->refreshToken)){ |
|
| 286 | + if (empty($newToken->refreshToken)) { |
|
| 287 | 287 | $newToken->refreshToken = $refreshToken; |
| 288 | 288 | } |
| 289 | 289 | |
@@ -306,17 +306,17 @@ discard block |
||
| 306 | 306 | */ |
| 307 | 307 | public function checkState(string $state = null):void{ |
| 308 | 308 | |
| 309 | - if(!$this instanceof CSRFToken){ |
|
| 309 | + if (!$this instanceof CSRFToken) { |
|
| 310 | 310 | throw new ProviderException('CSRF protection not supported'); |
| 311 | 311 | } |
| 312 | 312 | |
| 313 | - if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){ |
|
| 313 | + if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) { |
|
| 314 | 314 | throw new ProviderException('invalid state for '.$this->serviceName); |
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | $knownState = $this->storage->getCSRFState($this->serviceName); |
| 318 | 318 | |
| 319 | - if(!hash_equals($knownState, $state)){ |
|
| 319 | + if (!hash_equals($knownState, $state)) { |
|
| 320 | 320 | throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state); |
| 321 | 321 | } |
| 322 | 322 | |
@@ -336,11 +336,11 @@ discard block |
||
| 336 | 336 | */ |
| 337 | 337 | public function setState(array $params):array{ |
| 338 | 338 | |
| 339 | - if(!$this instanceof CSRFToken){ |
|
| 339 | + if (!$this instanceof CSRFToken) { |
|
| 340 | 340 | throw new ProviderException('CSRF protection not supported'); |
| 341 | 341 | } |
| 342 | 342 | |
| 343 | - if(!isset($params['state'])){ |
|
| 343 | + if (!isset($params['state'])) { |
|
| 344 | 344 | $params['state'] = sha1(random_bytes(256)); |
| 345 | 345 | } |
| 346 | 346 | |
@@ -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 = [ |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | OAuthStorageInterface $storage, |
| 157 | 157 | SettingsContainerInterface $options, |
| 158 | 158 | LoggerInterface $logger = null |
| 159 | - ){ |
|
| 159 | + ) { |
|
| 160 | 160 | $this->http = $http; |
| 161 | 161 | $this->storage = $storage; |
| 162 | 162 | $this->options = $options; |
@@ -168,10 +168,10 @@ discard block |
||
| 168 | 168 | |
| 169 | 169 | $this->serviceName = (new ReflectionClass($this))->getShortName(); |
| 170 | 170 | |
| 171 | - if(!empty($this->endpointMap) && class_exists($this->endpointMap)){ |
|
| 171 | + if (!empty($this->endpointMap) && class_exists($this->endpointMap)) { |
|
| 172 | 172 | $this->endpoints = new $this->endpointMap; |
| 173 | 173 | |
| 174 | - if(!$this->endpoints instanceof EndpointMapInterface){ |
|
| 174 | + if (!$this->endpoints instanceof EndpointMapInterface) { |
|
| 175 | 175 | throw new ApiClientException('invalid endpoint map'); // @codeCoverageIgnore |
| 176 | 176 | } |
| 177 | 177 | |
@@ -186,9 +186,9 @@ discard block |
||
| 186 | 186 | * |
| 187 | 187 | * @return mixed|null |
| 188 | 188 | */ |
| 189 | - public function __get(string $name){ |
|
| 189 | + public function __get(string $name) { |
|
| 190 | 190 | |
| 191 | - if(in_array($name, $this::ALLOWED_PROPERTIES, true)){ |
|
| 191 | + if (in_array($name, $this::ALLOWED_PROPERTIES, true)) { |
|
| 192 | 192 | return $this->{$name}; |
| 193 | 193 | } |
| 194 | 194 | |
@@ -246,11 +246,11 @@ discard block |
||
| 246 | 246 | */ |
| 247 | 247 | public function __call(string $endpointName, array $arguments):ResponseInterface{ |
| 248 | 248 | |
| 249 | - if(!$this->endpoints instanceof EndpointMap){ |
|
| 249 | + if (!$this->endpoints instanceof EndpointMap) { |
|
| 250 | 250 | throw new ApiClientException('MagicAPI not available'); // @codeCoverageIgnore |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | - if(!isset($this->endpoints->{$endpointName})){ |
|
| 253 | + if (!isset($this->endpoints->{$endpointName})) { |
|
| 254 | 254 | throw new ApiClientException('endpoint not found: "'.$endpointName.'"'); |
| 255 | 255 | } |
| 256 | 256 | |
@@ -270,25 +270,25 @@ discard block |
||
| 270 | 270 | $path_element_count = count($path_elements); |
| 271 | 271 | $query_param_count = count($query_params); |
| 272 | 272 | |
| 273 | - if($path_element_count > 0){ |
|
| 273 | + if ($path_element_count > 0) { |
|
| 274 | 274 | $path = $this->parsePathElements($path, $path_elements, $path_element_count, $arguments); |
| 275 | 275 | } |
| 276 | 276 | |
| 277 | - if($query_param_count > 0){ |
|
| 277 | + if ($query_param_count > 0) { |
|
| 278 | 278 | // $params is the first argument after path segments |
| 279 | 279 | $params = $arguments[$path_element_count] ?? null; |
| 280 | 280 | |
| 281 | - if(is_array($params)){ |
|
| 281 | + if (is_array($params)) { |
|
| 282 | 282 | $params = $this->cleanQueryParams($this->removeUnlistedParams($params, $query_params)); |
| 283 | 283 | } |
| 284 | 284 | } |
| 285 | 285 | |
| 286 | - if(in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE']) && $has_body){ |
|
| 286 | + if (in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE']) && $has_body) { |
|
| 287 | 287 | // if no query params are present, $body is the first argument after any path segments |
| 288 | 288 | $argPos = $query_param_count > 0 ? 1 : 0; |
| 289 | 289 | $body = $arguments[$path_element_count + $argPos] ?? null; |
| 290 | 290 | |
| 291 | - if(is_array($body)){ |
|
| 291 | + if (is_array($body)) { |
|
| 292 | 292 | $body = $this->cleanBodyParams($body); |
| 293 | 293 | } |
| 294 | 294 | } |
@@ -309,13 +309,13 @@ discard block |
||
| 309 | 309 | // we don't know if all of the given arguments are path elements... |
| 310 | 310 | $urlparams = array_slice($arguments, 0, $path_element_count); |
| 311 | 311 | |
| 312 | - if(count($urlparams) !== $path_element_count){ |
|
| 312 | + if (count($urlparams) !== $path_element_count) { |
|
| 313 | 313 | throw new APIClientException('too few URL params, required: '.implode(', ', $path_elements)); |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | - foreach($urlparams as $i => $param){ |
|
| 316 | + foreach ($urlparams as $i => $param) { |
|
| 317 | 317 | // ...but we do know that the arguments after the path elements are usually array or null |
| 318 | - if(!is_scalar($param)){ |
|
| 318 | + if (!is_scalar($param)) { |
|
| 319 | 319 | $msg = 'invalid path element value for "%s": %s'; |
| 320 | 320 | |
| 321 | 321 | throw new APIClientException(sprintf($msg, $path_elements[$i], var_export($param, true))); |
@@ -331,9 +331,9 @@ discard block |
||
| 331 | 331 | protected function removeUnlistedParams(array $params, array $allowed):array{ |
| 332 | 332 | $query = []; |
| 333 | 333 | // remove any params that are not listed |
| 334 | - foreach($params as $key => $value){ |
|
| 334 | + foreach ($params as $key => $value) { |
|
| 335 | 335 | |
| 336 | - if(!in_array($key, $allowed, true)){ |
|
| 336 | + if (!in_array($key, $allowed, true)) { |
|
| 337 | 337 | continue; |
| 338 | 338 | } |
| 339 | 339 | |
@@ -371,28 +371,28 @@ discard block |
||
| 371 | 371 | $request = $this->requestFactory |
| 372 | 372 | ->createRequest($method ?? 'GET', Query::merge($this->getRequestTarget($path), $params ?? [])); |
| 373 | 373 | |
| 374 | - foreach(array_merge($this->apiHeaders, $headers ?? []) as $header => $value){ |
|
| 374 | + foreach (array_merge($this->apiHeaders, $headers ?? []) as $header => $value) { |
|
| 375 | 375 | $request = $request->withAddedHeader($header, $value); |
| 376 | 376 | } |
| 377 | 377 | |
| 378 | - if($request->hasHeader('content-type')){ |
|
| 378 | + if ($request->hasHeader('content-type')) { |
|
| 379 | 379 | $contentType = strtolower($request->getHeaderLine('content-type')); |
| 380 | 380 | |
| 381 | - if(is_array($body)){ |
|
| 382 | - if($contentType === 'application/x-www-form-urlencoded'){ |
|
| 381 | + if (is_array($body)) { |
|
| 382 | + if ($contentType === 'application/x-www-form-urlencoded') { |
|
| 383 | 383 | $body = $this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738)); |
| 384 | 384 | } |
| 385 | - elseif($contentType === 'application/json' || $contentType === 'application/vnd.api+json'){ |
|
| 385 | + elseif ($contentType === 'application/json' || $contentType === 'application/vnd.api+json') { |
|
| 386 | 386 | $body = $this->streamFactory->createStream(json_encode($body)); |
| 387 | 387 | } |
| 388 | 388 | } |
| 389 | - elseif(is_string($body)){ |
|
| 389 | + elseif (is_string($body)) { |
|
| 390 | 390 | // we don't check if the given string matches the content type - this is the implementor's responsibility |
| 391 | 391 | $body = $this->streamFactory->createStream($body); |
| 392 | 392 | } |
| 393 | 393 | } |
| 394 | 394 | |
| 395 | - if($body instanceof StreamInterface){ |
|
| 395 | + if ($body instanceof StreamInterface) { |
|
| 396 | 396 | $request = $request |
| 397 | 397 | ->withBody($body) |
| 398 | 398 | ->withHeader('Content-length', (string)$body->getSize()) |
@@ -414,15 +414,15 @@ discard block |
||
| 414 | 414 | protected function getRequestTarget(string $uri):string{ |
| 415 | 415 | $parsedURL = parse_url($uri); |
| 416 | 416 | |
| 417 | - if(!isset($parsedURL['path'])){ |
|
| 417 | + if (!isset($parsedURL['path'])) { |
|
| 418 | 418 | throw new ProviderException('invalid path'); |
| 419 | 419 | } |
| 420 | 420 | |
| 421 | 421 | // for some reason we were given a host name |
| 422 | - if(isset($parsedURL['host'])){ |
|
| 422 | + if (isset($parsedURL['host'])) { |
|
| 423 | 423 | |
| 424 | 424 | // back out if it doesn't match |
| 425 | - if($parsedURL['host'] !== parse_url($this->apiURL, PHP_URL_HOST)){ |
|
| 425 | + if ($parsedURL['host'] !== parse_url($this->apiURL, PHP_URL_HOST)) { |
|
| 426 | 426 | throw new ProviderException('given host does not match provider host'); |
| 427 | 427 | } |
| 428 | 428 | |
@@ -440,15 +440,15 @@ discard block |
||
| 440 | 440 | public function sendRequest(RequestInterface $request):ResponseInterface{ |
| 441 | 441 | |
| 442 | 442 | // get authorization only if we request the provider API |
| 443 | - if(strpos((string)$request->getUri(), $this->apiURL) === 0){ |
|
| 443 | + if (strpos((string)$request->getUri(), $this->apiURL) === 0) { |
|
| 444 | 444 | $token = $this->storage->getAccessToken($this->serviceName); |
| 445 | 445 | |
| 446 | 446 | // attempt to refresh an expired token |
| 447 | - if( |
|
| 447 | + if ( |
|
| 448 | 448 | $this instanceof TokenRefresh |
| 449 | 449 | && $this->options->tokenAutoRefresh |
| 450 | 450 | && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN) |
| 451 | - ){ |
|
| 451 | + ) { |
|
| 452 | 452 | $token = $this->refreshAccessToken($token); |
| 453 | 453 | } |
| 454 | 454 | |