Completed
Push — master ( 5568a8...fb8117 )
by smiley
03:02
created
examples/oauth-example-common.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -35,7 +35,7 @@
 block discarded – undo
35 35
 ];
36 36
 
37 37
 /** @var \chillerlan\Settings\SettingsContainerInterface $options */
38
-$options = new class($options_arr) extends OAuthOptions{
38
+$options = new class($options_arr) extends OAuthOptions {
39 39
 	use LogOptionsTrait;
40 40
 
41 41
 	protected $sleep;
Please login to merge, or discard this patch.
src/Core/OAuth2Provider.php 2 patches
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -20,7 +20,7 @@  discard block
 block discarded – undo
20 20
  * @method array setState(array $params)
21 21
  * @method \chillerlan\OAuth\Core\OAuth2Interface checkState(string $state = null)
22 22
  */
23
-abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{
23
+abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface {
24 24
 
25 25
 	/**
26 26
 	 * @var int
@@ -56,7 +56,7 @@  discard block
 block discarded – undo
56 56
 	public function getAuthURL(array $params = null, array $scopes = null):UriInterface{
57 57
 		$params = $params ?? [];
58 58
 
59
-		if(isset($params['client_secret'])){
59
+		if (isset($params['client_secret'])) {
60 60
 			unset($params['client_secret']);
61 61
 		}
62 62
 
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
 			'type'          => 'web_server',
69 69
 		]);
70 70
 
71
-		if($this instanceof CSRFToken){
71
+		if ($this instanceof CSRFToken) {
72 72
 			$params = $this->setState($params);
73 73
 		}
74 74
 
@@ -84,19 +84,19 @@  discard block
 block discarded – undo
84 84
 	protected function parseTokenResponse(ResponseInterface $response):AccessToken{
85 85
 		$data = json_decode($response->getBody()->getContents(), true);
86 86
 
87
-		if(!is_array($data)){
87
+		if (!is_array($data)) {
88 88
 			throw new ProviderException('unable to parse token response');
89 89
 		}
90 90
 
91
-		foreach(['error_description', 'error'] as $field){
91
+		foreach (['error_description', 'error'] as $field) {
92 92
 
93
-			if(isset($data[$field])){
93
+			if (isset($data[$field])) {
94 94
 				throw new ProviderException('error retrieving access token: "'.$data[$field].'"');
95 95
 			}
96 96
 
97 97
 		}
98 98
 
99
-		if(!isset($data['access_token'])){
99
+		if (!isset($data['access_token'])) {
100 100
 			throw new ProviderException('token missing');
101 101
 		}
102 102
 
@@ -122,7 +122,7 @@  discard block
 block discarded – undo
122 122
 	 */
123 123
 	public function getAccessToken(string $code, string $state = null):AccessToken{
124 124
 
125
-		if($this instanceof CSRFToken){
125
+		if ($this instanceof CSRFToken) {
126 126
 			$this->checkState($state);
127 127
 		}
128 128
 
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
 			->withHeader('Content-Type', 'application/x-www-form-urlencoded')
140 140
 			->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738)));
141 141
 
142
-		foreach($this->authHeaders as $header => $value){
142
+		foreach ($this->authHeaders as $header => $value) {
143 143
 			$request = $request->withHeader($header, $value);
144 144
 		}
145 145
 
@@ -159,15 +159,15 @@  discard block
 block discarded – undo
159 159
 	 */
160 160
 	public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{
161 161
 
162
-		if(array_key_exists($this->authMethod, $this::AUTH_METHODS_HEADER)){
162
+		if (array_key_exists($this->authMethod, $this::AUTH_METHODS_HEADER)) {
163 163
 			$request = $request->withHeader('Authorization', $this::AUTH_METHODS_HEADER[$this->authMethod].$token->accessToken);
164 164
 		}
165
-		elseif(array_key_exists($this->authMethod, $this::AUTH_METHODS_QUERY)){
165
+		elseif (array_key_exists($this->authMethod, $this::AUTH_METHODS_QUERY)) {
166 166
 			$uri = Psr7\merge_query((string)$request->getUri(), [$this::AUTH_METHODS_QUERY[$this->authMethod] => $token->accessToken]);
167 167
 
168 168
 			$request = $request->withUri($this->uriFactory->createUri($uri));
169 169
 		}
170
-		else{
170
+		else {
171 171
 			throw new ProviderException('invalid auth type');
172 172
 		}
173 173
 
Please login to merge, or discard this patch.
Braces   +2 added lines, -4 removed lines patch added patch discarded remove patch
@@ -161,13 +161,11 @@
 block discarded – undo
161 161
 
162 162
 		if(array_key_exists($this->authMethod, $this::AUTH_METHODS_HEADER)){
163 163
 			$request = $request->withHeader('Authorization', $this::AUTH_METHODS_HEADER[$this->authMethod].$token->accessToken);
164
-		}
165
-		elseif(array_key_exists($this->authMethod, $this::AUTH_METHODS_QUERY)){
164
+		} elseif(array_key_exists($this->authMethod, $this::AUTH_METHODS_QUERY)){
166 165
 			$uri = Psr7\merge_query((string)$request->getUri(), [$this::AUTH_METHODS_QUERY[$this->authMethod] => $token->accessToken]);
167 166
 
168 167
 			$request = $request->withUri($this->uriFactory->createUri($uri));
169
-		}
170
-		else{
168
+		} else{
171 169
 			throw new ProviderException('invalid auth type');
172 170
 		}
173 171
 
Please login to merge, or discard this patch.
src/Core/OAuth2TokenRefreshTrait.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -24,7 +24,7 @@  discard block
 block discarded – undo
24 24
  * @property \Psr\Http\Message\RequestFactoryInterface       $requestFactory
25 25
  * @property \Psr\Http\Message\StreamFactoryInterface        $streamFactory
26 26
  */
27
-trait OAuth2TokenRefreshTrait{
27
+trait OAuth2TokenRefreshTrait {
28 28
 
29 29
 	/**
30 30
 	 * @param \chillerlan\OAuth\Core\AccessToken $token
@@ -34,15 +34,15 @@  discard block
 block discarded – undo
34 34
 	 */
35 35
 	public function refreshAccessToken(AccessToken $token = null):AccessToken{
36 36
 
37
-		if($token === null){
37
+		if ($token === null) {
38 38
 			$token = $this->storage->getAccessToken($this->serviceName);
39 39
 		}
40 40
 
41 41
 		$refreshToken = $token->refreshToken;
42 42
 
43
-		if(empty($refreshToken)){
43
+		if (empty($refreshToken)) {
44 44
 
45
-			if(!$this instanceof AccessTokenForRefresh){
45
+			if (!$this instanceof AccessTokenForRefresh) {
46 46
 				throw new ProviderException(sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires)));
47 47
 			}
48 48
 
@@ -63,13 +63,13 @@  discard block
 block discarded – undo
63 63
 			->withBody($this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738)))
64 64
 		;
65 65
 
66
-		foreach($this->authHeaders as $header => $value){
66
+		foreach ($this->authHeaders as $header => $value) {
67 67
 			$request = $request->withAddedHeader($header, $value);
68 68
 		}
69 69
 
70 70
 		$newToken = $this->parseTokenResponse($this->http->sendRequest($request));
71 71
 
72
-		if(empty($newToken->refreshToken)){
72
+		if (empty($newToken->refreshToken)) {
73 73
 			$newToken->refreshToken = $refreshToken;
74 74
 		}
75 75
 
Please login to merge, or discard this patch.
src/Core/OAuth2CSRFTokenTrait.php 1 patch
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -18,7 +18,7 @@  discard block
 block discarded – undo
18 18
  * @property string                                          $serviceName
19 19
  * @property \chillerlan\OAuth\Storage\OAuthStorageInterface $storage
20 20
  */
21
-trait OAuth2CSRFTokenTrait{
21
+trait OAuth2CSRFTokenTrait {
22 22
 
23 23
 	/**
24 24
 	 * @param string|null $state
@@ -28,13 +28,13 @@  discard block
 block discarded – undo
28 28
 	 */
29 29
 	protected function checkState(string $state = null):OAuth2Interface{
30 30
 
31
-		if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){
31
+		if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) {
32 32
 			throw new ProviderException('invalid state for '.$this->serviceName);
33 33
 		}
34 34
 
35 35
 		$knownState = $this->storage->getCSRFState($this->serviceName);
36 36
 
37
-		if(!hash_equals($knownState, $state)){
37
+		if (!hash_equals($knownState, $state)) {
38 38
 			throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state);
39 39
 		}
40 40
 
@@ -49,7 +49,7 @@  discard block
 block discarded – undo
49 49
 	 */
50 50
 	protected function setState(array $params):array{
51 51
 
52
-		if(!isset($params['state'])){
52
+		if (!isset($params['state'])) {
53 53
 			$params['state'] = sha1(random_bytes(256));
54 54
 		}
55 55
 
Please login to merge, or discard this patch.
src/Core/OAuth2ClientCredentialsTrait.php 1 patch
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -24,7 +24,7 @@  discard block
 block discarded – undo
24 24
  * @property \Psr\Http\Message\RequestFactoryInterface       $requestFactory
25 25
  * @property \Psr\Http\Message\StreamFactoryInterface        $streamFactory
26 26
  */
27
-trait OAuth2ClientCredentialsTrait{
27
+trait OAuth2ClientCredentialsTrait {
28 28
 
29 29
 	/**
30 30
 	 * @param array $scopes
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
 	public function getClientCredentialsToken(array $scopes = null):AccessToken{
35 35
 		$params = ['grant_type' => 'client_credentials'];
36 36
 
37
-		if($scopes !== null){
37
+		if ($scopes !== null) {
38 38
 			$params['scope'] = implode($this->scopesDelimiter, $scopes);
39 39
 		}
40 40
 
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
 			->withBody($this->streamFactory->createStream(http_build_query($params, '', '&', PHP_QUERY_RFC1738)))
46 46
 		;
47 47
 
48
-		foreach($this->authHeaders as $header => $value){
48
+		foreach ($this->authHeaders as $header => $value) {
49 49
 			$request = $request->withAddedHeader($header, $value);
50 50
 		}
51 51
 
Please login to merge, or discard this patch.
src/Core/AccessToken.php 1 patch
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -29,7 +29,7 @@  discard block
 block discarded – undo
29 29
  * @property int    $expires
30 30
  * @property string $provider
31 31
  */
32
-class AccessToken extends SettingsContainerAbstract{
32
+class AccessToken extends SettingsContainerAbstract {
33 33
 
34 34
 	/**
35 35
 	 * Denotes an unknown end of life time.
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 	 *
84 84
 	 * @param iterable|null $properties
85 85
 	 */
86
-	public function __construct(iterable $properties = null){
86
+	public function __construct(iterable $properties = null) {
87 87
 		parent::__construct($properties);
88 88
 
89 89
 		$this->setExpiry($this->expires);
@@ -99,7 +99,7 @@  discard block
 block discarded – undo
99 99
 	 */
100 100
 	public function __set(string $property, $value):void{
101 101
 
102
-		if(property_exists($this, $property)){
102
+		if (property_exists($this, $property)) {
103 103
 			$property === 'expires'
104 104
 				? $this->setExpiry($value)
105 105
 				: $this->{$property} = $value;
@@ -115,19 +115,19 @@  discard block
 block discarded – undo
115 115
 	public function setExpiry(int $expires = null):AccessToken{
116 116
 		$now = time();
117 117
 
118
-		if($expires!== null){
119
-			$expires =  intval($expires);
118
+		if ($expires !== null) {
119
+			$expires = intval($expires);
120 120
 		}
121 121
 
122 122
 		$this->expires = $this::EOL_UNKNOWN;
123 123
 
124
-		if($expires === 0 || $expires === $this::EOL_NEVER_EXPIRES){
124
+		if ($expires === 0 || $expires === $this::EOL_NEVER_EXPIRES) {
125 125
 			$this->expires = $this::EOL_NEVER_EXPIRES;
126 126
 		}
127
-		elseif($expires > $now){
127
+		elseif ($expires > $now) {
128 128
 			$this->expires = $expires;
129 129
 		}
130
-		elseif($expires > 0 && $expires < $this::EXPIRY_MAX){
130
+		elseif ($expires > 0 && $expires < $this::EXPIRY_MAX) {
131 131
 			$this->expires = $now + $expires;
132 132
 		}
133 133
 
Please login to merge, or discard this patch.
src/Core/OAuth1Provider.php 1 patch
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -16,7 +16,7 @@  discard block
 block discarded – undo
16 16
 use DateTime;
17 17
 use Psr\Http\Message\{RequestInterface, ResponseInterface, UriInterface};
18 18
 
19
-abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{
19
+abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface {
20 20
 
21 21
 	/**
22 22
 	 * @var string
@@ -59,7 +59,7 @@  discard block
 block discarded – undo
59 59
 			->withHeader('Authorization', 'OAuth '.Psr7\build_http_query($params, true, ', ', '"'));
60 60
 		;
61 61
 
62
-		foreach($this->authHeaders as $header => $value){
62
+		foreach ($this->authHeaders as $header => $value) {
63 63
 			$request = $request->withAddedHeader($header, $value);
64 64
 		}
65 65
 
@@ -76,17 +76,17 @@  discard block
 block discarded – undo
76 76
 	protected function parseTokenResponse(ResponseInterface $response, bool $checkCallbackConfirmed = null):AccessToken{
77 77
 		parse_str($response->getBody()->getContents(), $data);
78 78
 
79
-		if(!$data || !is_array($data)){
79
+		if (!$data || !is_array($data)) {
80 80
 			throw new ProviderException('unable to parse token response');
81 81
 		}
82
-		elseif(isset($data['error'])){
82
+		elseif (isset($data['error'])) {
83 83
 			throw new ProviderException('error retrieving access token: '.$data['error']);
84 84
 		}
85
-		elseif(!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])){
85
+		elseif (!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])) {
86 86
 			throw new ProviderException('token missing');
87 87
 		}
88 88
 
89
-		if($checkCallbackConfirmed && (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')){
89
+		if ($checkCallbackConfirmed && (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')) {
90 90
 			throw new ProviderException('oauth callback unconfirmed');
91 91
 		}
92 92
 
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
 	protected function getSignature(string $url, array $params, string $method, string $accessTokenSecret = null):string{
132 132
 		$parseURL = parse_url($url);
133 133
 
134
-		if(!isset($parseURL['host']) || !isset($parseURL['scheme']) || !in_array($parseURL['scheme'], ['http', 'https'], true)){
134
+		if (!isset($parseURL['host']) || !isset($parseURL['scheme']) || !in_array($parseURL['scheme'], ['http', 'https'], true)) {
135 135
 			throw new ProviderException('getSignature: invalid url');
136 136
 		}
137 137
 
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
 
140 140
 		$signatureParams = array_merge($query, $params);
141 141
 
142
-		if(isset($signatureParams['oauth_signature'])){
142
+		if (isset($signatureParams['oauth_signature'])) {
143 143
 			unset($signatureParams['oauth_signature']);
144 144
 		}
145 145
 
Please login to merge, or discard this patch.
src/Core/OAuthProvider.php 2 patches
Spacing   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -120,7 +120,7 @@  discard block
 block discarded – undo
120 120
 	 *
121 121
 	 * @throws \chillerlan\HTTP\MagicAPI\ApiClientException
122 122
 	 */
123
-	public function __construct(HTTPClientInterface $http, OAuthStorageInterface $storage, SettingsContainerInterface $options){
123
+	public function __construct(HTTPClientInterface $http, OAuthStorageInterface $storage, SettingsContainerInterface $options) {
124 124
 		$this->http    = $http;
125 125
 		$this->storage = $storage;
126 126
 		$this->options = $options;
@@ -132,10 +132,10 @@  discard block
 block discarded – undo
132 132
 
133 133
 		$this->serviceName = (new ReflectionClass($this))->getShortName();
134 134
 
135
-		if(!empty($this->endpointMap) && class_exists($this->endpointMap)){
135
+		if (!empty($this->endpointMap) && class_exists($this->endpointMap)) {
136 136
 			$this->endpoints = new $this->endpointMap;
137 137
 
138
-			if(!$this->endpoints instanceof EndpointMapInterface){
138
+			if (!$this->endpoints instanceof EndpointMapInterface) {
139 139
 				throw new ApiClientException('invalid endpoint map');
140 140
 			}
141 141
 
@@ -148,9 +148,9 @@  discard block
 block discarded – undo
148 148
 	 *
149 149
 	 * @return string|null
150 150
 	 */
151
-	public function __get(string $name){
151
+	public function __get(string $name) {
152 152
 
153
-		if(!in_array($name, ['serviceName', 'authURL', 'accessTokenURL', 'revokeURL', 'userRevokeURL'], true)){
153
+		if (!in_array($name, ['serviceName', 'authURL', 'accessTokenURL', 'revokeURL', 'userRevokeURL'], true)) {
154 154
 			return null;
155 155
 		}
156 156
 
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
 	 */
204 204
 	public function __call(string $name, array $arguments):ResponseInterface{
205 205
 
206
-		if(!$this->endpoints instanceof EndpointMapInterface || !$this->endpoints->__isset($name)){
206
+		if (!$this->endpoints instanceof EndpointMapInterface || !$this->endpoints->__isset($name)) {
207 207
 			throw new ApiClientException('endpoint not found');
208 208
 		}
209 209
 
@@ -216,21 +216,21 @@  discard block
 block discarded – undo
216 216
 		$path_elements = $m['path_elements'] ?? [];
217 217
 		$params_in_url = count($path_elements);
218 218
 		$params        = $arguments[$params_in_url] ?? [];
219
-		$urlparams     = array_slice($arguments,0 , $params_in_url);
219
+		$urlparams     = array_slice($arguments, 0, $params_in_url);
220 220
 
221
-		if($params_in_url > 0){
221
+		if ($params_in_url > 0) {
222 222
 
223
-			if(count($urlparams) < $params_in_url){
223
+			if (count($urlparams) < $params_in_url) {
224 224
 				throw new APIClientException('too few URL params, required: '.implode(', ', $path_elements));
225 225
 			}
226 226
 
227 227
 			$endpoint = sprintf($endpoint, ...$urlparams);
228 228
 		}
229 229
 
230
-		if(in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE'])){
230
+		if (in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE'])) {
231 231
 			$body = $arguments[$params_in_url + 1] ?? $params;
232 232
 
233
-			if($params === $body){
233
+			if ($params === $body) {
234 234
 				$params = [];
235 235
 			}
236 236
 
@@ -259,32 +259,32 @@  discard block
 block discarded – undo
259 259
 		$token = $this->storage->getAccessToken($this->serviceName);
260 260
 
261 261
 		// attempt to refresh an expired token
262
-		if($this instanceof TokenRefresh && $this->options->tokenAutoRefresh && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)){
262
+		if ($this instanceof TokenRefresh && $this->options->tokenAutoRefresh && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)) {
263 263
 			$token = $this->refreshAccessToken($token);
264 264
 		}
265 265
 
266 266
 		$request = $this->requestFactory
267 267
 			->createRequest($method ?? 'GET', Psr7\merge_query($this->apiURL.$path, $params));
268 268
 
269
-		foreach(array_merge($this->apiHeaders, $headers) as $header => $value){
269
+		foreach (array_merge($this->apiHeaders, $headers) as $header => $value) {
270 270
 			$request = $request->withAddedHeader($header, $value);
271 271
 		}
272 272
 
273 273
 		$request = $this->getRequestAuthorization($request, $token);
274 274
 
275
-		if(is_array($body) && $request->hasHeader('content-type')){
275
+		if (is_array($body) && $request->hasHeader('content-type')) {
276 276
 			$contentType = strtolower($request->getHeaderLine('content-type'));
277 277
 
278
-			if($contentType === 'application/x-www-form-urlencoded'){
278
+			if ($contentType === 'application/x-www-form-urlencoded') {
279 279
 				$body = $this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738));
280 280
 			}
281
-			elseif($contentType === 'application/json'){
281
+			elseif ($contentType === 'application/json') {
282 282
 				$body = $this->streamFactory->createStream(json_encode($body));
283 283
 			}
284 284
 
285 285
 		}
286 286
 
287
-		if($body instanceof StreamInterface){
287
+		if ($body instanceof StreamInterface) {
288 288
 			$request = $request->withBody($body);
289 289
 		}
290 290
 
Please login to merge, or discard this patch.
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -277,8 +277,7 @@
 block discarded – undo
277 277
 
278 278
 			if($contentType === 'application/x-www-form-urlencoded'){
279 279
 				$body = $this->streamFactory->createStream(http_build_query($body, '', '&', PHP_QUERY_RFC1738));
280
-			}
281
-			elseif($contentType === 'application/json'){
280
+			} elseif($contentType === 'application/json'){
282 281
 				$body = $this->streamFactory->createStream(json_encode($body));
283 282
 			}
284 283
 
Please login to merge, or discard this patch.
src/OAuthOptions.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -36,6 +36,6 @@
 block discarded – undo
36 36
  * @property string     $ca_info
37 37
  * @property int        $max_redirects
38 38
  */
39
-class OAuthOptions extends SettingsContainerAbstract{
39
+class OAuthOptions extends SettingsContainerAbstract {
40 40
 	use OAuthOptionsTrait, HTTPOptionsTrait;
41 41
 }
Please login to merge, or discard this patch.