You are here

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

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

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

Class

UsersGenerateKeyForm
Class UsersKeyForm.

Namespace

Drupal\users_jwt\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
  if (!$user) {
    return $form;
  }
  $new_id = $user
    ->id() . '-' . $this
    ->getRequest()->server
    ->get('REQUEST_TIME');
  $key = new UsersKey($user
    ->id(), $new_id, 'RS256');
  $form['key'] = [
    '#type' => 'value',
    '#value' => $key,
  ];
  $form['user'] = [
    '#type' => 'value',
    '#value' => $user,
  ];
  $form['instructions'] = [
    '#type' => 'item',
    '#markup' => $this
      ->t('When you click the button, a new key will be generated with ID %key_id. You will save the private key, the public key will be added to your account. If you lose the private key, it cannot be recovered.', [
      '%key_id' => $key->id,
    ]),
  ];
  $form['alg'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Key Type'),
    '#description' => $this
      ->t('The type of key to generate.'),
    '#options' => $this->keyRepository
      ->algorithmOptions(),
    '#size' => 1,
    '#default_value' => $key->alg,
    '#weight' => 10,
    '#required' => TRUE,
  ];
  $form['actions'] = [
    '#type' => 'actions',
    '#weight' => 30,
  ];
  $form['actions']['download'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Generate'),
  ];
  $cancel_url = Url::fromRoute('users_jwt.key_list', [
    'user' => $user
      ->id(),
  ]);
  $form['actions']['cancel'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Cancel'),
    '#attributes' => [
      'class' => [
        'button',
      ],
    ],
    '#url' => $cancel_url,
  ];
  $form['#attached']['library'][] = 'users_jwt/download_redirect';
  return $form;
}