1 <?php namespace BookStack\Services;
3 use BookStack\Exceptions\LdapException;
4 use Illuminate\Contracts\Auth\Authenticatable;
8 * Handles any app-specific LDAP tasks.
9 * @package BookStack\Services
15 protected $ldapConnection;
19 * LdapService constructor.
22 public function __construct(Ldap $ldap)
25 $this->config = config('services.ldap');
29 * Get the details of a user from LDAP using the given username.
30 * User found via configurable user filter.
33 * @throws LdapException
35 public function getUserDetails($userName)
37 $ldapConnection = $this->getConnection();
38 $this->bindSystemUser($ldapConnection);
41 $userFilter = $this->buildFilter($this->config['user_filter'], ['user' => $userName]);
42 $baseDn = $this->config['base_dn'];
43 $emailAttr = $this->config['email_attribute'];
44 $followReferrals = $this->config['follow_referrals'] ? 1 : 0;
45 $this->ldap->setOption($ldapConnection, LDAP_OPT_REFERRALS, $followReferrals);
46 $users = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, $userFilter, ['cn', 'uid', 'dn', $emailAttr]);
47 if ($users['count'] === 0) {
53 'uid' => (isset($user['uid'])) ? $user['uid'][0] : $user['dn'],
54 'name' => $user['cn'][0],
56 'email' => (isset($user[$emailAttr])) ? (is_array($user[$emailAttr]) ? $user[$emailAttr][0] : $user[$emailAttr]) : null
61 * @param Authenticatable $user
62 * @param string $username
63 * @param string $password
65 * @throws LdapException
67 public function validateUserCredentials(Authenticatable $user, $username, $password)
69 $ldapUser = $this->getUserDetails($username);
70 if ($ldapUser === null) {
73 if ($ldapUser['uid'] !== $user->external_auth_id) {
77 $ldapConnection = $this->getConnection();
79 $ldapBind = $this->ldap->bind($ldapConnection, $ldapUser['dn'], $password);
80 } catch (\ErrorException $e) {
88 * Bind the system user to the LDAP connection using the given credentials
89 * otherwise anonymous access is attempted.
91 * @throws LdapException
93 protected function bindSystemUser($connection)
95 $ldapDn = $this->config['dn'];
96 $ldapPass = $this->config['pass'];
98 $isAnonymous = ($ldapDn === false || $ldapPass === false);
100 $ldapBind = $this->ldap->bind($connection);
102 $ldapBind = $this->ldap->bind($connection, $ldapDn, $ldapPass);
106 throw new LdapException(($isAnonymous ? trans('errors.ldap_fail_anonymous') : trans('errors.ldap_fail_authed')));
111 * Get the connection to the LDAP server.
112 * Creates a new connection if one does not exist.
114 * @throws LdapException
116 protected function getConnection()
118 if ($this->ldapConnection !== null) {
119 return $this->ldapConnection;
122 // Check LDAP extension in installed
123 if (!function_exists('ldap_connect') && config('app.env') !== 'testing') {
124 throw new LdapException(trans('errors.ldap_extension_not_installed'));
127 // Get port from server string and protocol if specified.
128 $ldapServer = explode(':', $this->config['server']);
129 $hasProtocol = preg_match('/^ldaps{0,1}\:\/\//', $this->config['server']) === 1;
131 array_unshift($ldapServer, '');
133 $hostName = $ldapServer[0] . ($hasProtocol?':':'') . $ldapServer[1];
134 $defaultPort = $ldapServer[0] === 'ldaps' ? 636 : 389;
135 $ldapConnection = $this->ldap->connect($hostName, count($ldapServer) > 2 ? intval($ldapServer[2]) : $defaultPort);
137 if ($ldapConnection === false) {
138 throw new LdapException(trans('errors.ldap_cannot_connect'));
141 // Set any required options
142 if ($this->config['version']) {
143 $this->ldap->setVersion($ldapConnection, $this->config['version']);
146 $this->ldapConnection = $ldapConnection;
147 return $this->ldapConnection;
151 * Build a filter string by injecting common variables.
152 * @param string $filterString
153 * @param array $attrs
156 protected function buildFilter($filterString, array $attrs)
159 foreach ($attrs as $key => $attrText) {
160 $newKey = '${' . $key . '}';
161 $newAttrs[$newKey] = $attrText;
163 return strtr($filterString, $newAttrs);