forked from PHPGangsta/GoogleAuthenticator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add QR code payload generation for TOTP
- Loading branch information
1 parent
438b6c2
commit 149f38b
Showing
4 changed files
with
53 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ For a secure installation you have to make sure that used codes cannot be reused | |
- Built-in validation methods. | ||
- Secret key generation for TOTP. | ||
- Compatible with Google Authenticator and similar apps. | ||
- Generate QR code payloads for easy integration with TOTP applications. | ||
|
||
## Installation | ||
|
||
|
@@ -67,6 +68,18 @@ $secret = $ga->createSecret(); | |
echo "Your secret key is: " . $secret; | ||
``` | ||
|
||
### Generating a QR Code Payload | ||
|
||
To generate a QR code payload for a TOTP secret, use the `getQrCodePayload` function: | ||
|
||
```php | ||
$label = '[email protected]'; // The user's email or username | ||
$issuer = 'MyApp'; // Optional issuer name | ||
|
||
$qrcodePayload = $totp->getQrCodePayload($secret, $label, $issuer); | ||
echo "QR Code Payload: " . $qrcodePayload . "\n"; | ||
``` | ||
|
||
### Validating a TOTP Code | ||
|
||
You can validate a TOTP code using the `verifyCode` method: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,20 @@ public function testCreateSecretInvalidLengthThrowsException() | |
$this->totp->createSecret(10); // Invalid length less than 16 | ||
} | ||
|
||
public function testGetQrCodePayload() | ||
{ | ||
$secret = $this->totp->createSecret(); | ||
$label = '[email protected]'; | ||
$issuer = 'MyApp'; | ||
|
||
$payload = $this->totp->getQrCodePayload($secret, $label, $issuer); | ||
|
||
$this->assertStringStartsWith('otpauth://totp/', $payload); | ||
$this->assertStringContainsString('totp/' . $label, $payload); | ||
$this->assertStringContainsString('issuer=' . $issuer, $payload); | ||
$this->assertStringContainsString('secret=' . $secret, $payload); | ||
} | ||
|
||
public function testGetCode() | ||
{ | ||
$secret = $this->totp->createSecret(); | ||
|