You are here

class UserClaimsNormalizer in Simple OAuth (OAuth2) & OpenID Connect 5.x

Normalizes a user entity to extract the claims.

Hierarchy

  • class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
    • class \Drupal\simple_oauth\Normalizer\UserClaimsNormalizer implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface

Expanded class hierarchy of UserClaimsNormalizer

1 string reference to 'UserClaimsNormalizer'
simple_oauth.services.yml in ./simple_oauth.services.yml
simple_oauth.services.yml
1 service uses UserClaimsNormalizer
simple_oauth.openid_connect.user_claims_normalizer in ./simple_oauth.services.yml
Drupal\simple_oauth\Normalizer\UserClaimsNormalizer

File

src/Normalizer/UserClaimsNormalizer.php, line 16

Namespace

Drupal\simple_oauth\Normalizer
View source
class UserClaimsNormalizer extends NormalizerBase implements NormalizerInterface {
  protected $supportedInterfaceOrClass = UserEntityWithClaims::class;
  protected $format = 'json';

  /**
   * The user storage.
   *
   * @var \Drupal\user\UserStorageInterface
   */
  protected $userStorage;

  /**
   * The claims.
   *
   * @var string[]
   */
  protected $claims;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * UserClaimsNormalizer constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param string[] $claims
   *   The list of claims being selected.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, array $claims, ModuleHandlerInterface $module_handler) {
    $this->userStorage = $entity_type_manager
      ->getStorage('user');
    $this->claims = $claims;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($user_entity, $format = NULL, array $context = []) {
    assert($user_entity instanceof UserEntityWithClaims);
    $identifier = $user_entity
      ->getIdentifier();

    // Check if the account is in $context. If not, load it from the database.
    $account = $context[$identifier] instanceof AccountInterface ? $context[$identifier] : $this->userStorage
      ->load($identifier);
    assert($account instanceof AccountInterface);
    return $this
      ->getClaimsFromAccount($account);
  }

  /**
   * Gets the claims for a given user.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The user account.
   *
   * @return array
   *   The claims key/values.
   */
  private function getClaimsFromAccount(AccountInterface $account) {
    $profile_url = $account
      ->toUrl('canonical', [
      'absolute' => TRUE,
    ])
      ->toString();
    $claim_values = [
      'sub' => $account
        ->id(),
      'name' => $account
        ->getDisplayName(),
      'preferred_username' => $account
        ->getAccountName(),
      'email' => $account
        ->getEmail(),
      'email_verified' => TRUE,
      'profile' => $profile_url,
      'locale' => $account
        ->getPreferredLangcode(),
      'zoneinfo' => $account
        ->getTimeZone(),
    ];
    if ($account instanceof EntityChangedInterface) {
      $claim_values['updated_at'] = $account
        ->getChangedTime();
    }
    $context = [
      'account' => $account,
      'claims' => $this->claims,
    ];
    $this->moduleHandler
      ->alter('simple_oauth_oidc_claims', $claim_values, $context);
    return array_intersect_key($claim_values, array_flip($this->claims));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 1
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function 1
UserClaimsNormalizer::$claims protected property The claims.
UserClaimsNormalizer::$format protected property List of formats which supports (de-)normalization. Overrides NormalizerBase::$format
UserClaimsNormalizer::$moduleHandler protected property The module handler.
UserClaimsNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass
UserClaimsNormalizer::$userStorage protected property The user storage.
UserClaimsNormalizer::getClaimsFromAccount private function Gets the claims for a given user.
UserClaimsNormalizer::normalize public function
UserClaimsNormalizer::__construct public function UserClaimsNormalizer constructor.