-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathGenerateQrCodeAndSecretKey.php
54 lines (43 loc) · 1.44 KB
/
GenerateQrCodeAndSecretKey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace App\Actions\TwoFactorAuth;
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;
use App\Models\User;
use PragmaRX\Google2FA\Google2FA;
class GenerateQrCodeAndSecretKey
{
public string $companyName;
/**
* Generate new recovery codes for the user.
*
* @return array{string, string}
*/
public function __invoke($user): array
{
// Create a new Google2FA instance with explicit configuration
$google2fa = new Google2FA();
$google2fa->setOneTimePasswordLength(6);
// Generate a standard 16-character secret key
$secret_key = $google2fa->generateSecretKey(16);
// Set company name from config
$this->companyName = config('app.name', 'Laravel');
// Generate the QR code URL
$g2faUrl = $google2fa->getQRCodeUrl(
$this->companyName,
$user->email,
$secret_key
);
// Create the QR code image
$writer = new Writer(
new ImageRenderer(
new RendererStyle(400),
new SvgImageBackEnd()
)
);
// Generate the QR code as a base64 encoded SVG
$qrcode_image = base64_encode($writer->writeString($g2faUrl));
return [$qrcode_image, $secret_key];
}
}