Skip to content

Core\Ldap\Connection::changePassword

Changes the password of a user account from the old password to a new one. It returns nothing.

This is what a user does on a "change my password" page. The server checks the old password first. If it is wrong, the password does not change. An administrator who resets a password without the old one uses setPassword instead.

The connection must use TLS, because the server only accepts a password over an encrypted connection. On a connection without TLS, changePassword throws an error and sends nothing.

The domain checks the new password against its rules. A new password can be too short, the same as a recent one, or too soon after the last change. Then changePassword throws Core\Ldap\LdapError with $kind set to PasswordPolicy.

Good to know: both passwords are secret strings. A program cannot print them or write them to a log. Most domains have a minimum password age, often one day. A password set a moment ago can be changed at once only when pwdLastSet is 0, which means the user must change it now.

Parameters

  • $dnCore\Ldap\Dn|stringThe user account's DN, as a Dn or as text. Text cannot be tainted.
  • $oldsecret tainted stringThe password the account has now.
  • $newsecret tainted stringThe new password.

Return value

Returns void. Nothing.

Errors

  • LogicErrordn is text that is not a DN, or the connection is closed.
  • Core\Ldap\LdapError$kind is EncryptionRequired when the connection does not use TLS. Nothing is sent then. It is PasswordPolicy when the domain does not accept the new password, and another kind for any other error the server returns.

Examples

The examples below run with this configuration file.

nvs.toml
# These examples use the LDAP directory server on this computer at port 16636.
# They log in as the administrator, because they change accounts in the directory.
# `tls_ca_file` is the certificate that the server's certificate is checked against.
[ldap.corp-accounts]
url = "ldaps://127.0.0.1:16636"
base = "DC=example,DC=test"
user = "CN=Administrator,CN=Users,DC=example,DC=test"
password = "Novis-test-1"
tls_ca_file = "../../../../../tests/db/ca.crt"
timeout = "5s"
# The program may open the `corp-accounts` directory.
[capabilities.ldap]
connect = ["corp-accounts"]

A new account chooses its own password

<?nvs
// Shows how a new user replaces the first password the administrator gave them.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $unit = 'OU=Changing Staff 1,DC=example,DC=test';
string $user = 'CN=Jana Pohl,' . $unit;
// A run that stopped early can leave the account behind, so the program deletes it first.
array<string> $leftovers = [$user, $unit];
foreach ($leftovers as string $left) {
try {
$corp->delete($left);
} catch (Core\Ldap\LdapError $missing) {
}
}
$corp->add($unit, ['objectClass' => 'organizationalUnit']);
$corp->add($user, ['objectClass' => 'user', 'sAMAccountName' => 'jana.pohl']);
// The administrator gives a first password. `pwdLastSet` 0 means
// the user must change it at the next login.
$corp->setPassword($user, 'Given-Pass-of-Jana-1');
$corp->modify($user, [
Core\Ldap\Change::replace('userAccountControl', 512),
Core\Ldap\Change::replace('pwdLastSet', 0),
]);
// The first login fails. This prints `MustChangePassword`.
try {
$corp->authenticate($user, 'Given-Pass-of-Jana-1');
} catch (Core\Ldap\LdapError $error) {
echo $error->kind == Core\Ldap\LdapError::Kind::MustChangePassword ? 'MustChangePassword' : 'another reason', "\n";
}
// The user chooses a password of their own. Then this prints `login works`.
$corp->changePassword($user, 'Given-Pass-of-Jana-1', 'Own-Pass-of-Jana-2');
$corp->authenticate($user, 'Own-Pass-of-Jana-2');
echo 'login works', "\n";
$corp->delete($user);
$corp->delete($unit);
Output
MustChangePassword
login works

The old password is wrong

<?nvs
// Shows what happens when the old password is wrong.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $unit = 'OU=Changing Staff 2,DC=example,DC=test';
string $user = 'CN=Ivo Lang,' . $unit;
// A run that stopped early can leave the account behind, so the program deletes it first.
array<string> $leftovers = [$user, $unit];
foreach ($leftovers as string $left) {
try {
$corp->delete($left);
} catch (Core\Ldap\LdapError $missing) {
}
}
$corp->add($unit, ['objectClass' => 'organizationalUnit']);
$corp->add($user, ['objectClass' => 'user', 'sAMAccountName' => 'ivo.lang']);
// `pwdLastSet` 0 means the user must change the password now.
$corp->setPassword($user, 'First-Pass-of-Ivo-1');
$corp->modify($user, [
Core\Ldap\Change::replace('userAccountControl', 512),
Core\Ldap\Change::replace('pwdLastSet', 0),
]);
// The old password is wrong, so `changePassword` throws an error.
// This prints `not changed`.
try {
$corp->changePassword($user, 'Not-the-Pass-of-Ivo-1', 'Second-Pass-of-Ivo-2');
echo 'changed', "\n";
} catch (Core\Ldap\LdapError $error) {
echo 'not changed', "\n";
}
// The correct old password works. This prints `changed`.
$corp->changePassword($user, 'First-Pass-of-Ivo-1', 'Second-Pass-of-Ivo-2');
echo 'changed', "\n";
$corp->delete($user);
$corp->delete($unit);
Output
not changed
changed

A password that is too new to change

<?nvs
// Shows that a password set a moment ago cannot be changed again at once.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $unit = 'OU=Changing Staff 3,DC=example,DC=test';
string $user = 'CN=Hana Sato,' . $unit;
// A run that stopped early can leave the account behind, so the program deletes it first.
array<string> $leftovers = [$user, $unit];
foreach ($leftovers as string $left) {
try {
$corp->delete($left);
} catch (Core\Ldap\LdapError $missing) {
}
}
$corp->add($unit, ['objectClass' => 'organizationalUnit']);
$corp->add($user, ['objectClass' => 'user', 'sAMAccountName' => 'hana.sato']);
$corp->setPassword($user, 'First-Pass-of-Hana-1');
$corp->modify($user, [Core\Ldap\Change::replace('userAccountControl', 512)]);
// This domain has a minimum password age of one day.
// So `changePassword` throws an error, and `$kind` is `PasswordPolicy`.
try {
$corp->changePassword($user, 'First-Pass-of-Hana-1', 'Second-Pass-of-Hana-2');
echo 'changed', "\n";
} catch (Core\Ldap\LdapError $error) {
echo $error->kind == Core\Ldap\LdapError::Kind::PasswordPolicy ? 'PasswordPolicy' : 'another reason', "\n";
}
// The first password still works, so this prints `login works`.
$corp->authenticate($user, 'First-Pass-of-Hana-1');
echo 'login works', "\n";
$corp->delete($user);
$corp->delete($unit);
Output
PasswordPolicy
login works