'error', 'key' => 'token', 'method' => '', 'data' => '', ]; public function __construct( GeneratorTokenService $generatorTokenService, UserRepository $userRepository, RespondInterface $respond, UserQueryInterface $userQuery ) { $this->generatorTokenService = $generatorTokenService; $this->userRepository = $userRepository; $this->respond = $respond; $this->userQuery = $userQuery; } /** * @param Request $request [description] * @param AuthenticationException $authException [description] * * @return JsonResponse [description] */ public function start(Request $request, AuthenticationException $authException = null): JsonResponse { $this->body['method'] = 'start'; $this->body['data'] = 'Authentication Required !'; return $this->respond->statusUnauthorized()->createBody($this->body)->respond(); } /** * @param Request $request [description] * * @return bool [description] */ public function supports(Request $request): bool { return $request->headers->has('x-auth-token'); } /** * @param Request $request [description] * * @return array [description] */ public function getCredentials(Request $request): array { if (!$token = $request->headers->get('x-auth-token')) { // No token? return [ 'token' => null, ]; } try { return [ 'token' => SimpleJWS::load($token), ]; } catch (Exception $exception) { return [ 'token' => null, ]; } } /** * @param [type] $credentials [description] * @param UserProviderInterface $userProvider [description] * * @throws NonUniqueResultException * * @return UserInterface|null [description] */ public function getUser($credentials, UserProviderInterface $userProvider): ?UserInterface { if (null === $credentials['token']) { return null; } $payload = $credentials['token']->getPayload(); if (!isset($payload['sub']) || !$payload['sub']) { return null; } if ($payload['exp'] < time()) { return null; } $user = $this->userRepository->loadUserFromAdmin($payload['sub']); $groups = $this->userQuery->getGroups($credentials['token']->getTokenString()); $user->setGroups($groups); return $user; } /** * @param [type] $credentials [description] * @param UserInterface $user [description] * * @return bool [description] */ public function checkCredentials($credentials, UserInterface $user): bool { $publicKey = $this->generatorTokenService->checkToken(); return $credentials['token']->isValid($publicKey, 'RS256'); } /** * @param Request $request [description] * @param AuthenticationException $exception [description] * * @return JsonResponse [description] */ public function onAuthenticationFailure(Request $request, AuthenticationException $exception): JsonResponse { $this->body['method'] = 'onAuthenticationFailure'; $this->body['data'] = 'Not authorized !'; return $this->respond->statusUnauthorized()->createBody($this->body)->respond(); } /** * @param Request $request [description] * @param TokenInterface $token [description] * @param [type] $providerKey [description] * * @return [type] [description] */ public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) { return null; } /** * @return bool [description] */ public function supportsRememberMe(): bool { return false; } }