Passed
Push — main ( 02f08d...e8bbca )
by smiley
01:38
created
src/Core/OAuth2Provider.php 1 patch
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -27,7 +27,7 @@  discard block
 block discarded – undo
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:
@@ -78,11 +78,11 @@  discard block
 block discarded – undo
78 78
 			'type'          => 'web_server',
79 79
 		]);
80 80
 
81
-		if(!empty($scopes)){
81
+		if (!empty($scopes)) {
82 82
 			$params['scope'] = implode($this->scopesDelimiter, $scopes);
83 83
 		}
84 84
 
85
-		if($this instanceof CSRFToken){
85
+		if ($this instanceof CSRFToken) {
86 86
 			$params = $this->setState($params);
87 87
 		}
88 88
 
@@ -102,19 +102,19 @@  discard block
 block discarded – undo
102 102
 	protected function parseTokenResponse(ResponseInterface $response):AccessToken{
103 103
 		$data = json_decode(decompress_content($response), true); // silly amazon...
104 104
 
105
-		if(!is_array($data)){
105
+		if (!is_array($data)) {
106 106
 			throw new ProviderException('unable to parse token response');
107 107
 		}
108 108
 
109
-		foreach(['error_description', 'error'] as $field){
109
+		foreach (['error_description', 'error'] as $field) {
110 110
 
111
-			if(isset($data[$field])){
111
+			if (isset($data[$field])) {
112 112
 				throw new ProviderException('error retrieving access token: "'.$data[$field].'"');
113 113
 			}
114 114
 
115 115
 		}
116 116
 
117
-		if(!isset($data['access_token'])){
117
+		if (!isset($data['access_token'])) {
118 118
 			throw new ProviderException('token missing');
119 119
 		}
120 120
 
@@ -137,7 +137,7 @@  discard block
 block discarded – undo
137 137
 	 */
138 138
 	public function getAccessToken(string $code, string $state = null):AccessToken{
139 139
 
140
-		if($this instanceof CSRFToken){
140
+		if ($this instanceof CSRFToken) {
141 141
 			$this->checkState($state);
142 142
 		}
143 143
 
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
 			->withHeader('Accept-Encoding', 'identity')
156 156
 			->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738)));
157 157
 
158
-		foreach($this->authHeaders as $header => $value){
158
+		foreach ($this->authHeaders as $header => $value) {
159 159
 			$request = $request->withHeader($header, $value);
160 160
 		}
161 161
 
@@ -171,11 +171,11 @@  discard block
 block discarded – undo
171 171
 	 */
172 172
 	public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{
173 173
 
174
-		if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){
174
+		if ($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER) {
175 175
 			return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken);
176 176
 		}
177 177
 
178
-		if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){
178
+		if ($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY) {
179 179
 			$uri = merge_query((string)$request->getUri(), [$this->authMethodQuery => $token->accessToken]);
180 180
 
181 181
 			return $request->withUri($this->uriFactory->createUri($uri));
@@ -196,13 +196,13 @@  discard block
 block discarded – undo
196 196
 	 */
197 197
 	public function getClientCredentialsToken(array $scopes = null):AccessToken{
198 198
 
199
-		if(!$this instanceof ClientCredentials){
199
+		if (!$this instanceof ClientCredentials) {
200 200
 			throw new ProviderException('client credentials token not supported');
201 201
 		}
202 202
 
203 203
 		$params = ['grant_type' => 'client_credentials'];
204 204
 
205
-		if($scopes !== null){
205
+		if ($scopes !== null) {
206 206
 			$params['scope'] = implode($this->scopesDelimiter, $scopes);
207 207
 		}
208 208
 
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
 			->withBody($this->streamFactory->createStream(http_build_query($params, '', '&', PHP_QUERY_RFC1738)))
215 215
 		;
216 216
 
217
-		foreach($this->authHeaders as $header => $value){
217
+		foreach ($this->authHeaders as $header => $value) {
218 218
 			$request = $request->withAddedHeader($header, $value);
219 219
 		}
220 220
 
@@ -237,17 +237,17 @@  discard block
 block discarded – undo
237 237
 	 */
238 238
 	public function refreshAccessToken(AccessToken $token = null):AccessToken{
239 239
 
240
-		if(!$this instanceof TokenRefresh){
240
+		if (!$this instanceof TokenRefresh) {
241 241
 			throw new ProviderException('token refresh not supported');
242 242
 		}
243 243
 
244
-		if($token === null){
244
+		if ($token === null) {
245 245
 			$token = $this->storage->getAccessToken($this->serviceName);
246 246
 		}
247 247
 
248 248
 		$refreshToken = $token->refreshToken;
249 249
 
250
-		if(empty($refreshToken)){
250
+		if (empty($refreshToken)) {
251 251
 			throw new ProviderException(
252 252
 				sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires))
253 253
 			);
@@ -268,13 +268,13 @@  discard block
 block discarded – undo
268 268
 			->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738)))
269 269
 		;
270 270
 
271
-		foreach($this->authHeaders as $header => $value){
271
+		foreach ($this->authHeaders as $header => $value) {
272 272
 			$request = $request->withAddedHeader($header, $value);
273 273
 		}
274 274
 
275 275
 		$newToken = $this->parseTokenResponse($this->http->sendRequest($request));
276 276
 
277
-		if(empty($newToken->refreshToken)){
277
+		if (empty($newToken->refreshToken)) {
278 278
 			$newToken->refreshToken = $refreshToken;
279 279
 		}
280 280
 
@@ -297,17 +297,17 @@  discard block
 block discarded – undo
297 297
 	 */
298 298
 	public function checkState(string $state = null):void{
299 299
 
300
-		if(!$this instanceof CSRFToken){
300
+		if (!$this instanceof CSRFToken) {
301 301
 			throw new ProviderException('CSRF protection not supported');
302 302
 		}
303 303
 
304
-		if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){
304
+		if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) {
305 305
 			throw new ProviderException('invalid state for '.$this->serviceName);
306 306
 		}
307 307
 
308 308
 		$knownState = $this->storage->getCSRFState($this->serviceName);
309 309
 
310
-		if(!hash_equals($knownState, $state)){
310
+		if (!hash_equals($knownState, $state)) {
311 311
 			throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state);
312 312
 		}
313 313
 
@@ -327,11 +327,11 @@  discard block
 block discarded – undo
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
 
Please login to merge, or discard this patch.
src/Core/OAuth1Provider.php 1 patch
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -22,7 +22,7 @@  discard block
 block discarded – undo
22 22
 /**
23 23
  * Implements an abstract OAuth1 provider with all methods required by the OAuth1Interface.
24 24
  */
25
-abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{
25
+abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface {
26 26
 
27 27
 	/**
28 28
 	 * The request OAuth1 token URL
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
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
 
@@ -81,20 +81,20 @@  discard block
 block discarded – undo
81 81
 	protected function parseTokenResponse(ResponseInterface $response, bool $checkCallbackConfirmed = null):AccessToken{
82 82
 		parse_str(decompress_content($response), $data);
83 83
 
84
-		if(!$data || !is_array($data)){
84
+		if (!$data || !is_array($data)) {
85 85
 			throw new ProviderException('unable to parse token response');
86 86
 		}
87
-		elseif(isset($data['error'])){
87
+		elseif (isset($data['error'])) {
88 88
 			throw new ProviderException('error retrieving access token: '.$data['error']);
89 89
 		}
90
-		elseif(!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])){
90
+		elseif (!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])) {
91 91
 			throw new ProviderException('invalid token');
92 92
 		}
93 93
 
94
-		if(
94
+		if (
95 95
 			$checkCallbackConfirmed
96 96
 			&& (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')
97
-		){
97
+		) {
98 98
 			throw new ProviderException('oauth callback unconfirmed');
99 99
 		}
100 100
 
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
 	protected function getSignature(string $url, array $params, string $method, string $accessTokenSecret = null):string{
145 145
 		$parseURL = parse_url($url);
146 146
 
147
-		if(!isset($parseURL['host']) || !isset($parseURL['scheme']) || !in_array($parseURL['scheme'], ['http', 'https'], true)){
147
+		if (!isset($parseURL['host']) || !isset($parseURL['scheme']) || !in_array($parseURL['scheme'], ['http', 'https'], true)) {
148 148
 			throw new ProviderException('getSignature: invalid url');
149 149
 		}
150 150
 
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
 			$token->accessTokenSecret
204 204
 		);
205 205
 
206
-		if(isset($query['oauth_session_handle'])){
206
+		if (isset($query['oauth_session_handle'])) {
207 207
 			$parameters['oauth_session_handle'] = $query['oauth_session_handle']; // @codeCoverageIgnore
208 208
 		}
209 209
 
Please login to merge, or discard this patch.
src/Core/OAuthProvider.php 1 patch
Spacing   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
  * @property string                                          $serviceName
44 44
  * @property string|null                                     $userRevokeURL
45 45
  */
46
-abstract class OAuthProvider implements OAuthInterface{
46
+abstract class OAuthProvider implements OAuthInterface {
47 47
 	use LoggerAwareTrait;
48 48
 
49 49
 	protected const ALLOWED_PROPERTIES = [
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
 		OAuthStorageInterface $storage,
158 158
 		SettingsContainerInterface $options,
159 159
 		LoggerInterface $logger = null
160
-	){
160
+	) {
161 161
 		$this->http    = $http;
162 162
 		$this->storage = $storage;
163 163
 		$this->options = $options;
@@ -169,10 +169,10 @@  discard block
 block discarded – undo
169 169
 
170 170
 		$this->serviceName = (new ReflectionClass($this))->getShortName();
171 171
 
172
-		if(!empty($this->endpointMap) && class_exists($this->endpointMap)){
172
+		if (!empty($this->endpointMap) && class_exists($this->endpointMap)) {
173 173
 			$this->endpoints = new $this->endpointMap;
174 174
 
175
-			if(!$this->endpoints instanceof EndpointMapInterface){
175
+			if (!$this->endpoints instanceof EndpointMapInterface) {
176 176
 				throw new ApiClientException('invalid endpoint map'); // @codeCoverageIgnore
177 177
 			}
178 178
 
@@ -187,9 +187,9 @@  discard block
 block discarded – undo
187 187
 	 *
188 188
 	 * @return mixed|null
189 189
 	 */
190
-	public function __get(string $name){
190
+	public function __get(string $name) {
191 191
 
192
-		if(in_array($name, $this::ALLOWED_PROPERTIES, true)){
192
+		if (in_array($name, $this::ALLOWED_PROPERTIES, true)) {
193 193
 			return $this->{$name};
194 194
 		}
195 195
 
@@ -247,11 +247,11 @@  discard block
 block discarded – undo
247 247
 	 */
248 248
 	public function __call(string $endpointName, array $arguments):ResponseInterface{
249 249
 
250
-		if(!$this->endpoints instanceof EndpointMap){
250
+		if (!$this->endpoints instanceof EndpointMap) {
251 251
 			throw new ApiClientException('MagicAPI not available'); // @codeCoverageIgnore
252 252
 		}
253 253
 
254
-		if(!isset($this->endpoints->{$endpointName})){
254
+		if (!isset($this->endpoints->{$endpointName})) {
255 255
 			throw new ApiClientException('endpoint not found: "'.$endpointName.'"');
256 256
 		}
257 257
 
@@ -271,25 +271,25 @@  discard block
 block discarded – undo
271 271
 		$path_element_count = count($path_elements);
272 272
 		$query_param_count  = count($query_params);
273 273
 
274
-		if($path_element_count > 0){
274
+		if ($path_element_count > 0) {
275 275
 			$path = $this->parsePathElements($path, $path_elements, $path_element_count, $arguments);
276 276
 		}
277 277
 
278
-		if($query_param_count > 0){
278
+		if ($query_param_count > 0) {
279 279
 			// $params is the first argument after path segments
280 280
 			$params = $arguments[$path_element_count] ?? null;
281 281
 
282
-			if(is_array($params)){
282
+			if (is_array($params)) {
283 283
 				$params = $this->cleanQueryParams($this->removeUnlistedParams($params, $query_params));
284 284
 			}
285 285
 		}
286 286
 
287
-		if(in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE']) && $has_body){
287
+		if (in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE']) && $has_body) {
288 288
 			// if no query params are present, $body is the first argument after any path segments
289 289
 			$argPos = $query_param_count > 0 ? 1 : 0;
290 290
 			$body   = $arguments[$path_element_count + $argPos] ?? null;
291 291
 
292
-			if(is_array($body)){
292
+			if (is_array($body)) {
293 293
 				$body = $this->cleanBodyParams($body);
294 294
 			}
295 295
 		}
@@ -310,13 +310,13 @@  discard block
 block discarded – undo
310 310
 		// we don't know if all of the given arguments are path elements...
311 311
 		$urlparams = array_slice($arguments, 0, $path_element_count);
312 312
 
313
-		if(count($urlparams) !== $path_element_count){
313
+		if (count($urlparams) !== $path_element_count) {
314 314
 			throw new APIClientException('too few URL params, required: '.implode(', ', $path_elements));
315 315
 		}
316 316
 
317
-		foreach($urlparams as $i => $param){
317
+		foreach ($urlparams as $i => $param) {
318 318
 			// ...but we do know that the arguments after the path elements are usually array or null
319
-			if(!is_scalar($param)){
319
+			if (!is_scalar($param)) {
320 320
 				$msg = 'invalid path element value for "%s": %s';
321 321
 
322 322
 				throw new APIClientException(sprintf($msg, $path_elements[$i], var_export($param, true)));
@@ -332,9 +332,9 @@  discard block
 block discarded – undo
332 332
 	protected function removeUnlistedParams(array $params, array $allowed):array{
333 333
 		$query = [];
334 334
 		// remove any params that are not listed
335
-		foreach($params as $key => $value){
335
+		foreach ($params as $key => $value) {
336 336
 
337
-			if(!in_array($key, $allowed, true)){
337
+			if (!in_array($key, $allowed, true)) {
338 338
 				continue;
339 339
 			}
340 340
 
@@ -372,28 +372,28 @@  discard block
 block discarded – undo
372 372
 		$request = $this->requestFactory
373 373
 			->createRequest($method ?? 'GET', merge_query($this->getRequestTarget($path), $params ?? []));
374 374
 
375
-		foreach(array_merge($this->apiHeaders, $headers ?? []) as $header => $value){
375
+		foreach (array_merge($this->apiHeaders, $headers ?? []) as $header => $value) {
376 376
 			$request = $request->withAddedHeader($header, $value);
377 377
 		}
378 378
 
379
-		if($request->hasHeader('content-type')){
379
+		if ($request->hasHeader('content-type')) {
380 380
 			$contentType = strtolower($request->getHeaderLine('content-type'));
381 381
 
382
-			if(is_array($body)){
383
-				if($contentType === 'application/x-www-form-urlencoded'){
382
+			if (is_array($body)) {
383
+				if ($contentType === 'application/x-www-form-urlencoded') {
384 384
 					$body = $this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738));
385 385
 				}
386
-				elseif($contentType === 'application/json' || $contentType === 'application/vnd.api+json'){
386
+				elseif ($contentType === 'application/json' || $contentType === 'application/vnd.api+json') {
387 387
 					$body = $this->streamFactory->createStream(json_encode($body));
388 388
 				}
389 389
 			}
390
-			elseif(is_string($body)){
390
+			elseif (is_string($body)) {
391 391
 				// we don't check if the given string matches the content type - this is the implementor's responsibility
392 392
 				$body = $this->streamFactory->createStream($body);
393 393
 			}
394 394
 		}
395 395
 
396
-		if($body instanceof StreamInterface){
396
+		if ($body instanceof StreamInterface) {
397 397
 			$request = $request
398 398
 				->withBody($body)
399 399
 				->withHeader('Content-length', (string)$body->getSize())
@@ -415,15 +415,15 @@  discard block
 block discarded – undo
415 415
 	protected function getRequestTarget(string $uri):string{
416 416
 		$parsedURL = parse_url($uri);
417 417
 
418
-		if(!isset($parsedURL['path'])){
418
+		if (!isset($parsedURL['path'])) {
419 419
 			throw new ProviderException('invalid path');
420 420
 		}
421 421
 
422 422
 		// for some reason we were given a host name
423
-		if(isset($parsedURL['host'])){
423
+		if (isset($parsedURL['host'])) {
424 424
 
425 425
 			// back out if it doesn't match
426
-			if($parsedURL['host'] !== parse_url($this->apiURL, PHP_URL_HOST)){
426
+			if ($parsedURL['host'] !== parse_url($this->apiURL, PHP_URL_HOST)) {
427 427
 				throw new ProviderException('given host does not match provider host');
428 428
 			}
429 429
 
@@ -441,15 +441,15 @@  discard block
 block discarded – undo
441 441
 	public function sendRequest(RequestInterface $request):ResponseInterface{
442 442
 
443 443
 		// get authorization only if we request the provider API
444
-		if(strpos((string)$request->getUri(), $this->apiURL) === 0){
444
+		if (strpos((string)$request->getUri(), $this->apiURL) === 0) {
445 445
 			$token = $this->storage->getAccessToken($this->serviceName);
446 446
 
447 447
 			// attempt to refresh an expired token
448
-			if(
448
+			if (
449 449
 				$this instanceof TokenRefresh
450 450
 				&& $this->options->tokenAutoRefresh
451 451
 				&& ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)
452
-			){
452
+			) {
453 453
 				$token = $this->refreshAccessToken($token);
454 454
 			}
455 455
 
Please login to merge, or discard this patch.