| Conditions | 12 |
| Paths | 31 |
| Total Lines | 62 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 45 | public function authenticate(ServerRequestInterface $request, array $requestData): ClientInterface |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * require client authentication for confidential clients or for any |
||
| 49 | * client that was issued client credentials (or with other |
||
| 50 | * authentication requirements) |
||
| 51 | */ |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var ClientAuthenticationMethodInterface $clientAuthenticationMethod |
||
| 55 | */ |
||
| 56 | $clientAuthenticationMethodUsedIdentifier = null; |
||
| 57 | $clientAuthenticationMethodUsed = null; |
||
| 58 | $authenticated = false; |
||
| 59 | foreach ($this->clientAuthenticationMethods as $identifier => $clientAuthenticationMethod) { |
||
| 60 | if($clientAuthenticationMethod->support($request, $requestData)) { |
||
| 61 | if($clientAuthenticationMethodUsedIdentifier) { |
||
| 62 | throw new OAuthException('invalid_request', |
||
| 63 | 'The request utilizes more than one mechanism for authenticating the client.', |
||
| 64 | 'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
||
| 65 | } |
||
| 66 | $clientAuthenticationMethodUsedIdentifier = $identifier; |
||
| 67 | $clientAuthenticationMethodUsed = $clientAuthenticationMethod; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | if($clientAuthenticationMethodUsed) { |
||
| 72 | if(!$client = $clientAuthenticationMethod->authenticate($request, $requestData)) { |
||
| 73 | throw new OAuthException('invalid_client', |
||
| 74 | 'Client authentication failed. Unknown client.', |
||
| 75 | 'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
||
| 76 | } |
||
| 77 | } else { |
||
| 78 | if (empty($requestData['client_id'])) { |
||
| 79 | throw new OAuthException('invalid_request', 'The request is missing the required parameter client_id.', |
||
| 80 | 'https://tools.ietf.org/html/rfc6749#section-4.1'); |
||
| 81 | } |
||
| 82 | |||
| 83 | if (!$client = $this->clientStorage->get($requestData['client_id'])) { |
||
| 84 | throw new OAuthException('invalid_request', 'The request includes the invalid parameter client_id.', |
||
| 85 | 'https://tools.ietf.org/html/rfc6749#section-4.1'); |
||
| 86 | } |
||
| 87 | |||
| 88 | if($client->hasCredentials()) { |
||
| 89 | if (!$authenticated) { |
||
| 90 | throw new OAuthException('invalid_client', 'Client authentication failed. No client authentication included', |
||
| 91 | 'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | $clientAuthenticationMethodUsedIdentifier = 'none'; |
||
| 96 | } |
||
| 97 | |||
| 98 | $tokenEndpointAuthMethod = $client->getMetadata()->getTokenEndpointAuthMethod() ?: 'client_secret_basic'; |
||
| 99 | if($tokenEndpointAuthMethod !== $clientAuthenticationMethodUsedIdentifier) { |
||
| 100 | throw new OAuthException('invalid_client', |
||
| 101 | 'Client authentication failed. Unsupported authentication method.', |
||
| 102 | 'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
||
| 103 | |||
| 104 | } |
||
| 105 | |||
| 106 | return $client; |
||
| 107 | } |
||
| 108 | } |