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();
39 $this->bindSystemUser($ldapConnection);
42 $userFilter = $this->buildFilter($this->config['user_filter'], ['user' => $userName]);
43 $baseDn = $this->config['base_dn'];
44 $users = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, $userFilter, ['cn', 'uid', 'dn', 'mail']);
45 if ($users['count'] === 0) return null;
49 'uid' => (isset($user['uid'])) ? $user['uid'][0] : $user['dn'],
50 'name' => $user['cn'][0],
52 'email' => (isset($user['mail'])) ? $user['mail'][0] : null
57 * @param Authenticatable $user
58 * @param string $username
59 * @param string $password
61 * @throws LdapException
63 public function validateUserCredentials(Authenticatable $user, $username, $password)
65 $ldapUser = $this->getUserDetails($username);
66 if ($ldapUser === null) return false;
67 if ($ldapUser['uid'] !== $user->external_auth_id) return false;
69 $ldapConnection = $this->getConnection();
71 $ldapBind = $this->ldap->bind($ldapConnection, $ldapUser['dn'], $password);
72 } catch (\ErrorException $e) {
80 * Bind the system user to the LDAP connection using the given credentials
81 * otherwise anonymous access is attempted.
83 * @throws LdapException
85 protected function bindSystemUser($connection)
87 $ldapDn = $this->config['dn'];
88 $ldapPass = $this->config['pass'];
90 $isAnonymous = ($ldapDn === false || $ldapPass === false);
92 $ldapBind = $this->ldap->bind($connection);
94 $ldapBind = $this->ldap->bind($connection, $ldapDn, $ldapPass);
97 if (!$ldapBind) throw new LdapException(($isAnonymous ? trans('errors.ldap_fail_anonymous') : trans('errors.ldap_fail_authed')));
101 * Get the connection to the LDAP server.
102 * Creates a new connection if one does not exist.
104 * @throws LdapException
106 protected function getConnection()
108 if ($this->ldapConnection !== null) return $this->ldapConnection;
110 // Check LDAP extension in installed
111 if (!function_exists('ldap_connect') && config('app.env') !== 'testing') {
112 throw new LdapException(trans('errors.ldap_extension_not_installed'));
115 // Get port from server string if specified.
116 $ldapServer = explode(':', $this->config['server']);
117 $ldapConnection = $this->ldap->connect($ldapServer[0], count($ldapServer) > 1 ? $ldapServer[1] : 389);
119 if ($ldapConnection === false) {
120 throw new LdapException(trans('errors.ldap_cannot_connect'));
123 // Set any required options
124 if ($this->config['version']) {
125 $this->ldap->setVersion($ldapConnection, $this->config['version']);
128 $this->ldapConnection = $ldapConnection;
129 return $this->ldapConnection;
133 * Build a filter string by injecting common variables.
134 * @param string $filterString
135 * @param array $attrs
138 protected function buildFilter($filterString, array $attrs)
141 foreach ($attrs as $key => $attrText) {
142 $newKey = '${' . $key . '}';
143 $newAttrs[$newKey] = $attrText;
145 return strtr($filterString, $newAttrs);