You are here

public function Oauth2AuthorizeForm::submitForm in Simple OAuth (OAuth2) & OpenID Connect 8.2

Same name and namespace in other branches
  1. 8.3 simple_oauth_extras/src/Controller/Oauth2AuthorizeForm.php \Drupal\simple_oauth_extras\Controller\Oauth2AuthorizeForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

simple_oauth_extras/src/Controller/Oauth2AuthorizeForm.php, line 186

Class

Oauth2AuthorizeForm

Namespace

Drupal\simple_oauth_extras\Controller

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($auth_request = $form_state
    ->get('auth_request')) {

    // Once the user has logged in set the user on the AuthorizationRequest.
    $user_entity = new UserEntity();
    $user_entity
      ->setIdentifier($this
      ->currentUser()
      ->id());
    $auth_request
      ->setUser($user_entity);

    // Once the user has approved or denied the client update the status
    // (true = approved, false = denied).
    $can_grant_codes = $this
      ->currentUser()
      ->hasPermission('grant simple_oauth codes');
    $auth_request
      ->setAuthorizationApproved((bool) $form_state
      ->getValue('submit') && $can_grant_codes);

    // Return the HTTP redirect response.
    $response = $this->server
      ->completeAuthorizationRequest($auth_request, new Response());

    // Get the location and return a secure redirect response.
    $redirect_response = TrustedRedirectResponse::create($response
      ->getHeaderLine('location'), $response
      ->getStatusCode(), $response
      ->getHeaders());
    $form_state
      ->setResponse($redirect_response);
  }
  elseif ($params = $form_state
    ->getValue('redirect_params')) {
    $url = Url::fromRoute('user.login');
    $destination = Url::fromRoute('oauth2_token_extras.authorize', [], [
      'query' => UrlHelper::parse('/?' . $params)['query'],
    ]);
    $url
      ->setOption('query', [
      'destination' => $destination
        ->toString(),
    ]);
    $form_state
      ->setRedirectUrl($url);
  }
}