TokenEntityNormalizer.php in Simple OAuth (OAuth2) & OpenID Connect 8.4
File
src/Normalizer/TokenEntityNormalizer.php
View source
<?php
namespace Drupal\simple_oauth\Normalizer;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\serialization\Normalizer\NormalizerBase;
class TokenEntityNormalizer extends NormalizerBase implements TokenEntityNormalizerInterface {
protected $supportedInterfaceOrClass = '\\League\\OAuth2\\Server\\Entities\\TokenInterface';
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public function normalize($token_entity, $format = NULL, array $context = []) {
$scopes = array_map(function ($scope_entity) {
return [
'target_id' => $scope_entity
->getIdentifier(),
];
}, $token_entity
->getScopes());
$client = $token_entity
->getClient();
$client_drupal_entity = $client
->getDrupalEntity();
return [
'auth_user_id' => [
'target_id' => $token_entity
->getUserIdentifier(),
],
'client' => [
'target_id' => $client_drupal_entity
->id(),
],
'scopes' => $scopes,
'value' => $token_entity
->getIdentifier(),
'expire' => $token_entity
->getExpiryDateTime()
->format('U'),
'status' => TRUE,
];
}
}