You are here

class TfaTrustedBrowserSetup in TFA Basic plugins 7

Class TfaTrustedBrowserSetup

Hierarchy

Expanded class hierarchy of TfaTrustedBrowserSetup

File

includes/tfa_trusted_browser.inc, line 205
classes for TFA basic plugin

View source
class TfaTrustedBrowserSetup extends TfaTrustedBrowser implements TfaSetupPluginInterface {
  public function __construct(array $context) {
    parent::__construct($context);
  }

  /**
   * @copydoc TfaSetupPluginInterface::getSetupForm()
   */
  public function getSetupForm(array $form, array &$form_state) {
    $existing = $this
      ->getTrustedBrowsers();
    $time = variable_get('tfa_basic_trust_cookie_expiration', 3600 * 24 * 30) / (3600 * 24);
    $form['info'] = array(
      '#type' => 'markup',
      '#markup' => '<p>' . t("Trusted browsers are a method for simplifying login by avoiding verification code entry for a set amount of time, !time days from marking a browser as trusted. After !time days, to log in you'll need to enter a verification code with your username and password during which you can again mark the browser as trusted.", array(
        '!time' => $time,
      )) . '</p>',
    );

    // Present option to trust this browser if its not currently trusted.
    if (isset($_COOKIE[$this->cookieName]) && ($browser_id = $this
      ->trustedBrowser($_COOKIE[$this->cookieName])) !== FALSE) {
      $current_trusted = $browser_id;
    }
    else {
      $current_trusted = FALSE;
      $form['trust'] = array(
        '#type' => 'checkbox',
        '#title' => t('Trust this browser?'),
        '#default_value' => empty($existing) ? 1 : 0,
      );

      // Optional field to name this browser.
      $form['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name this browser'),
        '#maxlength' => 255,
        '#description' => t('Optionally, name the browser on your browser (e.g. "home firefox" or "office desktop windows"). Your current browser user agent is %browser', array(
          '%browser' => $_SERVER['HTTP_USER_AGENT'],
        )),
        '#default_value' => $this
          ->getAgent(),
        '#states' => array(
          'visible' => array(
            ':input[name="trust"]' => array(
              'checked' => TRUE,
            ),
          ),
        ),
      );
    }
    if (!empty($existing)) {
      $form['existing'] = array(
        '#type' => 'fieldset',
        '#title' => t('Existing browsers'),
        '#description' => t('Leave checked to keep these browsers in your trusted log in list.'),
        '#tree' => TRUE,
      );
      foreach ($existing as $browser) {
        $vars = array(
          '!expiration' => format_date($browser['created'] + variable_get('tfa_basic_trust_cookie_expiration', 3600 * 24 * 30)),
          '!time' => format_date($browser['last_used']),
        );
        if ($current_trusted == $browser['id']) {
          $name = '<strong>' . t('@name (current browser)', array(
            '@name' => $browser['name'],
          )) . '</strong>';
        }
        else {
          $name = check_plain($browser['name']);
        }
        if (empty($browser['last_used'])) {
          $message = t('Expires !expiration', $vars);
        }
        else {
          $message = t('Expires !expiration, last used for log in !time', $vars);
        }
        $form['existing']['trusted_browser_' . $browser['id']] = array(
          '#type' => 'checkbox',
          '#title' => $name,
          '#description' => $message,
          '#default_value' => 1,
        );
      }
    }
    $form['actions']['save'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
    return $form;
  }

  /**
   * @copydoc TfaSetupPluginInterface::validateSetupForm()
   */
  public function validateSetupForm(array $form, array &$form_state) {
    return TRUE;

    // Do nothing, no validation required.
  }

