I'm playing around with AbstractGuardAuthenticator
from the relatively new Guard
subsystem added in Symfony 2.8.
My setup is really simple. I send a request to a protected URL which takes a username:password base64 encoded. It checks both against the database and should return a token.
The authentication successful method:
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
//If login successful, return token
return new Response($this->tokenStorage->getToken());
}
What it returns is:
PostAuthenticationGuardToken(user="test", authenticated=true, roles="ROLE_ADVANCED,
ROLE_USER")
Now, this is what I'd expect given that the AbstractGuardAuthenticator
defines the method for creating this token exactly like this.
public function createAuthenticatedToken(UserInterface $user, $providerKey)
{
return new PostAuthenticationGuardToken(
$user,
$providerKey,
$user->getRoles()
);
}
UPDATE 1.1:
Using the LexikJWTAuthenticationBundle
I am now attempting to implement Json Web Tokens into my application's AbstractGuardAuthenticator. The Lexik bundle provides both a success and failure handler: lexik_jwt_authentication.handler.authentication_success
& lexik_jwt_authentication.handler.authentication_failure
which point at classes that get certain JWT variables injected into them. How do I hook them into AbstractGuardAuthenticator
's success and failure handlers?
crud:
anonymous: ~
guard:
authenticators:
- app.token_authenticator
pattern: ^/database/
And the Guard
success and failure methods
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
if ($token = $request->headers->get('X-AUTH-TOKEN')) {
//on success, let the request continue
} else {
//If login successful, return token
return new Response($this->tokenStorage->getToken());
}
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$data = array(
'message' => strtr($exception->getMessageKey(), $exception->getMessageData())
// or to translate this message
// $this->translator->trans($exception->getMessageKey(), $exception->getMessageData())
);
return new JsonResponse($data, 403);
}
I am currently extending and merging JWTTokenAuthenticator
with my own token authenticator instead of the AbstractGuardAuthenticator
as both implement GuardAuthenticatorInterface
.