View source
<?php
namespace Drupal\simple_oauth\Entity\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\simple_oauth\Service\KeyGeneratorService;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Oauth2GenerateKeyForm extends FormBase {
private $keyGen;
public function __construct(KeyGeneratorService $key_generator_service) {
$this->keyGen = $key_generator_service;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('simple_oauth.key.generator'));
}
public function getFormId() {
return 'oauth2_generate_key';
}
public function buildForm(array $form, FormStateInterface $form_state, $pubk_id = NULL, $pk_id = NULL) {
$form['key_settings']['pubk_id'] = [
'#type' => 'hidden',
'#required' => TRUE,
'#value' => $pubk_id,
];
$form['key_settings']['pk_id'] = [
'#type' => 'hidden',
'#required' => TRUE,
'#value' => $pk_id,
];
$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>';
$form['key_settings']['directory'] = [
'#type' => 'textfield',
'#title' => $this
->t('Directory for the keys'),
'#description' => $disclaimer,
'#required' => TRUE,
'#attributes' => [
'id' => "dir_path",
],
];
$form['key_settings']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Generate'),
'#ajax' => [
'callback' => '::generateKeys',
'event' => 'click',
],
];
return $form;
}
public function generateKeys(&$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$values = $form_state
->getValues();
$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 {
$this->keyGen
->generateKeys($dir_path);
} catch (\Exception $exception) {
watchdog_exception('simple_oauth', $exception);
$response
->addCommand(new InvokeCommand('#key-error-message', 'show'));
return $response
->addCommand(new HtmlCommand('#key-error-message', $exception
->getMessage()));
}
$response
->addCommand(new CloseDialogCommand());
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;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}