2024-03-04 03:18:17 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Webmasterskaya\Component\OauthServer\Site\Repository;
|
|
|
|
|
2024-03-06 12:52:30 +03:00
|
|
|
use Joomla\CMS\Object\CMSObject;
|
2024-03-07 23:38:11 +03:00
|
|
|
use Joomla\Utilities\ArrayHelper;
|
2024-03-04 03:18:17 +03:00
|
|
|
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-04 03:18:17 +03:00
|
|
|
|
|
|
|
class ClientRepository implements ClientRepositoryInterface
|
|
|
|
{
|
2024-03-06 12:52:30 +03:00
|
|
|
private ClientModel $clientModel;
|
2024-03-04 03:18:17 +03:00
|
|
|
|
2024-03-06 12:52:30 +03:00
|
|
|
public function __construct(ClientModel $clientModel)
|
2024-03-04 03:18:17 +03:00
|
|
|
{
|
2024-03-06 12:52:30 +03:00
|
|
|
$this->clientModel = $clientModel;
|
2024-03-04 03:18:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $clientIdentifier
|
2024-03-06 12:52:30 +03:00
|
|
|
* @return \League\OAuth2\Server\Entities\ClientEntityInterface|null
|
2024-03-04 03:18:17 +03:00
|
|
|
* @throws \Exception
|
|
|
|
* @since version
|
|
|
|
*/
|
2024-03-06 12:52:30 +03:00
|
|
|
public function getClientEntity($clientIdentifier): ?ClientEntityInterface
|
2024-03-04 03:18:17 +03:00
|
|
|
{
|
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-04 03:18:17 +03:00
|
|
|
}
|
|
|
|
|
2024-03-06 12:52:30 +03:00
|
|
|
public function validateClient($clientIdentifier, $clientSecret, $grantType): bool
|
2024-03-04 03:18:17 +03:00
|
|
|
{
|
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);
|
2024-03-07 23:38:11 +03:00
|
|
|
$clientEntity->setRedirectUri(ArrayHelper::getColumn((array)$client->redirect_uris, 'uri'));
|
2024-03-06 12:52:30 +03:00
|
|
|
$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);
|
2024-03-04 03:18:17 +03:00
|
|
|
}
|
|
|
|
}
|