3 namespace BookStack\Auth\Access\Mfa;
5 use BaconQrCode\Renderer\Color\Rgb;
6 use BaconQrCode\Renderer\Image\SvgImageBackEnd;
7 use BaconQrCode\Renderer\ImageRenderer;
8 use BaconQrCode\Renderer\RendererStyle\Fill;
9 use BaconQrCode\Renderer\RendererStyle\RendererStyle;
10 use BaconQrCode\Writer;
11 use PragmaRX\Google2FA\Google2FA;
12 use PragmaRX\Google2FA\Support\Constants;
18 public function __construct(Google2FA $google2fa)
20 $this->google2fa = $google2fa;
21 // Use SHA1 as a default, Personal testing of other options in 2021 found
22 // many apps lack support for other algorithms yet still will scan
23 // the code causing a confusing UX.
24 $this->google2fa->setAlgorithm(Constants::SHA1);
28 * Generate a new totp secret key.
30 public function generateSecret(): string
32 /** @noinspection PhpUnhandledExceptionInspection */
33 return $this->google2fa->generateSecretKey();
37 * Generate a TOTP URL from secret key.
39 public function generateUrl(string $secret): string
41 return $this->google2fa->getQRCodeUrl(
49 * Generate a QR code to display a TOTP URL.
51 public function generateQrCodeSvg(string $url): string
53 $color = Fill::uniformColor(new Rgb(255, 255, 255), new Rgb(32, 110, 167));
57 new RendererStyle(192, 0, null, null, $color),
60 ))->writeString($url);
64 * Verify that the user provided code is valid for the secret.
65 * The secret must be known, not user-provided.
67 public function verifyCode(string $code, string $secret): bool
69 /** @noinspection PhpUnhandledExceptionInspection */
70 return $this->google2fa->verifyKey($secret, $code);