  /**
   * @copydoc TfaSetupPluginInterface::submitSetupForm()
   */
  public function submitSetupForm(array $form, array &$form_state) {
    if (isset($form_state['values']['existing'])) {
      $count = 0;
      foreach ($form_state['values']['existing'] as $element => $value) {
        $id = str_replace('trusted_browser_', '', $element);
        if (!$value) {
          $this
            ->deleteTrusted($id);
          $count++;
        }
      }
      if ($count) {
        watchdog('tfa_basic', 'Removed !num TFA trusted browsers during trusted browser setup', array(
          '!num' => $count,
        ), WATCHDOG_INFO);
      }
    }
    if (!empty($form_state['values']['trust']) && $form_state['values']['trust']) {
      $name = '';
      if (!empty($form_state['values']['name'])) {
        $name = $form_state['values']['name'];
      }
      elseif (isset($_SERVER['HTTP_USER_AGENT'])) {
        $name = $this
          ->getAgent();
      }
      $this
        ->setTrusted($this
        ->generateBrowserId(), $name);
    }
    return TRUE;
  }

  /**
   * Get list of trusted browsers.
   *
   * @return array
   */
  public function getTrustedBrowsers() {
    $browsers = array();
    $result = db_query("SELECT did, name, ip, created, last_used FROM {tfa_trusted_browser} WHERE uid = :uid", array(
      ':uid' => $this->context['uid'],
    ));
    if ($result) {
      foreach ($result as $row) {
        $browsers[] = array(
          'id' => $row->did,
          'name' => $row->name,
          'created' => $row->created,
          'ip' => $row->ip,
          'last_used' => $row->last_used,
        );
      }
    }
    return $browsers;
  }

  /**
   * Delete a trusted browser by its ID.
   *
   * @param int $id
   * @return int
   */
  public function deleteTrustedId($id) {
    return $this
      ->deleteTrusted($id);
  }

  /**
   * Delete all trusted browsers.
   *
   * @return int
   */
  public function deleteTrustedBrowsers() {
    return $this
      ->deleteTrusted();
  }

}

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::ready public function Determine if the plugin can run for the current TFA context. 2
TfaBasePlugin::timingSafeEquals private function A timing safe equals comparison.
TfaBasePlugin::validate protected function Validate code.
TfaTrustedBrowser::$cookieName protected property
TfaTrustedBrowser::$domain protected property
TfaTrustedBrowser::$expiration protected property
TfaTrustedBrowser::$trustBrowser protected property
TfaTrustedBrowser::deleteTrusted protected function Delete users trusted browsers.
TfaTrustedBrowser::finalize public function
TfaTrustedBrowser::generateBrowserId protected function Generate a random value to identify the browser.
TfaTrustedBrowser::getAgent protected function Get simplified browser name from user agent.
TfaTrustedBrowser::getForm public function @copydoc TfaValidationPluginInterface::getForm()
TfaTrustedBrowser::loginAllowed public function Overrides TfaLoginPluginInterface::loginAllowed
TfaTrustedBrowser::setTrusted protected function Store browser value and issue cookie for user.
TfaTrustedBrowser::setUsed protected function Updated browser last used time.
TfaTrustedBrowser::submitForm public function @copydoc TfaBasePlugin::submitForm() Overrides TfaBasePlugin::submitForm
TfaTrustedBrowser::trustedBrowser protected function Check if browser value matches user's saved browser.
TfaTrustedBrowserSetup::deleteTrustedBrowsers public function Delete all trusted browsers.
TfaTrustedBrowserSetup::deleteTrustedId public function Delete a trusted browser by its ID.
TfaTrustedBrowserSetup::getSetupForm public function @copydoc TfaSetupPluginInterface::getSetupForm() Overrides TfaSetupPluginInterface::getSetupForm
TfaTrustedBrowserSetup::getTrustedBrowsers public function Get list of trusted browsers.
TfaTrustedBrowserSetup::submitSetupForm public function @copydoc TfaSetupPluginInterface::submitSetupForm() Overrides TfaSetupPluginInterface::submitSetupForm
TfaTrustedBrowserSetup::validateSetupForm public function @copydoc TfaSetupPluginInterface::validateSetupForm() Overrides TfaSetupPluginInterface::validateSetupForm
TfaTrustedBrowserSetup::__construct public function Plugin constructor. Overrides TfaTrustedBrowser::__construct