You are here

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

OpenID Connect scope repository decorator.

Hierarchy

Expanded class hierarchy of OpenIdConnectScopeRepository

File

src/OpenIdConnect/OpenIdConnectScopeRepository.php, line 14

Namespace

Drupal\simple_oauth\OpenIdConnect
View source
class OpenIdConnectScopeRepository implements ScopeRepositoryInterface {
  use StringTranslationTrait;

  /**
   * The inner scope repository.
   *
   * @var \League\OAuth2\Server\Repositories\ScopeRepositoryInterface
   */
  protected $innerScopeRepository;

  /**
   * OpenIdConnectScopeRepository constructor.
   *
   * @param \League\OAuth2\Server\Repositories\ScopeRepositoryInterface $inner_scope_repository
   *   The inner scope repository.
   */
  public function __construct(ScopeRepositoryInterface $inner_scope_repository) {
    $this->innerScopeRepository = $inner_scope_repository;
  }

  /**
   * {@inheritdoc}
   */
  public function getScopeEntityByIdentifier($identifier) {

    // First check if this scope exists as a role.
    $role_scope = $this->innerScopeRepository
      ->getScopeEntityByIdentifier($identifier);
    if ($role_scope) {
      return $role_scope;
    }

    // Fall back to a fixed list of OpenID scopes.
    $openid_scopes = $this
      ->getOpenIdScopes();
    if (isset($openid_scopes[$identifier])) {
      return new OpenIdConnectScopeEntity($identifier, $openid_scopes[$identifier]);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function finalizeScopes(array $scopes, $grantType, ClientEntityInterface $clientEntity, $userIdentifier = NULL) {
    $finalized_scopes = $this->innerScopeRepository
      ->finalizeScopes($scopes, $grantType, $clientEntity, $userIdentifier);

    // Make sure that the openid scopes are in the user list.
    $openid_scopes = $this
      ->getOpenIdScopes();
    foreach ($scopes as $scope) {
      if (isset($openid_scopes[$scope
        ->getIdentifier()])) {
        $finalized_scopes = $this
          ->addRoleToScopes($finalized_scopes, new OpenIdConnectScopeEntity($scope
          ->getIdentifier(), $openid_scopes[$scope
          ->getIdentifier()]));
      }
    }
    return $finalized_scopes;
  }

  /**
   * Returns fixed OpenID Connect scopes.
   *
   * @return array
   *   A list of scope names keyed by their identifier.
   */
  protected function getOpenIdScopes() {
    $openid_scopes = [
      'openid' => $this
        ->t('User information'),
      'profile' => $this
        ->t('Profile information'),
      'email' => $this
        ->t('E-Mail'),
      'phone' => $this
        ->t('Phone'),
      'address' => $this
        ->t('Address'),
    ];
    return $openid_scopes;
  }

  /**
   * Add an additional scope if it's not present.
   *
   * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes
   *   The list of scopes.
   * @param \League\OAuth2\Server\Entities\ScopeEntityInterface $new_scope
   *   The additional scope.
   *
   * @return \League\OAuth2\Server\Entities\ScopeEntityInterface[]
   *   The modified list of scopes.
   */
  protected function addRoleToScopes(array $scopes, ScopeEntityInterface $new_scope) {

    // Only add the role if it's not already in the list.
    $found = array_filter($scopes, function (ScopeEntityInterface $scope) use ($new_scope) {
      return $scope
        ->getIdentifier() == $new_scope
        ->getIdentifier();
    });
    if (empty($found)) {

      // If it's not there, then add it.
      array_push($scopes, $new_scope);
    }
    return $scopes;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OpenIdConnectScopeRepository::$innerScopeRepository protected property The inner scope repository.
OpenIdConnectScopeRepository::addRoleToScopes protected function Add an additional scope if it's not present.
OpenIdConnectScopeRepository::finalizeScopes public function
OpenIdConnectScopeRepository::getOpenIdScopes protected function Returns fixed OpenID Connect scopes.
OpenIdConnectScopeRepository::getScopeEntityByIdentifier public function
OpenIdConnectScopeRepository::__construct public function OpenIdConnectScopeRepository constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.