Skip to content

Core\Ldap\Connection::authenticate

$connection->authenticate(
    string $login,
    secret tainted string $password,
): void

Checks a user's login and password. It returns nothing when they are correct, and throws an error when they are not.

The login can be the user's DN, name@example.test or EXAMPLE\name. A login from a form is tainted, and authenticate accepts it as it is. The password is a secret, so it never shows up in an error message or a log.

authenticate opens a new connection and logs in as the user there. Then it closes that connection. The connection you call it on keeps its own account and rights.

If the login fails, authenticate throws Core\Ldap\LdapError, and its $kind says why. A wrong password and a user that does not exist both give InvalidCredentials, so the error does not show which users exist. Active Directory also gives reasons such as AccountDisabled, AccountLocked and PasswordExpired.

Good to know: an empty login or password is InvalidCredentials, and nothing is sent to the server. A password with a zero byte (\x00) is also InvalidCredentials, and it is not sent. Each call costs one more TLS handshake.

Parameters

  • $loginstringThe user's DN, user@example.test or DOMAIN\user. It can be tainted, such as a name from a login form.
  • $passwordsecret tainted stringThe user's password.

Return value

Returns void. Nothing. The login and password are correct when no error is thrown.

Errors

  • LogicErrorThe connection is closed.
  • Core\Ldap\LdapError$kind is InvalidCredentials for a wrong password and for a user that does not exist, so the error does not show which users exist. An empty login or password is also InvalidCredentials, and nothing is sent. So is a password with a zero byte (\x00), and it is not sent. Active Directory gives other reasons as their own kinds, such as AccountDisabled, AccountLocked or PasswordExpired.

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"]

Check a login form

<?nvs
// Shows how to check the login and password a person typed into a login form.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $unit = 'OU=Login Form 1,DC=example,DC=test';
string $user = 'CN=Max Brandt,' . $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' => 'max.brandt']);
$corp->setPassword($user, 'Garden-gate-81');
$corp->modify($user, [Core\Ldap\Change::replace('userAccountControl', 512)]);
class LoginForm
{
// `authenticate` returns nothing when the login is right, and throws an error when it is wrong.
public static function canLogIn(Core\Ldap\Connection $corp, tainted string $login, secret tainted string $password): bool
{
try {
$corp->authenticate($login, $password);
return true;
} catch (Core\Ldap\LdapError $error) {
return false;
}
}
}
// A login from a form is `tainted`, and `authenticate` accepts it.
// This prints `yes` and then `no`.
tainted string $login = "EXAMPLE\\max.brandt";
echo LoginForm::canLogIn($corp, $login, 'Garden-gate-81') ? 'yes' : 'no', "\n";
echo LoginForm::canLogIn($corp, $login, 'garden-gate-81') ? 'yes' : 'no', "\n";
$corp->delete($user);
$corp->delete($unit);
Output
yes
no

Three ways to write a login

<?nvs
// Shows the three ways to write the login of the same user.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $unit = 'OU=Login Names 2,DC=example,DC=test';
string $user = 'CN=Nora Falk,' . $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' => 'nora.falk',
'userPrincipalName' => 'nora.falk@example.test',
]);
$corp->setPassword($user, 'River-stone-52');
$corp->modify($user, [Core\Ldap\Change::replace('userAccountControl', 512)]);
// The DN, the user name with the domain after `@`, and the domain name before `\`.
// Each login works, so this prints `works` three times.
array<string> $logins = [$user, 'nora.falk@example.test', "EXAMPLE\\nora.falk"];
foreach ($logins as string $login) {
$corp->authenticate($login, 'River-stone-52');
echo 'works', "\n";
}
// The connection is still logged in as its own account. This prints `true`.
echo $corp->whoami() == "u:EXAMPLE\\Administrator" ? 'true' : 'false', "\n";
$corp->delete($user);
$corp->delete($unit);
Output
works
works
works
true

Why a login failed

<?nvs
// Shows how to find out why a login failed.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp-accounts');
string $unit = 'OU=Login Reasons 3,DC=example,DC=test';
string $user = 'CN=Paul Stein,' . $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' => 'paul.stein']);
$corp->setPassword($user, 'Mountain-road-36');
class Login
{
// `$kind` of the error says why the login failed.
public static function reason(Core\Ldap\Connection $corp, string $login, secret tainted string $password): string
{
try {
$corp->authenticate($login, $password);
return 'accepted';
} catch (Core\Ldap\LdapError $error) {
if ($error->kind == Core\Ldap\LdapError::Kind::AccountDisabled) {
return 'the account is turned off';
}
if ($error->kind == Core\Ldap\LdapError::Kind::InvalidCredentials) {
return 'wrong login or password';
}
return 'another reason';
}
}
}
// A new account is turned off. This prints `the account is turned off`.
echo Login::reason($corp, $user, 'Mountain-road-36'), "\n";
// The value 512 turns the account on. This prints `accepted`.
$corp->modify($user, [Core\Ldap\Change::replace('userAccountControl', 512)]);
echo Login::reason($corp, $user, 'Mountain-road-36'), "\n";
// A wrong password and a user that does not exist give the same reason,
// so nobody can find out which users exist. This prints `wrong login or password` twice.
echo Login::reason($corp, $user, 'Mountain-road-37'), "\n";
echo Login::reason($corp, 'CN=Nobody,' . $unit, 'Mountain-road-36'), "\n";
$corp->delete($user);
$corp->delete($unit);
Output
the account is turned off
accepted
wrong login or password
wrong login or password