Skip to content

Commit 217d696

Browse files
Jan-Schuppikjrauh01
authored andcommitted
Initial implementation
From #5397
1 parent ed546f2 commit 217d696

File tree

17 files changed

+1355
-20
lines changed

17 files changed

+1355
-20
lines changed

application/controllers/AccountController.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
namespace Icinga\Controllers;
55

66
use Icinga\Application\Config;
7+
use Icinga\Application\Icinga;
78
use Icinga\Authentication\User\UserBackend;
89
use Icinga\Data\ConfigObject;
910
use Icinga\Exception\ConfigurationError;
1011
use Icinga\Forms\Account\ChangePasswordForm;
12+
use Icinga\Forms\Account\TotpForm;
1113
use Icinga\Forms\PreferenceForm;
14+
use Icinga\Authentication\Totp;
1215
use Icinga\User\Preferences\PreferencesStore;
1316
use Icinga\Web\Controller;
17+
use Icinga\Web\Session;
1418

1519
/**
1620
* My Account
@@ -67,6 +71,28 @@ public function indexAction()
6771
}
6872
}
6973

74+
// form to add, remove, enable & disable 2FA via TOTP
75+
76+
if ($user->can('user/two-factor-authentication')) {
77+
if (isset($_POST['enabled_2fa'])) {
78+
Session::getSession()->set('enabled_2fa', $_POST['enabled_2fa'] == 1);
79+
}
80+
$totp = Session::getSession()->get('icingaweb_totp', null) ?? new Totp($user->getUsername());
81+
$totpForm = (new TotpForm())
82+
->setPreferences($user->getPreferences())
83+
->setTotp($totp)
84+
->setEnabled2FA(Session::getSession()->get('enabled_2fa', false));
85+
if (isset($config->config_resource)) {
86+
$totpForm->setStore(PreferencesStore::create(new ConfigObject(array(
87+
'resource' => $config->config_resource
88+
)), $user));
89+
}
90+
91+
$totpForm->handleRequest();
92+
93+
$this->view->totpForm = $totpForm;
94+
}
95+
7096
$form = new PreferenceForm();
7197
$form->setPreferences($user->getPreferences());
7298
if (isset($config->config_resource)) {

application/controllers/AuthenticationController.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
use Icinga\Application\Logger;
99
use Icinga\Common\Database;
1010
use Icinga\Exception\AuthenticationException;
11+
use Icinga\Forms\Authentication\Cancel2FAForm;
12+
use Icinga\Forms\Authentication\Challenge2FAForm;
1113
use Icinga\Forms\Authentication\LoginForm;
1214
use Icinga\Web\Controller;
1315
use Icinga\Web\Helper\CookieHelper;
1416
use Icinga\Web\RememberMe;
17+
use Icinga\Web\Session;
1518
use Icinga\Web\Url;
1619
use RuntimeException;
1720

@@ -41,7 +44,16 @@ public function loginAction()
4144
if (($requiresSetup = $icinga->requiresSetup()) && $icinga->setupTokenExists()) {
4245
$this->redirectNow(Url::fromPath('setup'));
4346
}
44-
$form = new LoginForm();
47+
48+
$user = $this->Auth()->getUser();
49+
if ($user !== null && $user->getTwoFactorEnabled()
50+
&& Session::getSession()->get('must_challenge_2fa_token', false) === true) {
51+
$form = new Challenge2FAForm();
52+
$cancel2faForm = new Cancel2FAForm();
53+
$cancel2faForm->handleRequest();
54+
} else {
55+
$form = new LoginForm();
56+
}
4557

4658
if (RememberMe::hasCookie() && $this->hasDb()) {
4759
$authenticated = false;
@@ -91,14 +103,10 @@ public function loginAction()
91103
->sendResponse();
92104
exit;
93105
}
94-
// FORM DOES NOT REDIRECT, IF USER HAS 2FA ENABLED and token hasn't been challenged
95106
$form->handleRequest();
96107
}
97-
// if ($user->has2FA() && irgendwas_mit_session()) {
98-
// // 2 FA form erstellen und zeigen und handeln
99-
// in der session speichern ob der token gepasst hat
100-
// }
101108
$this->view->form = $form;
109+
$this->view->cancel2faForm = $cancel2faForm ?? null;
102110
$this->view->defaultTitle = $this->translate('Icinga Web 2 Login');
103111
$this->view->requiresSetup = $requiresSetup;
104112
}

0 commit comments

Comments
 (0)