Passed
Push — main ( c92679...338ec7 )
by smiley
10:22
created
src/Core/OAuthProvider.php 1 patch
Spacing   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -41,7 +41,7 @@  discard block
 block discarded – undo
41 41
  * @property string                                          $serviceName
42 42
  * @property string|null                                     $userRevokeURL
43 43
  */
44
-abstract class OAuthProvider implements OAuthInterface{
44
+abstract class OAuthProvider implements OAuthInterface {
45 45
 	use LoggerAwareTrait;
46 46
 
47 47
 	protected const ALLOWED_PROPERTIES = [
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
 		OAuthStorageInterface $storage,
151 151
 		SettingsContainerInterface $options,
152 152
 		LoggerInterface $logger = null
153
-	){
153
+	) {
154 154
 		$this->http    = $http;
155 155
 		$this->storage = $storage;
156 156
 		$this->options = $options;
@@ -164,10 +164,10 @@  discard block
 block discarded – undo
164 164
 
165 165
 		$this->serviceName = (new ReflectionClass($this))->getShortName();
166 166
 
167
-		if(!empty($this->endpointMap) && class_exists($this->endpointMap)){
167
+		if (!empty($this->endpointMap) && class_exists($this->endpointMap)) {
168 168
 			$this->endpoints = new $this->endpointMap;
169 169
 
170
-			if(!$this->endpoints instanceof EndpointMapInterface){
170
+			if (!$this->endpoints instanceof EndpointMapInterface) {
171 171
 				throw new ApiClientException('invalid endpoint map'); // @codeCoverageIgnore
172 172
 			}
173 173
 
@@ -180,9 +180,9 @@  discard block
 block discarded – undo
180 180
 	 *
181 181
 	 * @return mixed|null
182 182
 	 */
183
-	public function __get(string $name){
183
+	public function __get(string $name) {
184 184
 
185
-		if(in_array($name, $this::ALLOWED_PROPERTIES, true)){
185
+		if (in_array($name, $this::ALLOWED_PROPERTIES, true)) {
186 186
 			return $this->{$name};
187 187
 		}
188 188
 
@@ -251,11 +251,11 @@  discard block
 block discarded – undo
251 251
 	 */
252 252
 	public function __call(string $endpointName, array $arguments):ResponseInterface{
253 253
 
254
-		if(!$this->endpoints instanceof EndpointMap){
254
+		if (!$this->endpoints instanceof EndpointMap) {
255 255
 			throw new ApiClientException('MagicAPI not available'); // @codeCoverageIgnore
256 256
 		}
257 257
 
258
-		if(!isset($this->endpoints->{$endpointName})){
258
+		if (!isset($this->endpoints->{$endpointName})) {
259 259
 			throw new ApiClientException('endpoint not found: "'.$endpointName.'"');
260 260
 		}
261 261
 
@@ -275,25 +275,25 @@  discard block
 block discarded – undo
275 275
 		$path_element_count = count($path_elements);
276 276
 		$query_param_count  = count($query_params);
277 277
 
278
-		if($path_element_count > 0){
278
+		if ($path_element_count > 0) {
279 279
 			$path = $this->parsePathElements($path, $path_elements, $path_element_count, $arguments);
280 280
 		}
281 281
 
282
-		if($query_param_count > 0){
282
+		if ($query_param_count > 0) {
283 283
 			// $params is the first argument after path segments
284 284
 			$params = $arguments[$path_element_count] ?? null;
285 285
 
286
-			if(is_array($params)){
286
+			if (is_array($params)) {
287 287
 				$params = $this->cleanQueryParams($this->removeUnlistedParams($params, $query_params));
288 288
 			}
289 289
 		}
290 290
 
291
-		if(in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE']) && $has_body){
291
+		if (in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE']) && $has_body) {
292 292
 			// if no query params are present, $body is the first argument after any path segments
293 293
 			$argPos = $query_param_count > 0 ? 1 : 0;
294 294
 			$body   = $arguments[$path_element_count + $argPos] ?? null;
295 295
 
296
-			if(is_array($body)){
296
+			if (is_array($body)) {
297 297
 				$body = $this->cleanBodyParams($body);
298 298
 			}
299 299
 		}
@@ -314,13 +314,13 @@  discard block
 block discarded – undo
314 314
 		// we don't know if all of the given arguments are path elements...
315 315
 		$urlparams = array_slice($arguments, 0, $path_element_count);
316 316
 
317
-		if(count($urlparams) !== $path_element_count){
317
+		if (count($urlparams) !== $path_element_count) {
318 318
 			throw new APIClientException('too few URL params, required: '.implode(', ', $path_elements));
319 319
 		}
320 320
 
321
-		foreach($urlparams as $i => $param){
321
+		foreach ($urlparams as $i => $param) {
322 322
 			// ...but we do know that the arguments after the path elements are usually array or null
323
-			if(!is_scalar($param)){
323
+			if (!is_scalar($param)) {
324 324
 				$msg = 'invalid path element value for "%s": %s';
325 325
 
326 326
 				throw new APIClientException(sprintf($msg, $path_elements[$i], var_export($param, true)));
@@ -336,9 +336,9 @@  discard block
 block discarded – undo
336 336
 	protected function removeUnlistedParams(array $params, array $allowed):array{
337 337
 		$query = [];
338 338
 		// remove any params that are not listed
339
-		foreach($params as $key => $value){
339
+		foreach ($params as $key => $value) {
340 340
 
341
-			if(!in_array($key, $allowed, true)){
341
+			if (!in_array($key, $allowed, true)) {
342 342
 				continue;
343 343
 			}
344 344
 
@@ -397,28 +397,28 @@  discard block
 block discarded – undo
397 397
 		$request = $this->requestFactory
398 398
 			->createRequest($method ?? 'GET', $this->mergeQuery($this->getRequestTarget($path), $params ?? []));
399 399
 
400
-		foreach(array_merge($this->apiHeaders, $headers ?? []) as $header => $value){
400
+		foreach (array_merge($this->apiHeaders, $headers ?? []) as $header => $value) {
401 401
 			$request = $request->withAddedHeader($header, $value);
402 402
 		}
403 403
 
404
-		if($request->hasHeader('content-type')){
404
+		if ($request->hasHeader('content-type')) {
405 405
 			$contentType = strtolower($request->getHeaderLine('content-type'));
406 406
 
407
-			if(is_array($body)){
408
-				if($contentType === 'application/x-www-form-urlencoded'){
407
+			if (is_array($body)) {
408
+				if ($contentType === 'application/x-www-form-urlencoded') {
409 409
 					$body = $this->streamFactory->createStream($this->buildQuery($body, PHP_QUERY_RFC1738));
410 410
 				}
411
-				elseif($contentType === 'application/json' || $contentType === 'application/vnd.api+json'){
411
+				elseif ($contentType === 'application/json' || $contentType === 'application/vnd.api+json') {
412 412
 					$body = $this->streamFactory->createStream(json_encode($body));
413 413
 				}
414 414
 			}
415
-			elseif(is_string($body)){
415
+			elseif (is_string($body)) {
416 416
 				// we don't check if the given string matches the content type - this is the implementor's responsibility
417 417
 				$body = $this->streamFactory->createStream($body);
418 418
 			}
419 419
 		}
420 420
 
421
-		if($body instanceof StreamInterface){
421
+		if ($body instanceof StreamInterface) {
422 422
 			$request = $request
423 423
 				->withBody($body)
424 424
 				->withHeader('Content-length', (string)$body->getSize())
@@ -440,16 +440,16 @@  discard block
 block discarded – undo
440 440
 	protected function getRequestTarget(string $uri):string{
441 441
 		$parsedURL = QueryUtil::parseUrl($uri);
442 442
 
443
-		if(!isset($parsedURL['path'])){
443
+		if (!isset($parsedURL['path'])) {
444 444
 			throw new ProviderException('invalid path');
445 445
 		}
446 446
 
447 447
 		// for some reason we were given a host name
448
-		if(isset($parsedURL['host'])){
448
+		if (isset($parsedURL['host'])) {
449 449
 
450 450
 			// back out if it doesn't match
451 451
 			/** @phan-suppress-next-line PhanTypeArraySuspiciousNullable - $this->>apiURL should always return a host */
452
-			if($parsedURL['host'] !== QueryUtil::parseUrl($this->apiURL)['host']){
452
+			if ($parsedURL['host'] !== QueryUtil::parseUrl($this->apiURL)['host']) {
453 453
 				throw new ProviderException('given host does not match provider host');
454 454
 			}
455 455
 
@@ -467,15 +467,15 @@  discard block
 block discarded – undo
467 467
 	public function sendRequest(RequestInterface $request):ResponseInterface{
468 468
 
469 469
 		// get authorization only if we request the provider API
470
-		if(strpos((string)$request->getUri(), $this->apiURL) === 0){
470
+		if (strpos((string)$request->getUri(), $this->apiURL) === 0) {
471 471
 			$token = $this->storage->getAccessToken($this->serviceName);
472 472
 
473 473
 			// attempt to refresh an expired token
474
-			if(
474
+			if (
475 475
 				$this instanceof TokenRefresh
476 476
 				&& $this->options->tokenAutoRefresh
477 477
 				&& ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)
478
-			){
478
+			) {
479 479
 				$token = $this->refreshAccessToken($token);
480 480
 			}
481 481
 
Please login to merge, or discard this patch.
src/Core/OAuth1Provider.php 1 patch
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -21,7 +21,7 @@  discard block
 block discarded – undo
21 21
 /**
22 22
  * Implements an abstract OAuth1 provider with all methods required by the OAuth1Interface.
23 23
  */
24
-abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{
24
+abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface {
25 25
 
26 26
 	/**
27 27
 	 * The request OAuth1 token URL
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
 			->withHeader('Content-Length', '0') // tumblr requires a content-length header set
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
 
@@ -77,20 +77,20 @@  discard block
 block discarded – undo
77 77
 	protected function parseTokenResponse(ResponseInterface $response, bool $checkCallbackConfirmed = null):AccessToken{
78 78
 		$data = $this->parseQuery(MessageUtil::decompress($response));
79 79
 
80
-		if(empty($data)){
80
+		if (empty($data)) {
81 81
 			throw new ProviderException('unable to parse token response');
82 82
 		}
83
-		elseif(isset($data['error'])){
83
+		elseif (isset($data['error'])) {
84 84
 			throw new ProviderException('error retrieving access token: '.$data['error']);
85 85
 		}
86
-		elseif(!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])){
86
+		elseif (!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])) {
87 87
 			throw new ProviderException('invalid token');
88 88
 		}
89 89
 
90
-		if(
90
+		if (
91 91
 			$checkCallbackConfirmed
92 92
 			&& (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')
93
-		){
93
+		) {
94 94
 			throw new ProviderException('oauth callback unconfirmed');
95 95
 		}
96 96
 
@@ -132,7 +132,7 @@  discard block
 block discarded – undo
132 132
 	protected function getSignature(string $url, array $params, string $method, string $accessTokenSecret = null):string{
133 133
 		$parsed = QueryUtil::parseUrl($url);
134 134
 
135
-		if(!isset($parsed['host']) || !isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)){
135
+		if (!isset($parsed['host']) || !isset($parsed['scheme']) || !in_array($parsed['scheme'], ['http', 'https'], true)) {
136 136
 			throw new ProviderException('getSignature: invalid url');
137 137
 		}
138 138
 
@@ -149,7 +149,7 @@  discard block
 block discarded – undo
149 149
 		]);
150 150
 
151 151
 		// https://tools.ietf.org/html/rfc5849#section-3.4.2
152
-		$key  = array_map('rawurlencode', [
152
+		$key = array_map('rawurlencode', [
153 153
 			$this->options->secret,
154 154
 			$accessTokenSecret ?? ''
155 155
 		]);
@@ -196,7 +196,7 @@  discard block
 block discarded – undo
196 196
 			$token->accessTokenSecret
197 197
 		);
198 198
 
199
-		if(isset($query['oauth_session_handle'])){
199
+		if (isset($query['oauth_session_handle'])) {
200 200
 			$parameters['oauth_session_handle'] = $query['oauth_session_handle']; // @codeCoverageIgnore
201 201
 		}
202 202
 
Please login to merge, or discard this patch.
src/Core/OAuth2Provider.php 1 patch
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 
@@ -106,19 +106,19 @@  discard block
 block discarded – undo
106 106
 		// silly amazon sends compressed data...
107 107
 		$data = json_decode(MessageUtil::decompress($response), true, 512, JSON_THROW_ON_ERROR);
108 108
 
109
-		if(!is_array($data)){
109
+		if (!is_array($data)) {
110 110
 			throw new ProviderException('unable to parse token response');
111 111
 		}
112 112
 
113
-		foreach(['error_description', 'error'] as $field){
113
+		foreach (['error_description', 'error'] as $field) {
114 114
 
115
-			if(isset($data[$field])){
115
+			if (isset($data[$field])) {
116 116
 				throw new ProviderException('error retrieving access token: "'.$data[$field].'"');
117 117
 			}
118 118
 
119 119
 		}
120 120
 
121
-		if(!isset($data['access_token'])){
121
+		if (!isset($data['access_token'])) {
122 122
 			throw new ProviderException('token missing');
123 123
 		}
124 124
 
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
 	 */
141 141
 	public function getAccessToken(string $code, string $state = null):AccessToken{
142 142
 
143
-		if($this instanceof CSRFToken){
143
+		if ($this instanceof CSRFToken) {
144 144
 			$this->checkState($state);
145 145
 		}
146 146
 
@@ -158,7 +158,7 @@  discard block
 block discarded – undo
158 158
 			->withHeader('Accept-Encoding', 'identity')
159 159
 			->withBody($this->streamFactory->createStream($this->buildQuery($body, PHP_QUERY_RFC1738)));
160 160
 
161
-		foreach($this->authHeaders as $header => $value){
161
+		foreach ($this->authHeaders as $header => $value) {
162 162
 			$request = $request->withHeader($header, $value);
163 163
 		}
164 164
 
@@ -174,11 +174,11 @@  discard block
 block discarded – undo
174 174
 	 */
175 175
 	public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{
176 176
 
177
-		if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){
177
+		if ($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER) {
178 178
 			return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken);
179 179
 		}
180 180
 
181
-		if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){
181
+		if ($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY) {
182 182
 			$uri = $this->mergeQuery((string)$request->getUri(), [$this->authMethodQuery => $token->accessToken]);
183 183
 
184 184
 			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($this->buildQuery($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
 
@@ -234,17 +234,17 @@  discard block
 block discarded – undo
234 234
 	 */
235 235
 	public function refreshAccessToken(AccessToken $token = null):AccessToken{
236 236
 
237
-		if(!$this instanceof TokenRefresh){
237
+		if (!$this instanceof TokenRefresh) {
238 238
 			throw new ProviderException('token refresh not supported');
239 239
 		}
240 240
 
241
-		if($token === null){
241
+		if ($token === null) {
242 242
 			$token = $this->storage->getAccessToken($this->serviceName);
243 243
 		}
244 244
 
245 245
 		$refreshToken = $token->refreshToken;
246 246
 
247
-		if(empty($refreshToken)){
247
+		if (empty($refreshToken)) {
248 248
 			throw new ProviderException(
249 249
 				sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires))
250 250
 			);
@@ -265,13 +265,13 @@  discard block
 block discarded – undo
265 265
 			->withBody($this->streamFactory->createStream($this->buildQuery($body, PHP_QUERY_RFC1738)))
266 266
 		;
267 267
 
268
-		foreach($this->authHeaders as $header => $value){
268
+		foreach ($this->authHeaders as $header => $value) {
269 269
 			$request = $request->withAddedHeader($header, $value);
270 270
 		}
271 271
 
272 272
 		$newToken = $this->parseTokenResponse($this->http->sendRequest($request));
273 273
 
274
-		if(empty($newToken->refreshToken)){
274
+		if (empty($newToken->refreshToken)) {
275 275
 			$newToken->refreshToken = $refreshToken;
276 276
 		}
277 277
 
@@ -291,17 +291,17 @@  discard block
 block discarded – undo
291 291
 	 */
292 292
 	public function checkState(string $state = null):void{
293 293
 
294
-		if(!$this instanceof CSRFToken){
294
+		if (!$this instanceof CSRFToken) {
295 295
 			throw new ProviderException('CSRF protection not supported');
296 296
 		}
297 297
 
298
-		if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){
298
+		if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) {
299 299
 			throw new ProviderException('invalid state for '.$this->serviceName);
300 300
 		}
301 301
 
302 302
 		$knownState = $this->storage->getCSRFState($this->serviceName);
303 303
 
304
-		if(!hash_equals($knownState, $state)){
304
+		if (!hash_equals($knownState, $state)) {
305 305
 			throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state);
306 306
 		}
307 307
 
@@ -318,11 +318,11 @@  discard block
 block discarded – undo
318 318
 	 */
319 319
 	public function setState(array $params):array{
320 320
 
321
-		if(!$this instanceof CSRFToken){
321
+		if (!$this instanceof CSRFToken) {
322 322
 			throw new ProviderException('CSRF protection not supported');
323 323
 		}
324 324
 
325
-		if(!isset($params['state'])){
325
+		if (!isset($params['state'])) {
326 326
 			$params['state'] = sha1(random_bytes(256));
327 327
 		}
328 328
 
Please login to merge, or discard this patch.