Skip to content

Core\Totp::check

Tests a one-time code that a user typed for two-factor login. The result is a step number when the code is right, and null when it is not.

Time is divided into steps of 30 seconds, and each step has its own code. A clock on a phone is often a few seconds wrong, so check also accepts the code of the step just before and just after the current one.

A code must never work twice. When check accepts a code, store the step number it returns with the account. Pass that number as $after on the next login. Then codes of that step and of earlier steps return null. A text that is not six digits also returns null, and does not throw an error.

The examples show a right code, the same code used twice, and a login form that stores the step number with the account.

Parameters

  • $codestringThe code the user typed. A string that is not six digits returns null and does not throw an error.
  • $secretsecret bytesThe same secret that code uses, at least 16 bytes long.
  • $afterintdefault 0The step number that the last accepted code returned. Codes of this step and earlier steps return null. The default is 0, which accepts every code of the current steps. Use it only for the first code of an account.

Return value

Returns ?int. The step number of the code, or null when the code is wrong, too old, too new or already used.

Errors

  • LogicError$secret is shorter than 16 bytes, or the clock is outside the range of dates a code can have.

Examples

A code from the phone

<?nvs
// Shows how to test a code that a user typed.
secret bytes $secret = Core\Random::bytes(20);
// `Core\Totp::code` returns the code that the phone shows right now.
string $typed = Core\Totp::code($secret);
// A right code returns a step number. A wrong code returns `null`.
// This prints "accepted" and then "rejected".
array<string> $attempts = [$typed, '12345x'];
foreach ($attempts as string $attempt) {
if (Core\Totp::check($attempt, $secret) == null) {
echo "rejected\n";
} else {
echo "accepted\n";
}
}
Output
accepted
rejected

The same code twice

<?nvs
// Shows how the step number stops a code from being used a second time.
secret bytes $secret = Core\Random::bytes(20);
string $typed = Core\Totp::code($secret);
// The first check accepts the code and returns its step number.
?int $step = Core\Totp::check($typed, $secret);
if ($step == null) {
echo "first time: rejected\n";
} else {
echo "first time: accepted\n";
// The second check gets that step number as `$after`.
// The same code is now too old, so the result is `null`.
if (Core\Totp::check($typed, $secret, $step) == null) {
echo "second time: rejected\n";
} else {
echo "second time: accepted\n";
}
}
Output
first time: accepted
second time: rejected

A login form

<?nvs
// Shows the second step of a login form, which stores the last step number with the account.
class Account
{
public int $lastStep = 0;
public function constructor(public secret bytes $secret)
{}
}
class Login
{
// Checks the code from the form. A right code saves its step number in the account.
public static function secondFactor(Account $account, string $typed): string
{
?int $step = Core\Totp::check($typed, $account->secret, $account->lastStep);
if ($step == null) {
return 'Wrong or used code. Please try again.';
}
$account->lastStep = $step;
return 'Welcome back.';
}
}
Account $account = new Account(Core\Random::bytes(20));
string $fromPhone = Core\Totp::code($account->secret);
// The user types a space by mistake, then the right code, then sends the form again.
// This prints the error message, "Welcome back." and the error message again.
array<string> $attempts = ['123 456', $fromPhone, $fromPhone];
foreach ($attempts as string $attempt) {
echo Login::secondFactor($account, $attempt), "\n";
}
Output
Wrong or used code. Please try again.
Welcome back.
Wrong or used code. Please try again.