/** * Attempts to authenticate a TokenInterface object. * * @param TokenInterface $token The TokenInterface instance to authenticate * * @return TokenInterface An authenticated TokenInterface instance, never null * * @throws AuthenticationException if the authentication fails */ public function authenticate(TokenInterface $token) { if (!$this->supports($token)) { return null; } $user = $token->getUser(); /** @var ApiToken $token */ if ($this->key !== $token->getKey()) { throw new BadCredentialsException('The presented key does not match.'); } $this->userChecker->checkPostAuth($user); $authenticatedToken = new ApiToken($user->getRoles(), $this->providerId, $this->key); $authenticatedToken->setUser($user); $authenticatedToken->setAttributes($token->getAttributes()); $authenticatedToken->setAuthenticated(true); return $authenticatedToken; }