Add: cancel button for 2fa challenge

This commit is contained in:
Jan Schuppik 2025-07-24 21:48:36 +02:00
parent dd2eefa50f
commit b8cc14dc35
3 changed files with 62 additions and 6 deletions

View File

@ -8,6 +8,7 @@ use Icinga\Application\Icinga;
use Icinga\Application\Logger; use Icinga\Application\Logger;
use Icinga\Common\Database; use Icinga\Common\Database;
use Icinga\Exception\AuthenticationException; use Icinga\Exception\AuthenticationException;
use Icinga\Forms\Authentication\Cancel2FAForm;
use Icinga\Forms\Authentication\Challenge2FAForm; use Icinga\Forms\Authentication\Challenge2FAForm;
use Icinga\Forms\Authentication\LoginForm; use Icinga\Forms\Authentication\LoginForm;
use Icinga\Web\Controller; use Icinga\Web\Controller;
@ -45,12 +46,14 @@ class AuthenticationController extends Controller
} }
$user = $this->Auth()->getUser(); $user = $this->Auth()->getUser();
$form = ($user !== null if ($user !== null && $user->getTwoFactorEnabled()
&& $user->getTwoFactorEnabled() && Session::getSession()->get('must_challenge_2fa_token', false) === true) {
&& Session::getSession()->get('must_challenge_2fa_token', false) === true) $form = new Challenge2FAForm();
? new Challenge2FAForm() $cancel2faForm = new Cancel2FAForm();
: new LoginForm(); $cancel2faForm->handleRequest();
} else {
$form = new LoginForm();
}
if (RememberMe::hasCookie() && $this->hasDb()) { if (RememberMe::hasCookie() && $this->hasDb()) {
$authenticated = false; $authenticated = false;
@ -103,6 +106,7 @@ class AuthenticationController extends Controller
$form->handleRequest(); $form->handleRequest();
} }
$this->view->form = $form; $this->view->form = $form;
$this->view->cancel2faForm = $cancel2faForm ?? null;
$this->view->defaultTitle = $this->translate('Icinga Web 2 Login'); $this->view->defaultTitle = $this->translate('Icinga Web 2 Login');
$this->view->requiresSetup = $requiresSetup; $this->view->requiresSetup = $requiresSetup;
} }

View File

@ -0,0 +1,51 @@
<?php
namespace Icinga\Forms\Authentication;
use Icinga\Web\Form;
use Icinga\Web\Session;
use Icinga\Web\Url;
class Cancel2FAForm extends Form
{
/**
* {@inheritdoc}
*/
public function init()
{
$this->setRequiredCue(null);
$this->setName('form_cancel_2fa');
$this->setSubmitLabel($this->translate('Cancel'));
$this->setProgressLabel($this->translate('Canceling'));
$this->setAttrib('class', 'content-centered');
}
/**
* {@inheritdoc}
*/
public function createElements(array $formData)
{
$this->addElement(
'hidden',
'redirect',
[
'value' => Url::fromRequest()->getParam('redirect')
]
);
$this->addElement(
'hidden',
'cancel_2fa',
[
'value' => true
]
);
}
public function onSuccess()
{
Session::getSession()->purge();
return true;
}
}

View File

@ -22,6 +22,7 @@
) ?></p> ) ?></p>
<?php endif ?> <?php endif ?>
<?= $this->form ?> <?= $this->form ?>
<?= $this->cancel2faForm ?>
<div id="login-footer"> <div id="login-footer">
<p>Icinga Web 2 &copy; 2013-<?= date('Y') ?></p> <p>Icinga Web 2 &copy; 2013-<?= date('Y') ?></p>
<?= $this->qlink($this->translate('icinga.com'), 'https://icinga.com') ?> <?= $this->qlink($this->translate('icinga.com'), 'https://icinga.com') ?>