1 <?php namespace BookStack\Auth\Access;
3 use BookStack\Auth\Access;
4 use BookStack\Auth\User;
5 use BookStack\Auth\UserRepo;
6 use BookStack\Exceptions\LdapException;
7 use Illuminate\Contracts\Auth\Authenticatable;
8 use Illuminate\Database\Eloquent\Builder;
12 * Handles any app-specific LDAP tasks.
13 * @package BookStack\Services
15 class LdapService extends Access\ExternalAuthService
19 protected $ldapConnection;
25 * LdapService constructor.
27 * @param \BookStack\Auth\UserRepo $userRepo
29 public function __construct(Access\Ldap $ldap, UserRepo $userRepo)
32 $this->config = config('services.ldap');
33 $this->userRepo = $userRepo;
34 $this->enabled = config('auth.method') === 'ldap';
38 * Check if groups should be synced.
41 public function shouldSyncGroups()
43 return $this->enabled && $this->config['user_to_groups'] !== false;
47 * Search for attributes for a specific user on the ldap
48 * @param string $userName
49 * @param array $attributes
51 * @throws LdapException
53 private function getUserWithAttributes($userName, $attributes)
55 $ldapConnection = $this->getConnection();
56 $this->bindSystemUser($ldapConnection);
59 $userFilter = $this->buildFilter($this->config['user_filter'], ['user' => $userName]);
60 $baseDn = $this->config['base_dn'];
62 $followReferrals = $this->config['follow_referrals'] ? 1 : 0;
63 $this->ldap->setOption($ldapConnection, LDAP_OPT_REFERRALS, $followReferrals);
64 $users = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, $userFilter, $attributes);
65 if ($users['count'] === 0) {
73 * Get the details of a user from LDAP using the given username.
74 * User found via configurable user filter.
77 * @throws LdapException
79 public function getUserDetails($userName)
81 $emailAttr = $this->config['email_attribute'];
82 $displayNameAttr = $this->config['display_name_attribute'];
84 $user = $this->getUserWithAttributes($userName, ['cn', 'uid', 'dn', $emailAttr, $displayNameAttr]);
90 $userCn = $this->getUserResponseProperty($user, 'cn', null);
92 'uid' => $this->getUserResponseProperty($user, 'uid', $user['dn']),
93 'name' => $this->getUserResponseProperty($user, $displayNameAttr, $userCn),
95 'email' => $this->getUserResponseProperty($user, $emailAttr, null),
100 * Get a property from an LDAP user response fetch.
101 * Handles properties potentially being part of an array.
102 * @param array $userDetails
103 * @param string $propertyKey
104 * @param $defaultValue
107 protected function getUserResponseProperty(array $userDetails, string $propertyKey, $defaultValue)
109 if (isset($userDetails[$propertyKey])) {
110 return (is_array($userDetails[$propertyKey]) ? $userDetails[$propertyKey][0] : $userDetails[$propertyKey]);
113 return $defaultValue;
117 * @param Authenticatable $user
118 * @param string $username
119 * @param string $password
121 * @throws LdapException
123 public function validateUserCredentials(Authenticatable $user, $username, $password)
125 $ldapUser = $this->getUserDetails($username);
126 if ($ldapUser === null) {
130 if ($ldapUser['uid'] !== $user->external_auth_id) {
134 $ldapConnection = $this->getConnection();
136 $ldapBind = $this->ldap->bind($ldapConnection, $ldapUser['dn'], $password);
137 } catch (\ErrorException $e) {
145 * Bind the system user to the LDAP connection using the given credentials
146 * otherwise anonymous access is attempted.
148 * @throws LdapException
150 protected function bindSystemUser($connection)
152 $ldapDn = $this->config['dn'];
153 $ldapPass = $this->config['pass'];
155 $isAnonymous = ($ldapDn === false || $ldapPass === false);
157 $ldapBind = $this->ldap->bind($connection);
159 $ldapBind = $this->ldap->bind($connection, $ldapDn, $ldapPass);
163 throw new LdapException(($isAnonymous ? trans('errors.ldap_fail_anonymous') : trans('errors.ldap_fail_authed')));
168 * Get the connection to the LDAP server.
169 * Creates a new connection if one does not exist.
171 * @throws LdapException
173 protected function getConnection()
175 if ($this->ldapConnection !== null) {
176 return $this->ldapConnection;
179 // Check LDAP extension in installed
180 if (!function_exists('ldap_connect') && config('app.env') !== 'testing') {
181 throw new LdapException(trans('errors.ldap_extension_not_installed'));
184 // Check if TLS_INSECURE is set. The handle is set to NULL due to the nature of
185 // the LDAP_OPT_X_TLS_REQUIRE_CERT option. It can only be set globally and not per handle.
186 if ($this->config['tls_insecure']) {
187 $this->ldap->setOption(null, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_NEVER);
190 $serverDetails = $this->parseServerString($this->config['server']);
191 $ldapConnection = $this->ldap->connect($serverDetails['host'], $serverDetails['port']);
193 if ($ldapConnection === false) {
194 throw new LdapException(trans('errors.ldap_cannot_connect'));
197 // Set any required options
198 if ($this->config['version']) {
199 $this->ldap->setVersion($ldapConnection, $this->config['version']);
202 $this->ldapConnection = $ldapConnection;
203 return $this->ldapConnection;
207 * Parse a LDAP server string and return the host and port for
208 * a connection. Is flexible to formats such as 'ldap.example.com:8069' or 'ldaps://ldap.example.com'
209 * @param $serverString
212 protected function parseServerString($serverString)
214 $serverNameParts = explode(':', $serverString);
216 // If we have a protocol just return the full string since PHP will ignore a separate port.
217 if ($serverNameParts[0] === 'ldaps' || $serverNameParts[0] === 'ldap') {
218 return ['host' => $serverString, 'port' => 389];
221 // Otherwise, extract the port out
222 $hostName = $serverNameParts[0];
223 $ldapPort = (count($serverNameParts) > 1) ? intval($serverNameParts[1]) : 389;
224 return ['host' => $hostName, 'port' => $ldapPort];
228 * Build a filter string by injecting common variables.
229 * @param string $filterString
230 * @param array $attrs
233 protected function buildFilter($filterString, array $attrs)
236 foreach ($attrs as $key => $attrText) {
237 $newKey = '${' . $key . '}';
238 $newAttrs[$newKey] = $this->ldap->escape($attrText);
240 return strtr($filterString, $newAttrs);
244 * Get the groups a user is a part of on ldap
245 * @param string $userName
247 * @throws LdapException
249 public function getUserGroups($userName)
251 $groupsAttr = $this->config['group_attribute'];
252 $user = $this->getUserWithAttributes($userName, [$groupsAttr]);
254 if ($user === null) {
258 $userGroups = $this->groupFilter($user);
259 $userGroups = $this->getGroupsRecursive($userGroups, []);
264 * Get the parent groups of an array of groups
265 * @param array $groupsArray
266 * @param array $checked
268 * @throws LdapException
270 private function getGroupsRecursive($groupsArray, $checked)
273 foreach ($groupsArray as $groupName) {
274 if (in_array($groupName, $checked)) {
278 $groupsToAdd = $this->getGroupGroups($groupName);
279 $groups_to_add = array_merge($groups_to_add, $groupsToAdd);
280 $checked[] = $groupName;
282 $groupsArray = array_unique(array_merge($groupsArray, $groups_to_add), SORT_REGULAR);
284 if (!empty($groups_to_add)) {
285 return $this->getGroupsRecursive($groupsArray, $checked);
292 * Get the parent groups of a single group
293 * @param string $groupName
295 * @throws LdapException
297 private function getGroupGroups($groupName)
299 $ldapConnection = $this->getConnection();
300 $this->bindSystemUser($ldapConnection);
302 $followReferrals = $this->config['follow_referrals'] ? 1 : 0;
303 $this->ldap->setOption($ldapConnection, LDAP_OPT_REFERRALS, $followReferrals);
305 $baseDn = $this->config['base_dn'];
306 $groupsAttr = strtolower($this->config['group_attribute']);
308 $groupFilter = 'CN=' . $this->ldap->escape($groupName);
309 $groups = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, $groupFilter, [$groupsAttr]);
310 if ($groups['count'] === 0) {
314 $groupGroups = $this->groupFilter($groups[0]);
319 * Filter out LDAP CN and DN language in a ldap search return
320 * Gets the base CN (common name) of the string
321 * @param array $userGroupSearchResponse
324 protected function groupFilter(array $userGroupSearchResponse)
326 $groupsAttr = strtolower($this->config['group_attribute']);
330 if (isset($userGroupSearchResponse[$groupsAttr]['count'])) {
331 $count = (int)$userGroupSearchResponse[$groupsAttr]['count'];
334 for ($i = 0; $i < $count; $i++) {
335 $dnComponents = $this->ldap->explodeDn($userGroupSearchResponse[$groupsAttr][$i], 1);
336 if (!in_array($dnComponents[0], $ldapGroups)) {
337 $ldapGroups[] = $dnComponents[0];
345 * Sync the LDAP groups to the user roles for the current user
346 * @param \BookStack\Auth\User $user
347 * @param string $username
348 * @throws LdapException
350 public function syncGroups(User $user, string $username)
352 $userLdapGroups = $this->getUserGroups($username);
353 $this->syncWithGroups($user, $userLdapGroups);