You are here

public function Oauth2GenerateKeyForm::generateKeys in Simple OAuth (OAuth2) & OpenID Connect 8.3

Same name and namespace in other branches
  1. 8.4 src/Entity/Form/Oauth2GenerateKeyForm.php \Drupal\simple_oauth\Entity\Form\Oauth2GenerateKeyForm::generateKeys()
  2. 5.x src/Entity/Form/Oauth2GenerateKeyForm.php \Drupal\simple_oauth\Entity\Form\Oauth2GenerateKeyForm::generateKeys()

Generate public and private keys.

Parameters

$form:

\Drupal\Core\Form\FormStateInterface $form_state:

Return value

\Drupal\Core\Ajax\AjaxResponse

File

src/Entity/Form/Oauth2GenerateKeyForm.php, line 110

Class

Oauth2GenerateKeyForm
@internal

Namespace

Drupal\simple_oauth\Entity\Form

Code

public function generateKeys(&$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();

  // Get all the values.
  $values = $form_state
    ->getValues();

  // Get Private key path.
  $dir_path = $values['directory'];
  if (!isset($dir_path)) {
    $response
      ->addCommand(new InvokeCommand('#key-error-message', 'show'));
    return $response
      ->addCommand(new HtmlCommand('#key-error-message', $this
      ->t('The directory is required.')));
  }
  try {

    // Generate keys.
    $this->keyGen
      ->generateKeys($dir_path);
  } catch (\Exception $exception) {

    // If exception log it and return an error message.
    watchdog_exception('simple_oauth', $exception);
    $response
      ->addCommand(new InvokeCommand('#key-error-message', 'show'));
    return $response
      ->addCommand(new HtmlCommand('#key-error-message', $exception
      ->getMessage()));
  }

  // Close dialog.
  $response
    ->addCommand(new CloseDialogCommand());

  // Update private key field if id was supplied on the build form.
  if (isset($values['pk_id'])) {
    $response
      ->addCommand(new InvokeCommand('#' . $values['pk_id'], 'val', [
      $dir_path . '/private.key',
    ]));
  }
  if (isset($values['pubk_id'])) {
    $response
      ->addCommand(new InvokeCommand('#' . $values['pubk_id'], 'val', [
      $dir_path . '/public.key',
    ]));
  }
  return $response;
}