1 <?php namespace BookStack\Services;
4 use BookStack\Exceptions\LdapException;
5 use Illuminate\Contracts\Auth\Authenticatable;
10 protected $ldapConnection;
13 * Get the details of a user from LDAP using the given username.
14 * User found via configurable user filter.
17 * @throws LdapException
19 public function getUserDetails($userName)
21 $ldapConnection = $this->getConnection();
24 $userFilter = $this->buildFilter(config('services.ldap.user_filter'), ['user' => $userName]);
25 $baseDn = config('services.ldap.base_dn');
26 $ldapSearch = ldap_search($ldapConnection, $baseDn, $userFilter, ['cn', 'uid', 'dn', 'mail']);
27 $users = ldap_get_entries($ldapConnection, $ldapSearch);
28 if ($users['count'] === 0) return null;
32 'uid' => $user['uid'][0],
33 'name' => $user['cn'][0],
35 'email' => (isset($user['mail'])) ? $user['mail'][0] : null
40 * @param Authenticatable $user
41 * @param string $username
42 * @param string $password
44 * @throws LdapException
46 public function validateUserCredentials(Authenticatable $user, $username, $password)
48 $ldapUser = $this->getUserDetails($username);
49 if ($ldapUser === null) return false;
50 if ($ldapUser['uid'] !== $user->external_auth_id) return false;
52 $ldapConnection = $this->getConnection();
53 $ldapBind = @ldap_bind($ldapConnection, $ldapUser['dn'], $password);
58 * Bind the system user to the LDAP connection using the given credentials
59 * otherwise anonymous access is attempted.
61 * @throws LdapException
63 protected function bindSystemUser($connection)
65 $ldapDn = config('services.ldap.dn');
66 $ldapPass = config('services.ldap.pass');
68 $isAnonymous = ($ldapDn === false || $ldapPass === false);
70 $ldapBind = ldap_bind($connection);
72 $ldapBind = ldap_bind($connection, $ldapDn, $ldapPass);
75 if (!$ldapBind) throw new LdapException('LDAP access failed using ' . $isAnonymous ? ' anonymous bind.' : ' given dn & pass details');
79 * Get the connection to the LDAP server.
80 * Creates a new connection if one does not exist.
82 * @throws LdapException
84 protected function getConnection()
86 if ($this->ldapConnection !== null) return $this->ldapConnection;
88 // Check LDAP extension in installed
89 if (!function_exists('ldap_connect')) {
90 throw new LdapException('LDAP PHP extension not installed');
93 // Get port from server string if specified.
94 $ldapServer = explode(':', config('services.ldap.server'));
95 $ldapConnection = ldap_connect($ldapServer[0], count($ldapServer) > 1 ? $ldapServer[1] : 389);
97 if ($ldapConnection === false) {
98 throw new LdapException('Cannot connect to ldap server, Initial connection failed');
101 // Set any required options
102 ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3); // TODO - make configurable
104 $this->ldapConnection = $ldapConnection;
105 return $this->ldapConnection;
109 * Build a filter string by injecting common variables.
110 * @param $filterString
111 * @param array $attrs
114 protected function buildFilter($filterString, array $attrs)
117 foreach ($attrs as $key => $attrText) {
118 $newKey = '${' . $key . '}';
119 $newAttrs[$newKey] = $attrText;
121 return strtr($filterString, $newAttrs);