| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  | <?php namespace BookStack\Services; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Exceptions\LdapException; | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  | use Illuminate\Contracts\Auth\Authenticatable; | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class LdapService | 
					
						
							|  |  |  |  * Handles any app-specific LDAP tasks. | 
					
						
							|  |  |  |  * @package BookStack\Services | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  | class LdapService | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |     protected $ldap; | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |     protected $ldapConnection; | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |     protected $config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * LdapService constructor. | 
					
						
							|  |  |  |      * @param Ldap $ldap | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(Ldap $ldap) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->ldap = $ldap; | 
					
						
							|  |  |  |         $this->config = config('services.ldap'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the details of a user from LDAP using the given username. | 
					
						
							|  |  |  |      * User found via configurable user filter. | 
					
						
							|  |  |  |      * @param $userName | 
					
						
							|  |  |  |      * @return array|null | 
					
						
							|  |  |  |      * @throws LdapException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  |     public function getUserDetails($userName) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |         $ldapConnection = $this->getConnection(); | 
					
						
							| 
									
										
										
										
											2016-01-17 23:20:07 +08:00
										 |  |  |         $this->bindSystemUser($ldapConnection); | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |         // Find user
 | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |         $userFilter = $this->buildFilter($this->config['user_filter'], ['user' => $userName]); | 
					
						
							|  |  |  |         $baseDn = $this->config['base_dn']; | 
					
						
							| 
									
										
										
										
											2017-02-26 22:51:49 +08:00
										 |  |  |         $emailAttr = $this->config['email_attribute']; | 
					
						
							| 
									
										
										
										
											2017-07-11 02:43:49 +08:00
										 |  |  |         $followReferrals = $this->config['follow_referrals'] ? 1 : 0; | 
					
						
							|  |  |  |         $this->ldap->setOption($ldapConnection, LDAP_OPT_REFERRALS, $followReferrals); | 
					
						
							| 
									
										
										
										
											2017-02-26 22:51:49 +08:00
										 |  |  |         $users = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, $userFilter, ['cn', 'uid', 'dn', $emailAttr]); | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         if ($users['count'] === 0) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $user = $users[0]; | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2016-02-09 03:45:01 +08:00
										 |  |  |             'uid'   => (isset($user['uid'])) ? $user['uid'][0] : $user['dn'], | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |             'name'  => $user['cn'][0], | 
					
						
							|  |  |  |             'dn'    => $user['dn'], | 
					
						
							| 
									
										
										
										
											2017-02-26 22:51:49 +08:00
										 |  |  |             'email' => (isset($user[$emailAttr])) ? (is_array($user[$emailAttr]) ? $user[$emailAttr][0] : $user[$emailAttr]) : null | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Authenticatable $user | 
					
						
							|  |  |  |      * @param string          $username | 
					
						
							|  |  |  |      * @param string          $password | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      * @throws LdapException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validateUserCredentials(Authenticatable $user, $username, $password) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $ldapUser = $this->getUserDetails($username); | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         if ($ldapUser === null) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($ldapUser['uid'] !== $user->external_auth_id) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |         $ldapConnection = $this->getConnection(); | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $ldapBind = $this->ldap->bind($ldapConnection, $ldapUser['dn'], $password); | 
					
						
							|  |  |  |         } catch (\ErrorException $e) { | 
					
						
							|  |  |  |             $ldapBind = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |         return $ldapBind; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Bind the system user to the LDAP connection using the given credentials | 
					
						
							|  |  |  |      * otherwise anonymous access is attempted. | 
					
						
							|  |  |  |      * @param $connection | 
					
						
							|  |  |  |      * @throws LdapException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function bindSystemUser($connection) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |         $ldapDn = $this->config['dn']; | 
					
						
							|  |  |  |         $ldapPass = $this->config['pass']; | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  |         $isAnonymous = ($ldapDn === false || $ldapPass === false); | 
					
						
							|  |  |  |         if ($isAnonymous) { | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |             $ldapBind = $this->ldap->bind($connection); | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |             $ldapBind = $this->ldap->bind($connection, $ldapDn, $ldapPass); | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         if (!$ldapBind) { | 
					
						
							|  |  |  |             throw new LdapException(($isAnonymous ? trans('errors.ldap_fail_anonymous') : trans('errors.ldap_fail_authed'))); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the connection to the LDAP server. | 
					
						
							|  |  |  |      * Creates a new connection if one does not exist. | 
					
						
							|  |  |  |      * @return resource | 
					
						
							|  |  |  |      * @throws LdapException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getConnection() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         if ($this->ldapConnection !== null) { | 
					
						
							|  |  |  |             return $this->ldapConnection; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |         // Check LDAP extension in installed
 | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |         if (!function_exists('ldap_connect') && config('app.env') !== 'testing') { | 
					
						
							| 
									
										
										
										
											2016-12-05 00:51:39 +08:00
										 |  |  |             throw new LdapException(trans('errors.ldap_extension_not_installed')); | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 01:55:09 +08:00
										 |  |  |         // Get port from server string and protocol if specified.
 | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |         $ldapServer = explode(':', $this->config['server']); | 
					
						
							| 
									
										
										
										
											2017-01-15 01:55:09 +08:00
										 |  |  |         $hasProtocol = preg_match('/^ldaps{0,1}\:\/\//', $this->config['server']) === 1; | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         if (!$hasProtocol) { | 
					
						
							|  |  |  |             array_unshift($ldapServer, ''); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-01-15 01:55:09 +08:00
										 |  |  |         $hostName = $ldapServer[0] . ($hasProtocol?':':'') . $ldapServer[1]; | 
					
						
							|  |  |  |         $defaultPort = $ldapServer[0] === 'ldaps' ? 636 : 389; | 
					
						
							|  |  |  |         $ldapConnection = $this->ldap->connect($hostName, count($ldapServer) > 2 ? intval($ldapServer[2]) : $defaultPort); | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($ldapConnection === false) { | 
					
						
							| 
									
										
										
										
											2016-12-05 00:51:39 +08:00
										 |  |  |             throw new LdapException(trans('errors.ldap_cannot_connect')); | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Set any required options
 | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |         if ($this->config['version']) { | 
					
						
							| 
									
										
										
										
											2016-05-02 18:38:07 +08:00
										 |  |  |             $this->ldap->setVersion($ldapConnection, $this->config['version']); | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->ldapConnection = $ldapConnection; | 
					
						
							|  |  |  |         return $this->ldapConnection; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Build a filter string by injecting common variables. | 
					
						
							| 
									
										
										
										
											2016-01-16 07:21:47 +08:00
										 |  |  |      * @param string $filterString | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |      * @param array $attrs | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function buildFilter($filterString, array $attrs) | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $newAttrs = []; | 
					
						
							|  |  |  |         foreach ($attrs as $key => $attrText) { | 
					
						
							| 
									
										
										
										
											2016-01-12 06:41:05 +08:00
										 |  |  |             $newKey = '${' . $key . '}'; | 
					
						
							| 
									
										
										
										
											2016-01-10 03:23:35 +08:00
										 |  |  |             $newAttrs[$newKey] = $attrText; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return strtr($filterString, $newAttrs); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  | } |