1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
|
<?php
use \LAM\PDF\PDFTable;
use \LAM\PDF\PDFTableCell;
use \LAM\PDF\PDFTableRow;
/*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2018 - 2024 Roland Gruber
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Manages YubiKey keys.
*
* @package modules
* @author Roland Gruber
*/
/**
* Manages YubiKey keys.
*
* @package modules
*/
class yubiKeyUser extends baseModule {
/** session variable for existing keys in self service */
const SESS_KEY_LIST = 'yubiKeyUser_keyList';
/**
* Creates a new yubiKeyUser object.
*
* @param string $scope account type (user, group, host)
*/
public function __construct($scope) {
// call parent constructor
parent::__construct($scope);
$this->autoAddObjectClasses = false;
}
/**
* Returns true if this module can manage accounts of the current type, otherwise false.
*
* @return boolean true if module fits
*/
public function can_manage() {
return in_array($this->get_scope(), ['user']);
}
/**
* Returns meta data that is interpreted by parent class
*
* @return array array with meta data
*
* @see baseModule::get_metaData()
*/
public function get_metaData() {
$return = [];
// icon
$return['icon'] = 'yubikey.png';
// alias name
$return["alias"] = "YubiKey";
// module dependencies
$return['dependencies'] = ['depends' => [], 'conflicts' => []];
// managed object classes
$objectClass = $this->getObjectClassName();
if (!empty($objectClass)) {
$return['objectClasses'] = [$objectClass];
}
// managed attributes
$attributeName = $this->getAttributeName();
$return['attributes'] = [$attributeName];
// help Entries
$return['help'] = [
$attributeName => [
"Headline" => _("YubiKey ids"), 'attr' => $attributeName,
"Text" => _("The YubiKey id are the first 12 letters of the key output. Simply touch your YubiKey in an empty field to set it.")
],
'keyList' => [
"Headline" => _("YubiKey ids"), 'attr' => $attributeName,
"Text" => _("Please a comma separated list of your YubiKey ids.")
],
'objectClass' => [
"Headline" => _("Object class"),
"Text" => _("Please enter the object class that should be used for YubiKey (e.g. 'yubiKeyUser').")
],
'attributeName' => [
"Headline" => _("Attribute name"),
"Text" => _("Please enter the attribute name that should be used for YubiKey (e.g. 'yubiKeyId').")
],
'whereToFind' => [
"Headline" => _("YubiKey ids"),
"Text" => _("The YubiKey id are the first 12 letters of the key output. Simply touch your YubiKey in an empty field to set it.")
],
];
// upload fields
$return['upload_columns'] = [
[
'name' => 'yubiKeyUser_yubiKeyId',
'description' => _('YubiKey ids'),
'help' => 'keyList',
'example' => 'abcd1234, vwyxz12345'
]
];
// available PDF fields
$return['PDF_fields'] = [
'yubiKeyId' => _('YubiKey ids')
];
// self service field settings
$return['selfServiceFieldSettings'] = [
'yubiKeyId' => _('YubiKey ids'),
];
$return['selfServiceReadOnlyFields'] = ['yubiKeyId'];
return $return;
}
/**
* This function fills the message array.
**/
protected function load_Messages() {
$this->messages['yubiKeyId'][0] = ['ERROR', _('Please enter a valid key id.')];
$this->messages['yubiKeyId'][1] = ['ERROR', _('Account %s:') . ' yubiKeyUser_yubiKeyId', _('Please enter a valid key id.')];
}
/**
* {@inheritDoc}
*/
public function loadAttributesFromAccountCopy(array $ldapAttributes, array $attributesToIgnore = []): void {
$attributesToIgnore = array_merge(baseModule::ATTRIBUTES_TO_IGNORE_ON_COPY_DEFAULT, [$this->getAttributeName()]);
parent::loadAttributesFromAccountCopy($ldapAttributes, $attributesToIgnore);
}
/**
* Returns the HTML meta data for the main account page.
*
* @return htmlElement HTML meta data
*/
public function display_html_attributes() {
$return = new htmlResponsiveRow();
$objectClass = $this->getObjectClassName();
$attributeName = $this->getAttributeName();
if (empty($attributeName)) {
$message = new htmlStatusMessage('ERROR', _('Invalid configuration detected. Please edit your server profile (module settings) and fill all required fields.'));
$return->add($message, 12);
return $return;
}
if (empty($objectClass) || in_array($objectClass, $this->attributes['objectClass'])) {
$htmlIds = [];
$this->addMultiValueInputTextField($return, $attributeName, _('YubiKey ids'), false, '12', false, null, null, $htmlIds, 'lam-prevent-enter');
if (!empty($objectClass)) {
$return->addVerticalSpacer('2rem');
$remButton = new htmlButton('remObjectClass', _('Remove YubiKey extension'));
$remButton->setCSSClasses(['lam-danger']);
$return->add($remButton, 12, 12, 12, 'text-center');
}
}
else {
$return->add(new htmlButton('addObjectClass', _('Add YubiKey extension')), 12);
}
return $return;
}
/**
* Processes user input of the primary module page.
* It checks if all input values are correct and updates the associated LDAP attributes.
*
* @return array list of info/error messages
*/
public function process_attributes() {
$messages = [];
$objectClass = $this->getObjectClassName();
$attributeName = $this->getAttributeName();
// add extension
if (isset($_POST['addObjectClass'])) {
$this->attributes['objectClass'][] = $objectClass;
return [];
}
// remove extension
elseif (isset($_POST['remObjectClass'])) {
$this->attributes['objectClass'] = array_delete([$objectClass], $this->attributes['objectClass']);
if (!empty($this->attributes[$attributeName])) {
unset($this->attributes[$attributeName]);
}
return [];
}
// skip processing if extension is not active
if (!empty($objectClass) && !in_array($objectClass, $this->attributes['objectClass'])) {
return [];
}
$this->processMultiValueInputTextField($attributeName, $messages, 'ascii');
$this->attributes[$attributeName] = array_values(array_unique($this->attributes[$attributeName]));
return $messages;
}
/**
* Returns a list of modifications which have to be made to the LDAP account.
*
* @return array list of modifications
* <br>This function returns an array with 3 entries:
* <br>array( DN1 ('add' => array($attr), 'remove' => array($attr), 'modify' => array($attr)), DN2 .... )
* <br>DN is the DN to change. It may be possible to change several DNs (e.g. create a new user and add him to some groups via attribute memberUid)
* <br>"add" are attributes which have to be added to LDAP entry
* <br>"remove" are attributes which have to be removed from LDAP entry
* <br>"modify" are attributes which have to been modified in LDAP entry
* <br>"info" are values with informational value (e.g. to be used later by pre/postModify actions)
*/
public function save_attributes() {
$objectClass = $this->getObjectClassName();
if (!empty($objectClass) && !in_array($objectClass, $this->attributes['objectClass']) && !in_array($objectClass, $this->orig['objectClass'])) {
// skip saving if the extension was not added/modified
return [];
}
return parent::save_attributes();
}
/**
* {@inheritDoc}
* @see baseModule::build_uploadAccounts()
*/
public function build_uploadAccounts($rawAccounts, $ids, &$partialAccounts, $selectedModules, &$type) {
$objectClass = $this->getObjectClassName();
$attributeName = $this->getAttributeName();
$messages = [];
for ($i = 0; $i < sizeof($rawAccounts); $i++) {
// add object class
if (!empty($objectClass) && !in_array($objectClass, $partialAccounts[$i]['objectClass'])) {
$partialAccounts[$i]['objectClass'][] = $objectClass;
}
// add keys
$message = $this->messages['yubiKeyId'][1];
$this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'yubiKeyUser_yubiKeyId', $attributeName, 'ascii', $message, $messages, '/,[ ]*/');
}
return $messages;
}
/**
* {@inheritDoc}
* @see baseModule::get_pdfEntries()
*/
public function get_pdfEntries($pdfKeys, $typeId) {
$return = [];
$attributeName = $this->getAttributeName();
if (!empty($this->attributes[$attributeName])) {
$pdfTable = new PDFTable(_('YubiKey ids'));
for ($i = 0; $i < sizeof($this->attributes[$attributeName]); $i++) {
$pdfRow = new PDFTableRow();
$pdfRow->cells[] = new PDFTableCell($this->attributes[$attributeName][$i]);
$pdfTable->rows[] = $pdfRow;
}
$this->addPDFTable($return, 'yubiKeyId', $pdfTable);
}
return $return;
}
/**
* Returns the meta HTML code for each input field.
* format: array(<field1> => array(<META HTML>), ...)
* It is not possible to display help links.
*
* @param array $fields list of active fields
* @param array $attributes attributes of LDAP account
* @param boolean $passwordChangeOnly indicates that the user is only allowed to change his password and no LDAP content is readable
* @param array $readOnlyFields list of read-only fields
* @return array list of meta HTML elements (field name => htmlResponsiveRow)
*/
public function getSelfServiceOptions($fields, $attributes, $passwordChangeOnly, $readOnlyFields) {
$return = [];
if ($passwordChangeOnly) {
return $return; // no fields as long no LDAP content can be read
}
$attributes = array_change_key_case($attributes, CASE_LOWER);
$objectClass = strtolower($this->getObjectClassName());
if (!empty($objectClass) && !in_array_ignore_case($objectClass, $attributes['objectclass'])) {
return $return;
}
if (in_array('yubiKeyId', $fields)) {
$attributeName = strtolower($this->getAttributeName());
$keyIds = [];
if (isset($attributes[$attributeName][0])) {
$keyIds = $attributes[$attributeName];
}
$_SESSION[self::SESS_KEY_LIST] = $keyIds;
$row = new htmlResponsiveRow();
// JavaScript functions
$row->add($this->getSelfServiceKeysJSBlock(), 0);
// input fields
$keyLabel = new htmlOutputText($this->getSelfServiceLabel('yubiKeyId', _('YubiKey ids')));
$row->addLabel($keyLabel);
$row->addField(new htmlDiv('yubiKeyIdDiv', $this->getSelfServiceKeys()));
$return['yubiKeyId'] = $row;
}
return $return;
}
/**
* Returns the meta HTML code to display the key area.
* This also includes the file upload.
*
* @return htmlTable key content
*/
private function getSelfServiceKeys() {
$keys = $_SESSION[self::SESS_KEY_LIST];
$content = new htmlResponsiveRow();
if (sizeof($keys) > 0) {
for ($i = 0; $i < sizeof($keys); $i++) {
$group = new htmlGroup();
$keyInput = new htmlInputField('yubiKeyId_' . $i, $keys[$i]);
$keyInput->setFieldMaxLength(12);
$keyInput->setCSSClasses(['lam-prevent-enter']);
$keyInput->setAccessibilityLabel($this->getSelfServiceLabel('yubiKeyId', _('YubiKey ids')));
$group->addElement($keyInput);
$delLink = new htmlLink('', '#', '../../graphics/del.svg');
$delLink->setTitle(_('Delete'));
$delLink->setOnClick('yubiKeyIdDeleteKey(' . $i . ', ' . sizeof($keys) . ');return false;');
$delLink->setCSSClasses(['icon']);
$group->addElement($delLink);
$content->add($group, 12, 12, 12, 'nowrap');
}
}
$addGroup = new htmlGroup();
$addLink = new htmlLink(_('Add'), '#', '../../graphics/add.svg', true);
$addLink->setTitle(_('Add'));
$addLink->setOnClick('yubiKeyIdAddKey(' . sizeof($keys) . ');return false;');
$addLink->setCSSClasses(['icon']);
$addGroup->addElement($addLink);
$addGroup->addElement(new htmlSpacer('0.5rem', null));
$addGroup->addElement(new htmlHelpLink('whereToFind', 'yubiKeyUser'));
$content->add($addGroup, 12);
return $content;
}
/**
* Returns the Java Script functions to manage the keys.
*
* @return htmlJavaScript JS block
*/
private static function getSelfServiceKeysJSBlock() {
$content = '
function yubiKeyIdDeleteKey(id, count) {
var actionJSON = {
"action": "deleteKey",
"id": id
};
for (c = 0; c < count; c++) {
actionJSON["yubiKeyId_" + c] = document.getElementById(\'yubiKeyId_\' + c).value;
}
let data = new FormData();
data.append("' . getSecurityTokenName() . '", "' . getSecurityTokenValue() . '");
data.append("jsonInput", JSON.stringify(actionJSON));
fetch(\'../misc/ajax.php?selfservice=1&module=yubiKeyUser&scope=user\', {
method: "POST",
body: data
})
.then(async response => {
const jsonData = await response.json();
yubiKeyIdDeleteKeyHandleReply(jsonData);
});
}
function yubiKeyIdDeleteKeyHandleReply(data) {
if (data.errorsOccurred == "false") {
document.getElementById(\'yubiKeyIdDiv\').innerHTML = data.html;
}
else {
window.lam.dialog.showError(data.errormessage, null, "' . _('Ok') . '");
}
}
function yubiKeyIdAddKey(count) {
var actionJSON = {
"action": "addKey"
};
for (c = 0; c < count; c++) {
actionJSON["yubiKeyId_" + c] = document.getElementById(\'yubiKeyId_\' + c).value;
}
let data = new FormData();
data.append("' . getSecurityTokenName() . '", "' . getSecurityTokenValue() . '");
data.append("jsonInput", JSON.stringify(actionJSON));
fetch(\'../misc/ajax.php?selfservice=1&module=yubiKeyUser&scope=user\', {
method: "POST",
body: data
})
.then(async response => {
const jsonData = await response.json();
yubiKeyIdAddKeyHandleReply(jsonData);
});
}
function yubiKeyIdAddKeyHandleReply(data) {
if (data.errorsOccurred == "false") {
document.getElementById(\'yubiKeyIdDiv\').innerHTML = data.html;
window.lam.html.preventEnter();
}
else {
window.lam.dialog.showError(data.errormessage, null, "' . _('Ok') . '");
}
}
';
return new htmlJavaScript($content);
}
/**
* Checks if all input values are correct and returns the LDAP attributes which should be changed.
* <br>Return values:
* <br>messages: array of parameters to create status messages
* <br>add: array of attributes to add
* <br>del: array of attributes to remove
* <br>mod: array of attributes to modify
* <br>info: array of values with informational value (e.g. to be used later by pre/postModify actions)
*
* Calling this method does not require the existence of an enclosing {@link accountContainer}.
*
* @param string $fields input fields
* @param array $attributes LDAP attributes
* @param boolean $passwordChangeOnly indicates that the user is only allowed to change his password and no LDAP content is readable
* @param array $readOnlyFields list of read-only fields
* @return array messages and attributes (array('messages' => [], 'add' => array('mail' => array('test@test.com')), 'del' => [], 'mod' => [], 'info' => []))
*/
public function checkSelfServiceOptions($fields, $attributes, $passwordChangeOnly, $readOnlyFields) {
$return = ['messages' => [], 'add' => [], 'del' => [], 'mod' => [], 'info' => []];
if ($passwordChangeOnly) {
return $return; // skip processing if only a password change is done
}
$attributes = array_change_key_case($attributes, CASE_LOWER);
$objectClass = strtolower($this->getObjectClassName());
if (!in_array_ignore_case($objectClass, $attributes['objectclass'])) {
return $return;
}
if (in_array('yubiKeyId', $fields)) {
$attributeName = strtolower($this->getAttributeName());
$newKeys = [];
$counter = 0;
while (isset($_POST['yubiKeyId_' . $counter])) {
$newKeys[] = $_POST['yubiKeyId_' . $counter];
$counter++;
}
$count = sizeof($newKeys);
for ($i = 0; $i < $count; $i++) {
if (trim($newKeys[$i]) == '') {
unset($newKeys[$i]);
}
elseif (!get_preg($newKeys[$i], 'ascii')) {
$return['messages'][] = ['ERROR', _('Please enter a valid key id.'), htmlspecialchars($newKeys[$i])];
}
}
$newKeys = array_values(array_unique($newKeys));
$oldKeys = [];
if (isset($attributes[$attributeName][0])) {
$oldKeys = $attributes[$attributeName];
}
$update = false;
if (sizeof($newKeys) != sizeof($oldKeys)) {
$update = true;
}
else {
for ($i = 0; $i < sizeof($newKeys); $i++) {
if (!in_array($newKeys[$i], $oldKeys)) {
$update = true;
break;
}
}
}
if ($update) {
if (sizeof($oldKeys) == 0) {
$return['add'][$attributeName] = $newKeys;
}
elseif (sizeof($newKeys) == 0) {
$return['del'][$attributeName] = $newKeys;
}
else {
$return['mod'][$attributeName] = $newKeys;
}
}
}
return $return;
}
/**
* Manages AJAX requests.
* This function may be called with or without an account container.
*/
public function handleAjaxRequest() {
$jsonInput = json_decode($_POST['jsonInput'], true);
$jsonReturn = self::invalidAjaxRequest();
if (isset($jsonInput['action'])) {
if ($jsonInput['action'] == 'deleteKey') {
$jsonReturn = $this->ajaxDeleteSelfServiceKey($jsonInput);
}
elseif ($jsonInput['action'] == 'addKey') {
$_SESSION[self::SESS_KEY_LIST][] = '';
ob_start();
$contentElement = $this->getSelfServiceKeys();
ob_end_clean();
ob_start();
parseHtml(null, $contentElement, [], true, $this->get_scope());
$content = ob_get_contents();
ob_end_clean();
$jsonReturn = [
'errorsOccurred' => 'false',
'html' => $content,
];
}
}
echo json_encode($jsonReturn);
}
/**
* Manages the deletion of a key.
*
* @param array $data JSON data
*/
private function ajaxDeleteSelfServiceKey($data) {
if (!isset($data['id'])) {
return self::invalidAjaxRequest();
}
$index = $data['id'];
if (array_key_exists($index, $_SESSION[self::SESS_KEY_LIST])) {
unset($_SESSION[self::SESS_KEY_LIST][$index]);
$_SESSION[self::SESS_KEY_LIST] = array_values($_SESSION[self::SESS_KEY_LIST]);
}
ob_start();
$contentElement = $this->getSelfServiceKeys();
ob_end_clean();
ob_start();
parseHtml(null, $contentElement, [], true, $this->get_scope());
$content = ob_get_contents();
ob_end_clean();
return [
'errorsOccurred' => 'false',
'html' => $content,
];
}
/**
* Invalid AJAX request received.
*
* @param String $message error message
*/
public static function invalidAjaxRequest($message = null) {
if ($message == null) {
$message = _('Invalid request');
}
return ['errorsOccurred' => 'true', 'errormessage' => $message];
}
/**
* Returns the object class used for YubiKey.
*
* @return string object class
*/
private function getObjectClassName() {
if (!empty($this->moduleSettings['yubiKeyUser_objectClass'][0])) {
return $this->moduleSettings['yubiKeyUser_objectClass'][0];
}
elseif (!empty($this->selfServiceSettings->moduleSettings['yubiKeyUser_objectClass'][0])) {
return $this->selfServiceSettings->moduleSettings['yubiKeyUser_objectClass'][0];
}
return null;
}
/**
* Returns the attribute name used for YubiKey.
*
* @return string attribute name
*/
private function getAttributeName() {
if (!empty($this->moduleSettings['yubiKeyUser_attributeName'][0])) {
return $this->moduleSettings['yubiKeyUser_attributeName'][0];
}
elseif (!empty($this->selfServiceSettings->moduleSettings['yubiKeyUser_attributeName'][0])) {
return $this->selfServiceSettings->moduleSettings['yubiKeyUser_attributeName'][0];
}
return null;
}
/**
* {@inheritDoc}
* @see baseModule::get_configOptions()
*/
public function get_configOptions($scopes, $allScopes) {
$configContainer = new htmlResponsiveRow();
$configContainer->add(new htmlResponsiveInputField(_('Object class'), 'yubiKeyUser_objectClass', null, 'objectClass'), 12);
$configContainer->add(new htmlResponsiveInputField(_('Attribute name'), 'yubiKeyUser_attributeName', 'yubiKeyId', 'attributeName', true), 12);
return $configContainer;
}
/**
* {@inheritDoc}
* @see baseModule::getSelfServiceSettings()
*/
public function getSelfServiceSettings($profile) {
$container = new htmlResponsiveRow();
$container->add(new htmlResponsiveInputField(_('Object class'), 'yubiKeyUser_objectClass', null, ['objectClass', 'yubiKeyUser']), 12);
$container->add(new htmlResponsiveInputField(_('Attribute name'), 'yubiKeyUser_attributeName', 'yubiKeyId', ['attributeName', 'yubiKeyUser']), 12);
return $container;
}
}
|