2014-06-03 17:59:22 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 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
|
|
|
|
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
|
|
|
{
|
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-05-04 12:15:50 +02:00
|
|
|
* Set this backend's name
|
|
|
|
*
|
|
|
|
* @param string $name
|
2014-06-11 15:33:33 +02:00
|
|
|
*
|
2015-05-04 12:15:50 +02:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return this backend's name
|
2014-06-11 15:33:33 +02:00
|
|
|
*
|
2015-05-04 12:15:50 +02:00
|
|
|
* @return string
|
2014-06-03 17:59:22 +02:00
|
|
|
*/
|
2015-05-04 12:15:50 +02:00
|
|
|
public function getName()
|
2014-06-03 17:59:22 +02:00
|
|
|
{
|
2015-05-04 12:15:50 +02:00
|
|
|
return $this->name;
|
2014-06-03 17:59:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-04 12:15:50 +02:00
|
|
|
* Authenticate the given user
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param string $password
|
2014-06-03 17:59:22 +02:00
|
|
|
*
|
2015-05-04 12:15:50 +02:00
|
|
|
* @return bool True on success, false on failure
|
2014-06-03 17:59:22 +02:00
|
|
|
*
|
2015-05-04 12:15:50 +02:00
|
|
|
* @throws AuthenticationException In case authentication is not possible due to an error
|
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
|
|
|
{
|
2014-06-11 14:47:15 +02:00
|
|
|
if (isset($_SERVER['REMOTE_USER'])) {
|
|
|
|
$username = $_SERVER['REMOTE_USER'];
|
2014-07-30 12:35:55 +02:00
|
|
|
$user->setRemoteUserInformation($username, 'REMOTE_USER');
|
2015-04-21 13:15:06 +02:00
|
|
|
|
2014-10-20 15:14:14 +02:00
|
|
|
if ($this->stripUsernameRegexp) {
|
2014-06-11 15:27:36 +02:00
|
|
|
$stripped = preg_replace($this->stripUsernameRegexp, '', $username);
|
|
|
|
if ($stripped !== false) {
|
|
|
|
// TODO(el): PHP issues a warning when PHP cannot compile the regular expression. Should we log an
|
|
|
|
// additional message in that case?
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|