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

105 lines
2.7 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;
2024-03-06 12:52:30 +03:00
use Joomla\CMS\Object\CMSObject;
use Joomla\Utilities\ArrayHelper;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
2024-03-06 12:52:30 +03:00
use Webmasterskaya\Component\OauthServer\Administrator\Model\ClientModel;
use Webmasterskaya\Component\OauthServer\Site\Entity\Client;
2024-03-09 19:03:09 +03:00
\defined('_JEXEC') or die;
class ClientRepository implements ClientRepositoryInterface
{
2024-03-06 12:52:30 +03:00
private ClientModel $clientModel;
2024-03-06 12:52:30 +03:00
public function __construct(ClientModel $clientModel)
{
2024-03-06 12:52:30 +03:00
$this->clientModel = $clientModel;
}
/**
* @param $clientIdentifier
2024-03-09 19:03:09 +03:00
*
2024-03-06 12:52:30 +03:00
* @return \League\OAuth2\Server\Entities\ClientEntityInterface|null
* @throws \Exception
* @since version
*/
2024-03-06 12:52:30 +03:00
public function getClientEntity($clientIdentifier): ?ClientEntityInterface
{
2024-03-06 12:52:30 +03:00
$item = $this->clientModel->getItemByIdentifier($clientIdentifier);
2024-03-09 19:03:09 +03:00
if (empty($item->id))
{
2024-03-06 12:52:30 +03:00
return null;
}
return $this->buildClientEntity($item);
}
2024-03-06 12:52:30 +03:00
public function validateClient($clientIdentifier, $clientSecret, $grantType): bool
{
2024-03-06 12:52:30 +03:00
$item = $this->clientModel->getItemByIdentifier($clientIdentifier);
2024-03-09 19:03:09 +03:00
if (empty($item->id))
{
2024-03-06 12:52:30 +03:00
return false;
}
2024-03-09 19:03:09 +03:00
if (!$item->active)
{
2024-03-06 12:52:30 +03:00
return false;
}
2024-03-09 19:03:09 +03:00
if (!$this->isGrantSupported($item, $grantType))
{
2024-03-06 12:52:30 +03:00
return false;
}
2024-03-09 19:03:09 +03:00
if (!!$item->public || hash_equals((string) $item->secret, (string) $clientSecret))
{
2024-03-06 12:52:30 +03:00
return true;
}
return false;
}
private function buildClientEntity(\stdClass|CMSObject $client): Client
{
$clientEntity = new Client();
$clientEntity->setName($client->name);
$clientEntity->setIdentifier($client->identifier);
2024-03-09 19:03:09 +03:00
$clientEntity->setRedirectUri(ArrayHelper::getColumn((array) $client->redirect_uris, 'uri'));
2024-03-06 12:52:30 +03:00
$clientEntity->setConfidential(!$client->public);
2024-03-09 19:03:09 +03:00
$clientEntity->setAllowPlainTextPkce((bool) $client->allow_plain_text_pkce);
2024-03-06 12:52:30 +03:00
return $clientEntity;
}
private function isGrantSupported(\stdClass|CMSObject $client, ?string $grant): bool
{
2024-03-09 19:03:09 +03:00
if (null === $grant)
{
2024-03-06 12:52:30 +03:00
return true;
}
2024-03-09 19:03:09 +03:00
$grants = array_map('strval', (array) $client->grants);
2024-03-06 12:52:30 +03:00
2024-03-09 19:03:09 +03:00
if (empty($grants))
{
2024-03-06 12:52:30 +03:00
return true;
}
return \in_array($grant, $grants);
}
2024-03-09 19:03:09 +03:00
}