@@ -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 | } |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | /** |
19 | 19 | * Implements an abstract OAuth storage adapter |
20 | 20 | */ |
21 | -abstract class OAuthStorageAbstract implements OAuthStorageInterface{ |
|
21 | +abstract class OAuthStorageAbstract implements OAuthStorageInterface { |
|
22 | 22 | use LoggerAwareTrait; |
23 | 23 | |
24 | 24 | 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 | } |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | public function setServiceName(string $service):OAuthStorageInterface{ |
39 | 39 | $service = trim($service); |
40 | 40 | |
41 | - if(empty($service)){ |
|
41 | + if (empty($service)) { |
|
42 | 42 | throw new OAuthStorageException('service name must not be empty'); |
43 | 43 | } |
44 | 44 | |
@@ -52,13 +52,13 @@ discard block |
||
52 | 52 | */ |
53 | 53 | public function getServiceName(string $service = null):string{ |
54 | 54 | |
55 | - if($service === null && !isset($this->serviceName)){ |
|
55 | + if ($service === null && !isset($this->serviceName)) { |
|
56 | 56 | throw new OAuthStorageException('invalid service'); |
57 | 57 | } |
58 | 58 | |
59 | 59 | $name = trim($service ?? $this->serviceName); |
60 | 60 | |
61 | - if(empty($name)){ |
|
61 | + if (empty($name)) { |
|
62 | 62 | throw new OAuthStorageException('service name must not be empty'); |
63 | 63 | } |
64 | 64 | |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | */ |
79 | 79 | public function fromStorage(mixed $data):AccessToken{ |
80 | 80 | |
81 | - if(!is_string($data)){ |
|
81 | + if (!is_string($data)) { |
|
82 | 82 | throw new OAuthStorageException('invalid data'); |
83 | 83 | } |
84 | 84 |
@@ -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 = null):AccessToken{ |
45 | 45 | |
46 | - if($this->hasAccessToken($service)){ |
|
46 | + if ($this->hasAccessToken($service)) { |
|
47 | 47 | return $this->tokens[$this->getServiceName($service)]; |
48 | 48 | } |
49 | 49 | |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | public function clearAccessToken(string $service = null):OAuthStorageInterface{ |
66 | 66 | $serviceName = $this->getServiceName($service); |
67 | 67 | |
68 | - if(array_key_exists($serviceName, $this->tokens)){ |
|
68 | + if (array_key_exists($serviceName, $this->tokens)) { |
|
69 | 69 | unset($this->tokens[$serviceName]); |
70 | 70 | } |
71 | 71 | |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | */ |
78 | 78 | public function clearAllAccessTokens():OAuthStorageInterface{ |
79 | 79 | |
80 | - foreach(array_keys($this->tokens) as $service){ |
|
80 | + foreach (array_keys($this->tokens) as $service) { |
|
81 | 81 | unset($this->tokens[$service]); |
82 | 82 | } |
83 | 83 | |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | */ |
101 | 101 | public function getCSRFState(string $service = null):string{ |
102 | 102 | |
103 | - if($this->hasCSRFState($service)){ |
|
103 | + if ($this->hasCSRFState($service)) { |
|
104 | 104 | return $this->states[$this->getServiceName($service)]; |
105 | 105 | } |
106 | 106 | |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | public function clearCSRFState(string $service = null):OAuthStorageInterface{ |
123 | 123 | $serviceName = $this->getServiceName($service); |
124 | 124 | |
125 | - if(array_key_exists($serviceName, $this->states)){ |
|
125 | + if (array_key_exists($serviceName, $this->states)) { |
|
126 | 126 | unset($this->states[$serviceName]); |
127 | 127 | } |
128 | 128 |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | /** |
24 | 24 | * Implements a session storage adapter. Session storage is half persistent as tokens are stored for the duration of the session. |
25 | 25 | */ |
26 | -class SessionStorage extends OAuthStorageAbstract{ |
|
26 | +class SessionStorage extends OAuthStorageAbstract { |
|
27 | 27 | |
28 | 28 | /** |
29 | 29 | * the key name for the token storage array in $_SESSION |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | /** |
39 | 39 | * SessionStorage constructor. |
40 | 40 | */ |
41 | - public function __construct(OAuthOptions|SettingsContainerInterface $options = null){ |
|
41 | + public function __construct(OAuthOptions|SettingsContainerInterface $options = null) { |
|
42 | 42 | parent::__construct($options); |
43 | 43 | |
44 | 44 | $this->tokenVar = $this->options->sessionTokenVar; |
@@ -46,15 +46,15 @@ discard block |
||
46 | 46 | |
47 | 47 | // Determine if the session has started. |
48 | 48 | // @link http://stackoverflow.com/a/18542272/1470961 |
49 | - if($this->options->sessionStart && !(session_status() !== PHP_SESSION_NONE)){ |
|
49 | + if ($this->options->sessionStart && !(session_status() !== PHP_SESSION_NONE)) { |
|
50 | 50 | session_start(); |
51 | 51 | } |
52 | 52 | |
53 | - if(!isset($_SESSION[$this->tokenVar])){ |
|
53 | + if (!isset($_SESSION[$this->tokenVar])) { |
|
54 | 54 | $_SESSION[$this->tokenVar] = []; |
55 | 55 | } |
56 | 56 | |
57 | - if(!isset($_SESSION[$this->stateVar])){ |
|
57 | + if (!isset($_SESSION[$this->stateVar])) { |
|
58 | 58 | $_SESSION[$this->stateVar] = []; |
59 | 59 | } |
60 | 60 | |
@@ -65,8 +65,8 @@ discard block |
||
65 | 65 | * |
66 | 66 | * @codeCoverageIgnore |
67 | 67 | */ |
68 | - public function __destruct(){ |
|
69 | - if($this->options->sessionStart){ |
|
68 | + public function __destruct() { |
|
69 | + if ($this->options->sessionStart) { |
|
70 | 70 | session_write_close(); |
71 | 71 | } |
72 | 72 | } |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | */ |
86 | 86 | public function getAccessToken(string $service = null):AccessToken{ |
87 | 87 | |
88 | - if($this->hasAccessToken($service)){ |
|
88 | + if ($this->hasAccessToken($service)) { |
|
89 | 89 | return $this->fromStorage($_SESSION[$this->tokenVar][$this->getServiceName($service)]); |
90 | 90 | } |
91 | 91 | |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | public function clearAccessToken(string $service = null):OAuthStorageInterface{ |
106 | 106 | $serviceName = $this->getServiceName($service); |
107 | 107 | |
108 | - if(array_key_exists($serviceName, $_SESSION[$this->tokenVar])){ |
|
108 | + if (array_key_exists($serviceName, $_SESSION[$this->tokenVar])) { |
|
109 | 109 | unset($_SESSION[$this->tokenVar][$serviceName]); |
110 | 110 | } |
111 | 111 | |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | */ |
118 | 118 | public function clearAllAccessTokens():OAuthStorageInterface{ |
119 | 119 | |
120 | - foreach(array_keys($_SESSION[$this->tokenVar]) as $service){ |
|
120 | + foreach (array_keys($_SESSION[$this->tokenVar]) as $service) { |
|
121 | 121 | unset($_SESSION[$this->tokenVar][$service]); |
122 | 122 | } |
123 | 123 | |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | */ |
141 | 141 | public function getCSRFState(string $service = null):string{ |
142 | 142 | |
143 | - if($this->hasCSRFState($service)){ |
|
143 | + if ($this->hasCSRFState($service)) { |
|
144 | 144 | return $_SESSION[$this->stateVar][$this->getServiceName($service)]; |
145 | 145 | } |
146 | 146 | |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | public function clearCSRFState(string $service = null):OAuthStorageInterface{ |
161 | 161 | $serviceName = $this->getServiceName($service); |
162 | 162 | |
163 | - if(array_key_exists($serviceName, $_SESSION[$this->stateVar])){ |
|
163 | + if (array_key_exists($serviceName, $_SESSION[$this->stateVar])) { |
|
164 | 164 | unset($_SESSION[$this->stateVar][$serviceName]); |
165 | 165 | } |
166 | 166 |
@@ -16,7 +16,7 @@ |
||
16 | 16 | /** |
17 | 17 | * Specifies the methods required for an OAuth storage adapter |
18 | 18 | */ |
19 | -interface OAuthStorageInterface extends LoggerAwareInterface{ |
|
19 | +interface OAuthStorageInterface extends LoggerAwareInterface { |
|
20 | 20 | |
21 | 21 | /** |
22 | 22 | * Sets the current service provider name |
@@ -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(!empty($scopes)){ |
|
213 | + if (!empty($scopes)) { |
|
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 | |
@@ -243,17 +243,17 @@ discard block |
||
243 | 243 | */ |
244 | 244 | public function refreshAccessToken(AccessToken $token = null):AccessToken{ |
245 | 245 | |
246 | - if(!$this instanceof TokenRefresh){ |
|
246 | + if (!$this instanceof TokenRefresh) { |
|
247 | 247 | throw new ProviderException('token refresh not supported'); |
248 | 248 | } |
249 | 249 | |
250 | - if($token === null){ |
|
250 | + if ($token === null) { |
|
251 | 251 | $token = $this->storage->getAccessToken($this->serviceName); |
252 | 252 | } |
253 | 253 | |
254 | 254 | $refreshToken = $token->refreshToken; |
255 | 255 | |
256 | - if(empty($refreshToken)){ |
|
256 | + if (empty($refreshToken)) { |
|
257 | 257 | throw new ProviderException( |
258 | 258 | sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires)) |
259 | 259 | ); |
@@ -274,13 +274,13 @@ discard block |
||
274 | 274 | ->withBody($this->streamFactory->createStream(QueryUtil::build($body, PHP_QUERY_RFC1738))) |
275 | 275 | ; |
276 | 276 | |
277 | - foreach($this->authHeaders as $header => $value){ |
|
277 | + foreach ($this->authHeaders as $header => $value) { |
|
278 | 278 | $request = $request->withAddedHeader($header, $value); |
279 | 279 | } |
280 | 280 | |
281 | 281 | $newToken = $this->parseTokenResponse($this->http->sendRequest($request)); |
282 | 282 | |
283 | - if(empty($newToken->refreshToken)){ |
|
283 | + if (empty($newToken->refreshToken)) { |
|
284 | 284 | $newToken->refreshToken = $refreshToken; |
285 | 285 | } |
286 | 286 | |
@@ -300,17 +300,17 @@ discard block |
||
300 | 300 | */ |
301 | 301 | public function checkState(string $state = null):void{ |
302 | 302 | |
303 | - if(!$this instanceof CSRFToken){ |
|
303 | + if (!$this instanceof CSRFToken) { |
|
304 | 304 | throw new ProviderException('CSRF protection not supported'); |
305 | 305 | } |
306 | 306 | |
307 | - if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){ |
|
307 | + if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) { |
|
308 | 308 | throw new ProviderException('invalid state for '.$this->serviceName); |
309 | 309 | } |
310 | 310 | |
311 | 311 | $knownState = $this->storage->getCSRFState($this->serviceName); |
312 | 312 | |
313 | - if(!hash_equals($knownState, $state)){ |
|
313 | + if (!hash_equals($knownState, $state)) { |
|
314 | 314 | throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state); |
315 | 315 | } |
316 | 316 | |
@@ -327,11 +327,11 @@ discard block |
||
327 | 327 | */ |
328 | 328 | public function setState(array $params):array{ |
329 | 329 | |
330 | - if(!$this instanceof CSRFToken){ |
|
330 | + if (!$this instanceof CSRFToken) { |
|
331 | 331 | throw new ProviderException('CSRF protection not supported'); |
332 | 332 | } |
333 | 333 | |
334 | - if(!isset($params['state'])){ |
|
334 | + if (!isset($params['state'])) { |
|
335 | 335 | $params['state'] = sha1(random_bytes(256)); |
336 | 336 | } |
337 | 337 |
@@ -27,7 +27,7 @@ |
||
27 | 27 | * @property string|null $applicationURL |
28 | 28 | * @property string|null $userRevokeURL |
29 | 29 | */ |
30 | -interface OAuthInterface extends ClientInterface{ |
|
30 | +interface OAuthInterface extends ClientInterface { |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * Prepares the URL with optional $params which redirects to the provider's authorization prompt |
@@ -13,7 +13,7 @@ |
||
13 | 13 | /** |
14 | 14 | * Indicates whether the service is capable of invalidating access tokens |
15 | 15 | */ |
16 | -interface TokenInvalidate{ |
|
16 | +interface TokenInvalidate { |
|
17 | 17 | |
18 | 18 | /** |
19 | 19 | * Allows to invalidate an access token |
@@ -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 | * Attempts to refresh an existing AccessToken with an associated refresh token and returns a fresh AccessToken. |