Padlock Icon

Authenticatron

A simple PHP script to create TOTP secrets and corresponding QR codes,
then verify the entered response over a given time variance.
homepagedocumentationglossaryserversource

Initialize Authenticatron

Information

Get ready to use Authenticatron.

Code

$auth = Authenticatron(
		string $issuerDefault = 'Example Site',
		string $phpQrCode = __DIR__ . '/_libs/phpqrcode_2010100721_1.1.4.php'
	): class

Input

$issuerDefault is a string containing the default name you with to use to identify your app.

$phpQrCode is an optional string containing the location of the PHP QR Code Library, if diferent from the default.

Authenticatron New

Information

Create a new Secret and get the QR Code all in one.

Code

$auth->new(string $accountName): array

Input

$accountName is a string containing the data your member will identify with.

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) "VZITNY6BJPXLFDGI"
  ["URL"]=>
  string(126) "otpauth://totp/Authenticatron Documentation Page: Member Name?secret=VZITNY6BJPXLFDGI&issuer=Authenticatron+Documentation+Page"
  ["QR"]=>
  string(918) "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMQAAADEAQMAAAD6aBwoAAAABlBMVEUAAAD///+l2Z/dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACP0lEQVRYhbWYUXLEMAhDuQH3v6VuQJGE89Nf7Xam2fh5JsEWAm9VVWMwVQPsv967rvsESc/0/u0HNRxb3nsfJ3stwX0LPnqn7Ej/hPQObqDglL3Or8hwIXkDzpifEF4BAr7HhgutcJpQCv8+n3ZyhNqo26p9l1lBSjAIE60jBWjt80r5d5oUBaEdm8uvnQhlWZRsWHunxzPM4Ttw/ypMiosqc4CeXhQM0zhMGFNvUg3N6KVXP4cKkpZImLk7TBFa7EgT2IycuMs0xiUIk+FWac8IyTwlTeTcUuCGqOGNdypOGOZKYqlHFu4XpMkZrMQIv4ESIU3WhoCzWkWpRW4NJIkra9HA+b1bVEU9Srh4Y3sdGhPNb24/k+TyiDr8uhKvaJawHSGGt4zNCaOdChNWdNYlDjBMyrDlg2Eyak8keEbZ8EiazKlRLtR0pFbfUmHSjK3kRTTX1rSSZrIEcFcnWUr4zDXEiRJW9ZUJxtxSx4UwOZFwgJ7nLHPDEiWQI7USSSvb5UDTxB5LbbRnzHUoWaII8dqGOslDXpUkLBX2PLVfNqfrXZKkvF+413FycSxMJA/brNuFuQlhAnXCd4RUpQWuTIUJNw7aL/nSuSvSxE2CWzpFyldSVxQlPg9BKeWcaryTWZLoBKauSzWWRmjJh0ldwSg3d/59oX0gSxL1v0op1UF1+K82RokOyPCPMufo9RlvltxZldVPxlH1OpQsUS2iNspNZasPCxO13doz/YYBvPzKEpWG+lrgvsNRhckfGcWjkUafu7oAAAAASUVORK5CYII="
}

Authenticatron Check

Information

This returns a simple boolean value to prevent data-leakage and zero-equivalent values from codes or keys.

Code

$auth->checkCode(string $code, string $secret, int $variance = 2): bool

Input

$code is what the user enters to authenticate. A 6 digit string, usually numeric, but not necessarily an integer.

$secret is the first result from Authenticatron_Check, that you securely stored for later.

$variance is an 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 or false.

bool(true)

lifefloat

Warning: The functions below are for advanced users only.

You should only need the two functions above this point to implement two-factor authentication.

Functions listed below this point should not need to be used in most production-ready environments.


Authenticatron Secret

Information

Generates a 16-digit secret, never to be shared with anyone except via internal non-cachable QR code.

Generated using RandomBytes if it is available, falling back to OpenSSL if it is secure.

RandomBytes is available.

OpenSSL is installed, and secure.

Your installation will use RandomBytes.

Code

$auth->makeSecret(int $length = 16): ?string

Input

$length should be an integer, longer than 16. Usually left to default.

Output

Returns a $length long string with 32bit only Characters, or null on failure (usually due to a lack of security).

Click the link to keep the secret the same when you refresh the page.

VFKC3ENSCA6UAIEJ

Authenticatron URL

Information

Generates the URL for launching and adding the Secret we made earlier.

This link won't do anything unless you have a Authentication program on your computer.

Code

$auth->getUrl(string $accountName, string $secret, string $issuer = null): string

Input

All parameters should be strings, with the optional issuer defaulting to the configured value if not passed.

Output

Outputs an OTPAuth URL that gives people their Secret along with a passed Member Name and an optional Issuer.

otpauth://totp/Authenticatron Documentation Page: Member Name?secret=VFKC3ENSCA6UAIEJ&issuer=Authenticatron+Documentation+Page

Authenticatron QR

Information

Outputs a QR Code in Data64 for direct embedding from a given URL.

The GD functions are loaded.

Code

generateQrCode(string $URL, int $Size = 4, int $Margin = 0, string $Level = 'M'): ?string

Input

$URL is a valid OTPAuth URL in string form.

$Size is a non-zero integer, defaults to 4.

$Margin is an integer, defaults to 0.

$Level is a string, defaults to 'M'. It defines the error correction level.

Output

Google Camera Icon

Outputs a QR Code image in 64bit data-URI form.

Try scanning this QR code with your phone.

This should open an app like Google Authenticator.

Authenticatron Code

Information

This is the current authentication code.

Check the Acceptable list to see the two either side.

Code

getCode(string $secret, int $timestamp = null, int $codeLength = 6): string

Input

$secret is a valid Base32 Secret in string form.

$timestamp is a unix timestamp, defaults to false to use the current timestamp.

$codeLength is a non-zero integer, the desired length of the generated code. Defaults to 6.

Output

Outputs the calculated code for the current or provided timestamp.

string(6) "209523"

Authenticatron Acceptable

Information

This is the array Authenticatron_Check uses to check for valid codes.

Code

$auth->getCodesInRange(string $secret, int $variance = 2): array

Input

$secret is a valid Base32 Secret in string form.

$variance is an integer indicating the adjustment of codes with a 30 second value. Defaults to 2, or 1 minute.

Output

Google Authenticator Icon

Outputs the calculated code for the current or provided timestamp.

Note the indexes, which can be used to determine the time difference, and perhaps warn users on the outer bounds.

Code generation is expensive, so avoid generating any you don't want to check against later.

array(5) {
  [-2]=>
  string(6) "366605"
  [-1]=>
  string(6) "611986"
  [0]=>
  string(6) "209523"
  [1]=>
  string(6) "535789"
  [2]=>
  string(6) "408835"
}

Your phone should produce one of these from the QR code above.

These are only valid for 30 seconds, so click the Secret link to get a new list.


lifefloat

Glossary

Base32 is an encoding, effectively an alphabet, that computers use made up of 32 characters.

Base32 Characters are A to Z (upper-case only), and 2 to 7.

HOTP is HMAC-based one-time password algorithm. HOTP Algorithms generate passwords from a given secret that do not expose the secret over time.

OATH is the short name for the Initiative for Open Authentication, an organisation dedicated to keeping secure authentication free.

OTP Auth stands for one-time password authentication.

QR Code (Quick Response Code) is a type of 2D matrix barcodes with built in redundancy, commonly used to scan links into mobile phones through cameras.

TOTP abbreviates Time-based One-time Password Algorithm. TOTP Algorithms generate passwords from a given secret that are only valid over a very specific time period.