2016-01-09 14:23:35 -05:00
|
|
|
<?php namespace BookStack\Services;
|
|
|
|
|
|
|
|
|
|
|
|
use BookStack\Exceptions\LdapException;
|
2016-01-11 17:41:05 -05:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2016-01-09 14:23:35 -05:00
|
|
|
|
2016-01-15 18:21:47 -05:00
|
|
|
/**
|
|
|
|
* Class LdapService
|
|
|
|
* Handles any app-specific LDAP tasks.
|
|
|
|
* @package BookStack\Services
|
|
|
|
*/
|
2016-01-09 14:23:35 -05:00
|
|
|
class LdapService
|
|
|
|
{
|
|
|
|
|
2016-01-15 18:21:47 -05:00
|
|
|
protected $ldap;
|
2016-01-11 17:41:05 -05:00
|
|
|
protected $ldapConnection;
|
2016-01-15 18:21:47 -05:00
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LdapService constructor.
|
|
|
|
* @param Ldap $ldap
|
|
|
|
*/
|
|
|
|
public function __construct(Ldap $ldap)
|
|
|
|
{
|
|
|
|
$this->ldap = $ldap;
|
|
|
|
$this->config = config('services.ldap');
|
|
|
|
}
|
2016-01-11 17:41:05 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the details of a user from LDAP using the given username.
|
|
|
|
* User found via configurable user filter.
|
|
|
|
* @param $userName
|
|
|
|
* @return array|null
|
|
|
|
* @throws LdapException
|
|
|
|
*/
|
2016-01-09 14:23:35 -05:00
|
|
|
public function getUserDetails($userName)
|
|
|
|
{
|
2016-01-11 17:41:05 -05:00
|
|
|
$ldapConnection = $this->getConnection();
|
2016-01-17 10:20:07 -05:00
|
|
|
$this->bindSystemUser($ldapConnection);
|
2016-01-09 14:23:35 -05:00
|
|
|
|
2016-01-11 17:41:05 -05:00
|
|
|
// Find user
|
2016-01-15 18:21:47 -05:00
|
|
|
$userFilter = $this->buildFilter($this->config['user_filter'], ['user' => $userName]);
|
|
|
|
$baseDn = $this->config['base_dn'];
|
|
|
|
$users = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, $userFilter, ['cn', 'uid', 'dn', 'mail']);
|
2016-01-11 17:41:05 -05:00
|
|
|
if ($users['count'] === 0) return null;
|
|
|
|
|
|
|
|
$user = $users[0];
|
|
|
|
return [
|
2016-02-08 14:45:01 -05:00
|
|
|
'uid' => (isset($user['uid'])) ? $user['uid'][0] : $user['dn'],
|
2016-01-15 18:21:47 -05:00
|
|
|
'name' => $user['cn'][0],
|
|
|
|
'dn' => $user['dn'],
|
2016-01-13 17:22:30 -05:00
|
|
|
'email' => (isset($user['mail'])) ? $user['mail'][0] : null
|
2016-01-11 17:41:05 -05:00
|
|
|
];
|
|
|
|
}
|
2016-01-09 14:23:35 -05:00
|
|
|
|
2016-01-11 17:41:05 -05:00
|
|
|
/**
|
|
|
|
* @param Authenticatable $user
|
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @return bool
|
|
|
|
* @throws LdapException
|
|
|
|
*/
|
|
|
|
public function validateUserCredentials(Authenticatable $user, $username, $password)
|
|
|
|
{
|
|
|
|
$ldapUser = $this->getUserDetails($username);
|
|
|
|
if ($ldapUser === null) return false;
|
|
|
|
if ($ldapUser['uid'] !== $user->external_auth_id) return false;
|
2016-01-09 14:23:35 -05:00
|
|
|
|
2016-01-11 17:41:05 -05:00
|
|
|
$ldapConnection = $this->getConnection();
|
2016-01-15 18:21:47 -05:00
|
|
|
try {
|
|
|
|
$ldapBind = $this->ldap->bind($ldapConnection, $ldapUser['dn'], $password);
|
|
|
|
} catch (\ErrorException $e) {
|
|
|
|
$ldapBind = false;
|
|
|
|
}
|
|
|
|
|
2016-01-11 17:41:05 -05:00
|
|
|
return $ldapBind;
|
|
|
|
}
|
2016-01-09 14:23:35 -05:00
|
|
|
|
2016-01-11 17:41:05 -05:00
|
|
|
/**
|
|
|
|
* Bind the system user to the LDAP connection using the given credentials
|
|
|
|
* otherwise anonymous access is attempted.
|
|
|
|
* @param $connection
|
|
|
|
* @throws LdapException
|
|
|
|
*/
|
|
|
|
protected function bindSystemUser($connection)
|
|
|
|
{
|
2016-01-15 18:21:47 -05:00
|
|
|
$ldapDn = $this->config['dn'];
|
|
|
|
$ldapPass = $this->config['pass'];
|
2016-01-11 17:41:05 -05:00
|
|
|
|
2016-01-09 14:23:35 -05:00
|
|
|
$isAnonymous = ($ldapDn === false || $ldapPass === false);
|
|
|
|
if ($isAnonymous) {
|
2016-01-15 18:21:47 -05:00
|
|
|
$ldapBind = $this->ldap->bind($connection);
|
2016-01-09 14:23:35 -05:00
|
|
|
} else {
|
2016-01-15 18:21:47 -05:00
|
|
|
$ldapBind = $this->ldap->bind($connection, $ldapDn, $ldapPass);
|
2016-01-09 14:23:35 -05:00
|
|
|
}
|
|
|
|
|
2016-01-17 10:20:07 -05:00
|
|
|
if (!$ldapBind) throw new LdapException('LDAP access failed using ' . ($isAnonymous ? ' anonymous bind.' : ' given dn & pass details'));
|
2016-01-11 17:41:05 -05:00
|
|
|
}
|
2016-01-09 14:23:35 -05:00
|
|
|
|
2016-01-11 17:41:05 -05:00
|
|
|
/**
|
|
|
|
* Get the connection to the LDAP server.
|
|
|
|
* Creates a new connection if one does not exist.
|
|
|
|
* @return resource
|
|
|
|
* @throws LdapException
|
|
|
|
*/
|
|
|
|
protected function getConnection()
|
|
|
|
{
|
|
|
|
if ($this->ldapConnection !== null) return $this->ldapConnection;
|
2016-01-09 14:23:35 -05:00
|
|
|
|
2016-01-11 17:41:05 -05:00
|
|
|
// Check LDAP extension in installed
|
2016-01-15 18:21:47 -05:00
|
|
|
if (!function_exists('ldap_connect') && config('app.env') !== 'testing') {
|
2016-01-11 17:41:05 -05:00
|
|
|
throw new LdapException('LDAP PHP extension not installed');
|
|
|
|
}
|
2016-01-09 14:23:35 -05:00
|
|
|
|
2016-01-11 17:41:05 -05:00
|
|
|
// Get port from server string if specified.
|
2016-01-15 18:21:47 -05:00
|
|
|
$ldapServer = explode(':', $this->config['server']);
|
|
|
|
$ldapConnection = $this->ldap->connect($ldapServer[0], count($ldapServer) > 1 ? $ldapServer[1] : 389);
|
2016-01-11 17:41:05 -05:00
|
|
|
|
|
|
|
if ($ldapConnection === false) {
|
|
|
|
throw new LdapException('Cannot connect to ldap server, Initial connection failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set any required options
|
2016-01-15 18:21:47 -05:00
|
|
|
if ($this->config['version']) {
|
|
|
|
$this->ldap->setOption($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, $this->config['version']);
|
|
|
|
}
|
2016-01-11 17:41:05 -05:00
|
|
|
|
|
|
|
$this->ldapConnection = $ldapConnection;
|
|
|
|
return $this->ldapConnection;
|
|
|
|
}
|
2016-01-09 14:23:35 -05:00
|
|
|
|
2016-01-11 17:41:05 -05:00
|
|
|
/**
|
|
|
|
* Build a filter string by injecting common variables.
|
2016-01-15 18:21:47 -05:00
|
|
|
* @param string $filterString
|
2016-01-11 17:41:05 -05:00
|
|
|
* @param array $attrs
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function buildFilter($filterString, array $attrs)
|
2016-01-09 14:23:35 -05:00
|
|
|
{
|
|
|
|
$newAttrs = [];
|
|
|
|
foreach ($attrs as $key => $attrText) {
|
2016-01-11 17:41:05 -05:00
|
|
|
$newKey = '${' . $key . '}';
|
2016-01-09 14:23:35 -05:00
|
|
|
$newAttrs[$newKey] = $attrText;
|
|
|
|
}
|
|
|
|
return strtr($filterString, $newAttrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|