LockrEncryptionKeyType.php in Lockr 8.2
File
src/Plugin/KeyType/LockrEncryptionKeyType.php
View source
<?php
namespace Drupal\lockr\Plugin\KeyType;
use Drupal\core\Form\FormStateInterface;
use Drupal\key\Plugin\KeyPluginFormInterface;
use Drupal\key\Plugin\KeyTypeBase;
class LockrEncryptionKeyType extends KeyTypeBase implements KeyPluginFormInterface {
public function defaultConfiguration() {
return [
'key_size' => 256,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$key_size_options = [
'128' => 128,
'192' => 192,
'256' => 256,
];
$key_size = $this
->getConfiguration()['key_size'];
$form['key_size'] = [
'#type' => 'select',
'#title' => $this
->t('Key size'),
'#description' => $this
->t('The size of the key in bits.'),
'#options' => $key_size_options,
'#default_value' => $key_size,
'#required' => TRUE,
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->setConfiguration($form_state
->getValues());
}
public static function generateKeyValue(array $configuration) {
$key_size = $configuration['key_size'];
$key_client = \Drupal::service('lockr.client_factory')
->getKeyClient();
return $key_client
->create($key_size);
}
public function validateKeyValue(array $form, FormStateInterface $form_state, $key_value) {
if (!$form_state
->getValue('key_size')) {
return;
}
$bytes = $form_state
->getValue('key_size') / 8;
if (strlen($key_value) != $bytes) {
$form_state
->setErrorByName('key_size', $this
->t('The selected key size does not match the actual size of the key.'));
}
}
}