class TfaTrustedBrowserSetup in TFA Basic plugins 7
Class TfaTrustedBrowserSetup
Hierarchy
- class \TfaBasePlugin
- class \TfaTrustedBrowser implements TfaLoginPluginInterface
- class \TfaTrustedBrowserSetup implements TfaSetupPluginInterface
- class \TfaTrustedBrowser implements TfaLoginPluginInterface
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TfaBasePlugin:: |
protected | property | TFA code. | |
TfaBasePlugin:: |
protected | property | Code Length. | |
TfaBasePlugin:: |
protected | property | Context of current TFA process. | |
TfaBasePlugin:: |
protected | property | Encryption key. | |
TfaBasePlugin:: |
protected | property | Error messages. | |
TfaBasePlugin:: |
protected | property | Code is valid. | |
TfaBasePlugin:: |
constant | |||
TfaBasePlugin:: |
protected | function | Decrypt a encrypted string. | |
TfaBasePlugin:: |
protected | function | Decrypt using the deprecated Mcrypt extension. | |
TfaBasePlugin:: |
protected | function | Use OpenSSL to decrypt data that was originally encrypted with Mcrypt. | |
TfaBasePlugin:: |
protected | function | Encrypt a plaintext string. | |
TfaBasePlugin:: |
protected | function | Encrypt using the deprecated Mcrypt extension. | |
TfaBasePlugin:: |
protected | function | Generate a random string of characters of length $this->codeLength. | |
TfaBasePlugin:: |
public | function | Get error messages suitable for form_set_error(). | |
TfaBasePlugin:: |
public | function | Determine if the plugin can run for the current TFA context. | 2 |
TfaBasePlugin:: |
private | function | A timing safe equals comparison. | |
TfaBasePlugin:: |
protected | function | Validate code. | |
TfaTrustedBrowser:: |
protected | property | ||
TfaTrustedBrowser:: |
protected | property | ||
TfaTrustedBrowser:: |
protected | property | ||
TfaTrustedBrowser:: |
protected | property | ||
TfaTrustedBrowser:: |
protected | function | Delete users trusted browsers. | |
TfaTrustedBrowser:: |
public | function | ||
TfaTrustedBrowser:: |
protected | function | Generate a random value to identify the browser. | |
TfaTrustedBrowser:: |
protected | function | Get simplified browser name from user agent. | |
TfaTrustedBrowser:: |
public | function | @copydoc TfaValidationPluginInterface::getForm() | |
TfaTrustedBrowser:: |
public | function |
Overrides TfaLoginPluginInterface:: |
|
TfaTrustedBrowser:: |
protected | function | Store browser value and issue cookie for user. | |
TfaTrustedBrowser:: |
protected | function | Updated browser last used time. | |
TfaTrustedBrowser:: |
public | function |
@copydoc TfaBasePlugin::submitForm() Overrides TfaBasePlugin:: |
|
TfaTrustedBrowser:: |
protected | function | Check if browser value matches user's saved browser. | |
TfaTrustedBrowserSetup:: |
public | function | Delete all trusted browsers. | |
TfaTrustedBrowserSetup:: |
public | function | Delete a trusted browser by its ID. | |
TfaTrustedBrowserSetup:: |
public | function |
@copydoc TfaSetupPluginInterface::getSetupForm() Overrides TfaSetupPluginInterface:: |
|
TfaTrustedBrowserSetup:: |
public | function | Get list of trusted browsers. | |
TfaTrustedBrowserSetup:: |
public | function |
@copydoc TfaSetupPluginInterface::submitSetupForm() Overrides TfaSetupPluginInterface:: |
|
TfaTrustedBrowserSetup:: |
public | function |
@copydoc TfaSetupPluginInterface::validateSetupForm() Overrides TfaSetupPluginInterface:: |
|
TfaTrustedBrowserSetup:: |
public | function |
Plugin constructor. Overrides TfaTrustedBrowser:: |