A simple PHP script to create TOTP secrets and corresponding QR codes,
then verify the entered response over a given time variance.
homepage
documentation
glossary
server
source
Install
composer require eustasy/authenticatron
Require
//// Import eustasy\Authenticatron with Composer require_once __DIR__ . '/vendor/autoload.php'; use eustasy\Authenticatron;
Authenticatron::new
to create a new secret for a member, and fetch a secure image for scanning.
Code
Authenticatron::new($accountName, $issuer)
Input
$accountName
is a string containing your members username or nice-name, perferably something unique and quickly identifiable.
$issuer
is a string containing the name of your app or site.
Output
Outputs an array, where Secret
is the Secret for the member, URL
is an OTPAuth URL, and QR
is the Data64 URI for the QR code.
array(3) { ["Secret"]=> string(16) "VPFTTXQCOZ3VRBCE" ["URL"]=> string(113) "otpauth://totp/Authenticatron Example Page: John Smith?secret=VPFTTXQCOZ3VRBCE&issuer=Authenticatron+Example+Page" ["QR"]=> string(722) "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAC0AQMAAAAHA5RxAAAABlBMVEX///8AAABVwtN+AAAACXBIWXMAAA7EAAAOxAGVKw4bAAABq0lEQVRYhdWXwXUDQQhD6UD9d0kHChLMi33XxZvn7PrvgQxIQKp+/SLZc0PNDQQfSPH5NBqDCpgHgyRn69Kr1scgzIsTsgznZZ6jXQqlLc+VNxdmQk/yvvIZ4Crz1/WhhwQvx+b9AJ/aTXC24ky+VrKl4iR58yrew0p3nG5DvPS4TOabx40b4rLzhJXjpLCHc1yJs7Ag8+lY6CTf3MHOk0O4eUtxXKa2NPNlzJfk1hNsaUW3DCrIS0xFkXbdWmt9F+J93/jcR9zfk+HK2lRCbutN4OUzxtWtVfFJHLfxJTlttu13e0irOMZdadodVq3c10HeLoYsp/KrR93cD/HrR+qv7Zo/38U4277AzgX26SrEVWetFJsy5a+i3JHKfbvbLe90m+KSEa1VvfYSluQ6lMalwrkHglVJrryZY7srcWtShsvQO0D9pKRFOeoWOq628PpSiLdDufbbvdU/gtzvNGwsWe7uG+TbkiRa3vLejpvi2ot0Ftm6r4tHORXIv+npwD1XkrsYOxWoTSbNlTqV32t7/f8/EuFVN/PdodbjQX4jjVasewbYQf7b1x/slu+D3r5M+gAAAABJRU5ErkJggg==" }
Handling
You'll want to store ['Secret']
with the member, but make sure you get them to confirm a code before enforcing it, or it might not have worked and they would be locked out of their account. Make sure that this is as protected as a password hash.
['QR']
is the Data64 URI for the QR code. You can simply echo it into an img
element like this:
<img src="<?php echo $secondAuth['QR']; ?>" alt="Second Factor Authentication Code">
Example
Try scanning this into an app like Google Authenticator. You should see a code and a countdown clock until it changes.
Use Authenticatron::checkCode
to confirm the setup and check time-unique codes at every login.
Code
Authenticatron::checkCode($code, $secret)
Input
$code
is the user input, the code that is generated on their device for authentication. Should be numeric-only in most cases, alpha-numeric if you change some settings.
$secret
is the secret the member scanned that you securely stored for later.
$variance
is an optional integer indicating the adjustment of codes with a 30 second value. Defaults to 2 either side, or 1 minute.
Output
Outputs a boolean value, true
if the entered code is within allowed range, false
if not.
bool(true)
Handling
You only need to check an input is alpha-numeric, and maybe 6 characters long before checking it against a retreieved secret.
$secret = ...; if ( strlen($_POST['secondfactor_code']) == 6 && ctype_alnum($_POST['secondfactor_code']) ) { if ( Authenticatron::checkCode($_POST['secondfactor_code'], $secret) ) { // Authenticated, log in... } else { // Incorrect code } } else { // Invalid entry }
Example
Enter the code that your device generates after scanning the image to from Step 1.
Visit our documentation for a more thorough description of the options and functions available to you.
Take a look at the glossary if there are any terms you don't understand.
The server page can be used if this script is installed on your server to check for requirements.
This work is predominantly MIT licensed. See the LICENSE.md file for more information.
If you're ready to rock, check out the source!