30 throw new \InvalidArgumentException(
'You must supply an instance of OAuth2\ClientAssertionType\ClientAssertionTypeInterface or only use grant types which implement OAuth2\ClientAssertionType\ClientAssertionTypeInterface');
54 $response->
addHttpHeaders(array(
'Cache-Control' =>
'no-store',
'Pragma' =>
'no-cache'));
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');
88 if (!$grantTypeIdentifier = $request->
request(
'grant_type')) {
89 $response->
setError(400,
'invalid_request',
'The grant type was not specified in the request');
94 if (!isset($this->grantTypes[$grantTypeIdentifier])) {
96 $response->
setError(400,
'unsupported_grant_type', sprintf(
'Grant type "%s" not supported', $grantTypeIdentifier));
101 $grantType = $this->grantTypes[$grantTypeIdentifier];
112 if (!$this->clientAssertionType->validateRequest($request, $response)) {
115 $clientId = $this->clientAssertionType->getClientId();
124 if (!$grantType->validateRequest($request, $response)) {
129 $clientId = $grantType->getClientId();
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));
142 if (!$this->clientStorage->checkRestrictedGrantType($clientId, $grantTypeIdentifier)) {
143 $response->
setError(400,
'unauthorized_client',
'The grant type is unauthorized for this client_id');
159 $requestedScope = $this->scopeUtil->getScopeFromRequest($request);
160 $availableScope = $grantType->getScope();
162 if ($requestedScope) {
164 if ($availableScope) {
165 if (!$this->scopeUtil->checkScope($requestedScope, $availableScope)) {
166 $response->
setError(400,
'invalid_scope',
'The scope requested is invalid for this request');
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');
178 } elseif (!$this->scopeUtil->scopeExists($requestedScope)) {
179 $response->
setError(400,
'invalid_scope',
'An unsupported scope was requested');
184 } elseif ($availableScope) {
186 $requestedScope = $availableScope;
189 $defaultScope = $this->scopeUtil->getDefaultScope($clientId);
192 if (
false === $defaultScope) {
193 $response->
setError(400,
'invalid_scope',
'This application requires you specify a scope parameter');
198 $requestedScope = $defaultScope;
201 return $grantType->createAccessToken($this->accessToken, $clientId, $grantType->getUserId(), $requestedScope);
214 if (is_null($identifier) || is_numeric($identifier)) {
218 $this->grantTypes[$identifier] = $grantType;
243 if (strtolower($request->
server(
'REQUEST_METHOD')) !=
'post') {
244 $response->
setError(405,
'invalid_request',
'The request method must be POST when revoking an access token',
'#section-3.2');
250 $token_type_hint = $request->
request(
'token_type_hint');
251 if (!in_array($token_type_hint, array(
null,
'access_token',
'refresh_token'),
true)) {
252 $response->
setError(400,
'invalid_request',
'Token type hint must be either \'access_token\' or \'refresh_token\'');
257 $token = $request->
request(
'token');
258 if ($token ===
null) {
259 $response->
setError(400,
'invalid_request',
'Missing token parameter to revoke');
265 if (!method_exists($this->accessToken,
'revokeToken')) {
266 $class = get_class($this->accessToken);
267 throw new \RuntimeException(
"AccessToken {$class} does not implement required revokeToken method");
270 $this->accessToken->revokeToken($token, $token_type_hint);