2014-06-03 11:59:22 -04:00
|
|
|
<?php
|
2016-02-08 09:41:00 -05:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-06-03 11:59:22 -04:00
|
|
|
|
2015-04-21 06:51:31 -04:00
|
|
|
namespace Icinga\Authentication\User;
|
2014-06-03 11:59:22 -04:00
|
|
|
|
2016-11-16 06:04:46 -05:00
|
|
|
use Icinga\Application\Logger;
|
2014-11-18 07:11:52 -05:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-06-03 11:59:22 -04:00
|
|
|
use Icinga\User;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test login with external authentication mechanism, e.g. Apache
|
|
|
|
|
*/
|
2015-05-04 06:15:50 -04:00
|
|
|
class ExternalBackend implements UserBackendInterface
|
2014-06-03 11:59:22 -04:00
|
|
|
{
|
2016-11-04 12:27:36 -04:00
|
|
|
/**
|
|
|
|
|
* Possible variables where to read the user from
|
|
|
|
|
*
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
2016-11-16 05:55:54 -05:00
|
|
|
public static $remoteUserEnvvars = array('REMOTE_USER', 'REDIRECT_REMOTE_USER');
|
2016-11-04 12:27:36 -04:00
|
|
|
|
2015-05-04 06:15:50 -04:00
|
|
|
/**
|
|
|
|
|
* The name of this backend
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $name;
|
|
|
|
|
|
2014-06-03 11:59:22 -04:00
|
|
|
/**
|
|
|
|
|
* Regexp expression to strip values from a username
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2015-05-04 06:15:50 -04:00
|
|
|
protected $stripUsernameRegexp;
|
2014-06-03 11:59:22 -04:00
|
|
|
|
|
|
|
|
/**
|
2015-01-27 03:49:36 -05:00
|
|
|
* Create new authentication backend of type "external"
|
2014-06-03 11:59:22 -04:00
|
|
|
*
|
2014-11-18 07:11:52 -05:00
|
|
|
* @param ConfigObject $config
|
2014-06-03 11:59:22 -04:00
|
|
|
*/
|
2014-11-18 07:11:52 -05:00
|
|
|
public function __construct(ConfigObject $config)
|
2014-06-03 11:59:22 -04:00
|
|
|
{
|
|
|
|
|
$this->stripUsernameRegexp = $config->get('strip_username_regexp');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-07-29 09:46:40 -04:00
|
|
|
* {@inheritdoc}
|
2015-05-04 06:15:50 -04:00
|
|
|
*/
|
2016-04-11 04:57:01 -04:00
|
|
|
public function getName()
|
2015-05-04 06:15:50 -04:00
|
|
|
{
|
2016-04-11 04:57:01 -04:00
|
|
|
return $this->name;
|
2015-05-04 06:15:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-07-29 09:46:40 -04:00
|
|
|
* {@inheritdoc}
|
2014-06-03 11:59:22 -04:00
|
|
|
*/
|
2016-04-11 04:57:01 -04:00
|
|
|
public function setName($name)
|
2014-06-03 11:59:22 -04:00
|
|
|
{
|
2016-04-11 04:57:01 -04:00
|
|
|
$this->name = $name;
|
|
|
|
|
return $this;
|
2014-06-03 11:59:22 -04:00
|
|
|
}
|
|
|
|
|
|
2016-04-11 08:01:36 -04:00
|
|
|
/**
|
|
|
|
|
* Get the remote user from environment or $_SERVER, if any
|
|
|
|
|
*
|
2016-11-16 05:55:54 -05:00
|
|
|
* @param string $variable The name of the variable where to read the user from
|
2016-04-11 08:01:36 -04:00
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
2016-11-16 05:55:54 -05:00
|
|
|
public static function getRemoteUser($variable = 'REMOTE_USER')
|
2016-04-11 08:01:36 -04:00
|
|
|
{
|
2016-10-18 04:22:06 -04:00
|
|
|
$username = getenv($variable);
|
2019-12-05 09:13:02 -05:00
|
|
|
if (! empty($username)) {
|
2016-10-18 04:22:06 -04:00
|
|
|
return $username;
|
|
|
|
|
}
|
2016-11-16 05:55:54 -05:00
|
|
|
|
2019-12-05 09:13:02 -05:00
|
|
|
if (array_key_exists($variable, $_SERVER) && ! empty($_SERVER[$variable])) {
|
2016-10-18 04:22:06 -04:00
|
|
|
return $_SERVER[$variable];
|
2016-04-11 08:01:36 -04:00
|
|
|
}
|
2016-11-16 05:55:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the remote user information from environment or $_SERVER, if any
|
|
|
|
|
*
|
|
|
|
|
* @return array Contains always two entries, the username and origin which may both set to null.
|
|
|
|
|
*/
|
|
|
|
|
public static function getRemoteUserInformation()
|
|
|
|
|
{
|
|
|
|
|
foreach (static::$remoteUserEnvvars as $envVar) {
|
|
|
|
|
$username = static::getRemoteUser($envVar);
|
|
|
|
|
if ($username !== null) {
|
|
|
|
|
return array($username, $envVar);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array(null, null);
|
2016-04-11 08:01:36 -04:00
|
|
|
}
|
|
|
|
|
|
2014-06-03 11:59:22 -04:00
|
|
|
/**
|
2015-07-29 09:46:40 -04:00
|
|
|
* {@inheritdoc}
|
2014-06-03 11:59:22 -04:00
|
|
|
*/
|
2015-04-21 07:15:06 -04:00
|
|
|
public function authenticate(User $user, $password = null)
|
2014-06-03 11:59:22 -04:00
|
|
|
{
|
2016-11-16 05:55:54 -05:00
|
|
|
list($username, $field) = static::getRemoteUserInformation();
|
2016-04-11 08:07:44 -04:00
|
|
|
if ($username !== null) {
|
2016-11-16 05:55:54 -05:00
|
|
|
$user->setExternalUserInformation($username, $field);
|
2015-04-21 07:15:06 -04:00
|
|
|
|
2014-10-20 09:14:14 -04:00
|
|
|
if ($this->stripUsernameRegexp) {
|
2016-11-16 06:04:46 -05:00
|
|
|
$stripped = @preg_replace($this->stripUsernameRegexp, '', $username);
|
|
|
|
|
if ($stripped === false) {
|
|
|
|
|
Logger::error('Failed to strip external username. The configured regular expression is invalid.');
|
|
|
|
|
return false;
|
2014-06-03 11:59:22 -04:00
|
|
|
}
|
2016-11-16 06:04:46 -05:00
|
|
|
|
|
|
|
|
$username = $stripped;
|
2014-06-03 11:59:22 -04:00
|
|
|
}
|
2015-04-21 07:15:06 -04:00
|
|
|
|
2014-06-11 09:27:36 -04:00
|
|
|
$user->setUsername($username);
|
|
|
|
|
return true;
|
2014-06-03 11:59:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|