vendor/league/oauth2-server/src/Grant/ClientCredentialsGrant.php line 50

  1. <?php
  2. /**
  3.  * OAuth 2.0 Client credentials grant.
  4.  *
  5.  * @author      Alex Bilbie <hello@alexbilbie.com>
  6.  * @copyright   Copyright (c) Alex Bilbie
  7.  * @license     http://mit-license.org/
  8.  *
  9.  * @link        https://github.com/thephpleague/oauth2-server
  10.  */
  11. namespace League\OAuth2\Server\Grant;
  12. use DateInterval;
  13. use League\OAuth2\Server\Exception\OAuthServerException;
  14. use League\OAuth2\Server\RequestAccessTokenEvent;
  15. use League\OAuth2\Server\RequestEvent;
  16. use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
  17. use Psr\Http\Message\ServerRequestInterface;
  18. /**
  19.  * Client credentials grant class.
  20.  */
  21. class ClientCredentialsGrant extends AbstractGrant
  22. {
  23.     /**
  24.      * {@inheritdoc}
  25.      */
  26.     public function respondToAccessTokenRequest(
  27.         ServerRequestInterface $request,
  28.         ResponseTypeInterface $responseType,
  29.         DateInterval $accessTokenTTL
  30.     ) {
  31.         list($clientId) = $this->getClientCredentials($request);
  32.         $client $this->getClientEntityOrFail($clientId$request);
  33.         if (!$client->isConfidential()) {
  34.             $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED$request));
  35.             throw OAuthServerException::invalidClient($request);
  36.         }
  37.         // Validate request
  38.         $this->validateClient($request);
  39.         $scopes $this->validateScopes($this->getRequestParameter('scope'$request$this->defaultScope));
  40.         // Finalize the requested scopes
  41.         $finalizedScopes $this->scopeRepository->finalizeScopes($scopes$this->getIdentifier(), $client);
  42.         // Issue and persist access token
  43.         $accessToken $this->issueAccessToken($accessTokenTTL$clientnull$finalizedScopes);
  44.         // Send event to emitter
  45.         $this->getEmitter()->emit(new RequestAccessTokenEvent(RequestEvent::ACCESS_TOKEN_ISSUED$request$accessToken));
  46.         // Inject access token into response type
  47.         $responseType->setAccessToken($accessToken);
  48.         return $responseType;
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function getIdentifier()
  54.     {
  55.         return 'client_credentials';
  56.     }
  57. }