| Conditions | 11 |
| Paths | 25 |
| Total Lines | 82 |
| 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 |
||
| 104 | public function authenticate(ServerRequestInterface $request, array $requestData): RegisteredClient |
||
| 105 | { |
||
| 106 | /** |
||
| 107 | * @var ClientAuthenticationMethodInterface $clientAuthenticationMethod |
||
| 108 | */ |
||
| 109 | $clientAuthenticationMethodUsedIdentifier = null; |
||
| 110 | $clientAuthenticationMethodUsed = null; |
||
| 111 | |||
| 112 | foreach ($this->clientAuthenticationMethods as $identifier => $clientAuthenticationMethod) { |
||
| 113 | if ($clientAuthenticationMethod->support($request, $requestData)) { |
||
| 114 | /** |
||
| 115 | * @see https://tools.ietf.org/html/rfc6749#section-2.3 |
||
| 116 | * The client MUST NOT use more than one authentication method in each |
||
| 117 | * request. |
||
| 118 | */ |
||
| 119 | if ($clientAuthenticationMethodUsedIdentifier) { |
||
| 120 | throw new OAuthException('invalid_request', |
||
| 121 | 'The request utilizes more than one mechanism for authenticating the client.', |
||
| 122 | 'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
||
| 123 | } |
||
| 124 | |||
| 125 | $clientAuthenticationMethodUsedIdentifier = $identifier; |
||
| 126 | $clientAuthenticationMethodUsed = $clientAuthenticationMethod; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @see https://tools.ietf.org/html/rfc6749#section-3.2.1 |
||
| 132 | * Confidential clients or other clients issued client credentials MUST |
||
| 133 | * authenticate with the authorization server as described in |
||
| 134 | * Section 2.3 when making requests to the token endpoint. |
||
| 135 | */ |
||
| 136 | if ($clientAuthenticationMethodUsed) { |
||
| 137 | if (!$client = $clientAuthenticationMethodUsed->authenticate($request, $requestData)) { |
||
| 138 | throw new OAuthException('invalid_client', |
||
| 139 | 'Client authentication failed. Unknown client.', |
||
| 140 | 'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
||
| 141 | } |
||
| 142 | } else { |
||
| 143 | /** |
||
| 144 | * @see https://tools.ietf.org/html/rfc6749#section-3.2.1 |
||
| 145 | * A client MAY use the "client_id" request parameter to identify itself |
||
| 146 | * when sending requests to the token endpoint. In the |
||
| 147 | * "authorization_code" "grant_type" request to the token endpoint, an |
||
| 148 | * unauthenticated client MUST send its "client_id" to prevent itself |
||
| 149 | * from inadvertently accepting a code intended for a client with a |
||
| 150 | * different "client_id". This protects the client from substitution of |
||
| 151 | * the authentication code. (It provides no additional security for the |
||
| 152 | * protected resource.) |
||
| 153 | */ |
||
| 154 | if (empty($requestData['client_id'])) { |
||
| 155 | throw new OAuthException('invalid_request', 'The request is missing the required parameter client_id.', |
||
| 156 | 'https://tools.ietf.org/html/rfc6749#section-4.1'); |
||
| 157 | } |
||
| 158 | |||
| 159 | if (!$client = $this->clientStorage->get($requestData['client_id'])) { |
||
| 160 | throw new OAuthException('invalid_request', 'The request includes the invalid parameter client_id.', |
||
| 161 | 'https://tools.ietf.org/html/rfc6749#section-4.1'); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @see https://tools.ietf.org/html/rfc6749#section-3.2.1 |
||
| 166 | * Confidential clients or other clients issued client credentials MUST |
||
| 167 | * authenticate with the authorization server as described in |
||
| 168 | * Section 2.3 when making requests to the token endpoint. |
||
| 169 | */ |
||
| 170 | if ($client->hasCredentials()) { |
||
| 171 | throw new OAuthException('invalid_client', 'Client authentication failed. No client authentication included', |
||
| 172 | 'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
||
| 173 | } |
||
| 174 | |||
| 175 | $clientAuthenticationMethodUsedIdentifier = 'none'; |
||
| 176 | } |
||
| 177 | |||
| 178 | $tokenEndpointAuthMethod = $client->getMetadata()->getTokenEndpointAuthMethod() ?: 'client_secret_basic'; |
||
| 179 | if ($tokenEndpointAuthMethod !== $clientAuthenticationMethodUsedIdentifier) { |
||
| 180 | throw new OAuthException('invalid_client', |
||
| 181 | 'Client authentication failed. Unsupported authentication method.', |
||
| 182 | 'https://tools.ietf.org/html/rfc6749#section-3.2.1'); |
||
| 183 | } |
||
| 184 | |||
| 185 | return $client; |
||
| 186 | } |
||
| 187 | } |