0
0
Fork 0
mirror of https://github.com/salesagility/SuiteCRM.git synced 2025-02-06 06:50:04 +00:00
salesagility_SuiteCRM/Api/V8/OAuth2/Repository/RefreshTokenRepository.php
j.dang daa9106104 Implement Refresh Token Grant
- Enable refresh token grant.
- Record already deleted when revoking access token, so include deleted fields when revoking refresh token.
2019-10-14 17:24:59 +01:00

92 lines
2.5 KiB
PHP

<?php
namespace Api\V8\OAuth2\Repository;
use Api\V8\BeanDecorator\BeanManager;
use Api\V8\OAuth2\Entity\RefreshTokenEntity;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
class RefreshTokenRepository implements RefreshTokenRepositoryInterface
{
/**
* @var BeanManager
*/
private $beanManager;
/**
* @param BeanManager $beanManager
*/
public function __construct(BeanManager $beanManager)
{
$this->beanManager = $beanManager;
}
/**
* @inheritdoc
*/
public function getNewRefreshToken()
{
return new RefreshTokenEntity();
}
/**
* @inheritdoc
*
* @throws \InvalidArgumentException When access token is not found.
*/
public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity)
{
/** @var \OAuth2Tokens $token */
$token = $this->beanManager->newBeanSafe(\OAuth2Tokens::class);
$token->retrieve_by_string_fields(
['access_token' => $refreshTokenEntity->getAccessToken()->getIdentifier()]
);
if ($token->id === null) {
throw new \InvalidArgumentException('Access token is not found for this client');
}
$token->refresh_token = $refreshTokenEntity->getIdentifier();
$token->refresh_token_expires = $refreshTokenEntity->getExpiryDateTime()->format('Y-m-d H:i:s');
$token->save();
}
/**
* @inheritdoc
*
* @throws \InvalidArgumentException When refresh token is not found.
*/
public function revokeRefreshToken($tokenId)
{
$token = $this->beanManager->newBeanSafe(\OAuth2Tokens::class);
$token->retrieve_by_string_fields(
['refresh_token' => $tokenId],
true,
false
);
if ($token->id === null) {
throw new \InvalidArgumentException('Refresh token is not found for this client');
}
$token->mark_deleted($token->id);
}
/**
* @inheritdoc
*/
public function isRefreshTokenRevoked($tokenId)
{
/** @var \OAuth2Tokens $token */
$token = $this->beanManager->newBeanSafe(\OAuth2Tokens::class);
$token->retrieve_by_string_fields(
['refresh_token' => $tokenId]
);
if (new \DateTime() > new \DateTime($token->refresh_token_expires) || $token->id === null) {
return true;
}
return false;
}
}