Cheetah
TokenController.php
Go to the documentation of this file.
1 <?php
2 
3 namespace OAuth2\Controller;
4 
13 
18 {
19  protected $accessToken;
20  protected $grantTypes;
22  protected $scopeUtil;
23  protected $clientStorage;
24 
26  {
27  if (is_null($clientAssertionType)) {
28  foreach ($grantTypes as $grantType) {
29  if (!$grantType instanceof ClientAssertionTypeInterface) {
30  throw new \InvalidArgumentException('You must supply an instance of OAuth2\ClientAssertionType\ClientAssertionTypeInterface or only use grant types which implement OAuth2\ClientAssertionType\ClientAssertionTypeInterface');
31  }
32  }
33  }
34  $this->clientAssertionType = $clientAssertionType;
35  $this->accessToken = $accessToken;
36  $this->clientStorage = $clientStorage;
37  foreach ($grantTypes as $grantType) {
38  $this->addGrantType($grantType);
39  }
40 
41  if (is_null($scopeUtil)) {
42  $scopeUtil = new Scope();
43  }
44  $this->scopeUtil = $scopeUtil;
45  }
46 
47  public function handleTokenRequest(RequestInterface $request, ResponseInterface $response)
48  {
49  if ($token = $this->grantAccessToken($request, $response)) {
50  // @see http://tools.ietf.org/html/rfc6749#section-5.1
51  // server MUST disable caching in headers when tokens are involved
52  $response->setStatusCode(200);
53  $response->addParameters($token);
54  $response->addHttpHeaders(array('Cache-Control' => 'no-store', 'Pragma' => 'no-cache'));
55  }
56  }
57 
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 
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 
111  if (!$grantType instanceof ClientAssertionTypeInterface) {
112  if (!$this->clientAssertionType->validateRequest($request, $response)) {
113  return null;
114  }
115  $clientId = $this->clientAssertionType->getClientId();
116  }
117 
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 
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 
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  }
203 
212  public function addGrantType(GrantTypeInterface $grantType, $identifier = null)
213  {
214  if (is_null($identifier) || is_numeric($identifier)) {
215  $identifier = $grantType->getQuerystringIdentifier();
216  }
217 
218  $this->grantTypes[$identifier] = $grantType;
219  }
220 
221  public function handleRevokeRequest(RequestInterface $request, ResponseInterface $response)
222  {
223  if ($this->revokeToken($request, $response)) {
224  $response->setStatusCode(200);
225  $response->addParameters(array('revoked' => true));
226  }
227  }
228 
241  public function revokeToken(RequestInterface $request, ResponseInterface $response)
242  {
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');
245  $response->addHttpHeaders(array('Allow' => 'POST'));
246 
247  return null;
248  }
249 
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\'');
253 
254  return null;
255  }
256 
257  $token = $request->request('token');
258  if ($token === null) {
259  $response->setError(400, 'invalid_request', 'Missing token parameter to revoke');
260 
261  return null;
262  }
263 
264  // @todo remove this check for v2.0
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");
268  }
269 
270  $this->accessToken->revokeToken($token, $token_type_hint);
271 
272  return true;
273  }
274 }
OAuth2\Controller\TokenController\grantAccessToken
grantAccessToken(RequestInterface $request, ResponseInterface $response)
Definition: TokenController.php:75
OAuth2\ResponseInterface\addParameters
addParameters(array $parameters)
OAuth2\Controller\TokenController\$scopeUtil
$scopeUtil
Definition: TokenController.php:22
OAuth2\Controller\TokenControllerInterface
Definition: TokenControllerInterface.php:19
OAuth2\GrantType\GrantTypeInterface
Definition: GrantTypeInterface.php:13
OAuth2\ResponseInterface\addHttpHeaders
addHttpHeaders(array $httpHeaders)
OAuth2\ScopeInterface
Definition: ScopeInterface.php:13
OAuth2\RequestInterface\request
request($name, $default=null)
OAuth2\ResponseInterface\setStatusCode
setStatusCode($statusCode)
OAuth2\RequestInterface\server
server($name, $default=null)
OAuth2\Controller\TokenController\__construct
__construct(AccessTokenInterface $accessToken, ClientInterface $clientStorage, array $grantTypes=array(), ClientAssertionTypeInterface $clientAssertionType=null, ScopeInterface $scopeUtil=null)
Definition: TokenController.php:25
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
OAuth2\Scope
Definition: Scope.php:12
php
OAuth2\Controller\TokenController\$accessToken
$accessToken
Definition: TokenController.php:19
OAuth2\Storage\ClientInterface
Definition: ClientInterface.php:12
OAuth2\Controller\TokenController\$clientStorage
$clientStorage
Definition: TokenController.php:23
OAuth2\ResponseType\AccessTokenInterface
Definition: AccessTokenInterface.php:10
OAuth2\ResponseInterface\setError
setError($statusCode, $name, $description=null, $uri=null)
OAuth2\Controller\TokenController\addGrantType
addGrantType(GrantTypeInterface $grantType, $identifier=null)
Definition: TokenController.php:212
OAuth2\Controller\TokenController\revokeToken
revokeToken(RequestInterface $request, ResponseInterface $response)
Definition: TokenController.php:241
OAuth2\Controller
Definition: AuthorizeController.php:3
OAuth2\Controller\TokenController\$grantTypes
$grantTypes
Definition: TokenController.php:20
OAuth2\ResponseInterface
Definition: ResponseInterface.php:12
OAuth2\ClientAssertionType\ClientAssertionTypeInterface
Definition: ClientAssertionTypeInterface.php:12
OAuth2\RequestInterface
Definition: RequestInterface.php:6
OAuth2\Controller\TokenController\handleTokenRequest
handleTokenRequest(RequestInterface $request, ResponseInterface $response)
Definition: TokenController.php:47
OAuth2\Controller\TokenController\handleRevokeRequest
handleRevokeRequest(RequestInterface $request, ResponseInterface $response)
Definition: TokenController.php:221
OAuth2\Controller\TokenController
Definition: TokenController.php:18
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
OAuth2\Controller\TokenController\$clientAssertionType
$clientAssertionType
Definition: TokenController.php:21
OAuth2\GrantType\GrantTypeInterface\getQuerystringIdentifier
getQuerystringIdentifier()