You are here

class TfaTestSend in Two-factor Authentication (TFA) 7.2

Class TfaTestSend.

Hierarchy

Expanded class hierarchy of TfaTestSend

1 string reference to 'TfaTestSend'
tfa_test_tfa_api in tests/tfa_test.module
Implements hook_tfa_api().

File

tests/includes/tfa_test.send.inc, line 11
Tests for the TfaSendPluginInterface.

View source
class TfaTestSend extends TfaBasePlugin implements TfaValidationPluginInterface, TfaSendPluginInterface {

  /**
   * Constructor.
   */
  public function __construct(array $context = array()) {
    parent::__construct($context);
    $this->code = variable_get('tfa_test_code', 'TEST');
  }

  /**
   * {@inheritdoc}
   */
  public function ready() {
    return variable_get('tfa_test_is_ready', TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function getForm(array $form, array &$form_state) {
    $form['code'] = array(
      '#type' => 'textfield',
      '#title' => t('Enter sent code'),
      '#required' => TRUE,
    );
    $form['actions']['login'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );
    $form['actions']['resend'] = array(
      '#type' => 'submit',
      '#value' => t('Resend'),
      '#submit' => array(
        'tfa_form_submit',
      ),
      '#limit_validation_errors' => array(),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array $form, array &$form_state) {
    if (!parent::validate($form_state['values']['code'])) {
      $this->errorMessages['code'] = t('Invalid sent code');
      return FALSE;
    }
    else {
      return TRUE;
    }
  }

  /**
   * Check resend flood.
   *
   * @param int $window
   *   Number of seconds in the time window for this event.
   *
   * @return bool
   *   TRUE if resend flood was not reached, FALSE otherwise.
   */
  public function floodIsAllowed($window) {
    if (!flood_is_allowed('tfa_test_resend', variable_get('tfa_test_resend_threshold', 6), $window, $this->context['uid'])) {
      $this->errorMessages['code'] = t('Resend flood hit');
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array $form, array &$form_state) {
    if ($form_state['values']['op'] === $form_state['values']['resend']) {

      // Register resend event for flood protection.
      flood_register_event('tfa_test_resend', 3600, $this->context['uid']);

      // Support causing send error.
      if (!variable_get('tfa_test_send_begin', TRUE)) {
        drupal_set_message(t('Error during resend'), 'error');
      }
      else {
        drupal_set_message(t('Code resent'));
      }
      return FALSE;
    }
    else {
      return parent::submitForm($form, $form_state);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function begin() {

    // Clear resend flood.
    flood_clear_event('tfa_test_resend');

    // Variable used by TfaTestCase::testSendError().
    if (!variable_get('tfa_test_send_begin', TRUE)) {
      drupal_set_message(t('Error during send'), 'error');
    }

    // A real plugin might send the code to the user.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TfaBasePlugin::$code protected property TFA code.
TfaBasePlugin::$codeLength protected property Code Length.
TfaBasePlugin::$context protected property Context of current TFA process.
TfaBasePlugin::$encryptionKey protected property Encryption key.
TfaBasePlugin::$errorMessages protected property Error messages.
TfaBasePlugin::$isValid protected property Code is valid.
TfaBasePlugin::CRYPT_VERSION constant
TfaBasePlugin::decrypt protected function Decrypt a encrypted string.
TfaBasePlugin::decryptLegacyDataWithMcrypt protected function Decrypt using the deprecated Mcrypt extension.
TfaBasePlugin::decryptLegacyDataWithOpenSSL protected function Use OpenSSL to decrypt data that was originally encrypted with Mcrypt.
TfaBasePlugin::encrypt protected function Encrypt a plaintext string.
TfaBasePlugin::encryptWithMcrypt protected function Encrypt using the deprecated Mcrypt extension.
TfaBasePlugin::generate protected function Generate a random string of characters of length $this->codeLength.
TfaBasePlugin::getErrorMessages public function Get error messages suitable for form_set_error().
TfaBasePlugin::timingSafeEquals private function A timing safe equals comparison.
TfaBasePlugin::validate protected function Validate code.
TfaTestSend::begin public function TFA process begin. Overrides TfaSendPluginInterface::begin
TfaTestSend::floodIsAllowed public function Check resend flood.
TfaTestSend::getForm public function Get TFA process form from plugin. Overrides TfaValidationPluginInterface::getForm
TfaTestSend::ready public function Determine if the plugin can run for the current TFA context. Overrides TfaBasePlugin::ready
TfaTestSend::submitForm public function Submit form. Overrides TfaBasePlugin::submitForm
TfaTestSend::validateForm public function Validate form. Overrides TfaValidationPluginInterface::validateForm
TfaTestSend::__construct public function Constructor. Overrides TfaBasePlugin::__construct