1 <?php namespace BookStack\Services;
4 use BookStack\Exceptions\LdapException;
5 use Illuminate\Contracts\Auth\Authenticatable;
9 * Handles any app-specific LDAP tasks.
10 * @package BookStack\Services
16 protected $ldapConnection;
20 * LdapService constructor.
23 public function __construct(Ldap $ldap)
26 $this->config = config('services.ldap');
30 * Get the details of a user from LDAP using the given username.
31 * User found via configurable user filter.
34 * @throws LdapException
36 public function getUserDetails($userName)
38 $ldapConnection = $this->getConnection();
41 $userFilter = $this->buildFilter($this->config['user_filter'], ['user' => $userName]);
42 $baseDn = $this->config['base_dn'];
43 $users = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, $userFilter, ['cn', 'uid', 'dn', 'mail']);
44 if ($users['count'] === 0) return null;
48 'uid' => $user['uid'][0],
49 'name' => $user['cn'][0],
51 'email' => (isset($user['mail'])) ? $user['mail'][0] : null
56 * @param Authenticatable $user
57 * @param string $username
58 * @param string $password
60 * @throws LdapException
62 public function validateUserCredentials(Authenticatable $user, $username, $password)
64 $ldapUser = $this->getUserDetails($username);
65 if ($ldapUser === null) return false;
66 if ($ldapUser['uid'] !== $user->external_auth_id) return false;
68 $ldapConnection = $this->getConnection();
70 $ldapBind = $this->ldap->bind($ldapConnection, $ldapUser['dn'], $password);
71 } catch (\ErrorException $e) {
79 * Bind the system user to the LDAP connection using the given credentials
80 * otherwise anonymous access is attempted.
82 * @throws LdapException
84 protected function bindSystemUser($connection)
86 $ldapDn = $this->config['dn'];
87 $ldapPass = $this->config['pass'];
89 $isAnonymous = ($ldapDn === false || $ldapPass === false);
91 $ldapBind = $this->ldap->bind($connection);
93 $ldapBind = $this->ldap->bind($connection, $ldapDn, $ldapPass);
96 if (!$ldapBind) throw new LdapException('LDAP access failed using ' . $isAnonymous ? ' anonymous bind.' : ' given dn & pass details');
100 * Get the connection to the LDAP server.
101 * Creates a new connection if one does not exist.
103 * @throws LdapException
105 protected function getConnection()
107 if ($this->ldapConnection !== null) return $this->ldapConnection;
109 // Check LDAP extension in installed
110 if (!function_exists('ldap_connect') && config('app.env') !== 'testing') {
111 throw new LdapException('LDAP PHP extension not installed');
114 // Get port from server string if specified.
115 $ldapServer = explode(':', $this->config['server']);
116 $ldapConnection = $this->ldap->connect($ldapServer[0], count($ldapServer) > 1 ? $ldapServer[1] : 389);
118 if ($ldapConnection === false) {
119 throw new LdapException('Cannot connect to ldap server, Initial connection failed');
122 // Set any required options
123 if ($this->config['version']) {
124 $this->ldap->setOption($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, $this->config['version']);
127 $this->ldapConnection = $ldapConnection;
128 return $this->ldapConnection;
132 * Build a filter string by injecting common variables.
133 * @param string $filterString
134 * @param array $attrs
137 protected function buildFilter($filterString, array $attrs)
140 foreach ($attrs as $key => $attrText) {
141 $newKey = '${' . $key . '}';
142 $newAttrs[$newKey] = $attrText;
144 return strtr($filterString, $newAttrs);