/usr/share/php/Horde/Auth/Base.php is in php-horde-auth 2.2.2-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?php
/**
* Copyright 1999-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you did
* not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL-2.1
* @package Auth
*/
/**
* The Horde_Auth_Base class provides a common abstracted interface to creating
* various authentication backends.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 1999-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL-2.1
* @package Auth
*/
abstract class Horde_Auth_Base
{
/**
* An array of capabilities, so that the driver can report which
* operations it supports and which it doesn't.
*
* @var array
*/
protected $_capabilities = array(
'add' => false,
'authenticate' => true,
'groups' => false,
'list' => false,
'resetpassword' => false,
'remove' => false,
'transparent' => false,
'update' => false,
'badlogincount' => false,
'lock' => false,
);
/**
* Hash containing parameters needed for the drivers.
*
* @var array
*/
protected $_params = array();
/**
* The credentials currently being authenticated.
*
* @var array
*/
protected $_credentials = array(
'change' => false,
'credentials' => array(),
'expire' => null,
'userId' => ''
);
/**
* Logger object.
*
* @var Horde_Log_Logger
*/
protected $_logger;
/**
* History object.
*
* @var Horde_History
*/
protected $_history_api;
/**
* Lock object.
*
* @var Horde_Lock
*/
protected $_lock_api;
/**
* Authentication error information.
*
* @var array
*/
protected $_error;
/**
* Constructor.
*
* @param array $params Optional parameters:
* - default_user: (string) The default user.
* - logger: (Horde_Log_Logger, optional) A logger object.
* - lock_api: (Horde_Lock, optional) A locking object.
* - history_api: (Horde_History, optional) A history object.
* - login_block_count: (integer, optional) How many failed logins
* trigger autoblocking? 0 disables the feature.
* - login_block_time: (integer, options) How many minutes should
* autoblocking last? 0 means no expiration.
*/
public function __construct(array $params = array())
{
if (isset($params['logger'])) {
$this->_logger = $params['logger'];
unset($params['logger']);
}
if (isset($params['lock_api'])) {
$this->_lock_api = $params['lock_api'];
$this->_capabilities['lock'] = true;
unset($params['lock_api']);
}
if (isset($params['history_api'])) {
$this->_history_api = $params['history_api'];
$this->_capabilities['badlogincount'] = true;
unset($params['history_api']);
}
$params = array_merge(array(
'default_user' => ''
), $params);
$this->_params = $params;
}
/**
* Finds out if a set of login credentials are valid, and if requested,
* mark the user as logged in in the current session.
*
* @param string $userId The userId to check.
* @param array $credentials The credentials to check.
* @param boolean $login Whether to log the user in. If false, we'll
* only test the credentials and won't modify
* the current session. Defaults to true.
*
* @return boolean Whether or not the credentials are valid.
*/
public function authenticate($userId, $credentials, $login = true)
{
$userId = trim($userId);
try {
$this->_credentials['userId'] = $userId;
if (($this->hasCapability('lock')) &&
$this->isLocked($userId)) {
$details = $this->isLocked($userId, true);
if ($details['lock_timeout'] == Horde_Lock::PERMANENT) {
$message = Horde_Auth_Translation::t("Your account has been permanently locked");
} else {
$message = sprintf(Horde_Auth_Translation::t("Your account has been locked for %d minutes"), ceil(($details['lock_timeout'] - time()) / 60));
}
throw new Horde_Auth_Exception($message, Horde_Auth::REASON_LOCKED);
}
$this->_authenticate($userId, $credentials);
$this->setCredential('userId', $this->_credentials['userId']);
$this->setCredential('credentials', $credentials);
if ($this->hasCapability('badlogincount')) {
$this->_resetBadLogins($userId);
}
return true;
} catch (Horde_Auth_Exception $e) {
if (($code = $e->getCode()) &&
$code != Horde_Auth::REASON_MESSAGE) {
if (($code == Horde_Auth::REASON_BADLOGIN) &&
$this->hasCapability('badlogincount')) {
$this->_badLogin($userId);
}
$this->setError($code, $e->getMessage());
} else {
$this->setError(Horde_Auth::REASON_MESSAGE, $e->getMessage());
}
return false;
}
}
/**
* Basic sort implementation.
*
* If the backend has listUsers and doesn't have a native sorting option,
* fall back to this method.
*
* @param array $users An array of usernames.
* @param boolean $sort Whether to sort or not.
*
* @return array the users, sorted or not
*
*/
protected function _sort($users, $sort)
{
if ($sort) {
sort($users);
}
return $users;
}
/**
* Authentication stub.
*
* On failure, Horde_Auth_Exception should pass a message string (if any)
* in the message field, and the Horde_Auth::REASON_* constant in the code
* field (defaults to Horde_Auth::REASON_MESSAGE).
*
* @param string $userId The userID to check.
* @param array $credentials An array of login credentials.
*
* @throws Horde_Auth_Exception
*/
abstract protected function _authenticate($userId, $credentials);
/**
* Checks for triggers that may invalidate the current auth.
* These triggers are independent of the credentials.
*
* @return boolean True if the results of authenticate() are still valid.
*/
public function validateAuth()
{
return true;
}
/**
* Adds a set of authentication credentials.
*
* @param string $userId The userId to add.
* @param array $credentials The credentials to use.
*
* @throws Horde_Auth_Exception
*/
public function addUser($userId, $credentials)
{
throw new Horde_Auth_Exception('Unsupported.');
}
/**
* Locks a user indefinitely or for a specified time.
*
* @param string $userId The user to lock.
* @param integer $time The duration in minutes, 0 = permanent.
*
* @throws Horde_Auth_Exception
*/
public function lockUser($userId, $time = 0)
{
if (!$this->_lock_api) {
throw new Horde_Auth_Exception('Unsupported.');
}
if ($time == 0) {
$time = Horde_Lock::PERMANENT;
} else {
$time *= 60;
}
try {
if ($this->_lock_api->setLock($userId, 'horde_auth', 'login:' . $userId, $time, Horde_Lock::TYPE_EXCLUSIVE)) {
return;
}
} catch (Horde_Lock_Exception $e) {
throw new Horde_Auth_Exception($e);
}
throw new Horde_Auth_Exception('User is already locked',
Horde_Auth::REASON_LOCKED);
}
/**
* Unlocks a user and optionally resets the bad login count.
*
* @param string $userId The user to unlock.
* @param boolean $resetBadLogins Reset bad login counter?
*
* @throws Horde_Auth_Exception
*/
public function unlockUser($userId, $resetBadLogins = false)
{
if (!$this->_lock_api) {
throw new Horde_Auth_Exception('Unsupported.');
}
try {
$locks = $this->_lock_api->getLocks(
'horde_auth', 'login:' . $userId, Horde_Lock::TYPE_EXCLUSIVE);
$lock_id = key($locks);
if ($lock_id) {
$this->_lock_api->clearLock($lock_id);
}
if ($resetBadLogins) {
$this->_resetBadLogins($userId);
}
} catch (Horde_Lock_Exception $e) {
throw new Horde_Auth_Exception($e);
}
}
/**
* Returns whether a user is currently locked.
*
* @param string $userId The user to check.
* @param boolean $show_details Return timeout too?
*
* @return boolean|array If $show_details is a true, an array with
* 'locked' and 'lock_timeout' values. Whether the
* user is locked, otherwise.
* @throws Horde_Auth_Exception
*/
public function isLocked($userId, $show_details = false)
{
if (!$this->_lock_api) {
throw new Horde_Auth_Exception('Unsupported.');
}
try {
$locks = $this->_lock_api->getLocks(
'horde_auth', 'login:' . $userId, Horde_Lock::TYPE_EXCLUSIVE);
} catch (Horde_Lock_Exception $e) {
throw new Horde_Auth_Exception($e);
}
if ($show_details) {
$lock_id = key($locks);
return empty($lock_id)
? array('locked' => false, 'lock_timeout' => 0)
: array('locked' => true, 'lock_timeout' => $locks[$lock_id]['lock_expiry_timestamp']);
}
return !empty($locks);
}
/**
* Handles a bad login.
*
* @param string $userId The user with a bad login.
*
* @throws Horde_Auth_Exception
*/
protected function _badLogin($userId)
{
if (!$this->_history_api) {
throw new Horde_Auth_Exception('Unsupported.');
}
$history_identifier = $userId . '@logins.failed';
try {
$this->_history_api->log(
$history_identifier,
array('action' => 'login_failed', 'who' => $userId));
$history_log = $this->_history_api->getHistory($history_identifier);
if ($this->_params['login_block_count'] > 0 &&
$this->_params['login_block_count'] <= $history_log->count() &&
$this->hasCapability('lock')) {
$this->lockUser($userId, $this->_params['login_block_time']);
}
} catch (Horde_History_Exception $e) {
throw new Horde_Auth_Exception($e);
}
}
/**
* Resets the bad login counter.
*
* @param string $userId The user to reset.
*
* @throws Horde_Auth_Exception
*/
protected function _resetBadLogins($userId)
{
if (!$this->_history_api) {
throw new Horde_Auth_Exception('Unsupported.');
}
try {
$this->_history_api->removeByNames(array($userId . '@logins.failed'));
} catch (Horde_History_Exception $e) {
throw new Horde_Auth_Exception($e);
}
}
/**
* Updates a set of authentication credentials.
*
* @param string $oldID The old userId.
* @param string $newID The new userId.
* @param array $credentials The new credentials
*
* @throws Horde_Auth_Exception
*/
public function updateUser($oldID, $newID, $credentials)
{
throw new Horde_Auth_Exception('Unsupported.');
}
/**
* Deletes a set of authentication credentials.
*
* @param string $userId The userId to delete.
*
* @throws Horde_Auth_Exception
*/
public function removeUser($userId)
{
throw new Horde_Auth_Exception('Unsupported.');
}
/**
* Lists all users in the system.
*
* @param boolean $sort Sort the users?
*
* @return mixed The array of userIds.
* @throws Horde_Auth_Exception
*/
public function listUsers($sort = false)
{
throw new Horde_Auth_Exception('Unsupported.');
}
/**
* Searches the users for a substring.
*
* @since Horde_Auth 2.2.0
*
* @param string $search The search term.
*
* @return array A list of all matching users.
*/
public function searchUsers($search)
{
try {
$users = $this->listUsers();
} catch (Horde_Auth_Exception $e) {
return array();
}
$matches = array();
foreach ($users as $user) {
if (Horde_String::ipos($user, $search) !== false) {
$matches[] = $user;
}
}
return $matches;
}
/**
* Checks if $userId exists in the system.
*
* @param string $userId User ID for which to check
*
* @return boolean Whether or not $userId already exists.
*/
public function exists($userId)
{
try {
$users = $this->listUsers();
return in_array($userId, $users);
} catch (Horde_Auth_Exception $e) {
return false;
}
}
/**
* Automatic authentication.
*
* Transparent authentication should set 'userId', 'credentials', or
* 'params' in $this->_credentials as needed - these values will be used
* to set the credentials in the session.
*
* Transparent authentication should normally never throw an error - false
* should be returned.
*
* @return boolean Whether transparent login is supported.
* @throws Horde_Auth_Exception
*/
public function transparent()
{
return false;
}
/**
* Reset a user's password. Used for example when the user does not
* remember the existing password.
*
* @param string $userId The user id for which to reset the password.
*
* @return string The new password on success.
* @throws Horde_Auth_Exception
*/
public function resetPassword($userId)
{
throw new Horde_Auth_Exception('Unsupported.');
}
/**
* Queries the current driver to find out if it supports the given
* capability.
*
* @param string $capability The capability to test for.
*
* @return boolean Whether or not the capability is supported.
*/
public function hasCapability($capability)
{
return !empty($this->_capabilities[$capability]);
}
/**
* Returns the named parameter for the current auth driver.
*
* @param string $param The parameter to fetch.
*
* @return string The parameter's value, or null if it doesn't exist.
*/
public function getParam($param)
{
return isset($this->_params[$param])
? $this->_params[$param]
: null;
}
/**
* Returns internal credential value(s).
*
* @param mixed $name The credential value to get. If null, will return
* the entire credential list. Valid names:
* - 'change': (boolean) Do credentials need to be changed?
* - 'credentials': (array) The credentials needed to authenticate.
* - 'expire': (integer) UNIX timestamp of the credential expiration date.
* - 'userId': (string) The user ID.
*
* @return mixed The credential information, or null if the credential
* doesn't exist.
*/
public function getCredential($name = null)
{
if (is_null($name)) {
return $this->_credentials;
}
return isset($this->_credentials[$name])
? $this->_credentials[$name]
: null;
}
/**
* Sets an internal credential value.
*
* @param string $type The credential name to set. See getCredential()
* for the list of valid credentials/types.
* @param mixed $value The credential value to set.
*/
public function setCredential($type, $value)
{
switch ($type) {
case 'change':
$this->_credentials['change'] = (bool)$value;
break;
case 'credentials':
$this->_credentials['credentials'] = array_filter(array_merge($this->_credentials['credentials'], $value));
break;
case 'expire':
$this->_credentials['expire'] = intval($value);
break;
case 'userId':
$this->_credentials['userId'] = strval($value);
break;
}
}
/**
* Sets the error message for an invalid authentication.
*
* @param string $type The type of error (Horde_Auth::REASON_* constant).
* @param string $msg The error message/reason for invalid
* authentication.
*/
public function setError($type, $msg = null)
{
$this->_error = array(
'msg' => $msg,
'type' => $type
);
}
/**
* Returns the error type or message for an invalid authentication.
*
* @param boolean $msg If true, returns the message string (if set).
*
* @return mixed Error type, error message (if $msg is true) or false
* if entry doesn't exist.
*/
public function getError($msg = false)
{
return isset($this->_error['type'])
? ($msg ? $this->_error['msg'] : $this->_error['type'])
: false;
}
}
|