Source for file totp.php
Documentation is available at totp.php
* @subpackage Twofactorauth.totp
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* Joomla! Two Factor Authentication using Google Authenticator TOTP Plugin
* @subpackage Twofactorauth.totp
* Affects constructor behavior. If true, language files will be loaded automatically.
* @param object &$subject The object to observe
* @param array $config An optional associative array of configuration settings.
* Recognized key values include 'name', 'group', 'params', 'language'
* (this list is not meant to be comprehensive).
public function __construct(&$subject, $config =
array())
// Load the Joomla! RAD layer
* This method returns the identification object for this two factor
* @return stdClass An object with public properties method and title
$section = (int)
$this->params->get('section', 3);
if (!($current_section & $section))
'title' =>
JText::_('PLG_TWOFACTORAUTH_TOTP_METHOD_TITLE')
* Shows the configuration page for this two factor authentication method.
* @param object $otpConfig The two factor auth configuration object
* @param integer $user_id The numeric user ID of the user whose form we'll display
* @return boolean|string False if the method is not ours, the HTML of the configuration page otherwise
* @see UsersModelUser::getOtpConfig
// Create a new TOTP class with Google Authenticator compatible settings
// This method is already activated. Reuse the same secret key.
$secret =
$otpConfig->config['code'];
// This methods is not activated yet. Create a new secret key.
$secret =
$totp->generateSecret();
// These are used by Google Authenticator to tell accounts apart
// This is the URL to the QR code for Google Authenticator
$url =
$totp->getUrl($username, $hostname, $secret);
// Is this a new TOTP setup? If so, we'll have to show the code validation field.
$new_totp =
$otpConfig->method !=
'totp';
// Start output buffering
// Include the form.php from a template override. If none is found use the default.
include_once $path .
'form.php';
include_once __DIR__ .
'/tmpl/form.php';
// Stop output buffering and get the form contents
// Return the form contents
* The save handler of the two factor configuration method's configuration
* @param string $method The two factor auth method for which we'll show the config page
* @return boolean|stdClass False if the method doesn't match or we have an error, OTP config object if it succeeds
* @see UsersModelUser::setOtpConfig
// Get a reference to the input data object
$rawData =
$input->get('jform', array(), 'array');
$data =
$rawData['twofactor']['totp'];
// Warn if the securitycode is empty
$app->enqueueMessage(JText::_('PLG_TWOFACTORAUTH_TOTP_ERR_VALIDATIONFAILED'), 'error');
// This only happens when we are in a CLI application. We cannot
// enqueue a message, so just do nothing.
// Create a new TOTP class with Google Authenticator compatible settings
// Check the security code entered by the user (exact time slot match)
$code =
$totp->getCode($data['key']);
$check =
$code ==
$data['securitycode'];
* If the check fails, test the previous 30 second slot. This allow the
* user to enter the security code when it's becoming red in Google
* Authenticator app (reaching the end of its 30 second lifetime)
$code =
$totp->getCode($data['key'], $time);
$check =
$code ==
$data['securitycode'];
* If the check fails, test the next 30 second slot. This allows some
* time drift between the authentication device and the server
$code =
$totp->getCode($data['key'], $time);
$check =
$code ==
$data['securitycode'];
// Check failed. Do not change two factor authentication settings.
// Check succeedeed; return an OTP configuration object
$otpConfig = (object)
array(
* This method should handle any two factor authentication and report back
* @param array $credentials Array holding the user credentials
* @param array $options Array of extra options
* @return boolean True if the user is authorised with this two-factor authentication method
// Get the OTP configuration object
$otpConfig =
$options['otp_config'];
// Make sure it's an object
if (empty($otpConfig) ||
!is_object($otpConfig))
// Check if we have the correct method
// Check if there is a security code
if (empty($credentials['secretkey']))
// Create a new TOTP class with Google Authenticator compatible settings
$code =
$totp->getCode($otpConfig->config['code']);
$check =
$code ==
$credentials['secretkey'];
* If the check fails, test the previous 30 second slot. This allow the
* user to enter the security code when it's becoming red in Google
* Authenticator app (reaching the end of its 30 second lifetime)
$code =
$totp->getCode($otpConfig->config['code'], $time);
$check =
$code ==
$credentials['secretkey'];
* If the check fails, test the next 30 second slot. This allows some
* time drift between the authentication device and the server
$code =
$totp->getCode($otpConfig->config['code'], $time);
$check =
$code ==
$credentials['secretkey'];
Documentation generated on Tue, 19 Nov 2013 15:15:57 +0100 by phpDocumentor 1.4.3