PHP Luminova: Time-Based One-Time Password Authenticator
Time-based one-time password setup and verification in Luminova, including authenticator secrets and enrollment QR codes.
Luminova’s TOTP class handles authenticator enrollment and code verification. During enrollment, the application generates a secret and presents a QR code for the user to scan. At sign-in, it verifies the code against the user’s stored secret.
The Google Authenticator client is the default. A custom client can implement the AuthenticatorInterface.
Usage Examples
Create a TOTP Instance
use DateTimeZone;
use Luminova\Security\TOTP;
use Luminova\Cache\FileCache;
use Luminova\Security\Authenticator\Google;
// Initialize the TOTP authenticator
$auth = TOTP::create(
new Google(
'MySiteAuthenticator',
'[email protected]',
new DateTimeZone('Africa/Lagos'), // Optional timezone
new FileCache('authenticator') // Optional PSR-6 cache instance
)
);Setup TOTP for a New User
// Generate a secret and associate it with the user
$secret = $auth->secret(Google::generateSecret());
// Display the QR Code image and secret for the user
echo $auth->image();
echo "\nScan this QR Code\nYour Authenticator Secret: {$secret}";Verify User Secret and TOTP Code
$user_secret = 'EWQAIIFPZPDLFS2K'; // User's secret
$code = '123456'; // Example six-digit code supplied by the user
// Assign the user's secret to the authenticator
$auth->secret($user_secret);
// Validate the provided code
if ($auth->validate($code)) {
echo 'Token is valid';
} else {
echo 'Token is invalid';
}Class Definition
- Full namespace:
Luminova\Security\TOTP - This class is final.
Methods
create
Factory method to create a TOTP instance.
public static function create(?AuthenticatorInterface $client = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$client | Luminova\Interface\AuthenticatorInterface|null | Optional custom authenticator client. Defaults to a Google Authenticator client. |
Return:
self - Return a new TOTP instance.
secret
Get or set the secret key used for TOTP (Time-Based One-Time Password).
This method allows you to either retrieve the current secret key or set a new one.If a secret is provided, it will be set as the new secret key.If no secret is provided, the current secret key will be returned.
public function secret(?string $secret = null): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$secret | string|null | Optional. The secret key to set. If null, the method will retrieve the current secret. |
Return:
string|null - Returns the newly set secret key if a new secret was provided, or the current secret key if no new secret was given. Returns null if no secret is set and none was provided.
url
Generate the QR code URL for the authenticator.
public function url(): stringReturn:
string - Return the URL for the QR code.
Throws:
- Luminova\Exceptions\EncryptionException - If called without a valid authentication secret.
image
Generate the QR code image for the TOTP setup.
This method generates a QR code image for Time-Based One-Time Password (TOTP) setup.It can create the image in different formats and sizes, either using an installed QR code library or a fallback method using an external service.
public function image(int $size = 200, string $format = 'svg'): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$size | int | The size of the QR code image in pixels. Default is 200. |
$format | string | The desired format of the QR code image. Supported formats are 'png', 'svg', and 'base64'. Default is 'svg'. |
Return:
string - Return the QR code image as HTML element, depending on the chosen format and generation method.
Throws:
- Luminova\Exceptions\EncryptionException - If the 'base64' format is requested but the QR code library is not installed, or if called without a valid authentication secret.
label
Retrieve the label for the TOTP account.
public function label(): stringReturn:
string - Return the label for the account.
account
Retrieve the account identifier used for the TOTP.
public function account(): stringReturn:
string - Return the account identifier.
validate
Validate a TOTP code.
public function validate(string $code, int $discrepancy = 1, int $timeStep = 30): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$code | string | The TOTP code to validate. |
$discrepancy | int | The allowed time window (in steps) for validation. Default: 1. |
$timeStep | int | The time step in seconds for TOTP generation. Default: 30. |
Return:
bool - Return true if the code is valid; otherwise, false.
Throws:
- Luminova\Exceptions\EncryptionException - If called without a valid authentication secret or an invalid base32 character is found in secret.