Files
Mangarr/src/Security/ApiTokenHandler.php
ext.jeremy.guillot@maxicoffee.domains c55cd62ec7 fix: phpcs-fixer
2025-02-05 21:32:04 +01:00

35 lines
1.1 KiB
PHP

<?php
namespace App\Security;
use App\Repository\ApiTokenRepository;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
readonly class ApiTokenHandler implements AccessTokenHandlerInterface
{
public function __construct(private ApiTokenRepository $apiTokenRepository)
{
}
public function getUserBadgeFrom(#[\SensitiveParameter] string $accessToken): UserBadge
{
$token = $this->apiTokenRepository->findOneBy(['token' => $accessToken]);
if(!$token) {
throw new BadCredentialsException();
}
if(!$token->isValid()) {
throw new CustomUserMessageAuthenticationException('Token expired');
}
$token->getOwnedBy()->markAsTokenAuthenticated($token->getScopes());
return new UserBadge($token->getOwnedBy()->getUserIdentifier());
}
}