AuthenticatedApiToken implemented for per-request ApiToken::$key value storage.

This commit is contained in:
Alexey Skobkin 2018-06-27 02:02:32 +03:00
parent 5136a20241
commit 61405ead60
2 changed files with 27 additions and 1 deletions

View file

@ -3,6 +3,7 @@
namespace App\Security;
use App\Api\V1\DTO\ApiResponse;
use App\Security\Token\AuthenticatedApiToken;
use Symfony\Component\HttpFoundation\{JsonResponse, Request};
use Symfony\Component\Security\Core\Authentication\Token\{PreAuthenticatedToken, TokenInterface};
use Symfony\Component\Security\Core\Exception\{AuthenticationException, BadCredentialsException, CustomUserMessageAuthenticationException};
@ -59,7 +60,7 @@ class ApiTokenAuthenticator implements SimplePreAuthenticatorInterface, Authenti
));
}
return new PreAuthenticatedToken(
return new AuthenticatedApiToken(
$user,
$apiTokenKey,
$providerKey,

View file

@ -0,0 +1,25 @@
<?php
namespace App\Security\Token;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
/** This token stores ApiToken key even after eraseCredentials() called */
class AuthenticatedApiToken extends PreAuthenticatedToken
{
/** @var string|null This token is stored only for this request and will not be erased by eraseCredentials() or serialized */
private $tokenKey;
public function __construct(User $user, string $credentials, string $providerKey, array $roles = [])
{
parent::__construct($user, $credentials, $providerKey, $roles);
// @todo probably separate constructor argument needed
$this->tokenKey = $credentials;
}
public function getTokenKey(): ?string
{
return $this->tokenKey;
}
}