class Oauth2GenerateKeyForm in Simple OAuth (OAuth2) & OpenID Connect 5.x
Same name and namespace in other branches
- 8.4 src/Entity/Form/Oauth2GenerateKeyForm.php \Drupal\simple_oauth\Entity\Form\Oauth2GenerateKeyForm
- 8.3 src/Entity/Form/Oauth2GenerateKeyForm.php \Drupal\simple_oauth\Entity\Form\Oauth2GenerateKeyForm
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\simple_oauth\Entity\Form\Oauth2GenerateKeyForm
Expanded class hierarchy of Oauth2GenerateKeyForm
1 file declares its use of Oauth2GenerateKeyForm
- Oauth2GenerateKey.php in src/
Controller/ Oauth2GenerateKey.php
File
- src/
Entity/ Form/ Oauth2GenerateKeyForm.php, line 17
Namespace
Drupal\simple_oauth\Entity\FormView source
class Oauth2GenerateKeyForm extends FormBase {
/**
* @var \Drupal\simple_oauth\Service\KeyGeneratorService
*/
private $keyGen;
/**
* Oauth2GenerateKeyForm constructor.
*
* @param \Drupal\simple_oauth\Service\KeyGeneratorService $key_generator_service
*/
public function __construct(KeyGeneratorService $key_generator_service) {
$this->keyGen = $key_generator_service;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('simple_oauth.key.generator'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'oauth2_generate_key';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $pubk_id = NULL, $pk_id = NULL) {
// Hidden public key id.
$form['key_settings']['pubk_id'] = [
'#type' => 'hidden',
'#required' => TRUE,
'#value' => $pubk_id,
];
// Hidden private key id.
$form['key_settings']['pk_id'] = [
'#type' => 'hidden',
'#required' => TRUE,
'#value' => $pk_id,
];
// Error Messages container.
$form['key_settings']['message'] = [
'#markup' => '<div id="key-error-message" class="messages messages--error"></div>',
'#hidden' => TRUE,
];
$disclaimer = '<p>' . $this
->t('This is the directory where the public and private keys will be stored after generation. This <strong>SHOULD</strong> be located outside of your webroot to avoid making them public unintentionally.') . '</p><p>' . $this
->t('Any keys already present in this directory will be deleted.') . '</p>';
// Private Key Path.
$form['key_settings']['directory'] = [
'#type' => 'textfield',
'#title' => $this
->t('Directory for the keys'),
'#description' => $disclaimer,
'#required' => TRUE,
'#attributes' => [
'id' => "dir_path",
],
];
// Submit.
$form['key_settings']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Generate'),
'#ajax' => [
'callback' => '::generateKeys',
'event' => 'click',
],
];
return $form;
}
/**
* Generate public and private keys.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return \Drupal\Core\Ajax\AjaxResponse
*/
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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Do nothing.
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
Oauth2GenerateKeyForm:: |
private | property | ||
Oauth2GenerateKeyForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
Oauth2GenerateKeyForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
Oauth2GenerateKeyForm:: |
public | function | Generate public and private keys. | |
Oauth2GenerateKeyForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
Oauth2GenerateKeyForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
Oauth2GenerateKeyForm:: |
public | function | Oauth2GenerateKeyForm constructor. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |