You are here

class TfaTestValidationPluginSetupPlugin in Two-factor Authentication (TFA) 8

TFA Test Validation Plugin Setup Plugin.

@package Drupal\tfa_test_plugins

Plugin annotation


@TfaSetup(
  id = "tfa_test_plugins_validation_setup",
  label = @Translation("TFA Test Validation Plugin Setup"),
  description = @Translation("TFA Test Validation Plugin Setup Plugin"),
  helpLinks = {},
  setupMessages = {}
)

Hierarchy

Expanded class hierarchy of TfaTestValidationPluginSetupPlugin

File

tests/modules/tfa_test_plugins/src/Plugin/TfaSetup/TfaTestValidationPluginSetupPlugin.php, line 25

Namespace

Drupal\tfa_test_plugins\Plugin\TfaSetup
View source
class TfaTestValidationPluginSetupPlugin extends TfaBasePlugin implements TfaSetupInterface {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function ready() {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getSetupForm(array $form, FormStateInterface $form_state) {
    $form['expected_field'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Expected field'),
      '#required' => TRUE,
    ];
    $form['actions']['#type'] = 'actions';
    $form['actions']['login'] = [
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => $this
        ->t('Verify and save'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateSetupForm(array $form, FormStateInterface $form_state) {
    $expected_value = $form_state
      ->getValue('expected_field');
    if (empty($expected_value)) {
      $form_state
        ->setError($form['expected_field'], $this
        ->t('Missing expected value.'));
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function submitSetupForm(array $form, FormStateInterface $form_state) {
    $encrypted = $this
      ->encrypt($form_state
      ->getValue('expected_field'));
    $record = [
      'test_data' => [
        'expected_field' => base64_encode($encrypted),
      ],
    ];
    $this
      ->setUserData($this->pluginDefinition['id'], $record, $this->uid, $this->userData);
    return TRUE;
  }

  /**
   * Get and decode the data expected during setup.
   *
   * @return null|string
   *   The string if found, otherwise NULL;
   *
   * @throws \Drupal\encrypt\Exception\EncryptionMethodCanNotDecryptException
   * @throws \Drupal\encrypt\Exception\EncryptException
   */
  public function getExpectedFieldData() {
    $data = $this
      ->getUserData($this->pluginDefinition['id'], 'test_data', $this->uid, $this->userData);
    if (!empty($data['expected_field'])) {
      return $this
        ->decrypt(base64_decode($data['expected_field']));
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getHelpLinks() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getSetupMessages() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getOverview(array $params) {
    return [
      'heading' => [
        '#type' => 'html_tag',
        '#tag' => 'h2',
        '#value' => $this
          ->t('TFA application'),
      ],
      'link' => [
        '#theme' => 'links',
        '#links' => [
          'admin' => [
            'title' => !$params['enabled'] ? $this
              ->t('Set up application') : $this
              ->t('Reset application'),
            'url' => Url::fromRoute('tfa.validation.setup', [
              'user' => $params['account']
                ->id(),
              'method' => $params['plugin_id'],
            ]),
          ],
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TfaBasePlugin::$alreadyAccepted protected property Whether the code has been used before.
TfaBasePlugin::$code protected property The user submitted code to be validated.
TfaBasePlugin::$codeLength protected property The allowed code length.
TfaBasePlugin::$encryptionProfile protected property Encryption profile.
TfaBasePlugin::$encryptService protected property Encryption service.
TfaBasePlugin::$errorMessages protected property The error for the current validation.
TfaBasePlugin::$isValid protected property Whether the validation succeeded or not.
TfaBasePlugin::$uid protected property The user id.
TfaBasePlugin::$userData protected property Provides the user data service object.
TfaBasePlugin::alreadyAcceptedCode protected function Whether code has already been used.
TfaBasePlugin::decrypt protected function Decrypt a encrypted string.
TfaBasePlugin::encrypt protected function Encrypt a plaintext string.
TfaBasePlugin::getErrorMessages public function Get error messages suitable for form_set_error().
TfaBasePlugin::getLabel public function Get the plugin label.
TfaBasePlugin::storeAcceptedCode protected function Store validated code to prevent replay attack.
TfaBasePlugin::submitForm public function Submit form. 1
TfaBasePlugin::validate protected function Validate code. 1
TfaBasePlugin::__construct public function Constructs a new Tfa plugin object. Overrides PluginBase::__construct 2
TfaDataTrait::deleteUserData protected function Deletes data stored for the current validated user account.
TfaDataTrait::getUserData protected function Returns data stored for the current validated user account.
TfaDataTrait::setUserData protected function Store user specific information.
TfaDataTrait::tfaGetTfaData protected function Get TFA data for an account.
TfaDataTrait::tfaSaveTfaData public function Save TFA data for an account.
TfaTestValidationPluginSetupPlugin::getExpectedFieldData public function Get and decode the data expected during setup.
TfaTestValidationPluginSetupPlugin::getHelpLinks public function Returns a list of links containing helpful information for plugin use. Overrides TfaSetupInterface::getHelpLinks
TfaTestValidationPluginSetupPlugin::getOverview public function Plugin overview page. Overrides TfaSetupInterface::getOverview
TfaTestValidationPluginSetupPlugin::getSetupForm public function Get the setup form for the validation method. Overrides TfaSetupInterface::getSetupForm
TfaTestValidationPluginSetupPlugin::getSetupMessages public function Returns a list of messages for plugin step. Overrides TfaSetupInterface::getSetupMessages
TfaTestValidationPluginSetupPlugin::ready public function Determine if the plugin can run for the current TFA context. Overrides TfaBasePlugin::ready
TfaTestValidationPluginSetupPlugin::submitSetupForm public function Submit the setup form. Overrides TfaSetupInterface::submitSetupForm
TfaTestValidationPluginSetupPlugin::validateSetupForm public function Validate the setup data. Overrides TfaSetupInterface::validateSetupForm