headers->get('X-AUTH-TOKEN'); // no token? Don't do anything :D if (!$token) { return; } return [ 'token' => $token, ]; } public function getUser($credentials, UserProviderInterface $userProvider) { $token = $credentials['token']; // this authenticator is configured to always receive our UserProvider /** @var UserProvider $userProvider */ $user = $userProvider->loadUserByToken($token); // we could just return null, but this allows us to control the message a bit more if (!$user) { throw new AuthenticationCredentialsNotFoundException(); } return $user; } public function checkCredentials($credentials, UserInterface $user) { // we're saying that the token is *always* ok } public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) { // on success, just let the request keep going! return null; } public function onAuthenticationFailure(Request $request, AuthenticationException $exception) { $data = array( // you might translate this message 'message' => $exception->getMessageKey() ); return new Response(json_encode($data), 403); } /** * Called when authentication is needed, but it's not sent */ public function start(Request $request, AuthenticationException $authException = null) { $data = array( // you might translate this message 'message' => 'Authentication Required' ); return new Response(json_encode($data), 401); } public function supportsRememberMe() { return false; } }