Browse Source

more work on LDAP, please test out

causefx 7 years ago
parent
commit
a2b89d0e4a
3 changed files with 32 additions and 4 deletions
  1. 2 0
      api/config/default.php
  2. 15 4
      api/functions/auth-functions.php
  3. 15 0
      api/functions/organizr-functions.php

+ 2 - 0
api/config/default.php

@@ -5,6 +5,8 @@ return array(
 	'authBackend' => '',
 	'authBackendHost' => '',
 	'authBackendHostPrefix' => '',
+	'ldapBindUsername' => '',
+	'ldapBindPassword' => '',
 	'authBaseDN' => '',
 	'authBackendDomain' => '',
 	'ldapType' => '1',

+ 15 - 4
api/functions/auth-functions.php

@@ -215,8 +215,8 @@ if (function_exists('ldap_connect')) {
 				// Mandatory Configuration Options
 				'hosts' => $ldapHosts,
 				'base_dn' => $GLOBALS['authBaseDN'],
-				'username' => checkHostPrefix($GLOBALS['authBackendHostPrefix']) . $username,
-				'password' => $password,
+				'username' => (empty($GLOBALS['ldapBindUsername'])) ? null : $GLOBALS['ldapBindUsername'],
+				'password' => (empty($GLOBALS['ldapBindPassword'])) ? null : decrypt($GLOBALS['ldapBindPassword']),
 				// Optional Configuration Options
 				'schema' => (($GLOBALS['ldapType'] == 1) ? Adldap\Schemas\ActiveDirectory::class : (($GLOBALS['ldapType'] == 2) ? Adldap\Schemas\OpenLDAP::class : Adldap\Schemas\FreeIPA::class)),
 				'account_prefix' => '',
@@ -238,11 +238,22 @@ if (function_exists('ldap_connect')) {
 			try {
 				// If a successful connection is made to your server, the provider will be returned.
 				$provider = $ad->connect();
-				$user = $provider->search()->find($username);
-				$bind = $user->exists;
+				if ($provider->auth()->attempt(checkHostPrefix($GLOBALS['authBackendHostPrefix']) . $username, $password)) {
+					// Passed.
+					return true;
+				} else {
+					// Failed.
+					return false;
+				}
 			} catch (\Adldap\Auth\BindException $e) {
 				writeLog('error', 'LDAP Function - Error: ' . $e->getMessage(), $username);
 				// There was an issue binding / connecting to the server.
+			} catch (Adldap\Auth\UsernameRequiredException $e) {
+				writeLog('error', 'LDAP Function - Error: ' . $e->getMessage(), $username);
+				// The user didn't supply a username.
+			} catch (Adldap\Auth\PasswordRequiredException $e) {
+				writeLog('error', 'LDAP Function - Error: ' . $e->getMessage(), $username);
+				// The user didn't supply a password.
 			}
 			return ($bind) ? true : false;
 		}

+ 15 - 0
api/functions/organizr-functions.php

@@ -576,6 +576,21 @@ function getSettingsMain()
 				'value' => $GLOBALS['authBackendHostPrefix'],
 				'placeholder' => 'Domain prefix - i.e. Controller from Controller\Username'
 			),
+			array(
+				'type' => 'input',
+				'name' => 'ldapBindUsername',
+				'class' => 'ldapAuth switchAuth',
+				'label' => 'Bind Username',
+				'value' => $GLOBALS['ldapBindUsername'],
+				'placeholder' => ''
+			),
+			array(
+				'type' => 'password',
+				'name' => 'ldapBindPassword',
+				'class' => 'ldapAuth switchAuth',
+				'label' => 'Password',
+				'value' => $GLOBALS['ldapBindPassword']
+			),
 			array(
 				'type' => 'input',
 				'name' => 'embyURL',