2016-11-30 01:45:56 +05:30
|
|
|
|
<?php
|
|
|
|
|
namespace api\components\OAuth2\Grants;
|
|
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
|
use api\components\OAuth2\Entities\AccessTokenEntity;
|
|
|
|
|
use api\components\OAuth2\Entities\RefreshTokenEntity;
|
2016-11-30 04:49:14 +05:30
|
|
|
|
use ErrorException;
|
2017-06-17 23:35:36 +05:30
|
|
|
|
use League\OAuth2\Server\Entity\AccessTokenEntity as BaseAccessTokenEntity;
|
|
|
|
|
use League\OAuth2\Server\Entity\ClientEntity as BaseClientEntity;
|
|
|
|
|
use League\OAuth2\Server\Entity\RefreshTokenEntity as BaseRefreshTokenEntity;
|
|
|
|
|
use League\OAuth2\Server\Event\ClientAuthenticationFailedEvent;
|
2016-11-30 04:49:14 +05:30
|
|
|
|
use League\OAuth2\Server\Exception;
|
2017-06-17 23:35:36 +05:30
|
|
|
|
use League\OAuth2\Server\Grant\AbstractGrant;
|
2016-11-30 04:49:14 +05:30
|
|
|
|
use League\OAuth2\Server\Util\SecureKey;
|
2016-11-30 01:45:56 +05:30
|
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
|
class RefreshTokenGrant extends AbstractGrant {
|
2016-11-30 01:45:56 +05:30
|
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
|
protected $identifier = 'refresh_token';
|
2016-11-30 01:45:56 +05:30
|
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
|
protected $refreshTokenTTL = 604800;
|
|
|
|
|
|
|
|
|
|
protected $refreshTokenRotate = false;
|
|
|
|
|
|
|
|
|
|
protected $requireClientSecret = true;
|
|
|
|
|
|
|
|
|
|
public function setRefreshTokenTTL($refreshTokenTTL): void {
|
|
|
|
|
$this->refreshTokenTTL = $refreshTokenTTL;
|
2016-11-30 01:45:56 +05:30
|
|
|
|
}
|
|
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
|
public function getRefreshTokenTTL(): int {
|
|
|
|
|
return $this->refreshTokenTTL;
|
2016-11-30 01:45:56 +05:30
|
|
|
|
}
|
|
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
|
public function setRefreshTokenRotation(bool $refreshTokenRotate = true): void {
|
|
|
|
|
$this->refreshTokenRotate = $refreshTokenRotate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function shouldRotateRefreshTokens(): bool {
|
|
|
|
|
return $this->refreshTokenRotate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setRequireClientSecret(string $required): void {
|
|
|
|
|
$this->requireClientSecret = $required;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function shouldRequireClientSecret(): bool {
|
|
|
|
|
return $this->requireClientSecret;
|
2016-11-30 01:45:56 +05:30
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 16:14:06 +05:30
|
|
|
|
/**
|
|
|
|
|
* По стандарту OAuth2 scopes должны разделяться пробелом, а не запятой. Косяк.
|
|
|
|
|
* Так что оборачиваем функцию разбора скоупов, заменяя пробелы на запятые.
|
|
|
|
|
*
|
|
|
|
|
* @param string $scopeParam
|
|
|
|
|
* @param ClientEntity $client
|
|
|
|
|
* @param string $redirectUri
|
|
|
|
|
*
|
|
|
|
|
* @return \League\OAuth2\Server\Entity\ScopeEntity[]
|
|
|
|
|
*/
|
|
|
|
|
public function validateScopes($scopeParam = '', ClientEntity $client, $redirectUri = null) {
|
|
|
|
|
$scopes = str_replace(' ', $this->server->getScopeDelimiter(), $scopeParam);
|
|
|
|
|
return parent::validateScopes($scopes, $client, $redirectUri);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 04:49:14 +05:30
|
|
|
|
/**
|
|
|
|
|
* Метод таки пришлось переписать по той причине, что нынче мы храним access_token в redis с expire значением,
|
|
|
|
|
* так что он может банально несуществовать на тот момент, когда к нему через refresh_token попытаются обратиться.
|
|
|
|
|
* Поэтому мы расширили логику RefreshTokenEntity и она теперь знает о сессии, в рамках которой была создана
|
|
|
|
|
*
|
|
|
|
|
* @inheritdoc
|
2017-06-17 23:35:36 +05:30
|
|
|
|
* @throws \League\OAuth2\Server\Exception\OAuthException
|
2016-11-30 04:49:14 +05:30
|
|
|
|
*/
|
2017-06-17 23:35:36 +05:30
|
|
|
|
public function completeFlow(): array {
|
2016-11-30 04:49:14 +05:30
|
|
|
|
$clientId = $this->server->getRequest()->request->get('client_id', $this->server->getRequest()->getUser());
|
2017-06-17 23:35:36 +05:30
|
|
|
|
if ($clientId === null) {
|
2016-11-30 04:49:14 +05:30
|
|
|
|
throw new Exception\InvalidRequestException('client_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$clientSecret = $this->server->getRequest()->request->get(
|
|
|
|
|
'client_secret',
|
|
|
|
|
$this->server->getRequest()->getPassword()
|
|
|
|
|
);
|
2017-06-17 23:35:36 +05:30
|
|
|
|
if ($clientSecret === null && $this->shouldRequireClientSecret()) {
|
2016-11-30 04:49:14 +05:30
|
|
|
|
throw new Exception\InvalidRequestException('client_secret');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate client ID and client secret
|
2017-06-17 23:35:36 +05:30
|
|
|
|
$client = $this->server->getClientStorage()->get($clientId, $clientSecret, null, $this->getIdentifier());
|
|
|
|
|
if (($client instanceof BaseClientEntity) === false) {
|
|
|
|
|
$this->server->getEventEmitter()->emit(new ClientAuthenticationFailedEvent($this->server->getRequest()));
|
2016-11-30 04:49:14 +05:30
|
|
|
|
throw new Exception\InvalidClientException();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
|
$oldRefreshTokenParam = $this->server->getRequest()->request->get('refresh_token');
|
2016-11-30 04:49:14 +05:30
|
|
|
|
if ($oldRefreshTokenParam === null) {
|
|
|
|
|
throw new Exception\InvalidRequestException('refresh_token');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate refresh token
|
|
|
|
|
$oldRefreshToken = $this->server->getRefreshTokenStorage()->get($oldRefreshTokenParam);
|
2017-06-17 23:35:36 +05:30
|
|
|
|
if (($oldRefreshToken instanceof BaseRefreshTokenEntity) === false) {
|
2016-11-30 04:49:14 +05:30
|
|
|
|
throw new Exception\InvalidRefreshException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure the old refresh token hasn't expired
|
|
|
|
|
if ($oldRefreshToken->isExpired()) {
|
|
|
|
|
throw new Exception\InvalidRefreshException();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
|
/** @var AccessTokenEntity|null $oldAccessToken */
|
2016-11-30 04:49:14 +05:30
|
|
|
|
$oldAccessToken = $oldRefreshToken->getAccessToken();
|
2017-06-17 23:35:36 +05:30
|
|
|
|
if ($oldAccessToken instanceof AccessTokenEntity) {
|
2016-11-30 04:49:14 +05:30
|
|
|
|
// Get the scopes for the original session
|
|
|
|
|
$session = $oldAccessToken->getSession();
|
|
|
|
|
} else {
|
2017-06-17 23:35:36 +05:30
|
|
|
|
if (!$oldRefreshToken instanceof RefreshTokenEntity) {
|
|
|
|
|
/** @noinspection ExceptionsAnnotatingAndHandlingInspection */
|
|
|
|
|
throw new ErrorException('oldRefreshToken must be instance of ' . RefreshTokenEntity::class);
|
2016-11-30 04:49:14 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$session = $oldRefreshToken->getSession();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scopes = $this->formatScopes($session->getScopes());
|
|
|
|
|
|
|
|
|
|
// Get and validate any requested scopes
|
|
|
|
|
$requestedScopesString = $this->server->getRequest()->request->get('scope', '');
|
|
|
|
|
$requestedScopes = $this->validateScopes($requestedScopesString, $client);
|
|
|
|
|
|
|
|
|
|
// If no new scopes are requested then give the access token the original session scopes
|
|
|
|
|
if (count($requestedScopes) === 0) {
|
|
|
|
|
$newScopes = $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 ($requestedScopes as $requestedScope) {
|
|
|
|
|
if (!isset($scopes[$requestedScope->getId()])) {
|
|
|
|
|
throw new Exception\InvalidScopeException($requestedScope->getId());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$newScopes = $requestedScopes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate a new access token and assign it the correct sessions
|
2017-06-17 23:35:36 +05:30
|
|
|
|
$newAccessToken = new AccessTokenEntity($this->server);
|
|
|
|
|
$newAccessToken->setId(SecureKey::generate()); // TODO: generate based on permissions
|
2016-11-30 04:49:14 +05:30
|
|
|
|
$newAccessToken->setExpireTime($this->getAccessTokenTTL() + time());
|
|
|
|
|
$newAccessToken->setSession($session);
|
|
|
|
|
|
|
|
|
|
foreach ($newScopes as $newScope) {
|
|
|
|
|
$newAccessToken->associateScope($newScope);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Expire the old token and save the new one
|
2017-06-17 23:35:36 +05:30
|
|
|
|
$oldAccessToken instanceof BaseAccessTokenEntity && $oldAccessToken->expire();
|
2016-11-30 04:49:14 +05:30
|
|
|
|
$newAccessToken->save();
|
|
|
|
|
|
|
|
|
|
$this->server->getTokenType()->setSession($session);
|
|
|
|
|
$this->server->getTokenType()->setParam('access_token', $newAccessToken->getId());
|
|
|
|
|
$this->server->getTokenType()->setParam('expires_in', $this->getAccessTokenTTL());
|
|
|
|
|
|
|
|
|
|
if ($this->shouldRotateRefreshTokens()) {
|
|
|
|
|
// Expire the old refresh token
|
|
|
|
|
$oldRefreshToken->expire();
|
|
|
|
|
|
|
|
|
|
// Generate a new refresh token
|
2017-06-17 23:35:36 +05:30
|
|
|
|
$newRefreshToken = new RefreshTokenEntity($this->server);
|
2016-11-30 04:49:14 +05:30
|
|
|
|
$newRefreshToken->setId(SecureKey::generate());
|
|
|
|
|
$newRefreshToken->setExpireTime($this->getRefreshTokenTTL() + time());
|
|
|
|
|
$newRefreshToken->setAccessToken($newAccessToken);
|
|
|
|
|
$newRefreshToken->save();
|
|
|
|
|
|
|
|
|
|
$this->server->getTokenType()->setParam('refresh_token', $newRefreshToken->getId());
|
|
|
|
|
} else {
|
|
|
|
|
$oldRefreshToken->setAccessToken($newAccessToken);
|
|
|
|
|
$oldRefreshToken->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->server->getTokenType()->generateResponse();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 01:45:56 +05:30
|
|
|
|
}
|