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

86 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace Webmasterskaya\Component\OauthServer\Site\Repository;
2024-03-06 12:52:30 +03:00
use Joomla\CMS\Object\CMSObject;
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;
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-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);
if (empty($item->id)) {
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);
if (empty($item->id)) {
return false;
}
if (!$item->active) {
return false;
}
if (!$this->isGrantSupported($item, $grantType)) {
return false;
}
if (!!$item->public || hash_equals((string)$item->secret, (string)$clientSecret)) {
return true;
}
return false;
}
private function buildClientEntity(\stdClass|CMSObject $client): Client
{
$clientEntity = new Client();
$clientEntity->setName($client->name);
$clientEntity->setIdentifier($client->identifier);
$clientEntity->setRedirectUri(array_map('strval', (array)$client->redirect_uris));
$clientEntity->setConfidential(!$client->public);
$clientEntity->setAllowPlainTextPkce((bool)$client->allow_plain_text_pkce);
return $clientEntity;
}
private function isGrantSupported(\stdClass|CMSObject $client, ?string $grant): bool
{
if (null === $grant) {
return true;
}
$grants = array_map('strval', (array)$client->grants);
if (empty($grants)) {
return true;
}
return \in_array($grant, $grants);
}
}