class JwtAuth in JSON Web Token Authentication (JWT) 8.0
Same name and namespace in other branches
- 8 src/Authentication/Provider/JwtAuth.php \Drupal\jwt\Authentication\Provider\JwtAuth
JWT Authentication Provider.
Hierarchy
- class \Drupal\jwt\Authentication\Provider\JwtAuth implements AuthenticationProviderInterface
Expanded class hierarchy of JwtAuth
1 file declares its use of JwtAuth
- JwtAuthIssuerController.php in modules/
jwt_auth_issuer/ src/ Controller/ JwtAuthIssuerController.php
1 string reference to 'JwtAuth'
1 service uses JwtAuth
File
- src/
Authentication/ Provider/ JwtAuth.php, line 20
Namespace
Drupal\jwt\Authentication\ProviderView source
class JwtAuth implements AuthenticationProviderInterface {
/**
* The JWT Transcoder service.
*
* @var \Drupal\jwt\Transcoder\JwtTranscoderInterface
*/
protected $transcoder;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Constructs a HTTP basic authentication provider object.
*
* @param \Drupal\jwt\Transcoder\JwtTranscoderInterface $transcoder
* The jwt transcoder service.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher service.
*/
public function __construct(JwtTranscoderInterface $transcoder, EventDispatcherInterface $event_dispatcher) {
$this->transcoder = $transcoder;
$this->eventDispatcher = $event_dispatcher;
}
/**
* {@inheritdoc}
*/
public function applies(Request $request) {
$auth = $request->headers
->get('Authorization');
return preg_match('/^Bearer .+/', $auth);
}
/**
* {@inheritdoc}
*/
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;
}
/**
* Generate a new JWT token calling all event handlers.
*
* @return string|bool
* The encoded JWT token. False if there is a problem encoding.
*/
public function generateToken() {
$event = new JwtAuthGenerateEvent(new JsonWebToken());
$this->eventDispatcher
->dispatch(JwtAuthEvents::GENERATE, $event);
$jwt = $event
->getToken();
return $this->transcoder
->encode($jwt);
}
/**
* Gets a raw JsonWebToken from the current request.
*
* @param Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return string|bool
* Raw JWT String if on request, false if not.
*/
protected function getJwtFromRequest(Request $request) {
$auth_header = $request->headers
->get('Authorization');
$matches = [];
if (!($hasJWT = preg_match('/^Bearer (.*)/', $auth_header, $matches))) {
return FALSE;
}
return $matches[1];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
JwtAuth:: |
protected | property | The event dispatcher. | |
JwtAuth:: |
protected | property | The JWT Transcoder service. | |
JwtAuth:: |
public | function |
Checks whether suitable authentication credentials are on the request. Overrides AuthenticationProviderInterface:: |
|
JwtAuth:: |
public | function |
Authenticates the user. Overrides AuthenticationProviderInterface:: |
|
JwtAuth:: |
public | function | Generate a new JWT token calling all event handlers. | |
JwtAuth:: |
protected | function | Gets a raw JsonWebToken from the current request. | |
JwtAuth:: |
public | function | Constructs a HTTP basic authentication provider object. |