vendor/league/oauth2-server/src/ResponseTypes/BearerTokenResponse.php line 28

Open in your IDE?
  1. <?php
  2. /**
  3.  * OAuth 2.0 Bearer Token Response.
  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\ResponseTypes;
  12. use DateTime;
  13. use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
  14. use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
  15. use Psr\Http\Message\ResponseInterface;
  16. class BearerTokenResponse extends AbstractResponseType
  17. {
  18.     /**
  19.      * {@inheritdoc}
  20.      */
  21.     public function generateHttpResponse(ResponseInterface $response)
  22.     {
  23.         $expireDateTime $this->accessToken->getExpiryDateTime()->getTimestamp();
  24.         $jwtAccessToken $this->accessToken->convertToJWT($this->privateKey);
  25.         $responseParams = [
  26.             'token_type'   => 'Bearer',
  27.             'expires_in'   => $expireDateTime - (new DateTime())->getTimestamp(),
  28.             'access_token' => (string) $jwtAccessToken,
  29.         ];
  30.         if ($this->refreshToken instanceof RefreshTokenEntityInterface) {
  31.             $refreshToken $this->encrypt(
  32.                 json_encode(
  33.                     [
  34.                         'client_id'        => $this->accessToken->getClient()->getIdentifier(),
  35.                         'refresh_token_id' => $this->refreshToken->getIdentifier(),
  36.                         'access_token_id'  => $this->accessToken->getIdentifier(),
  37.                         'scopes'           => $this->accessToken->getScopes(),
  38.                         'user_id'          => $this->accessToken->getUserIdentifier(),
  39.                         'expire_time'      => $this->refreshToken->getExpiryDateTime()->getTimestamp(),
  40.                     ]
  41.                 )
  42.             );
  43.             $responseParams['refresh_token'] = $refreshToken;
  44.         }
  45.         $responseParams array_merge($this->getExtraParams($this->accessToken), $responseParams);
  46.         $response $response
  47.             ->withStatus(200)
  48.             ->withHeader('pragma''no-cache')
  49.             ->withHeader('cache-control''no-store')
  50.             ->withHeader('content-type''application/json; charset=UTF-8');
  51.         $response->getBody()->write(json_encode($responseParams));
  52.         return $response;
  53.     }
  54.     /**
  55.      * Add custom fields to your Bearer Token response here, then override
  56.      * AuthorizationServer::getResponseType() to pull in your version of
  57.      * this class rather than the default.
  58.      *
  59.      * @param AccessTokenEntityInterface $accessToken
  60.      *
  61.      * @return array
  62.      */
  63.     protected function getExtraParams(AccessTokenEntityInterface $accessToken)
  64.     {
  65.         return [];
  66.     }
  67. }