You are here

class JwtAuthConsumerSubscriber in JSON Web Token Authentication (JWT) 8.0

Same name and namespace in other branches
  1. 8 modules/jwt_auth_consumer/src/EventSubscriber/JwtAuthConsumerSubscriber.php \Drupal\jwt_auth_consumer\EventSubscriber\JwtAuthConsumerSubscriber

Class JwtAuthConsumerSubscriber.

@package Drupal\jwt_auth_consumer

Hierarchy

  • class \Drupal\jwt_auth_consumer\EventSubscriber\JwtAuthConsumerSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of JwtAuthConsumerSubscriber

1 string reference to 'JwtAuthConsumerSubscriber'
jwt_auth_consumer.services.yml in modules/jwt_auth_consumer/jwt_auth_consumer.services.yml
modules/jwt_auth_consumer/jwt_auth_consumer.services.yml
1 service uses JwtAuthConsumerSubscriber
jwt_auth_consumer.subscriber in modules/jwt_auth_consumer/jwt_auth_consumer.services.yml
Drupal\jwt_auth_consumer\EventSubscriber\JwtAuthConsumerSubscriber

File

modules/jwt_auth_consumer/src/EventSubscriber/JwtAuthConsumerSubscriber.php, line 16

Namespace

Drupal\jwt_auth_consumer\EventSubscriber
View source
class JwtAuthConsumerSubscriber implements EventSubscriberInterface {

  /**
   * A User Interface.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
   */
  public function __construct(EntityManagerInterface $entity_manager) {
    $this->entityManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[JwtAuthEvents::VALIDATE][] = [
      'validate',
    ];
    $events[JwtAuthEvents::VALID][] = [
      'loadUser',
    ];
    return $events;
  }

  /**
   * Validates that a uid is present in the JWT.
   *
   * This validates the format of the JWT and validate the uid is a
   * valid uid in the system.
   *
   * @param \Drupal\jwt\Authentication\Event\JwtAuthValidateEvent $event
   *   A JwtAuth event.
   */
  public function validate(JwtAuthValidateEvent $event) {
    $token = $event
      ->getToken();
    $uid = $token
      ->getClaim([
      'drupal',
      'uid',
    ]);
    if ($uid === NULL) {
      $event
        ->invalidate("No Drupal uid was provided in the JWT payload.");
    }
    $user = $this->entityManager
      ->getStorage('user')
      ->load($uid);
    if ($user === NULL) {
      $event
        ->invalidate("No UID exists.");
    }
  }

  /**
   * Load and set a Drupal user to be authentication based on the JWT's uid.
   *
   * @param \Drupal\jwt\Authentication\Event\JwtAuthValidEvent $event
   *   A JwtAuth event.
   */
  public function loadUser(JwtAuthValidEvent $event) {
    $token = $event
      ->getToken();
    $user_storage = $this->entityManager
      ->getStorage('user');
    $uid = $token
      ->getClaim([
      'drupal',
      'uid',
    ]);
    $user = $user_storage
      ->load($uid);
    $event
      ->setUser($user);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JwtAuthConsumerSubscriber::$entityManager protected property A User Interface.
JwtAuthConsumerSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
JwtAuthConsumerSubscriber::loadUser public function Load and set a Drupal user to be authentication based on the JWT's uid.
JwtAuthConsumerSubscriber::validate public function Validates that a uid is present in the JWT.
JwtAuthConsumerSubscriber::__construct public function Constructor.