src/Security/Provider/ApiKeyUserProvider.php
<?php
declare(strict_types = 1);
/**
* /src/Security/Provider/ApiKeyUserProvider.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
namespace App\Security\Provider;
use App\Entity\ApiKey;
use App\Repository\ApiKeyRepository;
use App\Security\ApiKeyUser;
use App\Security\Interfaces\ApiKeyUserProviderInterface;
use App\Security\RolesService;
use Override;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
/**
* @package App\Security\Provider
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*
* @template-implements UserProviderInterface<ApiKeyUser>
*/
class ApiKeyUserProvider implements ApiKeyUserProviderInterface, UserProviderInterface
{
public function __construct(
private readonly ApiKeyRepository $apiKeyRepository,
private readonly RolesService $rolesService,
) {
}
#[Override]
public function supportsClass(string $class): bool
{
return $class === ApiKeyUser::class;
}
#[Override]
public function loadUserByIdentifier(string $identifier): ApiKeyUser
{
$apiKey = $this->getApiKeyForToken($identifier);
if ($apiKey === null) {
throw new UserNotFoundException('API key is not valid');
}
return new ApiKeyUser($apiKey, $this->rolesService->getInheritedRoles($apiKey->getRoles()));
}
#[Override]
public function refreshUser(UserInterface $user): UserInterface
{
throw new UnsupportedUserException('API key cannot refresh user');
}
#[Override]
public function getApiKeyForToken(string $token): ?ApiKey
{
return $this->apiKeyRepository->findOneBy([
'token' => $token,
]);
}
}