You are here

public function Oauth2Token::token in Simple OAuth (OAuth2) & OpenID Connect 8.4

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

Processes POST requests to /oauth/token.

1 string reference to 'Oauth2Token::token'
simple_oauth.routing.yml in ./simple_oauth.routing.yml
simple_oauth.routing.yml

File

src/Controller/Oauth2Token.php, line 42

Class

Oauth2Token

Namespace

Drupal\simple_oauth\Controller

Code

public function token(ServerRequestInterface $request) {

  // Extract the grant type from the request body.
  $body = $request
    ->getParsedBody();
  $grant_type_id = !empty($body['grant_type']) ? $body['grant_type'] : 'implicit';
  $client_drupal_entity = NULL;
  if (!empty($body['client_id'])) {
    $consumer_storage = $this
      ->entityTypeManager()
      ->getStorage('consumer');
    $client_drupal_entities = $consumer_storage
      ->loadByProperties([
      'uuid' => $body['client_id'],
    ]);
    if (empty($client_drupal_entities)) {
      return OAuthServerException::invalidClient()
        ->generateHttpResponse(new Response());
    }
    $client_drupal_entity = reset($client_drupal_entities);
  }

  // Get the auth server object from that uses the League library.
  try {

    // Respond to the incoming request and fill in the response.
    $auth_server = $this->grantManager
      ->getAuthorizationServer($grant_type_id, $client_drupal_entity);
    $response = $this
      ->handleToken($request, $auth_server);
  } catch (OAuthServerException $exception) {
    watchdog_exception('simple_oauth', $exception);
    $response = $exception
      ->generateHttpResponse(new Response());
  }
  return $response;
}