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