69 lines
2.0 KiB
PHP
69 lines
2.0 KiB
PHP
<?php
|
|
namespace App\Core;
|
|
use OTPHP\TOTP;
|
|
use Endroid\QrCode\QrCode;
|
|
use Endroid\QrCode\Writer\PngWriter;
|
|
use Endroid\QrCode\Color\Color;
|
|
use Endroid\QrCode\Encoding\Encoding;
|
|
use Endroid\QrCode\ErrorCorrectionLevel;
|
|
use Endroid\QrCode\Label\Label;
|
|
use Endroid\QrCode\Label\LabelAlignment;
|
|
|
|
final class TwoFactorService
|
|
{
|
|
|
|
private const APLIKACE = "Test Auth2FA Kowalski";
|
|
|
|
public function getQrCodeDataUri(string $secret, string $username): string
|
|
{
|
|
$totp = TOTP::create($secret);
|
|
$totp->setLabel($username);
|
|
$totp->setIssuer(self::APLIKACE);
|
|
$qrContent = $totp->getProvisioningUri();
|
|
|
|
$writer = new PngWriter();
|
|
|
|
// VE VERZI 6.x POUŽÍVÁME NEW + METODY (Fluent interface stále funguje)
|
|
$qrCode = new QrCode(
|
|
data: $qrContent,
|
|
encoding: new Encoding('UTF-8'),
|
|
errorCorrectionLevel: ErrorCorrectionLevel::High,
|
|
size: 300,
|
|
margin: 10,
|
|
foregroundColor: new Color(0, 0, 0),
|
|
backgroundColor: new Color(255, 255, 255)
|
|
);
|
|
|
|
// Pokud chceš přidat label (text pod kód)
|
|
$label = new Label(
|
|
text: 'Naskenujte v aplikaci',
|
|
textColor: new Color(100, 100, 100),
|
|
alignment: LabelAlignment::Center // Zarovnání na střed
|
|
);
|
|
|
|
// Vygenerování výsledku
|
|
$result = $writer->write($qrCode, null, $label);
|
|
|
|
return $result->getDataUri();
|
|
}
|
|
|
|
public function verifyCode(string $secret, string $code): bool
|
|
{
|
|
return TOTP::create($secret)->verify($code);
|
|
}
|
|
|
|
public function generateSecret(): string
|
|
{
|
|
// Vygeneruje bezpečný náhodný Base32 secret (standard pro TOTP)
|
|
return TOTP::create()->getSecret();
|
|
}
|
|
|
|
public function getProvisioningUri(string $secret, string $username): string
|
|
{
|
|
$totp = TOTP::create($secret);
|
|
$totp->setLabel($username);
|
|
$totp->setIssuer(self::APLIKACE); // Název, který uživatel uvidí v aplikaci
|
|
|
|
return $totp->getProvisioningUri();
|
|
}
|
|
} |