2024-03-04 03:18:17 +03:00
|
|
|
<?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
|
|
|
|
**/
|
2024-03-04 03:18:17 +03:00
|
|
|
|
|
|
|
namespace Webmasterskaya\Component\OauthServer\Site\Repository;
|
|
|
|
|
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
|
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface;
|
2024-03-06 15:16:14 +03:00
|
|
|
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
|
2024-03-04 03:18:17 +03:00
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
2024-03-06 15:16:14 +03:00
|
|
|
use Webmasterskaya\Component\OauthServer\Administrator\Model\AccessTokenModel;
|
|
|
|
use Webmasterskaya\Component\OauthServer\Administrator\Model\ClientModel;
|
|
|
|
use Webmasterskaya\Component\OauthServer\Site\Entity\AccessToken;
|
2024-03-04 03:18:17 +03:00
|
|
|
|
2024-03-09 19:03:09 +03:00
|
|
|
\defined('_JEXEC') or die;
|
|
|
|
|
2024-03-04 03:18:17 +03:00
|
|
|
class AccessTokenRepository implements AccessTokenRepositoryInterface
|
|
|
|
{
|
2024-03-06 15:16:14 +03:00
|
|
|
private AccessTokenModel $accessTokenModel;
|
|
|
|
private ClientModel $clientModel;
|
2024-03-04 03:18:17 +03:00
|
|
|
|
2024-03-06 15:16:14 +03:00
|
|
|
/**
|
2024-03-09 19:03:09 +03:00
|
|
|
* @param \Webmasterskaya\Component\OauthServer\Administrator\Model\AccessTokenModel $accessTokenModel
|
|
|
|
* @param \Webmasterskaya\Component\OauthServer\Administrator\Model\ClientModel $clientModel
|
2024-03-06 15:16:14 +03:00
|
|
|
*
|
|
|
|
* @since version
|
|
|
|
*/
|
|
|
|
public function __construct(AccessTokenModel $accessTokenModel, ClientModel $clientModel)
|
|
|
|
{
|
|
|
|
$this->accessTokenModel = $accessTokenModel;
|
2024-03-09 19:03:09 +03:00
|
|
|
$this->clientModel = $clientModel;
|
2024-03-06 15:16:14 +03:00
|
|
|
}
|
2024-03-04 03:18:17 +03:00
|
|
|
|
2024-03-06 15:16:14 +03:00
|
|
|
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null): AccessTokenEntityInterface
|
2024-03-04 03:18:17 +03:00
|
|
|
{
|
2024-03-06 15:16:14 +03:00
|
|
|
$accessToken = new AccessToken();
|
|
|
|
$accessToken->setClient($clientEntity);
|
|
|
|
$accessToken->setUserIdentifier($userIdentifier);
|
|
|
|
|
2024-03-09 19:03:09 +03:00
|
|
|
foreach ($scopes as $scope)
|
|
|
|
{
|
2024-03-06 15:16:14 +03:00
|
|
|
$accessToken->addScope($scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $accessToken;
|
2024-03-04 03:18:17 +03:00
|
|
|
}
|
|
|
|
|
2024-03-06 15:16:14 +03:00
|
|
|
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity): void
|
2024-03-04 03:18:17 +03:00
|
|
|
{
|
2024-03-08 02:19:04 +03:00
|
|
|
$found = false;
|
2024-03-09 19:03:09 +03:00
|
|
|
try
|
|
|
|
{
|
2024-03-08 02:19:04 +03:00
|
|
|
/** @var AccessToken $accessTokenEntity */
|
|
|
|
$accessToken = $this->accessTokenModel->getItemByIdentifier($accessTokenEntity->getIdentifier());
|
2024-03-09 19:03:09 +03:00
|
|
|
if ($accessToken->id > 0)
|
|
|
|
{
|
2024-03-08 02:19:04 +03:00
|
|
|
$found = true;
|
|
|
|
}
|
2024-03-09 19:03:09 +03:00
|
|
|
}
|
|
|
|
catch (\Throwable $e)
|
|
|
|
{
|
2024-03-08 02:19:04 +03:00
|
|
|
}
|
2024-03-06 15:16:14 +03:00
|
|
|
|
2024-03-09 19:03:09 +03:00
|
|
|
if ($found)
|
|
|
|
{
|
2024-03-06 15:16:14 +03:00
|
|
|
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);
|
2024-03-04 03:18:17 +03:00
|
|
|
}
|
|
|
|
|
2024-03-06 15:16:14 +03:00
|
|
|
public function revokeAccessToken($tokenId): void
|
2024-03-04 03:18:17 +03:00
|
|
|
{
|
2024-03-06 15:16:14 +03:00
|
|
|
$this->accessTokenModel->revoke($tokenId);
|
2024-03-04 03:18:17 +03:00
|
|
|
}
|
|
|
|
|
2024-03-06 15:16:14 +03:00
|
|
|
public function isAccessTokenRevoked($tokenId): bool
|
2024-03-04 03:18:17 +03:00
|
|
|
{
|
2024-03-06 15:16:14 +03:00
|
|
|
$accessToken = $this->accessTokenModel->getItemByIdentifier($tokenId);
|
|
|
|
|
2024-03-09 19:03:09 +03:00
|
|
|
if (!$accessToken->id)
|
|
|
|
{
|
2024-03-06 15:16:14 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!$accessToken->revoked;
|
2024-03-04 03:18:17 +03:00
|
|
|
}
|
2024-03-09 19:03:09 +03:00
|
|
|
}
|