Skip to content

Commit

Permalink
Improve LDAP error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tchapi committed May 19, 2024
1 parent 6c4fb7b commit 185a6a9
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/Services/LDAPAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,20 @@ public function __construct(ManagerRegistry $doctrine, Utils $utils, string $LDA
*/
protected function ldapOpen($username, $password)
{
$success = false;

try {
$ldap = ldap_connect($this->LDAPAuthUrl);
} catch (\ErrorException $e) {
error_log('LDAP Error (ldap_connect): '.ldap_error($ldap).' ('.ldap_errno($ldap).')');
} catch (\Exception $e) {
error_log('LDAP Error (ldap_connect with '.$this->LDAPAuthUrl.'): '.$e->getMessage());
return false;
}

if (!$ldap || !ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3)) {
if ($ldap === false) {
error_log('LDAP Error (ldap_connect with '.$this->LDAPAuthUrl.'): provided LDAP URI does not seems plausible');
return false;
}

if (!ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3)) {
error_log('LDAP Error (ldap_set_option): could not set LDAP_OPT_PROTOCOL_VERSION to 3');
return false;
}

Expand All @@ -118,13 +123,14 @@ protected function ldapOpen($username, $password)
$dn = str_replace('%'.$i, $domain_split[$i - 1], $dn);
}

$success = false;
try {
$bind = ldap_bind($ldap, $dn, $password);
if ($bind) {
$success = true;
}
} catch (\ErrorException $e) {
error_log('LDAP Error (ldap_bind): '.ldap_error($ldap).' ('.ldap_errno($ldap).')');
} catch (\Exception $e) {
error_log('LDAP Error (ldap_bind to '.$this->LDAPAuthUrl.'): '.ldap_error($ldap).' ('.ldap_errno($ldap).')');
}

if ($success && $this->autoCreate) {
Expand Down

0 comments on commit 185a6a9

Please sign in to comment.