You are here

public function Oauth2GrantManager::getAuthorizationServer in Simple OAuth (OAuth2) & OpenID Connect 8.4

Same name and namespace in other branches
  1. 8.2 src/Plugin/Oauth2GrantManager.php \Drupal\simple_oauth\Plugin\Oauth2GrantManager::getAuthorizationServer()
  2. 8.3 src/Plugin/Oauth2GrantManager.php \Drupal\simple_oauth\Plugin\Oauth2GrantManager::getAuthorizationServer()
  3. 5.x src/Plugin/Oauth2GrantManager.php \Drupal\simple_oauth\Plugin\Oauth2GrantManager::getAuthorizationServer()

Gets the authorization server.

Parameters

string $grant_type: The grant type used as plugin ID.

\Drupal\consumers\Entity\Consumer|null $client: The consumer entity. May be NULL for BC.

Return value

\League\OAuth2\Server\AuthorizationServer The authorization server.

Throws

\League\OAuth2\Server\Exception\OAuthServerException When the grant cannot be found.

Overrides Oauth2GrantManagerInterface::getAuthorizationServer

File

src/Plugin/Oauth2GrantManager.php, line 127

Class

Oauth2GrantManager
Provides the OAuth2 Grant plugin manager.

Namespace

Drupal\simple_oauth\Plugin

Code

public function getAuthorizationServer($grant_type, Consumer $client = NULL) {
  try {

    /** @var \Drupal\simple_oauth\Plugin\Oauth2GrantInterface $plugin */
    $plugin = $this
      ->createInstance($grant_type);
  } catch (PluginNotFoundException $exception) {
    throw OAuthServerException::invalidGrant('Check the configuration to see if the grant is enabled.');
  }
  $this
    ->checkKeyPaths();
  $salt = Settings::getHashSalt();

  // The hash salt must be at least 32 characters long.
  if (Core::ourStrlen($salt) < 32) {
    throw OAuthServerException::serverError('Hash salt must be at least 32 characters long.');
  }
  if (empty($this->server)) {
    $this->server = new AuthorizationServer($this->clientRepository, $this->accessTokenRepository, $this->scopeRepository, realpath($this->privateKeyPath), Core::ourSubstr($salt, 0, 32), $this->responseType);
  }
  $grant = $plugin
    ->getGrantType();

  // Optionally enable PKCE.
  if ($client && $grant instanceof AuthCodeGrant) {
    $client_has_pkce_enabled = $client
      ->hasField('pkce') && $client
      ->get('pkce')
      ->first()->value;
    if ($client_has_pkce_enabled) {
      $grant
        ->enableCodeExchangeProof();
    }
  }

  // Enable the grant on the server with a token TTL of X hours.
  $this->server
    ->enableGrantType($grant, $this->expiration);
  return $this->server;
}