diff --git a/src/OtpAuth/UriBuilder.php b/src/OtpAuth/UriBuilder.php index 2bea82b..5fa871b 100644 --- a/src/OtpAuth/UriBuilder.php +++ b/src/OtpAuth/UriBuilder.php @@ -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; @@ -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(); + } } \ No newline at end of file diff --git a/tests/OtpAuth/UriBuilderTest.php b/tests/OtpAuth/UriBuilderTest.php index 012b4c6..f6e79c8 100644 --- a/tests/OtpAuth/UriBuilderTest.php +++ b/tests/OtpAuth/UriBuilderTest.php @@ -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)