You are here

public function Oauth2TokenSettingsForm::buildForm in Simple OAuth (OAuth2) & OpenID Connect 8.3

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

Defines the settings form for Access Token entities.

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 Form definition array.

Overrides ConfigFormBase::buildForm

File

src/Entity/Form/Oauth2TokenSettingsForm.php, line 115

Class

Oauth2TokenSettingsForm
The settings form.

Namespace

Drupal\simple_oauth\Entity\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('simple_oauth.settings');
  $form['access_token_expiration'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Access token expiration time'),
    '#description' => $this
      ->t('The default value, in seconds, to be used as expiration time when creating new tokens.'),
    '#default_value' => $config
      ->get('access_token_expiration'),
  ];
  $form['refresh_token_expiration'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Refresh token expiration time'),
    '#description' => $this
      ->t('The default value, in seconds, to be used as expiration time when creating new tokens.'),
    '#default_value' => $config
      ->get('refresh_token_expiration'),
  ];
  $form['token_cron_batch_size'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Token batch size.'),
    '#description' => $this
      ->t('The number of expired token to delete per batch during cron cron.'),
    '#default_value' => $config
      ->get('token_cron_batch_size') ?: 0,
  ];
  $form['public_key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Public Key'),
    '#description' => $this
      ->t('The path to the public key file.'),
    '#default_value' => $config
      ->get('public_key'),
    '#element_validate' => [
      '::validateExistingFile',
    ],
    '#required' => TRUE,
    '#attributes' => [
      'id' => 'pubk',
    ],
  ];
  $form['private_key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Private Key'),
    '#description' => $this
      ->t('The path to the private key file.'),
    '#default_value' => $config
      ->get('private_key'),
    '#element_validate' => [
      '::validateExistingFile',
    ],
    '#required' => TRUE,
    '#attributes' => [
      'id' => 'pk',
    ],
  ];
  $form['remember_clients'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Remember previously approved clients'),
    '#description' => $this
      ->t('When enabled, autorized clients will be stored and a authorization requests for the same client with previously accepted scopes will automatically be accepted.'),
    '#default_value' => $config
      ->get('remember_clients'),
  ];
  $form['actions'] = [
    'actions' => [
      '#cache' => [
        'max-age' => 0,
      ],
      '#weight' => 20,
    ],
  ];

  // Generate Key Modal Button if openssl extension is enabled.
  if ($this->fileSystemChecker
    ->isExtensionEnabled('openssl')) {

    // Generate Modal Button.
    $form['actions']['generate']['keys'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Generate keys'),
      '#url' => Url::fromRoute('oauth2_token.settings.generate_key', [], [
        'query' => [
          'pubk_id' => 'pubk',
          'pk_id' => 'pk',
        ],
      ]),
      '#attributes' => [
        'class' => [
          'use-ajax',
          'button',
        ],
      ],
    ];

    // Attach Drupal Modal Dialog library.
    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
  }
  else {

    // Generate Notice Info Message about enabling openssl extension.
    $this->messenger
      ->addMessage($this
      ->t('Enabling the PHP OpenSSL Extension will permit you generate the keys from this form.'), 'warning');
  }
  return parent::buildForm($form, $form_state);
}