76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
namespace App\Presentation\Disable2fa;
|
|
|
|
use App\Core\AppPresenter;
|
|
use App\Core\TwoFactorService;
|
|
use App\Model\UzivatelFacade;
|
|
use Nette;
|
|
use Nette\Application\UI\Form;
|
|
use App\Model\Login\UserIdentity;
|
|
|
|
final class Disable2faPresenter extends AppPresenter
|
|
{
|
|
public function __construct(
|
|
private UzivatelFacade $userFacade,
|
|
private TwoFactorService $twoFactorService
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
public function renderDefault(): void
|
|
{
|
|
/**
|
|
* Uživatelská identita = přihlášený user.
|
|
* @var UserIdentity
|
|
*/
|
|
$identity = $this->getUser()->getIdentity();
|
|
|
|
if (!$identity->twoFactor) {
|
|
$this->flashMessage('Dvoufázové ověření nemáte aktivní.', 'info');
|
|
$this->redirect(':Home:');
|
|
}
|
|
}
|
|
|
|
protected function createComponentDisableForm(): Form
|
|
{
|
|
$form = new Form;
|
|
|
|
$form->addText('code', 'Ověřovací kód:')
|
|
->setRequired('Zadejte prosím kód z vaší aplikace.')
|
|
->addRule($form::Pattern, 'Kód musí mít 6 číslic', '\d{6}')
|
|
->setHtmlAttribute('placeholder', '000 000')
|
|
->setHtmlAttribute('autocomplete', 'one-time-code');
|
|
|
|
$form->addSubmit('disable', 'Potvrdit a vypnout 2FA')
|
|
->getControlPrototype()->class('btn btn-danger btn-lg w-100');
|
|
|
|
$form->onSuccess[] = $this->disableFormSucceeded(...);
|
|
|
|
return $form;
|
|
}
|
|
|
|
public function disableFormSucceeded(Form $form, \stdClass $data): void
|
|
{
|
|
/**
|
|
* Uživatelská identita = přihlášený user.
|
|
* @var UserIdentity
|
|
*/
|
|
$identity = $this->getUser()->getIdentity();
|
|
|
|
// Načteme secret z identity (předpokládáme, že tam je z přihlášení)
|
|
$secret = $identity->totpSecret;
|
|
|
|
if ($this->twoFactorService->verifyCode($secret, $data->code)) {
|
|
// 1. Zrušení v DB
|
|
$this->userFacade->disableTOTPSecret();
|
|
|
|
// 2. Aktualizace identity v session
|
|
$identity->twoFactor = false;
|
|
$identity->totpSecret = null;
|
|
|
|
$this->flashMessage('Dvoufázové ověření bylo úspěšně vypnuto.', 'success');
|
|
$this->redirect(':Home:');
|
|
} else {
|
|
$form->addError('Zadaný kód není správný. Zkuste to znovu.');
|
|
}
|
|
}
|
|
} |