Debian Codegraph @

templates/tools/webauthn.php

raw
1 <?php
2 namespace LAM\TOOLS\WEBAUTHN;
3 use \htmlButton;
4 use \htmlDiv;
5 use \htmlGroup;
6 use htmlHiddenInput;
7 use htmlInputField;
8 use \htmlOutputText;
9 use \htmlResponsiveRow;
10 use \htmlResponsiveTable;
11 use \htmlSpacer;
12 use \htmlStatusMessage;
13 use \htmlTitle;
14 use LAM\LOGIN\WEBAUTHN\WebauthnManager;
15 use LAM_INTERFACE;
16 use Webauthn\PublicKeyCredentialCreationOptions;
17
18 /*
19
20 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
21 Copyright (C) 2020 - 2025 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 * Allows webauthn device management.
41 *
42 * @author Roland Gruber
43 * @package tools
44 */
45
46 /** security functions */
47 include_once(__DIR__ . "/../../lib/security.inc");
48 /** access to configuration options */
49 include_once(__DIR__ . "/../../lib/config.inc");
50 /** webauthn */
51 include_once __DIR__ . '/../../lib/webauthn.inc';
52
53 // start session
54 startSecureSession();
55 enforceUserIsLoggedIn();
56 validateSecurityToken();
57
58 checkIfToolIsActive('toolWebauthn');
59
60 setlanguage();
61
62 include __DIR__ . '/../../lib/adminHeader.inc';
63 echo '<div class="smallPaddingContent">';
64 echo "<form id='webauthnform' action=\"webauthn.php\" method=\"post\">\n";
65 $container = new htmlResponsiveRow();
66
67 $container->add(new htmlTitle(_("WebAuthn devices")), 12);
68
69 $webauthnManager = new WebauthnManager();
70
71 $userDn = $_SESSION['ldap']->getUserName();
72 $database = $webauthnManager->getDatabase();
73 showRemoveMessage($container);
74 addNewDevice($container, $webauthnManager);
75 $container->addVerticalSpacer('0.5rem');
76 $container->add(new htmlHiddenInput('registrationData', ''), 12);
77 $errorMessageDiv = new htmlDiv('generic-webauthn-error', new htmlOutputText(''));
78 $errorMessageDiv->setCSSClasses(['hidden']);
79 $errorMessageDiv->addDataAttribute('button', _('Ok'));
80 $errorMessageDiv->addDataAttribute('title', _('WebAuthn failed'));
81 $container->add($errorMessageDiv, 12);
82 $buttonGroup = new htmlGroup();
83 $registerButton = new htmlButton('register', _('Register new device'));
84 $registerButton->addDataAttribute('dn', $userDn);
85 $registerButton->addDataAttribute('sec_token_value', getSecurityTokenValue());
86 $registerButton->addDataAttribute('sec_token_name', getSecurityTokenName());
87 $registration = $webauthnManager->getRegistrationObject($userDn, LAM_INTERFACE::ADMIN);
88 $registrationJson = json_encode($registration, JSON_THROW_ON_ERROR);
89 $_SESSION['webauthn_registration'] = $registrationJson;
90 $registerButton->addDataAttribute('publickey', $registrationJson);
91 $registerButton->setCSSClasses(['lam-primary']);
92 $registerButton->setOnClick('window.lam.webauthn.registerOwnDevice(event, false);');
93 $buttonGroup->addElement($registerButton);
94 $container->add($buttonGroup, 12);
95 $container->addVerticalSpacer('2rem');
96 $results = $database->searchDevices($userDn);
97 if (empty($results)) {
98 $container->add(new htmlStatusMessage('INFO', _('No devices found.')), 12);
99 }
100 else {
101 $titles = [
102 _('Name'),
103 _('Save'),
104 _('Registration'),
105 _('Last use'),
106 _('Delete')
107 ];
108 $data = [];
109 $id = 0;
110 foreach ($results as $result) {
111 $credentialId = $result['credentialId'];
112 $delButton = new htmlButton('deleteDevice' . $id, 'del.svg', true);
113 $delButton->addDataAttribute('credential', $credentialId);
114 $delButton->addDataAttribute('dn', $result['dn']);
115 $delButton->addDataAttribute('dialogtitle', _('Remove device'));
116 $delButton->addDataAttribute('oktext', _('Ok'));
117 $delButton->addDataAttribute('canceltext', _('Cancel'));
118 $delButton->setOnClick('window.lam.webauthn.removeOwnDevice(event, false);');
119 $saveButton = new htmlButton('saveDevice' . $id, 'save.svg', true);
120 $saveButton->addDataAttribute('credential', $credentialId);
121 $saveButton->addDataAttribute('dn', $result['dn']);
122 $saveButton->addDataAttribute('nameelement', 'deviceName_' . $id);
123 $saveButton->setOnClick('window.lam.webauthn.updateOwnDeviceName(event);');
124 $nameField = new htmlInputField('deviceName_' . $id, (string) $result['name']);
125 $nameFieldClasses = ['maxwidth20'];
126 if (!empty($_GET['updated']) && ($_GET['updated'] === $credentialId)) {
127 $nameFieldClasses[] = 'markPass';
128 }
129 $nameField->setCSSClasses($nameFieldClasses);
130 $data[] = [
131 $nameField,
132 $saveButton,
133 new htmlOutputText(date('Y-m-d H:i:s', $result['registrationTime'])),
134 new htmlOutputText(date('Y-m-d H:i:s', $result['lastUseTime'])),
135 $delButton
136 ];
137 $id++;
138 }
139 $table = new htmlResponsiveTable($titles, $data);
140 $tableDiv = new htmlDiv('webauthn_results', $table);
141 $tableDiv->addDataAttribute('sec_token_value', getSecurityTokenValue());
142 $container->add($tableDiv, 12);
143 }
144 $container->addVerticalSpacer('2rem');
145
146 $confirmationDiv = new htmlDiv('webauthnDeleteConfirm', new htmlOutputText(_('Do you really want to remove this device?')), ['hidden']);
147 $container->add($confirmationDiv, 12);
148
149 addSecurityTokenToMetaHTML($container);
150
151 parseHtml(null, $container, [], false, 'user');
152
153 echo '</form>';
154 echo '</div>';
155 include __DIR__ . '/../../lib/adminFooter.inc';
156
157 /**
158 * Checks if a new device should be registered and adds it.
159 *
160 * @param htmlResponsiveRow $container row
161 * @param WebauthnManager $webauthnManager webauthn manager
162 */
163 function addNewDevice(htmlResponsiveRow $container, WebauthnManager $webauthnManager): void {
164 if (empty($_POST['registrationData'])) {
165 return;
166 }
167 $registrationData = base64_decode($_POST['registrationData']);
168 $registrationObject = PublicKeyCredentialCreationOptions::createFromString((string) $_SESSION['webauthn_registration']);
169 $success = $webauthnManager->storeNewRegistration($registrationObject, $registrationData);
170 if ($success) {
171 $container->add(new htmlStatusMessage('INFO', _('The device was registered.')), 12);
172 }
173 else {
174 $container->add(new htmlStatusMessage('ERROR', _('The device failed to register.')), 12);
175 }
176 }
177
178 /**
179 * Shows the message if a device was removed.
180 *
181 * @param htmlResponsiveRow $container row
182 */
183 function showRemoveMessage(htmlResponsiveRow $container): void {
184 if (!empty($_POST['removed']) && ($_POST['removed'] === 'true')) {
185 $container->add(new htmlStatusMessage('INFO', _('The device was deleted.')), 12);
186 }
187 }