| Conditions | 19 |
| Paths | 48 |
| Total Lines | 127 |
| Code Lines | 50 |
| 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 |
||
| 75 | public function grantAccessToken(RequestInterface $request, ResponseInterface $response) |
||
| 76 | { |
||
| 77 | if (strtolower($request->server('REQUEST_METHOD')) != 'post') { |
||
| 78 | $response->setError(405, 'invalid_request', 'The request method must be POST when requesting an access token', '#section-3.2'); |
||
| 79 | $response->addHttpHeaders(array('Allow' => 'POST')); |
||
| 80 | |||
| 81 | return null; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Determine grant type from request |
||
| 86 | * and validate the request for that grant type |
||
| 87 | */ |
||
| 88 | if (!$grantTypeIdentifier = $request->request('grant_type')) { |
||
| 89 | $response->setError(400, 'invalid_request', 'The grant type was not specified in the request'); |
||
| 90 | |||
| 91 | return null; |
||
| 92 | } |
||
| 93 | |||
| 94 | if (!isset($this->grantTypes[$grantTypeIdentifier])) { |
||
| 95 | /* TODO: If this is an OAuth2 supported grant type that we have chosen not to implement, throw a 501 Not Implemented instead */ |
||
| 96 | $response->setError(400, 'unsupported_grant_type', sprintf('Grant type "%s" not supported', $grantTypeIdentifier)); |
||
| 97 | |||
| 98 | return null; |
||
| 99 | } |
||
| 100 | |||
| 101 | $grantType = $this->grantTypes[$grantTypeIdentifier]; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Retrieve the client information from the request |
||
| 105 | * ClientAssertionTypes allow for grant types which also assert the client data |
||
| 106 | * in which case ClientAssertion is handled in the validateRequest method |
||
| 107 | * |
||
| 108 | * @see OAuth2\GrantType\JWTBearer |
||
| 109 | * @see OAuth2\GrantType\ClientCredentials |
||
| 110 | */ |
||
| 111 | if (!$grantType instanceof ClientAssertionTypeInterface) { |
||
| 112 | if (!$this->clientAssertionType->validateRequest($request, $response)) { |
||
| 113 | return null; |
||
| 114 | } |
||
| 115 | $clientId = $this->clientAssertionType->getClientId(); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Retrieve the grant type information from the request |
||
| 120 | * The GrantTypeInterface object handles all validation |
||
| 121 | * If the object is an instance of ClientAssertionTypeInterface, |
||
| 122 | * That logic is handled here as well |
||
| 123 | */ |
||
| 124 | if (!$grantType->validateRequest($request, $response)) { |
||
| 125 | return null; |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($grantType instanceof ClientAssertionTypeInterface) { |
||
| 129 | $clientId = $grantType->getClientId(); |
||
| 130 | } else { |
||
| 131 | // validate the Client ID (if applicable) |
||
| 132 | if (!is_null($storedClientId = $grantType->getClientId()) && $storedClientId != $clientId) { |
||
| 133 | $response->setError(400, 'invalid_grant', sprintf('%s doesn\'t exist or is invalid for the client', $grantTypeIdentifier)); |
||
| 134 | |||
| 135 | return null; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Validate the client can use the requested grant type |
||
| 141 | */ |
||
| 142 | if (!$this->clientStorage->checkRestrictedGrantType($clientId, $grantTypeIdentifier)) { |
||
| 143 | $response->setError(400, 'unauthorized_client', 'The grant type is unauthorized for this client_id'); |
||
| 144 | |||
| 145 | return false; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Validate the scope of the token |
||
| 150 | * |
||
| 151 | * requestedScope - the scope specified in the token request |
||
| 152 | * availableScope - the scope associated with the grant type |
||
| 153 | * ex: in the case of the "Authorization Code" grant type, |
||
| 154 | * the scope is specified in the authorize request |
||
| 155 | * |
||
| 156 | * @see http://tools.ietf.org/html/rfc6749#section-3.3 |
||
| 157 | */ |
||
| 158 | |||
| 159 | $requestedScope = $this->scopeUtil->getScopeFromRequest($request); |
||
| 160 | $availableScope = $grantType->getScope(); |
||
| 161 | |||
| 162 | if ($requestedScope) { |
||
| 163 | // validate the requested scope |
||
| 164 | if ($availableScope) { |
||
| 165 | if (!$this->scopeUtil->checkScope($requestedScope, $availableScope)) { |
||
| 166 | $response->setError(400, 'invalid_scope', 'The scope requested is invalid for this request'); |
||
| 167 | |||
| 168 | return null; |
||
| 169 | } |
||
| 170 | } else { |
||
| 171 | // validate the client has access to this scope |
||
| 172 | if ($clientScope = $this->clientStorage->getClientScope($clientId)) { |
||
| 173 | if (!$this->scopeUtil->checkScope($requestedScope, $clientScope)) { |
||
| 174 | $response->setError(400, 'invalid_scope', 'The scope requested is invalid for this client'); |
||
| 175 | |||
| 176 | return false; |
||
| 177 | } |
||
| 178 | } elseif (!$this->scopeUtil->scopeExists($requestedScope)) { |
||
| 179 | $response->setError(400, 'invalid_scope', 'An unsupported scope was requested'); |
||
| 180 | |||
| 181 | return null; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } elseif ($availableScope) { |
||
| 185 | // use the scope associated with this grant type |
||
| 186 | $requestedScope = $availableScope; |
||
| 187 | } else { |
||
| 188 | // use a globally-defined default scope |
||
| 189 | $defaultScope = $this->scopeUtil->getDefaultScope($clientId); |
||
| 190 | |||
| 191 | // "false" means default scopes are not allowed |
||
| 192 | if (false === $defaultScope) { |
||
| 193 | $response->setError(400, 'invalid_scope', 'This application requires you specify a scope parameter'); |
||
| 194 | |||
| 195 | return null; |
||
| 196 | } |
||
| 197 | |||
| 198 | $requestedScope = $defaultScope; |
||
| 199 | } |
||
| 200 | |||
| 201 | return $grantType->createAccessToken($this->accessToken, $clientId, $grantType->getUserId(), $requestedScope); |
||
| 202 | } |
||
| 275 |