You are here

trait RevocableTokenRepositoryTrait in Simple OAuth (OAuth2) & OpenID Connect 8.3

Same name and namespace in other branches
  1. 8.4 src/Repositories/RevocableTokenRepositoryTrait.php \Drupal\simple_oauth\Repositories\RevocableTokenRepositoryTrait
  2. 8.2 src/Repositories/RevocableTokenRepositoryTrait.php \Drupal\simple_oauth\Repositories\RevocableTokenRepositoryTrait
  3. 5.x src/Repositories/RevocableTokenRepositoryTrait.php \Drupal\simple_oauth\Repositories\RevocableTokenRepositoryTrait

Common methods for token repositories on different grants.

Hierarchy

1 file declares its use of RevocableTokenRepositoryTrait
AuthCodeRepository.php in simple_oauth_extras/src/Repositories/AuthCodeRepository.php

File

src/Repositories/RevocableTokenRepositoryTrait.php, line 12

Namespace

Drupal\simple_oauth\Repositories
View source
trait RevocableTokenRepositoryTrait {

  /**
   * The entity type ID.
   *
   * @var string
   */
  protected static $entity_type_id = 'oauth2_token';

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The serializer.
   *
   * @var \Symfony\Component\Serializer\SerializerInterface
   */
  protected $serializer;

  /**
   * Construct a revocable token.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Symfony\Component\Serializer\SerializerInterface $serializer
   *   The normalizer for tokens.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, SerializerInterface $serializer) {
    $this->entityTypeManager = $entity_type_manager;
    $this->serializer = $serializer;
  }

  /**
   * {@inheritdoc}
   */
  public function persistNew($token_entity) {
    if (!is_a($token_entity, static::$entity_interface)) {
      throw new \InvalidArgumentException(sprintf('%s does not implement %s.', get_class($token_entity), static::$entity_interface));
    }
    $values = $this->serializer
      ->normalize($token_entity);
    $values['bundle'] = static::$bundle_id;
    $new_token = $this->entityTypeManager
      ->getStorage(static::$entity_type_id)
      ->create($values);
    if ($token_entity instanceof RefreshTokenEntityInterface) {
      $access_token = $token_entity
        ->getAccessToken();
      if (!empty($access_token
        ->getUserIdentifier())) {
        $new_token
          ->set('auth_user_id', $access_token
          ->getUserIdentifier());
      }
    }
    $new_token
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function revoke($token_id) {
    if (!($tokens = $this->entityTypeManager
      ->getStorage(static::$entity_type_id)
      ->loadByProperties([
      'value' => $token_id,
    ]))) {
      return;
    }

    /** @var \Drupal\simple_oauth\Entity\Oauth2TokenInterface $token */
    $token = reset($tokens);
    $token
      ->revoke();
    $token
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function isRevoked($token_id) {
    if (!($tokens = $this->entityTypeManager
      ->getStorage(static::$entity_type_id)
      ->loadByProperties([
      'value' => $token_id,
    ]))) {
      return TRUE;
    }

    /** @var \Drupal\simple_oauth\Entity\Oauth2TokenInterface $token */
    $token = reset($tokens);
    return $token
      ->isRevoked();
  }

  /**
   * {@inheritdoc}
   */
  public function getNew() {
    $class = static::$entity_class;
    return new $class();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RevocableTokenRepositoryTrait::$entityTypeManager protected property The entity type manager.
RevocableTokenRepositoryTrait::$entity_type_id protected static property The entity type ID.
RevocableTokenRepositoryTrait::$serializer protected property The serializer.
RevocableTokenRepositoryTrait::getNew public function
RevocableTokenRepositoryTrait::isRevoked public function
RevocableTokenRepositoryTrait::persistNew public function
RevocableTokenRepositoryTrait::revoke public function
RevocableTokenRepositoryTrait::__construct public function Construct a revocable token.