Files
2026-09-06 03:55:24 +08:00

52 lines
1.3 KiB
PHP

<?php
namespace App\Services;
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;
use PragmaRX\Google2FA\Google2FA;
class AdminGoogle2fa
{
private readonly Google2FA $google2fa;
public function __construct(?Google2FA $google2fa = null)
{
$this->google2fa = $google2fa ?? new Google2FA;
}
public function generateSecret(): string
{
return $this->google2fa->generateSecretKey();
}
public function verify(string $secret, string $code): bool
{
$code = trim($code);
if ($secret === '' || $code === '') {
return false;
}
return (bool) $this->google2fa->verifyKey($secret, $code);
}
public function otpAuthUrl(string $username, string $secret, string $label = 'admin'): string
{
$issuer = (string) config('app.name', 'Coruna Lab');
return $this->google2fa->getQRCodeUrl($issuer, $username.'@'.$label, $secret);
}
public function qrSvg(string $otpAuthUrl, int $size = 200): string
{
$writer = new Writer(new ImageRenderer(
new RendererStyle($size),
new SvgImageBackEnd
));
return $writer->writeString($otpAuthUrl);
}
}