OpenIdConnectScopeRepository.php in Simple OAuth (OAuth2) & OpenID Connect 5.x
File
src/OpenIdConnect/OpenIdConnectScopeRepository.php
View source
<?php
namespace Drupal\simple_oauth\OpenIdConnect;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\simple_oauth\Entities\OpenIdConnectScopeEntity;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
class OpenIdConnectScopeRepository implements ScopeRepositoryInterface {
use StringTranslationTrait;
protected $innerScopeRepository;
public function __construct(ScopeRepositoryInterface $inner_scope_repository) {
$this->innerScopeRepository = $inner_scope_repository;
}
public function getScopeEntityByIdentifier($identifier) {
$role_scope = $this->innerScopeRepository
->getScopeEntityByIdentifier($identifier);
if ($role_scope) {
return $role_scope;
}
$openid_scopes = $this
->getOpenIdScopes();
if (isset($openid_scopes[$identifier])) {
return new OpenIdConnectScopeEntity($identifier, $openid_scopes[$identifier]);
}
return NULL;
}
public function finalizeScopes(array $scopes, $grantType, ClientEntityInterface $clientEntity, $userIdentifier = NULL) {
$finalized_scopes = $this->innerScopeRepository
->finalizeScopes($scopes, $grantType, $clientEntity, $userIdentifier);
$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;
}
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;
}
protected function addRoleToScopes(array $scopes, ScopeEntityInterface $new_scope) {
$found = array_filter($scopes, function (ScopeEntityInterface $scope) use ($new_scope) {
return $scope
->getIdentifier() == $new_scope
->getIdentifier();
});
if (empty($found)) {
array_push($scopes, $new_scope);
}
return $scopes;
}
}