2009-10-30 01:03:17 +01:00
|
|
|
|
<?php
|
|
|
|
|
/*******************************************************************************
|
|
|
|
|
* Copyright (C) 2007 Easter-eggs
|
|
|
|
|
* http://ldapsaisie.labs.libre-entreprise.org
|
|
|
|
|
*
|
|
|
|
|
* Author: See AUTHORS file in top-level directory.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2020-05-12 16:37:35 +02:00
|
|
|
|
LSsession :: loadLSclass('LSlog_staticLoggerClass');
|
|
|
|
|
|
2009-10-30 01:03:17 +01:00
|
|
|
|
/**
|
|
|
|
|
* Gestion de l'authentification d'un utilisateur
|
|
|
|
|
*
|
2020-04-29 15:54:21 +02:00
|
|
|
|
* Cette classe gere l'authentification des utilisateurs <20> l'interface
|
2009-10-30 01:03:17 +01:00
|
|
|
|
*
|
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
|
*/
|
2020-05-12 16:37:35 +02:00
|
|
|
|
class LSauth extends LSlog_staticLoggerClass {
|
2020-04-29 15:54:21 +02:00
|
|
|
|
|
2009-10-31 02:33:01 +01:00
|
|
|
|
static private $authData=NULL;
|
2010-11-24 19:12:21 +01:00
|
|
|
|
static private $authObject=NULL;
|
|
|
|
|
static private $config=array();
|
|
|
|
|
static private $provider=NULL;
|
2020-04-29 15:54:21 +02:00
|
|
|
|
|
2010-11-24 19:12:21 +01:00
|
|
|
|
static private $params = array (
|
2009-10-31 02:33:01 +01:00
|
|
|
|
'displayLoginForm' => true,
|
2010-11-25 12:39:35 +01:00
|
|
|
|
'displayLogoutBtn' => true,
|
|
|
|
|
'displaySelfAccess' => true
|
2009-10-31 02:33:01 +01:00
|
|
|
|
);
|
2010-11-24 19:12:21 +01:00
|
|
|
|
|
2019-03-12 11:42:53 +01:00
|
|
|
|
public static function start() {
|
2020-05-12 16:37:35 +02:00
|
|
|
|
self :: log_debug('start()');
|
2010-11-24 19:12:21 +01:00
|
|
|
|
// Load Config
|
|
|
|
|
if (isset(LSsession :: $ldapServer['LSauth']) && is_array(LSsession :: $ldapServer['LSauth'])) {
|
|
|
|
|
self :: $config = LSsession :: $ldapServer['LSauth'];
|
2009-10-31 02:33:01 +01:00
|
|
|
|
}
|
2010-11-24 19:12:21 +01:00
|
|
|
|
if (!LSsession :: loadLSclass('LSauthMethod')) {
|
2020-05-12 16:37:35 +02:00
|
|
|
|
self :: log_debug('Failed to load LSauthMethod class');
|
2019-03-12 11:42:53 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2010-11-24 19:12:21 +01:00
|
|
|
|
if (!isset(self :: $config['method'])) {
|
|
|
|
|
self :: $config['method']='basic';
|
|
|
|
|
}
|
|
|
|
|
$class='LSauthMethod_'.self :: $config['method'];
|
2020-05-12 16:37:35 +02:00
|
|
|
|
self :: log_debug('provider -> '.$class);
|
2010-11-24 19:12:21 +01:00
|
|
|
|
if (LSsession :: loadLSclass($class)) {
|
|
|
|
|
self :: $provider = new $class();
|
|
|
|
|
if (!self :: $provider) {
|
|
|
|
|
LSerror :: addErrorCode('LSauth_05',self :: $config['method']);
|
2009-10-30 01:03:17 +01:00
|
|
|
|
}
|
2020-05-12 16:37:35 +02:00
|
|
|
|
self :: log_debug('Provider Started !');
|
2010-11-24 19:12:21 +01:00
|
|
|
|
return true;
|
2009-10-30 01:03:17 +01:00
|
|
|
|
}
|
|
|
|
|
else {
|
2010-11-24 19:12:21 +01:00
|
|
|
|
LSerror :: addErrorCode('LSauth_04',self :: $config['method']);
|
|
|
|
|
return;
|
2009-10-31 02:33:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-29 15:54:21 +02:00
|
|
|
|
|
2019-03-12 11:42:53 +01:00
|
|
|
|
public static function forceAuthentication() {
|
2020-05-12 16:37:35 +02:00
|
|
|
|
self :: log_debug('LSauth :: forceAuthentication()');
|
2019-03-12 11:42:53 +01:00
|
|
|
|
if (!is_null(self :: $provider)) {
|
|
|
|
|
self :: $authData = self :: $provider -> getAuthData();
|
|
|
|
|
if (self :: $authData) {
|
|
|
|
|
self :: $authObject = self :: $provider -> authenticate();
|
|
|
|
|
return self :: $authObject;
|
|
|
|
|
}
|
|
|
|
|
// No data : user has not filled the login form
|
2020-05-12 16:37:35 +02:00
|
|
|
|
self :: log_debug('No data -> user has not filled the login form');
|
2019-03-12 11:42:53 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LSerror :: addErrorCode('LSauth_06');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-11-24 19:12:21 +01:00
|
|
|
|
|
2020-05-12 19:23:24 +02:00
|
|
|
|
/**
|
|
|
|
|
* Return a configuration parameter (or default value)
|
|
|
|
|
*
|
|
|
|
|
* @param[] $param The configuration parameter
|
|
|
|
|
* @param[] $default The default value (default : null)
|
|
|
|
|
* @param[] $cast Cast resulting value in specific type (default : disabled)
|
|
|
|
|
*
|
|
|
|
|
* @retval mixed The configuration parameter value or default value if not set
|
|
|
|
|
**/
|
|
|
|
|
private static function getConfig($param, $default=null, $cast=null) {
|
|
|
|
|
return LSconfig :: get($param, $default, $cast, self :: $config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retreive auth object types info
|
|
|
|
|
* @return array Array of auth object type with type as key and type's parameters as value
|
|
|
|
|
*/
|
|
|
|
|
public static function getAuthObjectTypes() {
|
|
|
|
|
$objTypes = array();
|
|
|
|
|
foreach(self :: getConfig('LSobjects', array()) as $objType => $objParams) {
|
|
|
|
|
if (is_int($objType) && is_string($objParams)) {
|
|
|
|
|
// We just have the object type
|
2020-05-12 19:58:12 +02:00
|
|
|
|
$objTypes[$objParams] = array('filter' => null, 'password_attribute' => 'userPassword');
|
2020-05-12 19:23:24 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$objTypes[$objType] = array(
|
|
|
|
|
'filter' => self :: getConfig("LSobjects.$objType.filter", null, 'string'),
|
|
|
|
|
'password_attribute' => self :: getConfig("LSobjects.$objType.password_attribute", 'userPassword', 'string'),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// For retro-compatibility, also retreived old parameters:
|
|
|
|
|
$oldAuthObjectType = LSconfig :: get('authObjectType', null, 'string', LSsession :: $ldapServer);
|
|
|
|
|
if ($oldAuthObjectType && !array_key_exists($oldAuthObjectType, $objTypes)) {
|
|
|
|
|
$objTypes[$oldAuthObjectType] = array(
|
|
|
|
|
'filter' => LSconfig :: get('authObjectFilter', null, 'string', LSsession :: $ldapServer),
|
|
|
|
|
'password_attribute' => LSconfig :: get('authObjectTypeAttrPwd', 'userPassword', 'string', LSsession :: $ldapServer),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $objTypes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retreived LSobjects corresponding to a username
|
|
|
|
|
*
|
|
|
|
|
* @retval array|false Array of corresponding LSldapObject with object DN as key, or false in case of error
|
|
|
|
|
*/
|
|
|
|
|
public static function username2LSobjects($username) {
|
|
|
|
|
$user_objects = array();
|
|
|
|
|
foreach (self :: getAuthObjectTypes() as $objType => $objParams) {
|
|
|
|
|
if (!LSsession :: loadLSobject($objType)) {
|
|
|
|
|
LSerror :: addErrorCode('LSauth_03', $objType);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$authobject = new $objType();
|
|
|
|
|
$result = $authobject -> searchObject(
|
|
|
|
|
$username,
|
|
|
|
|
LSsession :: getTopDn(),
|
|
|
|
|
$objParams['filter'],
|
|
|
|
|
array('withoutCache' => true, 'onlyAccessible' => false)
|
|
|
|
|
);
|
|
|
|
|
for($i=0; $i<count($result); $i++)
|
|
|
|
|
$user_objects[$result[$i] -> getDn()] = $result[$i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$nbresult = count($user_objects);
|
|
|
|
|
if ($nbresult == 0) {
|
|
|
|
|
// incorrect login
|
|
|
|
|
self :: log_debug('Invalid username');
|
|
|
|
|
LSerror :: addErrorCode('LSauth_01');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else if ($nbresult > 1) {
|
|
|
|
|
// duplication of identity
|
|
|
|
|
self :: log_debug("More than one user detected for username '$username': ".implode(', ', array_keys($user_objects)));
|
|
|
|
|
if (!self :: getConfig('allow_multi_match', false, 'bool')) {
|
|
|
|
|
LSerror :: addErrorCode('LSauth_02');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $user_objects;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get user password attribute name
|
|
|
|
|
*
|
|
|
|
|
* @param[in] &object LSldapObject The user object
|
|
|
|
|
*
|
|
|
|
|
* @retval string|false The user password attribute name or false if not configured
|
|
|
|
|
*/
|
|
|
|
|
public static function getUserPasswordAttribute(&$object) {
|
|
|
|
|
$authObjectTypes = self :: getAuthObjectTypes();
|
|
|
|
|
$objType = $object -> getType();
|
|
|
|
|
if (array_key_exists($objType, $authObjectTypes))
|
|
|
|
|
return $authObjectTypes[$objType]['password_attribute'];
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-18 13:16:38 +01:00
|
|
|
|
/**
|
|
|
|
|
* Get LDAP credentials
|
|
|
|
|
*
|
|
|
|
|
* Return LDAP credentials or false
|
|
|
|
|
*
|
|
|
|
|
* @params[in] $user The LSldapObject of the user authificated
|
|
|
|
|
*
|
|
|
|
|
* @retval Array|false Array of LDAP credentials array('dn','pwd') or False
|
|
|
|
|
**/
|
2019-03-12 11:42:53 +01:00
|
|
|
|
public static function getLDAPcredentials($user) {
|
2014-11-18 13:16:38 +01:00
|
|
|
|
return self :: $provider -> getLDAPcredentials($user);
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-10 19:06:42 +01:00
|
|
|
|
/**
|
|
|
|
|
* Logout
|
2020-04-29 15:54:21 +02:00
|
|
|
|
*
|
2010-03-10 19:06:42 +01:00
|
|
|
|
* @retval void
|
|
|
|
|
**/
|
2019-03-12 11:42:53 +01:00
|
|
|
|
public static function logout() {
|
2019-03-27 18:02:04 +01:00
|
|
|
|
if (!is_null(self :: $provider)) {
|
|
|
|
|
return self :: $provider -> logout();
|
|
|
|
|
}
|
|
|
|
|
LSerror :: addErrorCode('LSauth_06');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* After logout
|
|
|
|
|
*
|
|
|
|
|
* This method is run by LSsession after the local session was
|
|
|
|
|
* was successfully destroyed.
|
|
|
|
|
*
|
|
|
|
|
* @retval void
|
|
|
|
|
**/
|
|
|
|
|
public static function afterLogout() {
|
|
|
|
|
if (!is_null(self :: $provider)) {
|
2019-03-27 18:28:05 +01:00
|
|
|
|
return self :: $provider -> afterLogout();
|
2019-03-12 11:42:53 +01:00
|
|
|
|
}
|
|
|
|
|
LSerror :: addErrorCode('LSauth_06');
|
|
|
|
|
return;
|
2010-03-10 19:06:42 +01:00
|
|
|
|
}
|
2010-11-24 19:12:21 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disable logout button in LSauth parameters
|
2020-04-29 15:54:21 +02:00
|
|
|
|
*
|
2010-11-24 19:12:21 +01:00
|
|
|
|
* @retval void
|
|
|
|
|
**/
|
2019-03-12 11:42:53 +01:00
|
|
|
|
public static function disableLogoutBtn() {
|
|
|
|
|
self :: $params['displayLogoutBtn'] = false;
|
|
|
|
|
}
|
2010-11-24 19:12:21 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Can display or not logout button in LSauth parameters
|
2020-04-29 15:54:21 +02:00
|
|
|
|
*
|
2010-11-24 19:12:21 +01:00
|
|
|
|
* @retval boolean
|
2019-03-12 11:42:53 +01:00
|
|
|
|
**/
|
|
|
|
|
public static function displayLogoutBtn() {
|
|
|
|
|
return self :: $params['displayLogoutBtn'];
|
|
|
|
|
}
|
2020-04-29 15:54:21 +02:00
|
|
|
|
|
2010-11-25 12:39:35 +01:00
|
|
|
|
/**
|
|
|
|
|
* Disable self access
|
|
|
|
|
*
|
|
|
|
|
* @retval void
|
|
|
|
|
**/
|
2019-03-12 11:42:53 +01:00
|
|
|
|
public static function disableSelfAccess() {
|
|
|
|
|
self :: $params['displaySelfAccess'] = false;
|
|
|
|
|
}
|
2010-11-25 12:39:35 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Can display or not self access
|
|
|
|
|
*
|
|
|
|
|
* @retval boolean
|
|
|
|
|
**/
|
2019-03-12 11:42:53 +01:00
|
|
|
|
public static function displaySelfAccess() {
|
2010-11-25 12:39:35 +01:00
|
|
|
|
return self :: $params['displaySelfAccess'];
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-24 19:12:21 +01:00
|
|
|
|
/*
|
|
|
|
|
* For compatibillity until loginForm is migrated in LSauth
|
|
|
|
|
*/
|
2019-03-12 11:42:53 +01:00
|
|
|
|
public static function disableLoginForm() {
|
2019-03-11 22:42:20 +01:00
|
|
|
|
self :: $params['displayLoginForm'] = false;
|
|
|
|
|
}
|
2019-03-12 11:42:53 +01:00
|
|
|
|
|
|
|
|
|
public static function displayLoginForm() {
|
2019-03-11 22:42:20 +01:00
|
|
|
|
return self :: $params['displayLoginForm'];
|
|
|
|
|
}
|
2010-11-24 19:12:21 +01:00
|
|
|
|
|
2009-10-30 01:03:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Error Codes
|
|
|
|
|
*/
|
|
|
|
|
LSerror :: defineError('LSauth_01',
|
|
|
|
|
_("LSauth : Login or password incorrect.")
|
|
|
|
|
);
|
|
|
|
|
LSerror :: defineError('LSauth_02',
|
|
|
|
|
_("LSauth : Impossible to identify you : Duplication of identities.")
|
|
|
|
|
);
|
|
|
|
|
LSerror :: defineError('LSauth_03',
|
2020-05-12 19:23:24 +02:00
|
|
|
|
_("LSauth : Could not load type of identifiable objects %{type}.")
|
2010-11-24 19:12:21 +01:00
|
|
|
|
);
|
|
|
|
|
LSerror :: defineError('LSauth_04',
|
|
|
|
|
_("LSauth : Can't load authentication method %{method}.")
|
2009-10-30 01:03:17 +01:00
|
|
|
|
);
|
2010-11-24 19:12:21 +01:00
|
|
|
|
LSerror :: defineError('LSauth_05',
|
|
|
|
|
_("LSauth : Failed to build the authentication provider %{method}.")
|
|
|
|
|
);
|
|
|
|
|
LSerror :: defineError('LSauth_06',
|
|
|
|
|
_("LSauth : Not correctly initialized.")
|
|
|
|
|
);
|
|
|
|
|
LSerror :: defineError('LSauth_07',
|
|
|
|
|
_("LSauth : Failed to get authentication informations from provider.")
|
|
|
|
|
);
|