You are here

public function UsersKeyForm::validateForm in JSON Web Token Authentication (JWT) 8

Form validation 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 FormBase::validateForm

File

modules/users_jwt/src/Form/UsersKeyForm.php, line 127

Class

UsersKeyForm
Class UsersKeyForm.

Namespace

Drupal\users_jwt\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $is_new = $form_state
    ->getValue('is_new');
  if ($is_new) {
    $id = trim($form_state
      ->getValue('id'));
    if ($this->keyRepository
      ->getKey($id)) {
      $form_state
        ->setErrorByName('id', $this
        ->t('%id is already in use as an ID', [
        '%id' => $id,
      ]));
    }
  }
  $alg = $form_state
    ->getValue('alg');
  $pubkey = trim($form_state
    ->getValue('pubkey'));
  if ($alg === 'RS256') {
    $key_resource = openssl_pkey_get_public($pubkey);
    $details = $key_resource ? openssl_pkey_get_details($key_resource) : FALSE;
    if ($details === FALSE || $details['type'] !== OPENSSL_KEYTYPE_RSA) {
      $form_state
        ->setErrorByName('pubkey', $this
        ->t('This does not look like a PEM formatted RSA public key'));
    }
    else {
      if ($details['bits'] < 2048) {
        $form_state
          ->setErrorByName('pubkey', $this
          ->t('You need to submit at least a 2048 bit key'));
      }
    }
  }
  parent::validateForm($form, $form_state);
}