Debian Codegraph @

templates/tools/webauthn.php

raw symbols SCIP
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 htmlStatusMessage;
12 use htmlTitle;
13 use LAM\LOGIN\WEBAUTHN\WebauthnManager;
14 use LAM_INTERFACE;
15 use Webauthn\PublicKeyCredentialCreationOptions;
16
17 /*
18
19 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
20 Copyright (C) 2020 - 2026 Roland Gruber
21
22 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35
36 */
37
38 /**
39 * Allows webauthn device management.
40 *
41 * @author Roland Gruber
42 * @package tools
43 */
44
45 /** security functions */
46 include_once(__DIR__ . "/../../lib/security.inc");
47 /** access to configuration options */
48 include_once(__DIR__ . "/../../lib/config.inc");
49 /** webauthn */
50 include_once __DIR__ . '/../../lib/webauthn.inc';
51
52 // start session
53 startSecureSession();
54 enforceUserIsLoggedIn();
55 validateSecurityToken();
56
57 checkIfToolIsActive('toolWebauthn');
58
59 setlanguage();
60
61 include_once __DIR__ . '/../../lib/adminHeader.inc';
62 echo '<div class="smallPaddingContent">';
63 echo "<form id='webauthnform' action=\"webauthn.php\" method=\"post\">\n";
64 $container = new htmlResponsiveRow();
65
66 $container->add(new htmlTitle(_("WebAuthn devices")));
67
68 $webauthnManager = new WebauthnManager();
69
70 $userDn = $_SESSION['ldap']->getUserName();
71 $database = $webauthnManager->getDatabase();
72 showRemoveMessage($container);
73 addNewDevice($container, $webauthnManager);
74 $container->addVerticalSpacer('0.5rem');
75 $container->add(new htmlHiddenInput('registrationData', ''));
76 $errorMessageDiv = new htmlDiv('generic-webauthn-error', new htmlOutputText(''));
77 $errorMessageDiv->setCSSClasses(['hidden']);
78 $errorMessageDiv->addDataAttribute('button', _('Ok'));
79 $errorMessageDiv->addDataAttribute('title', _('WebAuthn failed'));
80 $container->add($errorMessageDiv);
81 $buttonGroup = new htmlGroup();
82 $registerButton = new htmlButton('register', _('Register new device'));
83 $registerButton->addDataAttribute('dn', $userDn);
84 $registerButton->addDataAttribute('sec_token_value', getSecurityTokenValue());
85 $registerButton->addDataAttribute('sec_token_name', getSecurityTokenName());
86 $registration = $webauthnManager->getRegistrationObject($userDn, LAM_INTERFACE::ADMIN);
87 $loader = $webauthnManager->createPublicKeyCredentialLoader();
88 $registrationJson = $loader->serialize($registration, 'json');
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);
95 $container->addVerticalSpacer('2rem');
96 $results = $database->searchDevices($userDn);
97 if (empty($results)) {
98 $container->add(new htmlStatusMessage('INFO', _('No devices found.')));
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);
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);
148
149 addSecurityTokenToMetaHTML($container);
150
151 parseHtml(null, $container, [], false);
152
153 echo '</form>';
154 echo '</div>';
155 include_once __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 $loader = $webauthnManager->createPublicKeyCredentialLoader();
169 $registrationObject = $loader->deserialize($_SESSION['webauthn_registration'], PublicKeyCredentialCreationOptions::class, 'json');
170 $success = $webauthnManager->storeNewRegistration($registrationObject, $registrationData);
171 if ($success) {
172 $container->add(new htmlStatusMessage('INFO', _('The device was registered.')));
173 }
174 else {
175 $container->add(new htmlStatusMessage('ERROR', _('The device failed to register.')));
176 }
177 }
178
179 /**
180 * Shows the message if a device was removed.
181 *
182 * @param htmlResponsiveRow $container row
183 */
184 function showRemoveMessage(htmlResponsiveRow $container): void {
185 if (!empty($_POST['removed']) && ($_POST['removed'] === 'true')) {
186 $container->add(new htmlStatusMessage('INFO', _('The device was deleted.')));
187 }
188 }