You are here

public function OAuth2Controller::token in OAuth2 Server 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/OAuth2Controller.php \Drupal\oauth2_server\Controller\OAuth2Controller::token()

Token.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match object.

\Symfony\Component\HttpFoundation\Request $request: The request object.

Return value

\OAuth2\HttpFoundationBridge\Response|\Symfony\Component\HttpFoundation\JsonResponse A response object.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'OAuth2Controller::token'
oauth2_server.routing.yml in ./oauth2_server.routing.yml
oauth2_server.routing.yml

File

src/Controller/OAuth2Controller.php, line 163

Class

OAuth2Controller
Class OAuth2 Controller.

Namespace

Drupal\oauth2_server\Controller

Code

public function token(RouteMatchInterface $route_match, Request $request) {
  $bridgeRequest = BridgeRequest::createFromRequest($request);
  $client_credentials = Utility::getClientCredentials($bridgeRequest);

  // Get the client and use it to load the server and initialize the server.
  $client = FALSE;
  if ($client_credentials) {

    /** @var \Drupal\oauth2_server\ClientInterface[] $clients */
    $clients = $this
      ->entityTypeManager()
      ->getStorage('oauth2_server_client')
      ->loadByProperties([
      'client_id' => $client_credentials['client_id'],
    ]);
    if ($clients) {
      $client = reset($clients);
    }
  }
  if (!$client) {
    return new JsonResponse([
      'error' => 'Client could not be found.',
    ], JsonResponse::HTTP_NOT_FOUND);
  }
  $response = new BridgeResponse();
  $oauth2_server = Utility::startServer($client
    ->getServer(), $this->storage);
  $oauth2_server
    ->handleTokenRequest($bridgeRequest, $response);
  return $response;
}