userProvider = $userProvider; } public function createToken(Request $request, $providerKey) { $data = $request->getContent(); $usuario = json_decode($data); // look for an apikey query parameter //$apiKey = $request->query->get('apikey'); $apiKey = $usuario->password; sprintf("JSON login API Key ". $apiKey); // or if you want to use an "apikey" header, then do something like this: // $apiKey = $request->headers->get('apikey'); if (!$apiKey) { throw new BadCredentialsException('No API key found'); } return new PreAuthenticatedToken( 'anon.', $apiKey, $providerKey ); } public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) { $apiKey = $token->getCredentials(); $username = $this->userProvider->getUsernameForApiKey($apiKey); if (!$username) { throw new AuthenticationException( sprintf('API Key "%s" does not exist.', $apiKey) ); } $user = $this->userProvider->loadUserByUsername($username); return new PreAuthenticatedToken( $user, $apiKey, $providerKey, $user->getRoles() ); } public function supportsToken(TokenInterface $token, $providerKey) { return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey; } public function onAuthenticationFailure(Request $request, AuthenticationException $exception) { return new JsonResponse("Authentication Failed.", 403); } }