Debian Codegraph @

templates/login2Factor.php

raw
1 <?php
2 namespace LAM\LOGIN;
3 use Exception;
4 use htmlJavaScript;
5 use htmlResponsiveInputCheckbox;
6 use htmlStatusMessage;
7 use LAM\LIB\TWO_FACTOR\TwoFactorProviderService;
8 use htmlResponsiveRow;
9 use htmlGroup;
10 use htmlOutputText;
11 use htmlSpacer;
12 use htmlSelect;
13 use htmlInputField;
14 use htmlButton;
15 use LAMConfig;
16 use LAMException;
17
18 /*
19
20 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
21 Copyright (C) 2017 - 2026 Roland Gruber
22
23 This program is free software; you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation; either version 2 of the License, or
26 (at your option) any later version.
27
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public License
34 along with this program; if not, write to the Free Software
35 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36
37 */
38
39 /**
40 * This page redirects to the correct start page after checking 2nd factor.
41 *
42 * @package main
43 * @author Roland Gruber
44 */
45
46 /** config object */
47 include_once __DIR__ . '/../lib/config.inc';
48
49 // start session
50 startSecureSession();
51
52 setlanguage();
53
54 $config = $_SESSION['config'];
55 $password = $_SESSION['ldap']->getPassword();
56 $user = $_SESSION['ldap']->getUserName();
57
58 if (!($config instanceof LAMConfig)) {
59 logNewMessage(LOG_ERR, 'Config missing');
60 die();
61 }
62
63 // get serials
64 try {
65 $service = new TwoFactorProviderService($config);
66 $provider = $service->getProvider();
67 if ($provider->supportsToRememberDevice() && $service->isValidRememberedDevice($user)) {
68 unset($_SESSION['2factorRequired']);
69 metaRefresh("main.php");
70 die();
71 }
72 $serials = $provider->getSerials($user, $password);
73 }
74 catch (Exception $e) {
75 logNewMessage(LOG_ERR, 'Unable to get 2-factor serials for ' . $user . ' ' . $e->getMessage());
76 printHeader();
77 $scriptTag = new htmlJavaScript('window.lam.dialog.showErrorMessageAndRedirect("' . _("Unable to start 2-factor authentication.") . '", "", "' . _('Ok') . '", "login.php")');
78 parseHtml(null, $scriptTag, [], false, null);
79 printFooter();
80 die();
81 }
82
83 $twoFactorLabelConfig = $config->getTwoFactorAuthenticationLabel();
84 $twoFactorLabel = empty($twoFactorLabelConfig) ? _('PIN+Token') : $twoFactorLabelConfig;
85
86 if (isset($_POST['logout'])) {
87 // destroy session
88 session_destroy();
89 unset($_SESSION);
90 // redirect to login page
91 metaRefresh("login.php");
92 exit();
93 }
94
95 if (empty($serials) && $config->getTwoFactorAuthenticationOptional()) {
96 unset($_SESSION['2factorRequired']);
97 metaRefresh("main.php");
98 die();
99 }
100
101 if (empty($serials)) {
102 printHeader();
103 $scriptTag = new htmlJavaScript('window.lam.dialog.showErrorMessageAndRedirect("' . _("Unable to start 2-factor authentication because no tokens were found.") . '", "", "' . _('Ok') . '", "login.php")');
104 parseHtml(null, $scriptTag, [], false, null);
105 printFooter();
106 die();
107 }
108
109 if (isset($_POST['submit']) || isset($_POST['sig_response']) // WebAuthn
110 || (isset($_GET['state']) && isset($_GET['code'])) // Okta
111 || (isset($_GET['state']) && isset($_GET['duo_code'])) // Duo
112 || (isset($_GET['session_state']) && isset($_GET['redirect_uri']))) { // OpenID
113 $twoFactorInput = $_POST['2factor'] ?? null;
114 $serial = $_POST['serial'] ?? null;
115 if (!$provider->hasCustomInputForm() && (empty($twoFactorInput) || !in_array($serial, $serials))) {
116 $errorMessage = sprintf(_('Please enter "%s".'), $twoFactorLabel);
117 header("HTTP/1.1 403 Forbidden");
118 }
119 else {
120 $twoFactorValid = false;
121 try {
122 $twoFactorValid = $provider->verify2ndFactor($user, $password, $serial, $twoFactorInput);
123 if ($twoFactorValid
124 && $provider->supportsToRememberDevice()
125 && isset($_POST['rememberDevice'])
126 && ($_POST['rememberDevice'] === 'on')) {
127 $service->rememberDevice($user);
128 }
129 }
130 catch (Exception $e) {
131 $errorMessage = $e->getMessage();
132 if ($e->getPrevious() !== null) {
133 $errorMessage .= ' - ' . $e->getPrevious()->getMessage();
134 }
135 logNewMessage(LOG_WARNING, '2-factor verification failed: ' . $errorMessage);
136 header("HTTP/1.1 403 Forbidden");
137 }
138 if ($twoFactorValid) {
139 unset($_SESSION['2factorRequired']);
140 metaRefresh("main.php");
141 die();
142 }
143 else {
144 $errorMessage = _('Verification failed.');
145 }
146 }
147 }
148
149 /**
150 * Prints the page header.
151 */
152 function printHeader(): void {
153 echo $_SESSION['header'];
154 printHeaderContents(_("Login"), '..');
155 echo '</head><body>';
156 // include all JavaScript files
157 printJsIncludes('..');
158 ?>
159 <div id="lam-topnav" class="lam-header">
160 <div class="lam-header-left lam-menu-stay">
161 <a href="https://www.ldap-account-manager.org/" target="new_window">
162 <img class="align-middle" width="24" height="24" alt="help" src="../graphics/logo.svg">
163 <span class="hide-on-mobile">
164 <?php
165 echo getLAMVersionText();
166 ?>
167 </span>
168 </a>
169 </div>
170 </div>
171 <?php
172 }
173
174 /**
175 * Prints the page footer.
176 */
177 function printFooter(): void {
178 echo '</body></html>';
179 }
180
181 printHeader();
182
183
184 ?>
185 <br><br>
186 <form id="2faform" enctype="multipart/form-data" action="login2Factor.php" method="post" autocomplete="off">
187 <?php
188 echo $config->getTwoFactorAuthenticationCaption();
189
190 ?>
191 <div class="centeredTable">
192 <div class="roundedShadowBox limitWidth">
193 <?php
194
195 $group = new htmlGroup();
196 $row = new htmlResponsiveRow();
197 // error
198 if (!empty($errorMessage)) {
199 $row->add(new htmlStatusMessage('ERROR', $errorMessage));
200 $row->add(new htmlSpacer('1em', '1em'));
201 }
202
203 if (!$provider->hasCustomInputForm()) {
204 // serial
205 $row->add(new htmlOutputText(_('Serial number')), 12, 12, 12, 'text-left');
206 $serialSelect = new htmlSelect('serial', $serials);
207 $row->add($serialSelect);
208 // token
209 $row->add(new htmlOutputText($twoFactorLabel), 12, 12, 12, 'text-left');
210 $twoFactorInput = new htmlInputField('2factor', '');
211 $twoFactorInput->setFieldSize(null);
212 $twoFactorInput->setIsPassword(true);
213 $row->add($twoFactorInput);
214 }
215 else {
216 try {
217 $provider->addCustomInput($row, $user);
218 }
219 catch (LAMException $e) {
220 logNewMessage(LOG_ERR, 'Error rendering 2FA form. ' . $e->getTitle());
221 $row->add(new htmlStatusMessage('ERROR', _('Unable to start 2-factor authentication.')));
222 }
223 }
224
225 // buttons
226 $row->add(new htmlSpacer('1em', '1em'));
227 if ($provider->supportsToRememberDevice()) {
228 $remember = new htmlResponsiveInputCheckbox('rememberDevice', false, _('Remember device'), '560');
229 $remember->setCSSClasses(['lam-save-selection']);
230 $row->add($remember);
231 $row->add(new htmlSpacer('0.5em', '0.5em'));
232 }
233 if ($provider->isShowSubmitButton()) {
234 $submit = new htmlButton('submit', _("Submit"));
235 $submit->setCSSClasses(['fullwidth']);
236 $row->add($submit, 12, 12, 12, 'fullwidth');
237 $row->add(new htmlSpacer('0.5em', '0.5em'));
238 }
239 $logout = new htmlButton('logout', _("Cancel"));
240 $logout->setCSSClasses(['fullwidth']);
241 $row->add($logout);
242 $group->addElement($row);
243
244 addSecurityTokenToMetaHTML($group);
245 parseHtml(null, $group, [], false, 'user');
246
247 ?>
248 </div>
249 </div>
250 </form>
251 <br><br>
252
253 <script type="text/javascript">
254 myElement = document.getElementsByName('2factor')[0];
255 if (myElement) {
256 myElement.focus();
257 }
258 </script>
259
260 <?php
261 printFooter();