@@ -12,7 +12,7 @@ |
||
12 | 12 | |
13 | 13 | namespace chillerlan\OAuth\Core; |
14 | 14 | |
15 | -interface TokenRefresh{ |
|
15 | +interface TokenRefresh { |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * Tries to refresh an existing access token with an associated refresh token |
@@ -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 | * Obtains an OAuth1 request token and returns an AccessToken |
@@ -12,7 +12,7 @@ |
||
12 | 12 | |
13 | 13 | namespace chillerlan\OAuth\Core; |
14 | 14 | |
15 | -interface ClientCredentials{ |
|
15 | +interface ClientCredentials { |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * Obtains an OAuth2 client credentials token and returns an AccessToken |
@@ -15,7 +15,7 @@ |
||
15 | 15 | use chillerlan\OAuth\Core\AccessToken; |
16 | 16 | use Psr\Log\LoggerAwareInterface; |
17 | 17 | |
18 | -interface OAuthStorageInterface extends LoggerAwareInterface{ |
|
18 | +interface OAuthStorageInterface extends LoggerAwareInterface { |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * Stores an AccessToken for the given $service |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | |
19 | 19 | use function is_string; |
20 | 20 | |
21 | -abstract class OAuthStorageAbstract implements OAuthStorageInterface{ |
|
21 | +abstract class OAuthStorageAbstract implements OAuthStorageInterface { |
|
22 | 22 | use LoggerAwareTrait; |
23 | 23 | |
24 | 24 | /** |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * @param \chillerlan\Settings\SettingsContainerInterface|null $options |
33 | 33 | * @param \Psr\Log\LoggerInterface|null $logger |
34 | 34 | */ |
35 | - public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null){ |
|
35 | + public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null) { |
|
36 | 36 | $this->options = $options ?? new OAuthOptions; |
37 | 37 | |
38 | 38 | $this->setLogger($logger ?? new NullLogger); |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public function fromStorage($data):AccessToken{ |
56 | 56 | |
57 | - if(!is_string($data)){ |
|
57 | + if (!is_string($data)) { |
|
58 | 58 | throw new OAuthStorageException('invalid data'); |
59 | 59 | } |
60 | 60 |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | |
17 | 17 | use function array_keys, array_key_exists; |
18 | 18 | |
19 | -class MemoryStorage extends OAuthStorageAbstract{ |
|
19 | +class MemoryStorage extends OAuthStorageAbstract { |
|
20 | 20 | |
21 | 21 | /** |
22 | 22 | * @var array |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | */ |
43 | 43 | public function getAccessToken(string $service):AccessToken{ |
44 | 44 | |
45 | - if($this->hasAccessToken($service)){ |
|
45 | + if ($this->hasAccessToken($service)) { |
|
46 | 46 | return $this->tokens[$service]; |
47 | 47 | } |
48 | 48 | |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | */ |
62 | 62 | public function clearAccessToken(string $service):bool{ |
63 | 63 | |
64 | - if(array_key_exists($service, $this->tokens)){ |
|
64 | + if (array_key_exists($service, $this->tokens)) { |
|
65 | 65 | unset($this->tokens[$service]); |
66 | 66 | } |
67 | 67 | |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | */ |
74 | 74 | public function clearAllAccessTokens():bool{ |
75 | 75 | |
76 | - foreach(array_keys($this->tokens) as $service){ |
|
76 | + foreach (array_keys($this->tokens) as $service) { |
|
77 | 77 | unset($this->tokens[$service]); |
78 | 78 | } |
79 | 79 | |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | */ |
97 | 97 | public function getCSRFState(string $service):string{ |
98 | 98 | |
99 | - if($this->hasCSRFState($service)){ |
|
99 | + if ($this->hasCSRFState($service)) { |
|
100 | 100 | return $this->states[$service]; |
101 | 101 | } |
102 | 102 | |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | */ |
116 | 116 | public function clearCSRFState(string $service):bool{ |
117 | 117 | |
118 | - if(array_key_exists($service, $this->states)){ |
|
118 | + if (array_key_exists($service, $this->states)) { |
|
119 | 119 | unset($this->states[$service]); |
120 | 120 | } |
121 | 121 |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | |
20 | 20 | use const PHP_SESSION_NONE; |
21 | 21 | |
22 | -class SessionStorage extends OAuthStorageAbstract{ |
|
22 | +class SessionStorage extends OAuthStorageAbstract { |
|
23 | 23 | |
24 | 24 | /** |
25 | 25 | * @var string |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | * |
37 | 37 | * @param \chillerlan\Settings\SettingsContainerInterface|null $options |
38 | 38 | */ |
39 | - public function __construct(SettingsContainerInterface $options = null){ |
|
39 | + public function __construct(SettingsContainerInterface $options = null) { |
|
40 | 40 | parent::__construct($options); |
41 | 41 | |
42 | 42 | $this->sessionVar = $this->options->sessionTokenVar; |
@@ -44,15 +44,15 @@ discard block |
||
44 | 44 | |
45 | 45 | // Determine if the session has started. |
46 | 46 | // @link http://stackoverflow.com/a/18542272/1470961 |
47 | - if($this->options->sessionStart && !(session_status() !== PHP_SESSION_NONE)){ |
|
47 | + if ($this->options->sessionStart && !(session_status() !== PHP_SESSION_NONE)) { |
|
48 | 48 | session_start(); |
49 | 49 | } |
50 | 50 | |
51 | - if(!isset($_SESSION[$this->sessionVar])){ |
|
51 | + if (!isset($_SESSION[$this->sessionVar])) { |
|
52 | 52 | $_SESSION[$this->sessionVar] = []; |
53 | 53 | } |
54 | 54 | |
55 | - if(!isset($_SESSION[$this->stateVar])){ |
|
55 | + if (!isset($_SESSION[$this->stateVar])) { |
|
56 | 56 | $_SESSION[$this->stateVar] = []; |
57 | 57 | } |
58 | 58 | |
@@ -63,8 +63,8 @@ discard block |
||
63 | 63 | * |
64 | 64 | * @codeCoverageIgnore |
65 | 65 | */ |
66 | - public function __destruct(){ |
|
67 | - if($this->options->sessionStart){ |
|
66 | + public function __destruct() { |
|
67 | + if ($this->options->sessionStart) { |
|
68 | 68 | session_write_close(); |
69 | 69 | } |
70 | 70 | } |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | */ |
84 | 84 | public function getAccessToken(string $service):AccessToken{ |
85 | 85 | |
86 | - if($this->hasAccessToken($service)){ |
|
86 | + if ($this->hasAccessToken($service)) { |
|
87 | 87 | return $this->fromStorage($_SESSION[$this->sessionVar][$service]); |
88 | 88 | } |
89 | 89 | |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | */ |
103 | 103 | public function clearAccessToken(string $service):bool{ |
104 | 104 | |
105 | - if(array_key_exists($service, $_SESSION[$this->sessionVar])){ |
|
105 | + if (array_key_exists($service, $_SESSION[$this->sessionVar])) { |
|
106 | 106 | unset($_SESSION[$this->sessionVar][$service]); |
107 | 107 | } |
108 | 108 | |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | */ |
115 | 115 | public function clearAllAccessTokens():bool{ |
116 | 116 | |
117 | - foreach(array_keys($_SESSION[$this->sessionVar]) as $service){ |
|
117 | + foreach (array_keys($_SESSION[$this->sessionVar]) as $service) { |
|
118 | 118 | unset($_SESSION[$this->sessionVar][$service]); |
119 | 119 | } |
120 | 120 | |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | */ |
138 | 138 | public function getCSRFState(string $service):string{ |
139 | 139 | |
140 | - if($this->hasCSRFState($service)){ |
|
140 | + if ($this->hasCSRFState($service)) { |
|
141 | 141 | return $_SESSION[$this->stateVar][$service]; |
142 | 142 | } |
143 | 143 | |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | */ |
157 | 157 | public function clearCSRFState(string $service):bool{ |
158 | 158 | |
159 | - if(array_key_exists($service, $_SESSION[$this->stateVar])){ |
|
159 | + if (array_key_exists($service, $_SESSION[$this->stateVar])) { |
|
160 | 160 | unset($_SESSION[$this->stateVar][$service]); |
161 | 161 | } |
162 | 162 |
@@ -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 AUTH_METHOD_HEADER = 1; |
20 | 20 | const AUTH_METHOD_QUERY = 2; |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | |
23 | 23 | use const PHP_QUERY_RFC1738; |
24 | 24 | |
25 | -abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{ |
|
25 | +abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface { |
|
26 | 26 | |
27 | 27 | /** |
28 | 28 | * @var int |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | public function getAuthURL(array $params = null, array $scopes = null):UriInterface{ |
61 | 61 | $params = $params ?? []; |
62 | 62 | |
63 | - if(isset($params['client_secret'])){ |
|
63 | + if (isset($params['client_secret'])) { |
|
64 | 64 | unset($params['client_secret']); |
65 | 65 | } |
66 | 66 | |
@@ -71,11 +71,11 @@ discard block |
||
71 | 71 | 'type' => 'web_server', |
72 | 72 | ]); |
73 | 73 | |
74 | - if(!empty($scopes)){ |
|
74 | + if (!empty($scopes)) { |
|
75 | 75 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
76 | 76 | } |
77 | 77 | |
78 | - if($this instanceof CSRFToken){ |
|
78 | + if ($this instanceof CSRFToken) { |
|
79 | 79 | $params = $this->setState($params); |
80 | 80 | } |
81 | 81 | |
@@ -91,19 +91,19 @@ discard block |
||
91 | 91 | protected function parseTokenResponse(ResponseInterface $response):AccessToken{ |
92 | 92 | $data = json_decode(decompress_content($response), true); // silly amazon... |
93 | 93 | |
94 | - if(!is_array($data)){ |
|
94 | + if (!is_array($data)) { |
|
95 | 95 | throw new ProviderException('unable to parse token response'); |
96 | 96 | } |
97 | 97 | |
98 | - foreach(['error_description', 'error'] as $field){ |
|
98 | + foreach (['error_description', 'error'] as $field) { |
|
99 | 99 | |
100 | - if(isset($data[$field])){ |
|
100 | + if (isset($data[$field])) { |
|
101 | 101 | throw new ProviderException('error retrieving access token: "'.$data[$field].'"'); |
102 | 102 | } |
103 | 103 | |
104 | 104 | } |
105 | 105 | |
106 | - if(!isset($data['access_token'])){ |
|
106 | + if (!isset($data['access_token'])) { |
|
107 | 107 | throw new ProviderException('token missing'); |
108 | 108 | } |
109 | 109 | |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | */ |
127 | 127 | public function getAccessToken(string $code, string $state = null):AccessToken{ |
128 | 128 | |
129 | - if($this instanceof CSRFToken){ |
|
129 | + if ($this instanceof CSRFToken) { |
|
130 | 130 | $this->checkState($state); |
131 | 131 | } |
132 | 132 | |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | ->withHeader('Accept-Encoding', 'identity') |
145 | 145 | ->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738))); |
146 | 146 | |
147 | - foreach($this->authHeaders as $header => $value){ |
|
147 | + foreach ($this->authHeaders as $header => $value) { |
|
148 | 148 | $request = $request->withHeader($header, $value); |
149 | 149 | } |
150 | 150 | |
@@ -160,11 +160,11 @@ discard block |
||
160 | 160 | */ |
161 | 161 | public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{ |
162 | 162 | |
163 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){ |
|
163 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER) { |
|
164 | 164 | return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken); |
165 | 165 | } |
166 | 166 | |
167 | - if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){ |
|
167 | + if ($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY) { |
|
168 | 168 | $uri = merge_query($request->getUri()->__toString(), [$this->authMethodQuery => $token->accessToken]); |
169 | 169 | |
170 | 170 | return $request->withUri($this->uriFactory->createUri($uri)); |
@@ -179,13 +179,13 @@ discard block |
||
179 | 179 | */ |
180 | 180 | public function getClientCredentialsToken(array $scopes = null):AccessToken{ |
181 | 181 | |
182 | - if(!$this instanceof ClientCredentials){ |
|
182 | + if (!$this instanceof ClientCredentials) { |
|
183 | 183 | throw new ProviderException('client credentials token not supported'); |
184 | 184 | } |
185 | 185 | |
186 | 186 | $params = ['grant_type' => 'client_credentials']; |
187 | 187 | |
188 | - if($scopes !== null){ |
|
188 | + if ($scopes !== null) { |
|
189 | 189 | $params['scope'] = implode($this->scopesDelimiter, $scopes); |
190 | 190 | } |
191 | 191 | |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | ->withBody($this->streamFactory->createStream(http_build_query($params, '', '&', PHP_QUERY_RFC1738))) |
198 | 198 | ; |
199 | 199 | |
200 | - foreach($this->authHeaders as $header => $value){ |
|
200 | + foreach ($this->authHeaders as $header => $value) { |
|
201 | 201 | $request = $request->withAddedHeader($header, $value); |
202 | 202 | } |
203 | 203 | |
@@ -214,17 +214,17 @@ discard block |
||
214 | 214 | */ |
215 | 215 | public function refreshAccessToken(AccessToken $token = null):AccessToken{ |
216 | 216 | |
217 | - if(!$this instanceof TokenRefresh){ |
|
217 | + if (!$this instanceof TokenRefresh) { |
|
218 | 218 | throw new ProviderException('token refresh not supported'); |
219 | 219 | } |
220 | 220 | |
221 | - if($token === null){ |
|
221 | + if ($token === null) { |
|
222 | 222 | $token = $this->storage->getAccessToken($this->serviceName); |
223 | 223 | } |
224 | 224 | |
225 | 225 | $refreshToken = $token->refreshToken; |
226 | 226 | |
227 | - if(empty($refreshToken)){ |
|
227 | + if (empty($refreshToken)) { |
|
228 | 228 | throw new ProviderException(sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires))); |
229 | 229 | } |
230 | 230 | |
@@ -243,13 +243,13 @@ discard block |
||
243 | 243 | ->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738))) |
244 | 244 | ; |
245 | 245 | |
246 | - foreach($this->authHeaders as $header => $value){ |
|
246 | + foreach ($this->authHeaders as $header => $value) { |
|
247 | 247 | $request = $request->withAddedHeader($header, $value); |
248 | 248 | } |
249 | 249 | |
250 | 250 | $newToken = $this->parseTokenResponse($this->http->sendRequest($request)); |
251 | 251 | |
252 | - if(empty($newToken->refreshToken)){ |
|
252 | + if (empty($newToken->refreshToken)) { |
|
253 | 253 | $newToken->refreshToken = $refreshToken; |
254 | 254 | } |
255 | 255 | |
@@ -266,13 +266,13 @@ discard block |
||
266 | 266 | */ |
267 | 267 | protected function checkState(string $state = null):void{ |
268 | 268 | |
269 | - if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){ |
|
269 | + if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) { |
|
270 | 270 | throw new ProviderException('invalid state for '.$this->serviceName); |
271 | 271 | } |
272 | 272 | |
273 | 273 | $knownState = $this->storage->getCSRFState($this->serviceName); |
274 | 274 | |
275 | - if(!hash_equals($knownState, $state)){ |
|
275 | + if (!hash_equals($knownState, $state)) { |
|
276 | 276 | throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state); |
277 | 277 | } |
278 | 278 | |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | */ |
286 | 286 | protected function setState(array $params):array{ |
287 | 287 | |
288 | - if(!isset($params['state'])){ |
|
288 | + if (!isset($params['state'])) { |
|
289 | 289 | $params['state'] = sha1(random_bytes(256)); |
290 | 290 | } |
291 | 291 |