You are here

public function OAuth2Controller::authorize in OAuth2 Server 8

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

Authorize.

Parameters

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

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

Return value

array|\OAuth2\HttpFoundationBridge\Response|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse A form array or a response object.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

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

File

src/Controller/OAuth2Controller.php, line 65

Class

OAuth2Controller
Class OAuth2 Controller.

Namespace

Drupal\oauth2_server\Controller

Code

public function authorize(RouteMatchInterface $route_match, Request $request) {
  $this
    ->moduleHandler()
    ->invokeAll('oauth2_server_pre_authorize');

  // Workaround https://www.drupal.org/project/oauth2_server/issues/3049250
  // Create a duplicate request with the parameters removed, so that the
  // object can survive being serialized.
  $duplicated_request = $request
    ->duplicate(NULL, NULL, []);
  $bridgeRequest = BridgeRequest::createFromRequest($duplicated_request);
  if ($this
    ->currentUser()
    ->isAnonymous()) {
    $_SESSION['oauth2_server_authorize'] = $bridgeRequest;
    $url = new Url('user.login', [], [
      'query' => [
        'destination' => Url::fromRoute('oauth2_server.authorize')
          ->toString(),
      ],
    ]);
    $url
      ->setAbsolute(TRUE);
    return new RedirectResponse($url
      ->toString());
  }

  // A login happened: Create the request with parameters from the session.
  if (!empty($_SESSION['oauth2_server_authorize'])) {
    $bridgeRequest = $_SESSION['oauth2_server_authorize'];
  }
  $client = FALSE;
  if ($bridgeRequest
    ->get('client_id')) {

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

  // Initialize the server.
  $oauth2_server = Utility::startServer($client
    ->getServer(), $this->storage);

  // Automatic authorization is enabled for this client. Finish authorization.
  // handleAuthorizeRequest() will call validateAuthorizeRequest().
  $response = new BridgeResponse();
  if ($client->automatic_authorization) {
    unset($_SESSION['oauth2_server_authorize']);
    $oauth2_server
      ->handleAuthorizeRequest($bridgeRequest, $response, TRUE, $this
      ->currentUser()
      ->id());
    return $response;
  }
  else {

    // Validate the request.
    if (!$oauth2_server
      ->validateAuthorizeRequest($bridgeRequest, $response)) {

      // Clear the parameters saved in the session to avoid reusing them when
      // doing an other request while logged in.
      unset($_SESSION['oauth2_server_authorize']);
      return $response;
    }

    // Determine the scope for this request.
    $scope_util = new ScopeUtility($client
      ->getServer());
    if (!($scope = $scope_util
      ->getScopeFromRequest($bridgeRequest))) {
      $scope = $scope_util
        ->getDefaultScope();
    }

    // Convert the scope string to a set of entities.
    $scope_names = explode(' ', $scope);
    $scopes = $this
      ->entityTypeManager()
      ->getStorage('oauth2_server_scope')
      ->loadByProperties([
      'server_id' => $client
        ->getServer()
        ->id(),
      'scope_id' => $scope_names,
    ]);

    // Show the authorize form.
    return $this
      ->formBuilder()
      ->getForm('Drupal\\oauth2_server\\Form\\AuthorizeForm', [
      'client' => $client,
      'scopes' => $scopes,
    ]);
  }
}