@@ -78,11 +78,9 @@ |
||
78 | 78 | |
79 | 79 | if(!$data || !is_array($data)){ |
80 | 80 | throw new ProviderException('unable to parse token response'); |
81 | - } |
|
82 | - elseif(isset($data['error'])){ |
|
81 | + } elseif(isset($data['error'])){ |
|
83 | 82 | throw new ProviderException('error retrieving access token: '.$data['error']); |
84 | - } |
|
85 | - elseif(!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])){ |
|
83 | + } elseif(!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])){ |
|
86 | 84 | throw new ProviderException('invalid token'); |
87 | 85 | } |
88 | 86 |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | use DateTime; |
17 | 17 | use Psr\Http\Message\{RequestInterface, ResponseInterface, UriInterface}; |
18 | 18 | |
19 | -abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{ |
|
19 | +abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface { |
|
20 | 20 | |
21 | 21 | /** |
22 | 22 | * @var string |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | ->withHeader('Accept-Encoding', 'identity') |
61 | 61 | ; |
62 | 62 | |
63 | - foreach($this->authHeaders as $header => $value){ |
|
63 | + foreach ($this->authHeaders as $header => $value) { |
|
64 | 64 | $request = $request->withAddedHeader($header, $value); |
65 | 65 | } |
66 | 66 | |
@@ -77,17 +77,17 @@ discard block |
||
77 | 77 | protected function parseTokenResponse(ResponseInterface $response, bool $checkCallbackConfirmed = null):AccessToken{ |
78 | 78 | parse_str(Psr7\decompress_content($response), $data); |
79 | 79 | |
80 | - if(!$data || !is_array($data)){ |
|
80 | + if (!$data || !is_array($data)) { |
|
81 | 81 | throw new ProviderException('unable to parse token response'); |
82 | 82 | } |
83 | - elseif(isset($data['error'])){ |
|
83 | + elseif (isset($data['error'])) { |
|
84 | 84 | throw new ProviderException('error retrieving access token: '.$data['error']); |
85 | 85 | } |
86 | - elseif(!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])){ |
|
86 | + elseif (!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])) { |
|
87 | 87 | throw new ProviderException('invalid token'); |
88 | 88 | } |
89 | 89 | |
90 | - if($checkCallbackConfirmed && (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')){ |
|
90 | + if ($checkCallbackConfirmed && (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')) { |
|
91 | 91 | throw new ProviderException('oauth callback unconfirmed'); |
92 | 92 | } |
93 | 93 | |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | protected function getSignature(string $url, array $params, string $method, string $accessTokenSecret = null):string{ |
132 | 132 | $parseURL = parse_url($url); |
133 | 133 | |
134 | - if(!isset($parseURL['host']) || !isset($parseURL['scheme']) || !in_array($parseURL['scheme'], ['http', 'https'], true)){ |
|
134 | + if (!isset($parseURL['host']) || !isset($parseURL['scheme']) || !in_array($parseURL['scheme'], ['http', 'https'], true)) { |
|
135 | 135 | throw new ProviderException('getSignature: invalid url'); |
136 | 136 | } |
137 | 137 | |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | |
140 | 140 | $signatureParams = array_merge($query, $params); |
141 | 141 | |
142 | - if(isset($signatureParams['oauth_signature'])){ |
|
142 | + if (isset($signatureParams['oauth_signature'])) { |
|
143 | 143 | unset($signatureParams['oauth_signature']); |
144 | 144 | } |
145 | 145 | |
@@ -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']; |
202 | 202 | } |
203 | 203 |
@@ -14,7 +14,7 @@ |
||
14 | 14 | |
15 | 15 | use Psr\Http\Message\UriInterface; |
16 | 16 | |
17 | -interface OAuth2Interface extends OAuthInterface{ |
|
17 | +interface OAuth2Interface extends OAuthInterface { |
|
18 | 18 | |
19 | 19 | const HEADER_OAUTH = 0; |
20 | 20 | const HEADER_BEARER = 1; |
@@ -36,6 +36,6 @@ |
||
36 | 36 | * @property bool $ssl_verifypeer |
37 | 37 | * @property string $curlHandle |
38 | 38 | */ |
39 | -class OAuthOptions extends SettingsContainerAbstract{ |
|
39 | +class OAuthOptions extends SettingsContainerAbstract { |
|
40 | 40 | use OAuthOptionsTrait, HTTPOptionsTrait; |
41 | 41 | } |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * @property int $expires |
30 | 30 | * @property string $provider |
31 | 31 | */ |
32 | -class AccessToken extends SettingsContainerAbstract{ |
|
32 | +class AccessToken extends SettingsContainerAbstract { |
|
33 | 33 | |
34 | 34 | /** |
35 | 35 | * Denotes an unknown end of life time. |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | * |
84 | 84 | * @param iterable|null $properties |
85 | 85 | */ |
86 | - public function __construct(iterable $properties = null){ |
|
86 | + public function __construct(iterable $properties = null) { |
|
87 | 87 | parent::__construct($properties); |
88 | 88 | |
89 | 89 | $this->setExpiry($this->expires); |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | */ |
100 | 100 | public function __set(string $property, $value):void{ |
101 | 101 | |
102 | - if(property_exists($this, $property)){ |
|
102 | + if (property_exists($this, $property)) { |
|
103 | 103 | $property === 'expires' |
104 | 104 | ? $this->setExpiry($value) |
105 | 105 | : $this->{$property} = $value; |
@@ -115,19 +115,19 @@ discard block |
||
115 | 115 | public function setExpiry(int $expires = null):AccessToken{ |
116 | 116 | $now = time(); |
117 | 117 | |
118 | - if($expires!== null){ |
|
119 | - $expires = intval($expires); |
|
118 | + if ($expires !== null) { |
|
119 | + $expires = intval($expires); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | $this->expires = self::EOL_UNKNOWN; |
123 | 123 | |
124 | - if($expires === 0 || $expires === self::EOL_NEVER_EXPIRES){ |
|
124 | + if ($expires === 0 || $expires === self::EOL_NEVER_EXPIRES) { |
|
125 | 125 | $this->expires = self::EOL_NEVER_EXPIRES; |
126 | 126 | } |
127 | - elseif($expires > $now){ |
|
127 | + elseif ($expires > $now) { |
|
128 | 128 | $this->expires = $expires; |
129 | 129 | } |
130 | - elseif($expires > 0 && $expires < self::EXPIRY_MAX){ |
|
130 | + elseif ($expires > 0 && $expires < self::EXPIRY_MAX) { |
|
131 | 131 | $this->expires = $now + $expires; |
132 | 132 | } |
133 | 133 |
@@ -123,11 +123,9 @@ |
||
123 | 123 | |
124 | 124 | if($expires === 0 || $expires === self::EOL_NEVER_EXPIRES){ |
125 | 125 | $this->expires = self::EOL_NEVER_EXPIRES; |
126 | - } |
|
127 | - elseif($expires > $now){ |
|
126 | + } elseif($expires > $now){ |
|
128 | 127 | $this->expires = $expires; |
129 | - } |
|
130 | - elseif($expires > 0 && $expires < self::EXPIRY_MAX){ |
|
128 | + } elseif($expires > 0 && $expires < self::EXPIRY_MAX){ |
|
131 | 129 | $this->expires = $now + $expires; |
132 | 130 | } |
133 | 131 |
@@ -164,13 +164,11 @@ |
||
164 | 164 | |
165 | 165 | if(array_key_exists($this->authMethod, OAuth2Interface::AUTH_METHODS_HEADER)){ |
166 | 166 | $request = $request->withHeader('Authorization', OAuth2Interface::AUTH_METHODS_HEADER[$this->authMethod].$token->accessToken); |
167 | - } |
|
168 | - elseif(array_key_exists($this->authMethod, OAuth2Interface::AUTH_METHODS_QUERY)){ |
|
167 | + } elseif(array_key_exists($this->authMethod, OAuth2Interface::AUTH_METHODS_QUERY)){ |
|
169 | 168 | $uri = Psr7\merge_query((string)$request->getUri(), [OAuth2Interface::AUTH_METHODS_QUERY[$this->authMethod] => $token->accessToken]); |
170 | 169 | |
171 | 170 | $request = $request->withUri($this->uriFactory->createUri($uri)); |
172 | - } |
|
173 | - else{ |
|
171 | + } else{ |
|
174 | 172 | throw new ProviderException('invalid auth type'); |
175 | 173 | } |
176 | 174 |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | * @method array setState(array $params) |
21 | 21 | * @method \chillerlan\OAuth\Core\OAuth2Interface checkState(string $state = null) |
22 | 22 | */ |
23 | -abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{ |
|
23 | +abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface { |
|
24 | 24 | |
25 | 25 | /** |
26 | 26 | * @var int |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | public function getAuthURL(array $params = null, array $scopes = null):UriInterface{ |
57 | 57 | $params = $params ?? []; |
58 | 58 | |
59 | - if(isset($params['client_secret'])){ |
|
59 | + if (isset($params['client_secret'])) { |
|
60 | 60 | unset($params['client_secret']); |
61 | 61 | } |
62 | 62 | |
@@ -67,11 +67,11 @@ discard block |
||
67 | 67 | 'type' => 'web_server', |
68 | 68 | ]); |
69 | 69 | |
70 | - if($scopes !== null){ |
|
70 | + if ($scopes !== null) { |
|
71 | 71 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
72 | 72 | } |
73 | 73 | |
74 | - if($this instanceof CSRFToken){ |
|
74 | + if ($this instanceof CSRFToken) { |
|
75 | 75 | $params = $this->setState($params); |
76 | 76 | } |
77 | 77 | |
@@ -87,19 +87,19 @@ discard block |
||
87 | 87 | protected function parseTokenResponse(ResponseInterface $response):AccessToken{ |
88 | 88 | $data = json_decode(Psr7\decompress_content($response), true); // silly amazon... |
89 | 89 | |
90 | - if(!is_array($data)){ |
|
90 | + if (!is_array($data)) { |
|
91 | 91 | throw new ProviderException('unable to parse token response'); |
92 | 92 | } |
93 | 93 | |
94 | - foreach(['error_description', 'error'] as $field){ |
|
94 | + foreach (['error_description', 'error'] as $field) { |
|
95 | 95 | |
96 | - if(isset($data[$field])){ |
|
96 | + if (isset($data[$field])) { |
|
97 | 97 | throw new ProviderException('error retrieving access token: "'.$data[$field].'"'); |
98 | 98 | } |
99 | 99 | |
100 | 100 | } |
101 | 101 | |
102 | - if(!isset($data['access_token'])){ |
|
102 | + if (!isset($data['access_token'])) { |
|
103 | 103 | throw new ProviderException('token missing'); |
104 | 104 | } |
105 | 105 | |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | */ |
126 | 126 | public function getAccessToken(string $code, string $state = null):AccessToken{ |
127 | 127 | |
128 | - if($this instanceof CSRFToken){ |
|
128 | + if ($this instanceof CSRFToken) { |
|
129 | 129 | $this->checkState($state); |
130 | 130 | } |
131 | 131 | |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | ->withHeader('Accept-Encoding', 'identity') |
144 | 144 | ->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738))); |
145 | 145 | |
146 | - foreach($this->authHeaders as $header => $value){ |
|
146 | + foreach ($this->authHeaders as $header => $value) { |
|
147 | 147 | $request = $request->withHeader($header, $value); |
148 | 148 | } |
149 | 149 | |
@@ -163,15 +163,15 @@ discard block |
||
163 | 163 | */ |
164 | 164 | public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{ |
165 | 165 | |
166 | - if(array_key_exists($this->authMethod, OAuth2Interface::AUTH_METHODS_HEADER)){ |
|
166 | + if (array_key_exists($this->authMethod, OAuth2Interface::AUTH_METHODS_HEADER)) { |
|
167 | 167 | $request = $request->withHeader('Authorization', OAuth2Interface::AUTH_METHODS_HEADER[$this->authMethod].$token->accessToken); |
168 | 168 | } |
169 | - elseif(array_key_exists($this->authMethod, OAuth2Interface::AUTH_METHODS_QUERY)){ |
|
169 | + elseif (array_key_exists($this->authMethod, OAuth2Interface::AUTH_METHODS_QUERY)) { |
|
170 | 170 | $uri = Psr7\merge_query((string)$request->getUri(), [OAuth2Interface::AUTH_METHODS_QUERY[$this->authMethod] => $token->accessToken]); |
171 | 171 | |
172 | 172 | $request = $request->withUri($this->uriFactory->createUri($uri)); |
173 | 173 | } |
174 | - else{ |
|
174 | + else { |
|
175 | 175 | throw new ProviderException('invalid auth type'); |
176 | 176 | } |
177 | 177 |
@@ -12,7 +12,7 @@ |
||
12 | 12 | |
13 | 13 | namespace chillerlan\OAuth\Core; |
14 | 14 | |
15 | -interface OAuth1Interface extends OAuthInterface{ |
|
15 | +interface OAuth1Interface extends OAuthInterface { |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * @return \chillerlan\OAuth\Core\AccessToken|\chillerlan\Settings\SettingsContainerInterface |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | * @property string $serviceName |
19 | 19 | * @property \chillerlan\OAuth\Storage\OAuthStorageInterface $storage |
20 | 20 | */ |
21 | -trait OAuth2CSRFTokenTrait{ |
|
21 | +trait OAuth2CSRFTokenTrait { |
|
22 | 22 | |
23 | 23 | /** |
24 | 24 | * @param string|null $state |
@@ -28,13 +28,13 @@ discard block |
||
28 | 28 | */ |
29 | 29 | protected function checkState(string $state = null):void{ |
30 | 30 | |
31 | - if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){ |
|
31 | + if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) { |
|
32 | 32 | throw new ProviderException('invalid state for '.$this->serviceName); |
33 | 33 | } |
34 | 34 | |
35 | 35 | $knownState = $this->storage->getCSRFState($this->serviceName); |
36 | 36 | |
37 | - if(!hash_equals($knownState, $state)){ |
|
37 | + if (!hash_equals($knownState, $state)) { |
|
38 | 38 | throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state); |
39 | 39 | } |
40 | 40 | |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | */ |
48 | 48 | protected function setState(array $params):array{ |
49 | 49 | |
50 | - if(!isset($params['state'])){ |
|
50 | + if (!isset($params['state'])) { |
|
51 | 51 | $params['state'] = sha1(random_bytes(256)); |
52 | 52 | } |
53 | 53 |
@@ -296,8 +296,7 @@ |
||
296 | 296 | // @todo: content type support |
297 | 297 | if($contentType === 'application/x-www-form-urlencoded'){ |
298 | 298 | $body = $this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738)); |
299 | - } |
|
300 | - elseif($contentType === 'application/json'){ |
|
299 | + } elseif($contentType === 'application/json'){ |
|
301 | 300 | $body = $this->streamFactory->createStream(json_encode($body)); |
302 | 301 | } |
303 | 302 |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | * |
123 | 123 | * @throws \chillerlan\HTTP\MagicAPI\ApiClientException |
124 | 124 | */ |
125 | - public function __construct(ClientInterface $http, OAuthStorageInterface $storage, SettingsContainerInterface $options, LoggerInterface $logger = null){ |
|
125 | + public function __construct(ClientInterface $http, OAuthStorageInterface $storage, SettingsContainerInterface $options, LoggerInterface $logger = null) { |
|
126 | 126 | $this->http = $http; |
127 | 127 | $this->storage = $storage; |
128 | 128 | $this->options = $options; |
@@ -134,10 +134,10 @@ discard block |
||
134 | 134 | |
135 | 135 | $this->serviceName = (new ReflectionClass($this))->getShortName(); |
136 | 136 | |
137 | - if($this instanceof ApiClientInterface && !empty($this->endpointMap) && class_exists($this->endpointMap)){ |
|
137 | + if ($this instanceof ApiClientInterface && !empty($this->endpointMap) && class_exists($this->endpointMap)) { |
|
138 | 138 | $this->endpoints = new $this->endpointMap; |
139 | 139 | |
140 | - if(!$this->endpoints instanceof EndpointMapInterface){ |
|
140 | + if (!$this->endpoints instanceof EndpointMapInterface) { |
|
141 | 141 | throw new ApiClientException('invalid endpoint map'); // @codeCoverageIgnore |
142 | 142 | } |
143 | 143 | |
@@ -150,9 +150,9 @@ discard block |
||
150 | 150 | * |
151 | 151 | * @return string|null |
152 | 152 | */ |
153 | - public function __get(string $name):?string{ |
|
153 | + public function __get(string $name): ?string{ |
|
154 | 154 | |
155 | - if(!in_array($name, ['serviceName', 'authURL', 'accessTokenURL', 'revokeURL', 'userRevokeURL', 'apiURL'], true)){ |
|
155 | + if (!in_array($name, ['serviceName', 'authURL', 'accessTokenURL', 'revokeURL', 'userRevokeURL', 'apiURL'], true)) { |
|
156 | 156 | return null; |
157 | 157 | } |
158 | 158 | |
@@ -209,11 +209,11 @@ discard block |
||
209 | 209 | */ |
210 | 210 | public function __call(string $name, array $arguments):ResponseInterface{ |
211 | 211 | |
212 | - if(!$this instanceof ApiClientInterface){ |
|
212 | + if (!$this instanceof ApiClientInterface) { |
|
213 | 213 | throw new ApiClientException('MagicAPI not available'); |
214 | 214 | } |
215 | 215 | |
216 | - if(!$this->endpoints->__isset($name)){ |
|
216 | + if (!$this->endpoints->__isset($name)) { |
|
217 | 217 | throw new ApiClientException('endpoint not found'); |
218 | 218 | } |
219 | 219 | |
@@ -226,21 +226,21 @@ discard block |
||
226 | 226 | $path_elements = $m['path_elements'] ?? []; |
227 | 227 | $params_in_url = count($path_elements); |
228 | 228 | $params = $arguments[$params_in_url] ?? []; |
229 | - $urlparams = array_slice($arguments,0 , $params_in_url); |
|
229 | + $urlparams = array_slice($arguments, 0, $params_in_url); |
|
230 | 230 | |
231 | - if($params_in_url > 0){ |
|
231 | + if ($params_in_url > 0) { |
|
232 | 232 | |
233 | - if(count($urlparams) < $params_in_url){ |
|
233 | + if (count($urlparams) < $params_in_url) { |
|
234 | 234 | throw new APIClientException('too few URL params, required: '.implode(', ', $path_elements)); |
235 | 235 | } |
236 | 236 | |
237 | 237 | $endpoint = sprintf($endpoint, ...$urlparams); |
238 | 238 | } |
239 | 239 | |
240 | - if(in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE'])){ |
|
240 | + if (in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE'])) { |
|
241 | 241 | $body = $arguments[$params_in_url + 1] ?? $params; |
242 | 242 | |
243 | - if($params === $body){ |
|
243 | + if ($params === $body) { |
|
244 | 244 | $params = []; |
245 | 245 | } |
246 | 246 | |
@@ -290,24 +290,24 @@ discard block |
||
290 | 290 | $request = $this->requestFactory |
291 | 291 | ->createRequest($method ?? 'GET', Psr7\merge_query($this->apiURL.$path, $params ?? [])); |
292 | 292 | |
293 | - foreach(array_merge($this->apiHeaders, $headers ?? []) as $header => $value){ |
|
293 | + foreach (array_merge($this->apiHeaders, $headers ?? []) as $header => $value) { |
|
294 | 294 | $request = $request->withAddedHeader($header, $value); |
295 | 295 | } |
296 | 296 | |
297 | - if(is_array($body) && $request->hasHeader('content-type')){ |
|
297 | + if (is_array($body) && $request->hasHeader('content-type')) { |
|
298 | 298 | $contentType = strtolower($request->getHeaderLine('content-type')); |
299 | 299 | |
300 | 300 | // @todo: content type support |
301 | - if($contentType === 'application/x-www-form-urlencoded'){ |
|
301 | + if ($contentType === 'application/x-www-form-urlencoded') { |
|
302 | 302 | $body = $this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738)); |
303 | 303 | } |
304 | - elseif($contentType === 'application/json'){ |
|
304 | + elseif ($contentType === 'application/json') { |
|
305 | 305 | $body = $this->streamFactory->createStream(json_encode($body)); |
306 | 306 | } |
307 | 307 | |
308 | 308 | } |
309 | 309 | |
310 | - if($body instanceof StreamInterface){ |
|
310 | + if ($body instanceof StreamInterface) { |
|
311 | 311 | $request = $request |
312 | 312 | ->withBody($body) |
313 | 313 | ->withHeader('Content-length', $body->getSize()) |
@@ -325,11 +325,11 @@ discard block |
||
325 | 325 | public function sendRequest(RequestInterface $request):ResponseInterface{ |
326 | 326 | |
327 | 327 | // get authorization only if we request the provider API |
328 | - if(strpos((string)$request->getUri(), $this->apiURL) === 0){ |
|
328 | + if (strpos((string)$request->getUri(), $this->apiURL) === 0) { |
|
329 | 329 | $token = $this->storage->getAccessToken($this->serviceName); |
330 | 330 | |
331 | 331 | // attempt to refresh an expired token |
332 | - if($this instanceof TokenRefresh && $this->options->tokenAutoRefresh && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)){ |
|
332 | + if ($this instanceof TokenRefresh && $this->options->tokenAutoRefresh && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)) { |
|
333 | 333 | $token = $this->refreshAccessToken($token); |
334 | 334 | } |
335 | 335 |