| Conditions | 23 |
| Paths | 71 |
| Total Lines | 118 |
| Code Lines | 76 |
| 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 |
||
| 115 | protected function verify(ServerRequestInterface $request) |
||
| 116 | { |
||
| 117 | $authorizationRequest = AuthorizationRequest::createFromServerRequest($request); |
||
| 118 | |||
| 119 | if (!$authorizationRequest->getClientId()) { |
||
| 120 | return new Response(400, [], json_encode([ |
||
| 121 | 'error' => 'invalid_request', |
||
| 122 | 'error_description' => 'Missing a required parameter : client_id', |
||
| 123 | 'error_uri' => 'https://tools.ietf.org/html/rfc6749#section-4.1', |
||
| 124 | ])); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var ClientStorageInterface $clientStorage |
||
| 129 | */ |
||
| 130 | $clientStorage = $this->server->getStorageRepository()->getStorage('client'); |
||
| 131 | |||
| 132 | $client = $clientStorage->get($authorizationRequest->getClientId()); |
||
| 133 | if (!$client) { |
||
| 134 | return new Response(400, [], json_encode([ |
||
| 135 | 'error' => 'invalid_request', |
||
| 136 | 'error_description' => 'Invalid parameter : client_id', |
||
| 137 | 'error_uri' => 'https://tools.ietf.org/html/rfc6749#section-4.1', |
||
| 138 | ])); |
||
| 139 | } |
||
| 140 | if (!$client instanceof RegisteredClient) { |
||
| 141 | return new Response(400, [], json_encode([ |
||
| 142 | 'error' => 'invalid_request', |
||
| 143 | 'error_description' => 'Client type is not supported', |
||
| 144 | 'error_uri' => 'https://tools.ietf.org/html/rfc6749#section-4.1', |
||
| 145 | ])); |
||
| 146 | } |
||
| 147 | |||
| 148 | try { |
||
| 149 | $redirectUri = $this->checkClientRedirectUri($client, $authorizationRequest->getRedirectUri()); |
||
| 150 | } catch (\Exception $e) { |
||
| 151 | return new Response(400, [], json_encode([ |
||
| 152 | 'error' => 'invalid_request', |
||
| 153 | 'error_description' => $e->getMessage(), |
||
| 154 | 'error_uri' => 'https://tools.ietf.org/html/rfc6749#section-4.1', |
||
| 155 | ])); |
||
| 156 | } |
||
| 157 | |||
| 158 | try { |
||
| 159 | if (!$authorizationRequest->getResponseType()) { |
||
| 160 | throw new OAuthException('invalid_request', |
||
| 161 | 'Missing a required parameter : response_type', |
||
| 162 | 'https://tools.ietf.org/html/rfc6749#section-4.1' |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | |||
| 166 | if ($this->server->getConfigurationRepository()->getConfig(Config::ENFORCE_STATE) && |
||
| 167 | !$authorizationRequest->getState()) { |
||
| 168 | throw new OAuthException('invalid_request', |
||
| 169 | 'Missing a required parameter : state', |
||
| 170 | 'https://tools.ietf.org/html/rfc6749#section-4.1' |
||
| 171 | ); |
||
| 172 | } |
||
| 173 | |||
| 174 | $enforceTls = $this->server->getConfigurationRepository()->getConfig(Config::ENFORCE_TLS); |
||
| 175 | $responseMode = ResponseTypeInterface::RESPONSE_MODE_QUERY; |
||
| 176 | $isImplicit = false; |
||
| 177 | $isInsecure = false; |
||
| 178 | $responseTypes = []; |
||
| 179 | foreach (explode(' ', $authorizationRequest->getResponseType()) as $responseTypeName) { |
||
| 180 | $responseType = $this->server->getResponseTypeRepository()->getResponseType($responseTypeName); |
||
| 181 | if (!$responseType) { |
||
| 182 | throw new OAuthException('unsupported_response_type', |
||
| 183 | 'Invalid response_type parameter', |
||
| 184 | 'https://tools.ietf.org/html/rfc6749#section-3.1.1'); |
||
| 185 | } |
||
| 186 | |||
| 187 | if (($enforceTls == true && !$redirectUri->getScheme() != 'https') || |
||
| 188 | (is_null($enforceTls) && $responseType->requireTLS() && !$redirectUri->getScheme() != 'https')) { |
||
| 189 | if (is_null($enforceTls) && !$client->isTLSSupported()) { |
||
| 190 | $isInsecure = true; |
||
| 191 | } else { |
||
| 192 | throw new OAuthException('access_denied', |
||
| 193 | 'Require the use of TLS for the redirect URI', |
||
| 194 | 'https://tools.ietf.org/html/rfc6749#section-3.1.2.1'); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | $responseType->verifyRequest($request); |
||
| 199 | $responseTypes[] = $responseType; |
||
| 200 | |||
| 201 | if ($responseType->getDefaultResponseMode() == ResponseTypeInterface::RESPONSE_MODE_FRAGMENT) { |
||
| 202 | $responseMode = $responseType->getDefaultResponseMode(); |
||
| 203 | } |
||
| 204 | |||
| 205 | if ($responseType->isImplicit()) { |
||
| 206 | $isImplicit = true; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | if ($isImplicit && !$client->isImplicitAllowed()) { |
||
| 211 | throw new OAuthException('unauthorized_client', |
||
| 212 | 'Client is not allowed to use implicit grant', |
||
| 213 | 'https://tools.ietf.org/html/rfc6749#section-3.1.1'); |
||
| 214 | } |
||
| 215 | |||
| 216 | $scopePolicyManager = $this->server->getScopePolicyManager(); |
||
| 217 | |||
| 218 | $scope = $scopePolicyManager->getScopeArray($client, $authorizationRequest->getScope()); |
||
| 219 | if (!$scopePolicyManager->checkScope($client, $scope)) { |
||
| 220 | $supportedScopes = implode(', ', $scopePolicyManager->getSupportedScopes($client)); |
||
| 221 | throw new OAuthException('invalid_scope', |
||
| 222 | 'Some of requested scopes are not supported. Scope supported : ' . $supportedScopes, |
||
| 223 | 'https://tools.ietf.org/html/rfc6749#section-4.1'); |
||
| 224 | } |
||
| 225 | |||
| 226 | } catch (OAuthException $e) { |
||
| 227 | return new ErrorResponse($redirectUri, |
||
| 228 | $e->getError(), $e->getErrorDescription(), $e->getErrorUri(), |
||
| 229 | $authorizationRequest->getState()); |
||
| 230 | } |
||
| 231 | |||
| 232 | return compact('client', 'responseTypes', 'redirectUri', 'scope', 'responseMode', 'isInsecure'); |
||
| 233 | } |
||
| 311 | } |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.