Skip to content

Commit

Permalink
UriBuilder: Add getQRCodeDataUri method
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpanderson committed Jan 31, 2024
1 parent 35d58bb commit c98adc4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/OtpAuth/UriBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Vectorface\OtpAuth;

use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Writer\PngWriter;
use Vectorface\OtpAuth\Paramters\Algorithm;
use Vectorface\OtpAuth\Paramters\Type;

Expand Down Expand Up @@ -136,4 +138,15 @@ public function __toString(): string
{
return $this->getUri();
}

public function getQRCodeDataUri(): string
{
return Builder::create()
->data($this->getUri())
->writer(new PngWriter)
->size(260)
->margin(10)
->build()
->getDataUri();
}
}
4 changes: 4 additions & 0 deletions tests/OtpAuth/UriBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ public function synopsis()
->account("MyAcct")
->secret("FOO");

/* The builder can generate otpauth URLs */
$this->assertEquals("otpauth://totp/MyAcct?secret=FOO", "$uriBuilder");

/* ... or QR codes as data URIs */
$this->assertStringStartsWith("data:image/png;base64,", $uriBuilder->getQRCodeDataUri());

/* It is also possible to construct complex OTP URIs, including HOTP */
$uriBuilder = (new UriBuilder())
->type(Type::HOTP)
Expand Down

0 comments on commit c98adc4

Please sign in to comment.