mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 04:04:45 +02:00
54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Users;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
|
|
|
class TwoFactorSetupService
|
|
{
|
|
public const VALID_BASE32_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
|
|
|
|
/**
|
|
* TwoFactorSetupService constructor.
|
|
*/
|
|
public function __construct(
|
|
private Encrypter $encrypter,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Generate a 2FA token and store it in the database before returning the
|
|
* QR code URL. This URL will need to be attached to a QR generating service in
|
|
* order to function.
|
|
*
|
|
* @throws \App\Exceptions\Model\DataValidationException
|
|
*/
|
|
public function handle(User $user): array
|
|
{
|
|
$secret = '';
|
|
try {
|
|
for ($i = 0; $i < config('panel.auth.2fa.bytes', 16); $i++) {
|
|
$secret .= substr(self::VALID_BASE32_CHARACTERS, random_int(0, 31), 1);
|
|
}
|
|
} catch (\Exception $exception) {
|
|
throw new \RuntimeException($exception->getMessage(), 0, $exception);
|
|
}
|
|
|
|
$user->totp_secret = $this->encrypter->encrypt($secret);
|
|
$user->save();
|
|
|
|
$company = urlencode(preg_replace('/\s/', '', config('app.name')));
|
|
|
|
return [
|
|
'image_url_data' => sprintf(
|
|
'otpauth://totp/%1$s:%2$s?secret=%3$s&issuer=%1$s',
|
|
rawurlencode($company),
|
|
rawurlencode($user->email),
|
|
rawurlencode($secret),
|
|
),
|
|
'secret' => $secret,
|
|
];
|
|
}
|
|
}
|