2024-03-04 03:18:17 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Webmasterskaya\Component\OauthServer\Site\Repository;
|
|
|
|
|
|
|
|
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
|
2024-03-07 03:59:12 +03:00
|
|
|
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
|
2024-03-04 03:18:17 +03:00
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
|
2024-03-07 03:59:12 +03:00
|
|
|
use Webmasterskaya\Component\OauthServer\Administrator\Model\AccessTokenModel;
|
|
|
|
use Webmasterskaya\Component\OauthServer\Administrator\Model\RefreshTokenModel;
|
|
|
|
use Webmasterskaya\Component\OauthServer\Site\Entity\RefreshToken;
|
2024-03-04 03:18:17 +03:00
|
|
|
|
|
|
|
class RefreshTokenRepository implements RefreshTokenRepositoryInterface
|
|
|
|
{
|
|
|
|
|
2024-03-07 03:59:12 +03:00
|
|
|
private RefreshTokenModel $refreshTokenModel;
|
|
|
|
|
|
|
|
private AccessTokenModel $accessTokenModel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Webmasterskaya\Component\OauthServer\Administrator\Model\RefreshTokenModel $refreshTokenModel
|
|
|
|
* @param \Webmasterskaya\Component\OauthServer\Administrator\Model\AccessTokenModel $accessTokenModel
|
|
|
|
* @since version
|
|
|
|
*/
|
|
|
|
public function __construct(RefreshTokenModel $refreshTokenModel, AccessTokenModel $accessTokenModel)
|
2024-03-04 03:18:17 +03:00
|
|
|
{
|
2024-03-07 03:59:12 +03:00
|
|
|
$this->refreshTokenModel = $refreshTokenModel;
|
|
|
|
$this->accessTokenModel = $accessTokenModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getNewRefreshToken(): RefreshToken
|
|
|
|
{
|
|
|
|
return new RefreshToken();
|
2024-03-04 03:18:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity)
|
|
|
|
{
|
2024-03-08 02:18:46 +03:00
|
|
|
$found = false;
|
|
|
|
try {
|
|
|
|
$refreshToken = $this->refreshTokenModel->getItemByIdentifier($refreshTokenEntity->getIdentifier());
|
|
|
|
|
|
|
|
if ($refreshToken->id > 0) {
|
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
}
|
2024-03-07 03:59:12 +03:00
|
|
|
|
2024-03-08 02:18:46 +03:00
|
|
|
if ($found) {
|
2024-03-07 03:59:12 +03:00
|
|
|
throw UniqueTokenIdentifierConstraintViolationException::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $refreshTokenEntity->getData();
|
|
|
|
|
2024-03-08 02:18:46 +03:00
|
|
|
$accessToken = $this->accessTokenModel->getItemByIdentifier($refreshTokenEntity->getAccessToken()->getIdentifier());
|
2024-03-07 03:59:12 +03:00
|
|
|
|
|
|
|
unset($data['access_token_identifier']);
|
|
|
|
$data['access_token_id'] = $accessToken->id;
|
|
|
|
|
|
|
|
$this->refreshTokenModel->save($data);
|
2024-03-04 03:18:17 +03:00
|
|
|
}
|
|
|
|
|
2024-03-07 03:59:12 +03:00
|
|
|
public function revokeRefreshToken($tokenId): void
|
2024-03-04 03:18:17 +03:00
|
|
|
{
|
2024-03-07 03:59:12 +03:00
|
|
|
$this->refreshTokenModel->revoke($tokenId);
|
2024-03-04 03:18:17 +03:00
|
|
|
}
|
|
|
|
|
2024-03-07 03:59:12 +03:00
|
|
|
public function isRefreshTokenRevoked($tokenId): bool
|
2024-03-04 03:18:17 +03:00
|
|
|
{
|
2024-03-07 03:59:12 +03:00
|
|
|
$refreshToken = $this->refreshTokenModel->getItemByIdentifier($tokenId);
|
|
|
|
|
|
|
|
if (empty($refreshToken->id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!$refreshToken->revoked;
|
2024-03-04 03:18:17 +03:00
|
|
|
}
|
|
|
|
}
|