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 $emailAttr = $this->config['email_attribute'];
45 $followReferrals = $this->config['follow_referrals'] ? 1 : 0;
46 $this->ldap->setOption($ldapConnection, LDAP_OPT_REFERRALS, $followReferrals);
47 $users = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, $userFilter, ['cn', 'uid', 'dn', $emailAttr]);
48 if ($users['count'] === 0) return null;
52 'uid' => (isset($user['uid'])) ? $user['uid'][0] : $user['dn'],
53 'name' => $user['cn'][0],
55 'email' => (isset($user[$emailAttr])) ? (is_array($user[$emailAttr]) ? $user[$emailAttr][0] : $user[$emailAttr]) : null
60 * @param Authenticatable $user
61 * @param string $username
62 * @param string $password
64 * @throws LdapException
66 public function validateUserCredentials(Authenticatable $user, $username, $password)
68 $ldapUser = $this->getUserDetails($username);
69 if ($ldapUser === null) return false;
70 if ($ldapUser['uid'] !== $user->external_auth_id) return false;
72 $ldapConnection = $this->getConnection();
74 $ldapBind = $this->ldap->bind($ldapConnection, $ldapUser['dn'], $password);
75 } catch (\ErrorException $e) {
83 * Bind the system user to the LDAP connection using the given credentials
84 * otherwise anonymous access is attempted.
86 * @throws LdapException
88 protected function bindSystemUser($connection)
90 $ldapDn = $this->config['dn'];
91 $ldapPass = $this->config['pass'];
93 $isAnonymous = ($ldapDn === false || $ldapPass === false);
95 $ldapBind = $this->ldap->bind($connection);
97 $ldapBind = $this->ldap->bind($connection, $ldapDn, $ldapPass);
100 if (!$ldapBind) throw new LdapException(($isAnonymous ? trans('errors.ldap_fail_anonymous') : trans('errors.ldap_fail_authed')));
104 * Get the connection to the LDAP server.
105 * Creates a new connection if one does not exist.
107 * @throws LdapException
109 protected function getConnection()
111 if ($this->ldapConnection !== null) return $this->ldapConnection;
113 // Check LDAP extension in installed
114 if (!function_exists('ldap_connect') && config('app.env') !== 'testing') {
115 throw new LdapException(trans('errors.ldap_extension_not_installed'));
118 // Get port from server string and protocol if specified.
119 $ldapServer = explode(':', $this->config['server']);
120 $hasProtocol = preg_match('/^ldaps{0,1}\:\/\//', $this->config['server']) === 1;
121 if (!$hasProtocol) array_unshift($ldapServer, '');
122 $hostName = $ldapServer[0] . ($hasProtocol?':':'') . $ldapServer[1];
123 $defaultPort = $ldapServer[0] === 'ldaps' ? 636 : 389;
124 $ldapConnection = $this->ldap->connect($hostName, count($ldapServer) > 2 ? intval($ldapServer[2]) : $defaultPort);
126 if ($ldapConnection === false) {
127 throw new LdapException(trans('errors.ldap_cannot_connect'));
130 // Set any required options
131 if ($this->config['version']) {
132 $this->ldap->setVersion($ldapConnection, $this->config['version']);
135 $this->ldapConnection = $ldapConnection;
136 return $this->ldapConnection;
140 * Build a filter string by injecting common variables.
141 * @param string $filterString
142 * @param array $attrs
145 protected function buildFilter($filterString, array $attrs)
148 foreach ($attrs as $key => $attrText) {
149 $newKey = '${' . $key . '}';
150 $newAttrs[$newKey] = $attrText;
152 return strtr($filterString, $newAttrs);