RevocableTokenRepositoryTrait.php in Simple OAuth (OAuth2) & OpenID Connect 8.2
File
src/Repositories/RevocableTokenRepositoryTrait.php
View source
<?php
namespace Drupal\simple_oauth\Repositories;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\Serializer\Serializer;
trait RevocableTokenRepositoryTrait {
protected static $entity_type_id = 'oauth2_token';
protected $entityTypeManager;
protected $serializer;
public function __construct(EntityTypeManagerInterface $entity_type_manager, Serializer $serializer) {
$this->entityTypeManager = $entity_type_manager;
$this->serializer = $serializer;
}
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);
$new_token
->save();
}
public function revoke($token_id) {
if (!($tokens = $this->entityTypeManager
->getStorage(static::$entity_type_id)
->loadByProperties([
'value' => $token_id,
]))) {
return;
}
$token = reset($tokens);
$token
->revoke();
$token
->save();
}
public function isRevoked($token_id) {
if (!($tokens = $this->entityTypeManager
->getStorage(static::$entity_type_id)
->loadByProperties([
'value' => $token_id,
]))) {
return TRUE;
}
$token = reset($tokens);
return $token
->isRevoked();
}
public function getNew() {
$class = static::$entity_class;
return new $class();
}
}