You are here

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

Same name and namespace in other branches
  1. 8.0 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 61

Class

JwtAuth
JWT Authentication Provider.

Namespace

Drupal\jwt\Authentication\Provider

Code

public function authenticate(Request $request) {
  $raw_jwt = self::getJwtFromRequest($request);

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

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