@@ -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: |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | public function getAuthURL(array $params = null, array $scopes = null):UriInterface{ |
70 | 70 | $params ??= []; |
71 | 71 | |
72 | - if(isset($params['client_secret'])){ |
|
72 | + if (isset($params['client_secret'])) { |
|
73 | 73 | unset($params['client_secret']); |
74 | 74 | } |
75 | 75 | |
@@ -80,11 +80,11 @@ discard block |
||
80 | 80 | 'type' => 'web_server', |
81 | 81 | ]); |
82 | 82 | |
83 | - if(!empty($scopes)){ |
|
83 | + if (!empty($scopes)) { |
|
84 | 84 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
85 | 85 | } |
86 | 86 | |
87 | - if($this instanceof CSRFToken){ |
|
87 | + if ($this instanceof CSRFToken) { |
|
88 | 88 | $params = $this->setState($params); |
89 | 89 | } |
90 | 90 | |
@@ -100,19 +100,19 @@ discard block |
||
100 | 100 | protected function parseTokenResponse(ResponseInterface $response):AccessToken{ |
101 | 101 | $data = json_decode(decompress_content($response), true); // silly amazon... |
102 | 102 | |
103 | - if(!is_array($data)){ |
|
103 | + if (!is_array($data)) { |
|
104 | 104 | throw new ProviderException('unable to parse token response'); |
105 | 105 | } |
106 | 106 | |
107 | - foreach(['error_description', 'error'] as $field){ |
|
107 | + foreach (['error_description', 'error'] as $field) { |
|
108 | 108 | |
109 | - if(isset($data[$field])){ |
|
109 | + if (isset($data[$field])) { |
|
110 | 110 | throw new ProviderException('error retrieving access token: "'.$data[$field].'"'); |
111 | 111 | } |
112 | 112 | |
113 | 113 | } |
114 | 114 | |
115 | - if(!isset($data['access_token'])){ |
|
115 | + if (!isset($data['access_token'])) { |
|
116 | 116 | throw new ProviderException('token missing'); |
117 | 117 | } |
118 | 118 | |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | */ |
136 | 136 | public function getAccessToken(string $code, string $state = null):AccessToken{ |
137 | 137 | |
138 | - if($this instanceof CSRFToken){ |
|
138 | + if ($this instanceof CSRFToken) { |
|
139 | 139 | $this->checkState($state); |
140 | 140 | } |
141 | 141 | |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | ->withHeader('Accept-Encoding', 'identity') |
154 | 154 | ->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738))); |
155 | 155 | |
156 | - foreach($this->authHeaders as $header => $value){ |
|
156 | + foreach ($this->authHeaders as $header => $value) { |
|
157 | 157 | $request = $request->withHeader($header, $value); |
158 | 158 | } |
159 | 159 | |
@@ -169,11 +169,11 @@ discard block |
||
169 | 169 | */ |
170 | 170 | public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{ |
171 | 171 | |
172 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){ |
|
172 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER) { |
|
173 | 173 | return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken); |
174 | 174 | } |
175 | 175 | |
176 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){ |
|
176 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY) { |
|
177 | 177 | $uri = merge_query($request->getUri()->__toString(), [$this->authMethodQuery => $token->accessToken]); |
178 | 178 | |
179 | 179 | return $request->withUri($this->uriFactory->createUri($uri)); |
@@ -194,13 +194,13 @@ discard block |
||
194 | 194 | */ |
195 | 195 | public function getClientCredentialsToken(array $scopes = null):AccessToken{ |
196 | 196 | |
197 | - if(!$this instanceof ClientCredentials){ |
|
197 | + if (!$this instanceof ClientCredentials) { |
|
198 | 198 | throw new ProviderException('client credentials token not supported'); |
199 | 199 | } |
200 | 200 | |
201 | 201 | $params = ['grant_type' => 'client_credentials']; |
202 | 202 | |
203 | - if($scopes !== null){ |
|
203 | + if ($scopes !== null) { |
|
204 | 204 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
205 | 205 | } |
206 | 206 | |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | ->withBody($this->streamFactory->createStream(http_build_query($params, '', '&', PHP_QUERY_RFC1738))) |
213 | 213 | ; |
214 | 214 | |
215 | - foreach($this->authHeaders as $header => $value){ |
|
215 | + foreach ($this->authHeaders as $header => $value) { |
|
216 | 216 | $request = $request->withAddedHeader($header, $value); |
217 | 217 | } |
218 | 218 | |
@@ -235,17 +235,17 @@ discard block |
||
235 | 235 | */ |
236 | 236 | public function refreshAccessToken(AccessToken $token = null):AccessToken{ |
237 | 237 | |
238 | - if(!$this instanceof TokenRefresh){ |
|
238 | + if (!$this instanceof TokenRefresh) { |
|
239 | 239 | throw new ProviderException('token refresh not supported'); |
240 | 240 | } |
241 | 241 | |
242 | - if($token === null){ |
|
242 | + if ($token === null) { |
|
243 | 243 | $token = $this->storage->getAccessToken($this->serviceName); |
244 | 244 | } |
245 | 245 | |
246 | 246 | $refreshToken = $token->refreshToken; |
247 | 247 | |
248 | - if(empty($refreshToken)){ |
|
248 | + if (empty($refreshToken)) { |
|
249 | 249 | throw new ProviderException( |
250 | 250 | sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires)) |
251 | 251 | ); |
@@ -266,13 +266,13 @@ discard block |
||
266 | 266 | ->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738))) |
267 | 267 | ; |
268 | 268 | |
269 | - foreach($this->authHeaders as $header => $value){ |
|
269 | + foreach ($this->authHeaders as $header => $value) { |
|
270 | 270 | $request = $request->withAddedHeader($header, $value); |
271 | 271 | } |
272 | 272 | |
273 | 273 | $newToken = $this->parseTokenResponse($this->http->sendRequest($request)); |
274 | 274 | |
275 | - if(empty($newToken->refreshToken)){ |
|
275 | + if (empty($newToken->refreshToken)) { |
|
276 | 276 | $newToken->refreshToken = $refreshToken; |
277 | 277 | } |
278 | 278 | |
@@ -295,17 +295,17 @@ discard block |
||
295 | 295 | */ |
296 | 296 | public function checkState(string $state = null):void{ |
297 | 297 | |
298 | - if(!$this instanceof CSRFToken){ |
|
298 | + if (!$this instanceof CSRFToken) { |
|
299 | 299 | throw new ProviderException('CSRF protection not supported'); |
300 | 300 | } |
301 | 301 | |
302 | - if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){ |
|
302 | + if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) { |
|
303 | 303 | throw new ProviderException('invalid state for '.$this->serviceName); |
304 | 304 | } |
305 | 305 | |
306 | 306 | $knownState = $this->storage->getCSRFState($this->serviceName); |
307 | 307 | |
308 | - if(!hash_equals($knownState, $state)){ |
|
308 | + if (!hash_equals($knownState, $state)) { |
|
309 | 309 | throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state); |
310 | 310 | } |
311 | 311 | |
@@ -325,11 +325,11 @@ discard block |
||
325 | 325 | */ |
326 | 326 | public function setState(array $params):array{ |
327 | 327 | |
328 | - if(!$this instanceof CSRFToken){ |
|
328 | + if (!$this instanceof CSRFToken) { |
|
329 | 329 | throw new ProviderException('CSRF protection not supported'); |
330 | 330 | } |
331 | 331 | |
332 | - if(!isset($params['state'])){ |
|
332 | + if (!isset($params['state'])) { |
|
333 | 333 | $params['state'] = sha1(random_bytes(256)); |
334 | 334 | } |
335 | 335 |