refonte
This commit is contained in:
35
src/Security/ApiTokenHandler.php
Normal file
35
src/Security/ApiTokenHandler.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user