You are here

public function JwtAuth::authenticate in JSON Web Token Authentication (JWT) 8.0

Same name and namespace in other branches
  1. 8 src/Authentication/Provider/JwtAuth.php \Drupal\jwt\Authentication\Provider\JwtAuth::authenticate()

Authenticates the user.

Parameters

\Symfony\Component\HttpFoundation\Request|null $request: The request object.

Return value

\Drupal\Core\Session\AccountInterface|null AccountInterface - in case of a successful authentication. NULL - in case where authentication failed.

Overrides AuthenticationProviderInterface::authenticate

File

src/Authentication/Provider/JwtAuth.php, line 63

Class

JwtAuth
JWT Authentication Provider.

Namespace

Drupal\jwt\Authentication\Provider

Code

public function authenticate(Request $request) {
  $raw_jwt = $this
    ->getJwtFromRequest($request);

  // Decode JWT and validate signature.
  try {
    $jwt = $this->transcoder
      ->decode($raw_jwt);
  } catch (JwtDecodeException $e) {
    throw new AccessDeniedHttpException($e
      ->getMessage(), $e);
  }
  $validate = new JwtAuthValidateEvent($jwt);

  // Signature is validated, but allow modules to do additional validation.
  $this->eventDispatcher
    ->dispatch(JwtAuthEvents::VALIDATE, $validate);
  if (!$validate
    ->isValid()) {
    throw new AccessDeniedHttpException($validate
      ->invalidReason());
  }
  $valid = new JwtAuthValidEvent($jwt);
  $this->eventDispatcher
    ->dispatch(JwtAuthEvents::VALID, $valid);
  $user = $valid
    ->getUser();
  if (!$user) {
    throw new AccessDeniedHttpException('Unable to load user from provided JWT.');
  }
  return $user;
}