sFire PHP Framework

Time-based One Time Password

sFire\OTP\Driver\TOTP is a driver for sFire\OTP\OTP.

In this section we will handle:

  • Setting the TOTP driver
  • Create an time-based one time password
  • Verify an time-based one time password
  • Setting driver options
  • Retrieving the provision url

Setting the TOTP driver

To set-up the TOTP driver, you need to inject the driver into a new instance of sFire\OTP\OTP. You can do so by calling the setDriver method.

Set driver
use sFire\OTP\OTP;

$otp = new OTP();
$otp -> setDriver('totp');

Create an time-based one time password

After setting the driver, you can create a time-based password

$otp = new OTP();
$otp -> setDriver('totp');
$otp -> setSecret('ABCDEFGHIJKLMNOP');
$password = $otp -> now();

var_dump($password); //Outputs similar to "574252"

The now method will use the current timestamp, but you may also set the timestamp yourself by calling the timestamp method to create a time-based on time password:

$password = $otp -> timestamp(time());

Verify an time-based one time password

You may use the verify method to verify if a given password is correct. This method takes two parameter. The first argument is the time-based one time password as an integer. The second optional parameter is an unix timestamp, if you want to verify an old or even a password in the future.

$otp = new OTP();
$otp -> setDriver('totp');
$otp -> setSecret('ABCDEFGHIJKLMNOP');
$password = $otp -> now();


$otp -> verify($password); //Outputs boolean true
$otp -> verify($password, time()); //Outputs boolean true

Setting driver options

You may set a couple of options:

Interval

You can set the expiration of an password in seconds by using the setInterval method:

$otp -> setInterval(40);

Digits

Even the amount of digits the password needs to contain can be set with the setDigits method:

$otp -> setDigits(8);

Algorithm

You may set the used algorithm with the setAlgorithm method:

$otp -> setAlgorithm('sha256');

Retrieving the provision url

You can use the provision url to create a QR code for mobile deviced to scan and add the account and secret automatically.

$otp = new OTP();
$otp -> setDriver('totp');
$otp -> setSecret('ABCDEFGHIJKLMNOP');
$url = $otp -> getProvisioningUrl('sFire');

var_dump($url); //Output similair to "otpauth://totp/sFire?secret=ABCDEFGHIJKLMNOP"