@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | /** |
18 | 18 | * Implements a memory storage adapter. Memory storage is not persistent as tokens are only stored during script runtime. |
19 | 19 | */ |
20 | -class MemoryStorage extends OAuthStorageAbstract{ |
|
20 | +class MemoryStorage extends OAuthStorageAbstract { |
|
21 | 21 | |
22 | 22 | /** |
23 | 23 | * the token storage array |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | */ |
44 | 44 | public function getAccessToken(string $service):AccessToken{ |
45 | 45 | |
46 | - if($this->hasAccessToken($service)){ |
|
46 | + if ($this->hasAccessToken($service)) { |
|
47 | 47 | return $this->tokens[$service]; |
48 | 48 | } |
49 | 49 | |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public function clearAccessToken(string $service):bool{ |
64 | 64 | |
65 | - if(array_key_exists($service, $this->tokens)){ |
|
65 | + if (array_key_exists($service, $this->tokens)) { |
|
66 | 66 | unset($this->tokens[$service]); |
67 | 67 | } |
68 | 68 | |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | */ |
75 | 75 | public function clearAllAccessTokens():bool{ |
76 | 76 | |
77 | - foreach(array_keys($this->tokens) as $service){ |
|
77 | + foreach (array_keys($this->tokens) as $service) { |
|
78 | 78 | unset($this->tokens[$service]); |
79 | 79 | } |
80 | 80 | |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | */ |
98 | 98 | public function getCSRFState(string $service):string{ |
99 | 99 | |
100 | - if($this->hasCSRFState($service)){ |
|
100 | + if ($this->hasCSRFState($service)) { |
|
101 | 101 | return $this->states[$service]; |
102 | 102 | } |
103 | 103 | |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | */ |
117 | 117 | public function clearCSRFState(string $service):bool{ |
118 | 118 | |
119 | - if(array_key_exists($service, $this->states)){ |
|
119 | + if (array_key_exists($service, $this->states)) { |
|
120 | 120 | unset($this->states[$service]); |
121 | 121 | } |
122 | 122 |
@@ -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 | /** |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * @param \chillerlan\Settings\SettingsContainerInterface|null $options |
34 | 34 | * @param \Psr\Log\LoggerInterface|null $logger |
35 | 35 | */ |
36 | - public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null){ |
|
36 | + public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null) { |
|
37 | 37 | $this->options = $options ?? new OAuthOptions; |
38 | 38 | |
39 | 39 | $this->setLogger($logger ?? new NullLogger); |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function fromStorage($data):AccessToken{ |
55 | 55 | |
56 | - if(!is_string($data)){ |
|
56 | + if (!is_string($data)) { |
|
57 | 57 | throw new OAuthStorageException('invalid data'); |
58 | 58 | } |
59 | 59 |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | /** |
21 | 21 | * Implements a session storage adapter. Session storage is half persistent as tokens are stored for the duration of the session. |
22 | 22 | */ |
23 | -class SessionStorage extends OAuthStorageAbstract{ |
|
23 | +class SessionStorage extends OAuthStorageAbstract { |
|
24 | 24 | |
25 | 25 | /** |
26 | 26 | * the key name for the token storage array in $_SESSION |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * |
38 | 38 | * @param \chillerlan\Settings\SettingsContainerInterface|null $options |
39 | 39 | */ |
40 | - public function __construct(SettingsContainerInterface $options = null){ |
|
40 | + public function __construct(SettingsContainerInterface $options = null) { |
|
41 | 41 | parent::__construct($options); |
42 | 42 | |
43 | 43 | $this->tokenVar = $this->options->sessionTokenVar; |
@@ -45,15 +45,15 @@ discard block |
||
45 | 45 | |
46 | 46 | // Determine if the session has started. |
47 | 47 | // @link http://stackoverflow.com/a/18542272/1470961 |
48 | - if($this->options->sessionStart && !(session_status() !== PHP_SESSION_NONE)){ |
|
48 | + if ($this->options->sessionStart && !(session_status() !== PHP_SESSION_NONE)) { |
|
49 | 49 | session_start(); |
50 | 50 | } |
51 | 51 | |
52 | - if(!isset($_SESSION[$this->tokenVar])){ |
|
52 | + if (!isset($_SESSION[$this->tokenVar])) { |
|
53 | 53 | $_SESSION[$this->tokenVar] = []; |
54 | 54 | } |
55 | 55 | |
56 | - if(!isset($_SESSION[$this->stateVar])){ |
|
56 | + if (!isset($_SESSION[$this->stateVar])) { |
|
57 | 57 | $_SESSION[$this->stateVar] = []; |
58 | 58 | } |
59 | 59 | |
@@ -64,8 +64,8 @@ discard block |
||
64 | 64 | * |
65 | 65 | * @codeCoverageIgnore |
66 | 66 | */ |
67 | - public function __destruct(){ |
|
68 | - if($this->options->sessionStart){ |
|
67 | + public function __destruct() { |
|
68 | + if ($this->options->sessionStart) { |
|
69 | 69 | session_write_close(); |
70 | 70 | } |
71 | 71 | } |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function getAccessToken(string $service):AccessToken{ |
86 | 86 | |
87 | - if($this->hasAccessToken($service)){ |
|
87 | + if ($this->hasAccessToken($service)) { |
|
88 | 88 | return $this->fromStorage($_SESSION[$this->tokenVar][$service]); |
89 | 89 | } |
90 | 90 | |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | */ |
104 | 104 | public function clearAccessToken(string $service):bool{ |
105 | 105 | |
106 | - if(array_key_exists($service, $_SESSION[$this->tokenVar])){ |
|
106 | + if (array_key_exists($service, $_SESSION[$this->tokenVar])) { |
|
107 | 107 | unset($_SESSION[$this->tokenVar][$service]); |
108 | 108 | } |
109 | 109 | |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | */ |
116 | 116 | public function clearAllAccessTokens():bool{ |
117 | 117 | |
118 | - foreach(array_keys($_SESSION[$this->tokenVar]) as $service){ |
|
118 | + foreach (array_keys($_SESSION[$this->tokenVar]) as $service) { |
|
119 | 119 | unset($_SESSION[$this->tokenVar][$service]); |
120 | 120 | } |
121 | 121 | |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | */ |
139 | 139 | public function getCSRFState(string $service):string{ |
140 | 140 | |
141 | - if($this->hasCSRFState($service)){ |
|
141 | + if ($this->hasCSRFState($service)) { |
|
142 | 142 | return $_SESSION[$this->stateVar][$service]; |
143 | 143 | } |
144 | 144 | |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | */ |
158 | 158 | public function clearCSRFState(string $service):bool{ |
159 | 159 | |
160 | - if(array_key_exists($service, $_SESSION[$this->stateVar])){ |
|
160 | + if (array_key_exists($service, $_SESSION[$this->stateVar])) { |
|
161 | 161 | unset($_SESSION[$this->stateVar][$service]); |
162 | 162 | } |
163 | 163 |
@@ -15,7 +15,7 @@ |
||
15 | 15 | * |
16 | 16 | * @link https://tools.ietf.org/html/rfc6749#section-10.4 |
17 | 17 | */ |
18 | -interface TokenRefresh{ |
|
18 | +interface TokenRefresh { |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * Tries to refresh an existing AccessToken with an associated refresh token and returns a fresh AccessToken. |
@@ -15,7 +15,7 @@ |
||
15 | 15 | * |
16 | 16 | * @link https://tools.ietf.org/html/rfc6749#section-4.4 |
17 | 17 | */ |
18 | -interface ClientCredentials{ |
|
18 | +interface ClientCredentials { |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * Obtains an OAuth2 client credentials token and returns an AccessToken |
@@ -13,7 +13,7 @@ |
||
13 | 13 | /** |
14 | 14 | * Specifies the basic methods for an OAuth1 provider. |
15 | 15 | */ |
16 | -interface OAuth1Interface extends OAuthInterface{ |
|
16 | +interface OAuth1Interface extends OAuthInterface { |
|
17 | 17 | |
18 | 18 | /** |
19 | 19 | * Obtains an OAuth1 request token and returns an AccessToken object for use in the authentication request. |
@@ -15,7 +15,7 @@ |
||
15 | 15 | /** |
16 | 16 | * Specifies the basic methods for an OAuth2 provider. |
17 | 17 | */ |
18 | -interface OAuth2Interface extends OAuthInterface{ |
|
18 | +interface OAuth2Interface extends OAuthInterface { |
|
19 | 19 | |
20 | 20 | const AUTH_METHOD_HEADER = 1; |
21 | 21 | const AUTH_METHOD_QUERY = 2; |
@@ -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. |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | * Implements an abstract OAuth2 provider with all methods required by the OAuth2Interface. |
27 | 27 | * It also implements the ClientCredentials, CSRFToken and TokenRefresh interfaces in favor over traits. |
28 | 28 | */ |
29 | -abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{ |
|
29 | +abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface { |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * Specifies the authentication method: |
@@ -83,11 +83,11 @@ discard block |
||
83 | 83 | 'type' => 'web_server', |
84 | 84 | ]); |
85 | 85 | |
86 | - if(!empty($scopes)){ |
|
86 | + if (!empty($scopes)) { |
|
87 | 87 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
88 | 88 | } |
89 | 89 | |
90 | - if($this instanceof CSRFToken){ |
|
90 | + if ($this instanceof CSRFToken) { |
|
91 | 91 | $params = $this->setState($params); |
92 | 92 | } |
93 | 93 | |
@@ -109,19 +109,19 @@ discard block |
||
109 | 109 | // silly amazon sends compressed data... |
110 | 110 | $data = json_decode(decompress_content($response), true, 512, JSON_THROW_ON_ERROR); |
111 | 111 | |
112 | - if(!is_array($data)){ |
|
112 | + if (!is_array($data)) { |
|
113 | 113 | throw new ProviderException('unable to parse token response'); |
114 | 114 | } |
115 | 115 | |
116 | - foreach(['error_description', 'error'] as $field){ |
|
116 | + foreach (['error_description', 'error'] as $field) { |
|
117 | 117 | |
118 | - if(isset($data[$field])){ |
|
118 | + if (isset($data[$field])) { |
|
119 | 119 | throw new ProviderException('error retrieving access token: "'.$data[$field].'"'); |
120 | 120 | } |
121 | 121 | |
122 | 122 | } |
123 | 123 | |
124 | - if(!isset($data['access_token'])){ |
|
124 | + if (!isset($data['access_token'])) { |
|
125 | 125 | throw new ProviderException('token missing'); |
126 | 126 | } |
127 | 127 | |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | */ |
145 | 145 | public function getAccessToken(string $code, string $state = null):AccessToken{ |
146 | 146 | |
147 | - if($this instanceof CSRFToken){ |
|
147 | + if ($this instanceof CSRFToken) { |
|
148 | 148 | $this->checkState($state); |
149 | 149 | } |
150 | 150 | |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | ->withHeader('Accept-Encoding', 'identity') |
163 | 163 | ->withBody($this->streamFactory->createStream($this->buildQuery($body, PHP_QUERY_RFC1738))); |
164 | 164 | |
165 | - foreach($this->authHeaders as $header => $value){ |
|
165 | + foreach ($this->authHeaders as $header => $value) { |
|
166 | 166 | $request = $request->withHeader($header, $value); |
167 | 167 | } |
168 | 168 | |
@@ -178,11 +178,11 @@ discard block |
||
178 | 178 | */ |
179 | 179 | public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{ |
180 | 180 | |
181 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){ |
|
181 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER) { |
|
182 | 182 | return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken); |
183 | 183 | } |
184 | 184 | |
185 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){ |
|
185 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY) { |
|
186 | 186 | $uri = $this->mergeQuery((string)$request->getUri(), [$this->authMethodQuery => $token->accessToken]); |
187 | 187 | |
188 | 188 | return $request->withUri($this->uriFactory->createUri($uri)); |
@@ -203,13 +203,13 @@ discard block |
||
203 | 203 | */ |
204 | 204 | public function getClientCredentialsToken(array $scopes = null):AccessToken{ |
205 | 205 | |
206 | - if(!$this instanceof ClientCredentials){ |
|
206 | + if (!$this instanceof ClientCredentials) { |
|
207 | 207 | throw new ProviderException('client credentials token not supported'); |
208 | 208 | } |
209 | 209 | |
210 | 210 | $params = ['grant_type' => 'client_credentials']; |
211 | 211 | |
212 | - if($scopes !== null){ |
|
212 | + if ($scopes !== null) { |
|
213 | 213 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
214 | 214 | } |
215 | 215 | |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | ->withBody($this->streamFactory->createStream($this->buildQuery($params, PHP_QUERY_RFC1738))) |
222 | 222 | ; |
223 | 223 | |
224 | - foreach($this->authHeaders as $header => $value){ |
|
224 | + foreach ($this->authHeaders as $header => $value) { |
|
225 | 225 | $request = $request->withAddedHeader($header, $value); |
226 | 226 | } |
227 | 227 | |
@@ -244,17 +244,17 @@ discard block |
||
244 | 244 | */ |
245 | 245 | public function refreshAccessToken(AccessToken $token = null):AccessToken{ |
246 | 246 | |
247 | - if(!$this instanceof TokenRefresh){ |
|
247 | + if (!$this instanceof TokenRefresh) { |
|
248 | 248 | throw new ProviderException('token refresh not supported'); |
249 | 249 | } |
250 | 250 | |
251 | - if($token === null){ |
|
251 | + if ($token === null) { |
|
252 | 252 | $token = $this->storage->getAccessToken($this->serviceName); |
253 | 253 | } |
254 | 254 | |
255 | 255 | $refreshToken = $token->refreshToken; |
256 | 256 | |
257 | - if(empty($refreshToken)){ |
|
257 | + if (empty($refreshToken)) { |
|
258 | 258 | throw new ProviderException( |
259 | 259 | sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires)) |
260 | 260 | ); |
@@ -275,13 +275,13 @@ discard block |
||
275 | 275 | ->withBody($this->streamFactory->createStream($this->buildQuery($body, PHP_QUERY_RFC1738))) |
276 | 276 | ; |
277 | 277 | |
278 | - foreach($this->authHeaders as $header => $value){ |
|
278 | + foreach ($this->authHeaders as $header => $value) { |
|
279 | 279 | $request = $request->withAddedHeader($header, $value); |
280 | 280 | } |
281 | 281 | |
282 | 282 | $newToken = $this->parseTokenResponse($this->http->sendRequest($request)); |
283 | 283 | |
284 | - if(empty($newToken->refreshToken)){ |
|
284 | + if (empty($newToken->refreshToken)) { |
|
285 | 285 | $newToken->refreshToken = $refreshToken; |
286 | 286 | } |
287 | 287 | |
@@ -304,17 +304,17 @@ discard block |
||
304 | 304 | */ |
305 | 305 | public function checkState(string $state = null):void{ |
306 | 306 | |
307 | - if(!$this instanceof CSRFToken){ |
|
307 | + if (!$this instanceof CSRFToken) { |
|
308 | 308 | throw new ProviderException('CSRF protection not supported'); |
309 | 309 | } |
310 | 310 | |
311 | - if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){ |
|
311 | + if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) { |
|
312 | 312 | throw new ProviderException('invalid state for '.$this->serviceName); |
313 | 313 | } |
314 | 314 | |
315 | 315 | $knownState = $this->storage->getCSRFState($this->serviceName); |
316 | 316 | |
317 | - if(!hash_equals($knownState, $state)){ |
|
317 | + if (!hash_equals($knownState, $state)) { |
|
318 | 318 | throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state); |
319 | 319 | } |
320 | 320 | |
@@ -334,11 +334,11 @@ discard block |
||
334 | 334 | */ |
335 | 335 | public function setState(array $params):array{ |
336 | 336 | |
337 | - if(!$this instanceof CSRFToken){ |
|
337 | + if (!$this instanceof CSRFToken) { |
|
338 | 338 | throw new ProviderException('CSRF protection not supported'); |
339 | 339 | } |
340 | 340 | |
341 | - if(!isset($params['state'])){ |
|
341 | + if (!isset($params['state'])) { |
|
342 | 342 | $params['state'] = sha1(random_bytes(256)); |
343 | 343 | } |
344 | 344 |