Total Complexity | 50 |
Total Lines | 482 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Provider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Provider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Provider |
||
27 | { |
||
28 | use ErrorCollectorTrait; |
||
29 | |||
30 | /** |
||
31 | * Provider::$oauth |
||
32 | * |
||
33 | * @var \OAuthProvider |
||
34 | */ |
||
35 | protected $oauth; |
||
36 | |||
37 | /** |
||
38 | * Provider::$consumer |
||
39 | * |
||
40 | * @var \O2System\Security\Authentication\Oauth\DataStructures\Consumer |
||
41 | */ |
||
42 | protected $consumer; |
||
43 | |||
44 | /** |
||
45 | * Provider::$token |
||
46 | * |
||
47 | * @var \O2System\Security\Authentication\Oauth\DataStructures\Token |
||
48 | */ |
||
49 | protected $token; |
||
50 | |||
51 | /** |
||
52 | * Provider::$model |
||
53 | * |
||
54 | * @var \O2System\Security\Authentication\Oauth\Interfaces\ProviderModelInterface |
||
55 | */ |
||
56 | protected $model; |
||
57 | |||
58 | // ------------------------------------------------------------------------ |
||
59 | |||
60 | /** |
||
61 | * Provider::__construct |
||
62 | */ |
||
63 | public function __construct() |
||
64 | { |
||
65 | language() |
||
66 | ->addFilePath(str_replace('Authentication' . DIRECTORY_SEPARATOR . 'Oauth', '', |
||
67 | __DIR__) . DIRECTORY_SEPARATOR) |
||
68 | ->loadFile('oauth'); |
||
69 | |||
70 | $this->oauth = new \OAuthProvider([ |
||
71 | 'signature_method' => 'HMAC-SHA1', |
||
72 | ]); |
||
73 | |||
74 | $this->oauth->consumerHandler([$this, 'handleConsumer']); |
||
75 | $this->oauth->timestampNonceHandler([$this, 'handleTimestampNonce']); |
||
76 | $this->oauth->tokenHandler([$this, 'handleToken']); |
||
77 | |||
78 | if (models()->has('oauth')) { |
||
79 | $this->modelHander(models()->get('oauth')); |
||
80 | } |
||
81 | |||
82 | $this->oauth->signature_method = OAUTH_SIG_METHOD_HMACSHA1; |
||
83 | $this->oauth->consumer_key = input()->get('consumer_key'); |
||
84 | $this->oauth->consumer_secret = input()->get('consumer_secret'); |
||
85 | $this->oauth->token = input()->get('oauth_token'); |
||
86 | $this->oauth->token_secret = input()->get('oauth_token_secret'); |
||
87 | $this->oauth->token_type = null; |
||
88 | |||
89 | // Get HTTP_AUTHORIZATION |
||
90 | if ($httpAuthorization = input()->server('HTTP_AUTHORIZATION')) { |
||
91 | $httpAuthorization = explode(' ', $httpAuthorization); |
||
92 | $httpAuthorization = array_map('trim', $httpAuthorization); |
||
93 | |||
94 | switch (strtoupper($httpAuthorization[ 0 ])) { |
||
95 | default: |
||
96 | case 'OAUTH': |
||
97 | array_shift($httpAuthorization); |
||
98 | $httpAuthorization = array_map(function ($string) { |
||
99 | $string = str_replace(['"', ','], '', $string); |
||
100 | $string = explode('=', $string); |
||
101 | |||
102 | return [ |
||
103 | 'key' => str_replace('oauth_', '', $string[ 0 ]), |
||
104 | 'value' => $string[ 1 ], |
||
105 | ]; |
||
106 | }, $httpAuthorization); |
||
107 | |||
108 | $oauthParams = []; |
||
109 | foreach ($httpAuthorization as $param) { |
||
110 | $oauthParams[ $param[ 'key' ] ] = $param[ 'value' ]; |
||
111 | } |
||
112 | |||
113 | $this->oauth->signature_method = $oauthParams[ 'signature_method' ]; |
||
114 | $this->oauth->nonce = $oauthParams[ 'nonce' ]; |
||
115 | $this->oauth->timestamp = $oauthParams[ 'timestamp' ]; |
||
116 | $this->oauth->consumer_key = $oauthParams[ 'consumer_key' ]; |
||
117 | $this->oauth->version = $oauthParams[ 'version' ]; |
||
118 | |||
119 | if (isset($oauthParams[ 'callback' ])) { |
||
120 | $this->oauth->callback = urldecode($oauthParams[ 'callback' ]); |
||
121 | } |
||
122 | |||
123 | if (isset($oauthParams[ 'signature' ])) { |
||
124 | $this->oauth->signature = $oauthParams[ 'signature' ]; |
||
125 | } |
||
126 | |||
127 | $this->oauth->callconsumerHandler(); |
||
128 | break; |
||
129 | case 'BASIC': |
||
130 | case 'BEARER': |
||
131 | $this->oauth->bearer = $httpAuthorization[ 1 ]; |
||
132 | $bearer = base64_decode($this->oauth->bearer); |
||
133 | $bearer = explode(':', $bearer); |
||
134 | $bearer = array_map('trim', $bearer); |
||
135 | |||
136 | if (count($bearer) == 2) { |
||
137 | $this->oauth->consumer_key = $bearer[ 0 ]; |
||
138 | $this->oauth->consumer_secret = $bearer[ 1 ]; |
||
139 | |||
140 | $this->oauth->callconsumerHandler(); |
||
141 | } |
||
142 | |||
143 | break; |
||
144 | } |
||
145 | } elseif ($oauthVerifier = input()->post('oauth_verifier')) { |
||
146 | $this->oauth->verifier = $oauthVerifier; |
||
147 | $verifier = base64_decode($this->oauth->verifier); |
||
148 | $verifier = explode(':', $verifier); |
||
149 | $verifier = array_map('trim', $verifier); |
||
150 | |||
151 | if (count($verifier) == 2) { |
||
152 | $this->oauth->token = $verifier[ 0 ]; |
||
153 | $this->oauth->token_secret = $verifier[ 1 ]; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | if ( ! empty($this->oauth->token)) { |
||
158 | $this->oauth->calltokenHandler(); |
||
159 | } |
||
160 | |||
161 | if ( ! $this->hasErrors()) { |
||
162 | if ( ! empty($this->oauth->timestamp) && ! empty($this->oauth->nonce)) { |
||
163 | $this->oauth->callTimestampNonceHandler(); |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | // ------------------------------------------------------------------------ |
||
169 | |||
170 | /** |
||
171 | * Provider::modelHandler |
||
172 | * |
||
173 | * Sets OAuth Provider model handler. |
||
174 | * |
||
175 | * @param $model |
||
176 | * |
||
177 | * @return void |
||
178 | */ |
||
179 | public function modelHander(ProviderModelInterface $model) |
||
180 | { |
||
181 | $this->model = $model; |
||
182 | } |
||
183 | |||
184 | // ------------------------------------------------------------------------ |
||
185 | |||
186 | /** |
||
187 | * Provider::getAccessToken |
||
188 | * |
||
189 | * Gets OAuth Access Token. |
||
190 | * |
||
191 | * @return array|bool|\O2System\Security\Authentication\Oauth\DataStructures\Token |
||
192 | */ |
||
193 | public function getAccessToken() |
||
194 | { |
||
195 | if ( ! empty($this->token)) { |
||
196 | if ($this->model->insertTokenNonce([ |
||
197 | 'id_consumer_token' => $this->token->id, |
||
198 | 'nonce' => $token[ 'nonce' ] = Oauth::generateNonce(), |
||
199 | 'timestamp' => $token[ 'timestamp' ] = date('Y-m-d H:m:s'), |
||
200 | 'expires' => $token[ 'expires' ] = time() + 3600, |
||
201 | ])) { |
||
202 | return new DataStructures\Token([ |
||
203 | 'key' => $this->token->key, |
||
204 | 'secret' => $this->token->secret, |
||
205 | 'nonce' => $token[ 'nonce' ], |
||
206 | 'timestamp' => $token[ 'timestamp' ], |
||
207 | 'expires' => $token[ 'expires' ], |
||
208 | 'verifier' => (new Token($this->token->key, $this->token->secret))->getVerifier(), |
||
209 | ]); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | $token = $this->generateToken('ACCESS'); |
||
214 | $token = new DataStructures\Token([ |
||
215 | 'id' => $token[ 'id' ], |
||
216 | 'key' => $token[ 'key' ], |
||
217 | 'secret' => $token[ 'secret' ], |
||
218 | 'verifier' => (new Token($token[ 'key' ], $token[ 'secret' ]))->getVerifier(), |
||
219 | ]); |
||
220 | |||
221 | if ($this->model->insertTokenNonce([ |
||
222 | 'id_consumer_token' => $token[ 'id' ], |
||
223 | 'nonce' => $token[ 'nonce' ] = Oauth::generateNonce(), |
||
224 | 'timestamp' => $token[ 'timestamp' ] = date('Y-m-d H:m:s'), |
||
225 | 'expires' => $token[ 'expires' ] = time() + 3600, |
||
226 | ])) { |
||
227 | return $token; |
||
228 | } |
||
229 | |||
230 | return false; |
||
231 | } |
||
232 | |||
233 | // ------------------------------------------------------------------------ |
||
234 | |||
235 | /** |
||
236 | * Provider::generateToken |
||
237 | * |
||
238 | * @param string $type |
||
239 | * @param int $length |
||
240 | * @param bool $strong |
||
241 | * |
||
242 | * @return array|bool Returns FALSE if failed. |
||
243 | */ |
||
244 | protected function generateToken($type = 'ACCESS', $length = 32, $strong = true) |
||
245 | { |
||
246 | if ( ! empty($this->consumer->secret)) { |
||
247 | return [ |
||
248 | 'oauth_token' => hash_hmac('sha1', \OAuthProvider::generateToken($length, $strong), |
||
249 | $this->consumer->secret), |
||
250 | 'oauth_token_secret' => hash_hmac('sha1', \OAuthProvider::generateToken($length, $strong), |
||
251 | $this->consumer->secret), |
||
252 | ]; |
||
253 | switch ($this->oauth->signature_method) { |
||
254 | default: |
||
255 | case OAUTH_SIG_METHOD_HMACSHA1: |
||
256 | case OAUTH_SIG_METHOD_RSASHA1: |
||
257 | |||
258 | $token = [ |
||
259 | 'key' => hash_hmac('sha1', \OAuthProvider::generateToken($length, $strong), |
||
260 | $this->consumer->secret), |
||
261 | 'secret' => hash_hmac('sha1', \OAuthProvider::generateToken($length, $strong), |
||
262 | $this->consumer->secret), |
||
263 | ]; |
||
264 | break; |
||
265 | |||
266 | case OAUTH_SIG_METHOD_HMACSHA256: |
||
267 | |||
268 | $token = [ |
||
269 | 'key' => hash_hmac('sha256', \OAuthProvider::generateToken($length, $strong), |
||
270 | $this->consumer->secret), |
||
271 | 'secret' => hash_hmac('sha256', \OAuthProvider::generateToken($length, $strong), |
||
272 | $this->consumer->secret), |
||
273 | ]; |
||
274 | break; |
||
275 | } |
||
276 | |||
277 | $nonce = (empty($this->oauth->nonce) ? Oauth::generateNonce() : $this->oauth->nonce); |
||
278 | $callback = (empty($this->oauth->callback) ? null : $this->oauth->callback); |
||
279 | |||
280 | if ($this->model->insertToken([ |
||
281 | 'id_consumer' => $this->consumer->id, |
||
282 | 'key' => $token[ 'key' ], |
||
283 | 'secret' => $token[ 'secret' ], |
||
284 | 'type' => $type, |
||
285 | 'callback' => $callback, |
||
286 | ])) { |
||
287 | $token[ 'id' ] = $this->model->db->getLastInsertId(); |
||
288 | |||
289 | if ($this->model->insertTokenNonce([ |
||
290 | 'id_consumer_token' => $token[ 'id' ], |
||
291 | 'nonce' => $nonce, |
||
292 | 'timestamp' => date('Y-m-d H:m:s'), |
||
293 | 'expires' => time() + 3600, |
||
294 | ])) { |
||
295 | return $token; |
||
296 | } |
||
297 | } |
||
298 | } |
||
299 | |||
300 | return false; |
||
301 | } |
||
302 | |||
303 | // ------------------------------------------------------------------------ |
||
304 | |||
305 | /** |
||
306 | * Provider::getRequestToken |
||
307 | * |
||
308 | * Gets OAuth Request Token. |
||
309 | * |
||
310 | * @return array|bool Returns FALSE if failed. |
||
311 | */ |
||
312 | public function getRequestToken() |
||
313 | { |
||
314 | return $this->generateToken('REQUEST'); |
||
315 | } |
||
316 | |||
317 | // ------------------------------------------------------------------------ |
||
318 | |||
319 | /** |
||
320 | * Provider::handleConsumer |
||
321 | * |
||
322 | * OAuth Consumer Handler. |
||
323 | * |
||
324 | * @param \OAuth $provider |
||
325 | * |
||
326 | * @return int |
||
327 | */ |
||
328 | public function handleConsumer($provider) |
||
329 | { |
||
330 | $this->consumer = new DataStructures\Consumer(); |
||
331 | |||
332 | if (false !== ($consumer = $this->model->findConsumer(['key' => $provider->consumer_key]))) { |
||
333 | $this->consumer->id = $consumer->id; |
||
334 | $this->consumer->key = $consumer->key; |
||
335 | $this->consumer->secret = $provider->consumer_secret = $consumer->secret; |
||
336 | $this->consumer->status = $consumer->status; |
||
337 | |||
338 | if ($consumer->status === 'ENABLED') { |
||
339 | return OAUTH_OK; |
||
340 | } |
||
341 | |||
342 | $this->addError(OAUTH_CONSUMER_KEY_REFUSED, language()->getLine('OAUTH_CONSUMER_KEY_REFUSED')); |
||
343 | |||
344 | return OAUTH_CONSUMER_KEY_REFUSED; |
||
345 | } |
||
346 | |||
347 | if (empty($this->oauth->bearer)) { |
||
348 | $this->addError(OAUTH_CONSUMER_KEY_UNKNOWN, language()->getLine('OAUTH_CONSUMER_KEY_UNKNOWN')); |
||
349 | } else { |
||
350 | $this->addError(OAUTH_CONSUMER_KEY_UNKNOWN, language()->getLine('OAUTH_AUTHORIZATION_UNKNOWN')); |
||
351 | } |
||
352 | |||
353 | return OAUTH_CONSUMER_KEY_UNKNOWN; |
||
354 | } |
||
355 | |||
356 | // ------------------------------------------------------------------------ |
||
357 | |||
358 | /** |
||
359 | * Provider::revokeToken |
||
360 | * |
||
361 | * Revoke OAuth Consumer Token. |
||
362 | * |
||
363 | * @param string $token oauth_token |
||
364 | * |
||
365 | * @return bool |
||
366 | */ |
||
367 | public function revokeToken($token) |
||
368 | { |
||
369 | $this->oauth->token = $token; |
||
370 | $this->oauth->calltokenHandler(); |
||
371 | |||
372 | if ( ! $this->hasErrors()) { |
||
373 | return $this->model->deleteToken(['key' => $token]); |
||
374 | } |
||
375 | |||
376 | return false; |
||
377 | } |
||
378 | |||
379 | // ------------------------------------------------------------------------ |
||
380 | |||
381 | /** |
||
382 | * Provider::handleToken |
||
383 | * |
||
384 | * OAuth Token Handler. |
||
385 | * |
||
386 | * @param \OAuth $provider |
||
387 | * |
||
388 | * @return int |
||
389 | */ |
||
390 | public function handleToken($provider) |
||
391 | { |
||
392 | if (false !== ($token = $this->model->findToken(['key' => $provider->token]))) { |
||
393 | if (isset($token->consumer)) { |
||
394 | $this->token = $token; |
||
395 | |||
396 | $this->consumer = $token->consumer; |
||
397 | $provider->consumer_key = $this->consumer->key; |
||
398 | $provider->consumer_secret = $this->consumer->secret; |
||
399 | $provider->token_secret = $token->secret; |
||
400 | } |
||
401 | |||
402 | return OAUTH_OK; |
||
403 | } |
||
404 | |||
405 | if (empty($this->oauth->verifier)) { |
||
406 | $this->addError(OAUTH_TOKEN_REJECTED, language()->getLine('OAUTH_TOKEN_REJECTED')); |
||
407 | |||
408 | return OAUTH_TOKEN_REJECTED; |
||
409 | } else { |
||
410 | $this->addError(OAUTH_VERIFIER_INVALID, language()->getLine('OAUTH_TOKEN_VERIFIER_REJECTED')); |
||
411 | |||
412 | return OAUTH_VERIFIER_INVALID; |
||
413 | } |
||
414 | } |
||
415 | |||
416 | // ------------------------------------------------------------------------ |
||
417 | |||
418 | /** |
||
419 | * Provider::handleTimestampNonce |
||
420 | * |
||
421 | * OAuth Timestamp and Nonce Handler. |
||
422 | * |
||
423 | * @param \OAuth $provider |
||
424 | * |
||
425 | * @return int |
||
426 | */ |
||
427 | public function handleTimestampNonce($provider) |
||
428 | { |
||
429 | if (empty($provider->timestamp)) { |
||
430 | $this->addError(OAUTH_BAD_TIMESTAMP, language()->getLine('OAUTH_BAD_TIMESTAMP')); |
||
431 | |||
432 | return OAUTH_BAD_TIMESTAMP; |
||
433 | } |
||
434 | |||
435 | if (false !== ($token = $this->model->findTokenNonce([ |
||
436 | 'nonce' => $provider->nonce, |
||
437 | ]))) { |
||
438 | if (time() > $token->expires) { |
||
439 | $this->addError(OAUTH_TOKEN_EXPIRED, language()->getLine('OAUTH_TOKEN_EXPIRED')); |
||
440 | |||
441 | return OAUTH_TOKEN_EXPIRED; |
||
442 | } |
||
443 | |||
444 | return OAUTH_OK; |
||
445 | } |
||
446 | |||
447 | $this->addError(OAUTH_BAD_NONCE, language()->getLine('OAUTH_BAD_NONCE')); |
||
448 | |||
449 | return OAUTH_BAD_NONCE; |
||
450 | } |
||
451 | |||
452 | // ------------------------------------------------------------------------ |
||
453 | |||
454 | /** |
||
455 | * Provider::isValidRequest |
||
456 | * |
||
457 | * Determine if the OAuth Request is valid. |
||
458 | * |
||
459 | * @return bool |
||
460 | */ |
||
461 | public function isValidRequest() |
||
508 | } |
||
509 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths