mirror of
https://github.com/elyby/oauth2-server.git
synced 2025-05-31 14:12:07 +05:30
144 lines
5.2 KiB
PHP
144 lines
5.2 KiB
PHP
<?php
|
|
/**
|
|
* OAuth 2.0 Refresh token grant.
|
|
*
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
|
* @copyright Copyright (c) Alex Bilbie
|
|
* @license http://mit-license.org/
|
|
*
|
|
* @link https://github.com/thephpleague/oauth2-server
|
|
*/
|
|
namespace League\OAuth2\Server\Grant;
|
|
|
|
use League\Event\Event;
|
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
|
|
use League\OAuth2\Server\Utils\KeyCrypt;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
/**
|
|
* Refresh token grant.
|
|
*/
|
|
class RefreshTokenGrant extends AbstractGrant
|
|
{
|
|
/**
|
|
* @param \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface $refreshTokenRepository
|
|
*/
|
|
public function __construct(RefreshTokenRepositoryInterface $refreshTokenRepository)
|
|
{
|
|
$this->setRefreshTokenRepository($refreshTokenRepository);
|
|
|
|
$this->refreshTokenTTL = new \DateInterval('P1M');
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function respondToRequest(
|
|
ServerRequestInterface $request,
|
|
ResponseTypeInterface $responseType,
|
|
\DateInterval $accessTokenTTL
|
|
) {
|
|
// Validate request
|
|
$client = $this->validateClient($request);
|
|
$oldRefreshToken = $this->validateOldRefreshToken($request, $client->getIdentifier());
|
|
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request), $client);
|
|
|
|
// If no new scopes are requested then give the access token the original session scopes
|
|
if (count($scopes) === 0) {
|
|
$scopes = array_map(function ($scopeId) use ($client) {
|
|
$scope = $this->scopeRepository->getScopeEntityByIdentifier(
|
|
$scopeId,
|
|
$this->getIdentifier(),
|
|
$client->getIdentifier()
|
|
);
|
|
|
|
if (!$scope) {
|
|
throw OAuthServerException::invalidScope($scopeId);
|
|
}
|
|
|
|
return $scope;
|
|
}, $oldRefreshToken['scopes']);
|
|
} else {
|
|
// The OAuth spec says that a refreshed access token can have the original scopes or fewer so ensure
|
|
// the request doesn't include any new scopes
|
|
foreach ($scopes as $scope) {
|
|
if (in_array($scope->getIdentifier(), $oldRefreshToken['scopes']) === false) {
|
|
$this->getEmitter()->emit(new Event('scope.selection.failed', $request));
|
|
|
|
throw OAuthServerException::invalidScope($scope->getIdentifier());
|
|
}
|
|
}
|
|
}
|
|
|
|
// Expire old tokens
|
|
$this->accessTokenRepository->revokeAccessToken($oldRefreshToken['access_token_id']);
|
|
$this->getRefreshTokenRepository()->revokeRefreshToken($oldRefreshToken['refresh_token_id']);
|
|
|
|
// Issue and persist new tokens
|
|
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $oldRefreshToken['user_id'], $scopes);
|
|
$refreshToken = $this->issueRefreshToken($accessToken);
|
|
$this->accessTokenRepository->persistNewAccessToken($accessToken);
|
|
$this->getRefreshTokenRepository()->persistNewRefreshToken($refreshToken);
|
|
|
|
// Inject tokens into response
|
|
$responseType->setAccessToken($accessToken);
|
|
$responseType->setRefreshToken($refreshToken);
|
|
|
|
return $responseType;
|
|
}
|
|
|
|
/**
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request
|
|
* @param string $clientId
|
|
*
|
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function validateOldRefreshToken(ServerRequestInterface $request, $clientId)
|
|
{
|
|
$encryptedRefreshToken = $this->getRequestParameter('refresh_token', $request);
|
|
if (is_null($encryptedRefreshToken)) {
|
|
throw OAuthServerException::invalidRequest('refresh_token', null, '`%s` parameter is missing');
|
|
}
|
|
|
|
// Validate refresh token
|
|
try {
|
|
$refreshToken = KeyCrypt::decrypt($encryptedRefreshToken, $this->pathToPublicKey);
|
|
} catch (\LogicException $e) {
|
|
throw OAuthServerException::invalidRefreshToken('Cannot parse refresh token: ' . $e->getMessage());
|
|
}
|
|
|
|
$refreshTokenData = json_decode($refreshToken, true);
|
|
if ($refreshTokenData['client_id'] !== $clientId) {
|
|
$this->getEmitter()->emit(new Event('refresh_token.client.failed', $request));
|
|
|
|
throw OAuthServerException::invalidRefreshToken(
|
|
'Token is not linked to client,' .
|
|
' got: ' . $clientId .
|
|
' expected: ' . $refreshTokenData['client_id']
|
|
);
|
|
}
|
|
|
|
if ($refreshTokenData['expire_time'] < time()) {
|
|
throw OAuthServerException::invalidRefreshToken('Token has expired');
|
|
}
|
|
|
|
if ($this->getRefreshTokenRepository()->isRefreshTokenRevoked($refreshTokenData['refresh_token_id']) === true) {
|
|
throw OAuthServerException::invalidRefreshToken('Token has been revoked');
|
|
}
|
|
|
|
return $refreshTokenData;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getIdentifier()
|
|
{
|
|
return 'refresh_token';
|
|
}
|
|
}
|