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

102 lines
3.1 KiB
PHP
Raw Normal View History

<?php
2024-03-09 19:03:09 +03:00
/**
* @package Joomla.Site
* @subpackage com_oauthserver
*
* @copyright (c) 2024. Webmasterskaya. <https://webmasterskaya.xyz>
* @license MIT; see LICENSE.txt
**/
namespace Webmasterskaya\Component\OauthServer\Site\Repository;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use Webmasterskaya\Component\OauthServer\Administrator\Model\AccessTokenModel;
use Webmasterskaya\Component\OauthServer\Administrator\Model\ClientModel;
use Webmasterskaya\Component\OauthServer\Site\Entity\AccessToken;
2024-03-09 19:03:09 +03:00
\defined('_JEXEC') or die;
class AccessTokenRepository implements AccessTokenRepositoryInterface
{
private AccessTokenModel $accessTokenModel;
private ClientModel $clientModel;
/**
2024-03-09 19:03:09 +03:00
* @param \Webmasterskaya\Component\OauthServer\Administrator\Model\AccessTokenModel $accessTokenModel
* @param \Webmasterskaya\Component\OauthServer\Administrator\Model\ClientModel $clientModel
*
* @since version
*/
public function __construct(AccessTokenModel $accessTokenModel, ClientModel $clientModel)
{
$this->accessTokenModel = $accessTokenModel;
2024-03-09 19:03:09 +03:00
$this->clientModel = $clientModel;
}
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null): AccessTokenEntityInterface
{
$accessToken = new AccessToken();
$accessToken->setClient($clientEntity);
$accessToken->setUserIdentifier($userIdentifier);
2024-03-09 19:03:09 +03:00
foreach ($scopes as $scope)
{
$accessToken->addScope($scope);
}
return $accessToken;
}
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity): void
{
$found = false;
2024-03-09 19:03:09 +03:00
try
{
/** @var AccessToken $accessTokenEntity */
$accessToken = $this->accessTokenModel->getItemByIdentifier($accessTokenEntity->getIdentifier());
2024-03-09 19:03:09 +03:00
if ($accessToken->id > 0)
{
$found = true;
}
2024-03-09 19:03:09 +03:00
}
catch (\Throwable $e)
{
}
2024-03-09 19:03:09 +03:00
if ($found)
{
throw UniqueTokenIdentifierConstraintViolationException::create();
}
/** @var AccessToken $accessTokenEntity */
$data = $accessTokenEntity->getData();
$client = $this->clientModel->getItemByIdentifier($accessTokenEntity->getClient()->getIdentifier());
$data['client_id'] = $client->id;
unset($data['client_identifier']);
$this->accessTokenModel->save($data);
}
public function revokeAccessToken($tokenId): void
{
$this->accessTokenModel->revoke($tokenId);
}
public function isAccessTokenRevoked($tokenId): bool
{
$accessToken = $this->accessTokenModel->getItemByIdentifier($tokenId);
2024-03-09 19:03:09 +03:00
if (!$accessToken->id)
{
return true;
}
return !!$accessToken->revoked;
}
2024-03-09 19:03:09 +03:00
}