Luminova Framework

PHP Luminova: Time-Based One-Time Password: Google Authenticator Client

Last updated: 2026-11-28 20:57:12

Luminova’s Google Authenticator-compatible TOTP client, including secret generation, enrollment URLs, code verification, and optional caching.

The Google client generates Base32 secrets, builds enrollment URLs for authenticator apps, and verifies time-based one-time passwords against the stored secret. An optional cache records recently accepted codes to help prevent reuse.

The client implements AuthenticatorInterface, the contract used by Luminova’s TOTP class.

Google Authenticator Client

The Google Authenticator client allow the generation of secure authentication secrets, QR code creation, and TOTP validation. It offers flexibility through customizable account details, issuer names, time zones, and caching mechanisms. By adhering to PSR standards, it integrates with various cache implementations, ensuring one-time code usage for enhanced security.

Key features include:

  • Generating Base32-encoded secrets for authentication.
  • Creating QR codes for easy configuration with TOTP apps.
  • Verifying user-provided codes against the stored secret.
  • Flexible support for time zones and caching to prevent code reuse.
  • Compliance with PSR caching interfaces and the Luminova base cache.

Usage Examples

Initializing the Authenticator

You can create a Google authenticator instance by providing account details, issuer name, and optional time zone or caching instance.

use Luminova\Cache\FileCache;
use Luminova\Security\Authenticator\Google;

$authenticator = new Google(
    '[email protected]',       // Account name (email or username)
    'MyAppName',              // Issuer (your application name)
    new DateTimeZone('UTC'),  // Optional timezone
    new FileCache('totp')     // Optional PSR-compliant cache instance
);

Generating a Secret

Generate a secure Base32-encoded secret key for a new user. The secret is stored and can be used to generate or validate TOTP codes.

$secret = Google::generateSecret(24);
echo "User Secret: {$secret}";

Note: Generate a Base32 secret key of a specific length (must be divisible by 8):


Creating a QR Code

Generate a QR code URL for easy configuration in Google Authenticator or similar apps. The QR code encodes the secret and account details.

$secret = Google::generateSecret();
$authenticator->setSecret($secret); // Generate new secret for user
$qrCodeUrl = $authenticator->getQRCodeUrl();

echo "<img src='{$qrCodeUrl}' alt='Scan this QR Code to configure your authenticator'>";

Validating a Code

Verify a TOTP code provided by the user. Specify optional parameters like time-step duration and allowed discrepancies for added flexibility.

$userProvidedCode = '123456';
$authenticator->setSecret('USER_OLD_SECRET123'); // Set user own secret
$isValid = $authenticator->verify($userProvidedCode);

if ($isValid) {
    echo "Authentication successful!";
} else {
    echo "Invalid code. Please try again.";
}

Updating Secret, Timezone, or Cache

The authenticator instance can be updated dynamically to set a new secret, time zone, or caching instance:

$authenticator->setSecret('NEWSECRET123');
$authenticator->setTimezone(new DateTimeZone('America/New_York'));
$authenticator->setCache(new FileCache('totp-storage'));

Class Definition


Methods

__construct

Initialize a Google Authenticator instance with the necessary parameters.

public function __construct(
    private string $accountName,
    private string $issuer,
    private DateTimeZone|string|null $timezone = null,
    private CacheItemPoolInterface|CacheInterface|AbstractCache|null $cache = null
)

Parameters:

ParameterTypeDescription
$accountNamestringThe account name (e.g., user email or username).
$issuerstringThe issuer's name (e.g., your app or website name).
$timezoneDateTimeZone|string|nullThe timezone for time-based calculations (optional).
$cacheCacheItemPoolInterface|CacheInterface|AbstractCache|nullThe instance of PSR cache or Luminova base-cache for preventing code reuse (optional).

Throws:


getLabel

Get the label used in the TOTP configuration, optionally URL-encoded.

public function getLabel(bool $encode = false): string

Parameters:

ParameterTypeDescription
$encodeboolWhether to URL-encode the label.

Return:

string - Return the label in the format 'Issuer:AccountName'.


getAccount

Get the account name associated with this instance.

public function getAccount(): string

Return:

string - Return the account name.


getSecret

Get the shared secret key.

public function getSecret(): ?string

Return:

?string - The configured secret, or null before one is set.


getQRCodeUrl

Generate a QR code URL for configuring Google Authenticator.

public function getQRCodeUrl(): string

Return:

string - Return the URL for the QR code.

Throws:


setSecret

Initialize a Google Authenticator instance with the necessary parameters.

public function setSecret(string $secret): self

Parameters:

ParameterTypeDescription
$secretstringThe shared secret key for TOTP generation.

Return:

Luminova\Interface\AuthenticatorInterface - Return the current instance of authenticator client.


setTimezone

Set the timezone for time-based calculations.

public function setTimezone(DateTimeZone|string|null $timezone): self

Parameters:

ParameterTypeDescription
$timezone\DateTimeZone|string|nullThe new timezone.

Return:

Luminova\Interface\AuthenticatorInterface - Return the current instance of authenticator client.


setCache

Set the cache instance for code reuse prevention.

public function setCache(
    CacheItemPoolInterface|CacheInterface|AbstractCache $cache
): self

Parameters:

ParameterTypeDescription
$cacheCacheItemPoolInterface|CacheInterface|AbstractCacheThe instance of PSR cache or Luminova base-cache.

Return:

Luminova\Interface\AuthenticatorInterface - Return the current instance of authenticator client.


generateSecret

Generate a new Base32-encoded secret key.

public static function generateSecret(int $length = 16): string

Parameters:

ParameterTypeDescription
$lengthintThe desired length of the secret (default: 16).

Return:

string - Return the generated Base32 secret key.

Throws:


verify

Verify a given TOTP code against the shared secret.

public function verify(string $code, int $discrepancy = 1, int $timeStep = 30): bool

Parameters:

ParameterTypeDescription
$codestringThe user-provided TOTP code.
$discrepancyintThe allowed time-step discrepancy (default: 1).
$timeStepintThe time step duration in seconds (default: 30).

Return:

bool - Return true if the code is valid, false otherwise.

Throws: