joomla-oauth-server/com_oauthserver/site/src/Repository/RefreshTokenRepository.php

77 lines
2.4 KiB
PHP
Raw Normal View History

<?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;
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;
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-07 03:59:12 +03:00
$this->refreshTokenModel = $refreshTokenModel;
$this->accessTokenModel = $accessTokenModel;
}
public function getNewRefreshToken(): RefreshToken
{
return new RefreshToken();
}
public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity)
{
$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
if ($found) {
2024-03-07 03:59:12 +03:00
throw UniqueTokenIdentifierConstraintViolationException::create();
}
$data = $refreshTokenEntity->getData();
$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-07 03:59:12 +03:00
public function revokeRefreshToken($tokenId): void
{
2024-03-07 03:59:12 +03:00
$this->refreshTokenModel->revoke($tokenId);
}
2024-03-07 03:59:12 +03:00
public function isRefreshTokenRevoked($tokenId): bool
{
2024-03-07 03:59:12 +03:00
$refreshToken = $this->refreshTokenModel->getItemByIdentifier($tokenId);
if (empty($refreshToken->id)) {
return true;
}
return !!$refreshToken->revoked;
}
}