Skip to content

Commit d2155cc

Browse files
caseysoftwareclaude
andcommitted
LDAP: hash new users' passwords once and only update them when they change
- createsqluser() hashed the password and then CUser::store() hashed the hash again, so the local copy never matched the real password and the SQL fallback (ldap_allow_login) could not work. - For existing users, authenticate() compared a fresh salted hash with the stored one, which never matches, so every LDAP login tried to save the user again. That save went through CUser::store(), which needs edit permission and nobody is logged in yet. - Missing directory attributes (country, mobile, postalcode... are not in every schema) raised "Undefined array key" warnings. createsqluser() now passes the plain password to CUser, the existing-user path checks it with password_verify() and updates the hash directly only when it no longer matches, and absent attributes become empty strings. Refs #320 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016CP89WVmGZ1VVCCZMS6ntN
1 parent 39d27f8 commit d2155cc

1 file changed

Lines changed: 54 additions & 21 deletions

File tree

‎classes/w2p/Authenticators/LDAP.class.php‎

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,7 @@ public function authenticate($username, $password)
7171
// (only one user should match sAMAccountName or uid etc..)
7272
if (ldap_bind($rs, $ldap_user_dn, $password)) {
7373
if ($this->userExists($username)) {
74-
// Update password if different
75-
$tmpUser = new CUser();
76-
$tmpUser->load($this->userId($username));
77-
$hash_pass = $this->hashPassword($password);
78-
if ($hash_pass != $tmpUser->user_password) {
79-
$tmpUser->user_password = $hash_pass;
80-
$tmpUser->store();
81-
}
74+
$this->updateSqlPassword($username, $password);
8275
return true;
8376
} else {
8477
$this->createsqluser($username, $password,
@@ -127,30 +120,70 @@ public function contactId($user_id)
127120
return $row['contact_id'];
128121
}
129122

130-
public function createsqluser($username, $password, $ldap_attribs = array())
123+
/**
124+
* Keeps the local copy of an LDAP user's password current, so the SQL
125+
* fallback (ldap_allow_login) accepts the same password. Stored hashes are
126+
* salted, so the password is checked with password_verify() and the row is
127+
* only written when it no longer matches. The update goes straight to the
128+
* table because nobody is logged in yet, so CUser::store() would be refused.
129+
*
130+
* @param string $username
131+
* @param string $password
132+
*/
133+
protected function updateSqlPassword($username, $password)
131134
{
132-
$hash_pass = $this->hashPassword($password);
135+
$q = new w2p_Database_Query();
136+
$q->addTable('users');
137+
$q->addQuery('user_password');
138+
$q->addWhere('user_username = ' . $q->quote($username));
139+
$hash = (string) $q->loadResult();
140+
141+
if (!password_verify($password, $hash)) {
142+
$q->clear();
143+
$q->addTable('users');
144+
$q->addUpdate('user_password', $this->hashPassword($password));
145+
$q->addWhere('user_username = ' . $q->quote($username));
146+
$q->exec();
147+
}
148+
}
133149

150+
/**
151+
* Returns the first value of an LDAP attribute, or '' when the directory
152+
* does not provide it (not every schema has every inetOrgPerson field).
153+
*
154+
* @param array<string, mixed> $ldap_attribs
155+
* @param string $name
156+
*
157+
* @return string
158+
*/
159+
protected function ldapAttribute(array $ldap_attribs, $name)
160+
{
161+
return isset($ldap_attribs[$name][0]) ? (string) $ldap_attribs[$name][0] : '';
162+
}
163+
164+
public function createsqluser($username, $password, $ldap_attribs = array())
165+
{
134166
$c = new CContact();
135167
if (count($ldap_attribs)) {
136168
// Contact information based on the inetOrgPerson class schema
137-
$c->contact_first_name = $ldap_attribs['givenname'][0];
138-
$c->contact_last_name = $ldap_attribs['sn'][0];
139-
$c->contact_city = $ldap_attribs['l'][0];
140-
$c->contact_country = $ldap_attribs['country'][0];
141-
$c->contact_state = $ldap_attribs['st'][0];
142-
$c->contact_zip = $ldap_attribs['postalcode'][0];
143-
$c->contact_job = $ldap_attribs['title'][0];
144-
$c->contact_email = $ldap_attribs['mail'][0];
145-
$c->contact_phone = $ldap_attribs['telephonenumber'][0];
169+
$c->contact_first_name = $this->ldapAttribute($ldap_attribs, 'givenname');
170+
$c->contact_last_name = $this->ldapAttribute($ldap_attribs, 'sn');
171+
$c->contact_city = $this->ldapAttribute($ldap_attribs, 'l');
172+
$c->contact_country = $this->ldapAttribute($ldap_attribs, 'country');
173+
$c->contact_state = $this->ldapAttribute($ldap_attribs, 'st');
174+
$c->contact_zip = $this->ldapAttribute($ldap_attribs, 'postalcode');
175+
$c->contact_job = $this->ldapAttribute($ldap_attribs, 'title');
176+
$c->contact_email = $this->ldapAttribute($ldap_attribs, 'mail');
177+
$c->contact_phone = $this->ldapAttribute($ldap_attribs, 'telephonenumber');
146178
$result = $c->store();
147-
$contactArray = array('phone_mobile' => $ldap_attribs['mobile'][0]);
179+
$contactArray = array('phone_mobile' => $this->ldapAttribute($ldap_attribs, 'mobile'));
148180
$c->setContactMethods($contactArray);
149181
}
150182

151183
$u = new CUser();
152184
$u->user_username = $username;
153-
$u->user_password = $hash_pass;
185+
// Plain text: CUser hashes it when the user is created.
186+
$u->user_password = $password;
154187
$u->user_type = 0; // Changed from 1 (administrator) to 0 (Default user)
155188
$u->user_contact = (int) $c->contact_id;
156189
$result = $u->store(null, true);

0 commit comments

Comments
 (0)