You are here

public function UsersGenerateKeyForm::submitForm in JSON Web Token Authentication (JWT) 8

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

modules/users_jwt/src/Form/UsersGenerateKeyForm.php, line 107

Class

UsersGenerateKeyForm
Class UsersKeyForm.

Namespace

Drupal\users_jwt\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $key = $form_state
    ->getValue('key');
  $alg = $form_state
    ->getValue('alg');
  if ($alg === 'RS256') {
    $config = [
      'private_key_bits' => 4096,
      'private_key_type' => OPENSSL_KEYTYPE_RSA,
    ];
    $private_key = openssl_pkey_new($config);
    $pub = openssl_pkey_get_details($private_key);
    $pubkey = $pub['key'];
    openssl_pkey_export($private_key, $out);
  }
  else {
    throw new \InvalidArgumentException(sprintf('Unknown alg %s', $alg));
  }
  $this->keyRepository
    ->saveKey($key->uid, $key->id, $alg, $pubkey);

  /** @var \Drupal\user\UserInterface $user */
  $user = $form_state
    ->getValue('user');
  $filename = $user
    ->getAccountName() . '__private-key__' . $key->id . '.key';
  $response = Response::create($out);
  $response
    ->setPrivate();

  // Clear the cookie from the browser that is set in JavaScript.
  $response->headers
    ->clearCookie('users_jwt_download', '/', NULL, FALSE, FALSE);
  $disposition = $response->headers
    ->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
  $response->headers
    ->set('Content-Disposition', $disposition);
  $form_state
    ->setResponse($response);
}