Skip to content

Core\Ldap\Connection::whoami

$connection->whoami(): string

Returns the account the connection is logged in as, as the server reports it.

The result is a tainted string, because the server sends it. It has one of two forms. It starts with dn: and then the DN of the account, or with u: and then a login name. A DN (distinguished name) is the full path of an entry in the directory. Active Directory and Samba return the login name with the domain in front, such as u:EXAMPLE\Reader. A connection that did not log in returns an empty string.

Use it to check that a program uses the account you expect, for example when a job starts or in a health check. Each call sends one request to the server.

Good to know: a closed connection throws LogicError. When the server does not answer or returns an error, whoami throws Core\Ldap\LdapError.

Return value

Returns string. A tainted string, because the server sent it. It is dn: and then the account's DN (distinguished name), or u: and then its login name. The result is an empty string when the connection did not log in.

Errors

  • LogicErrorThe connection is closed.
  • Core\Ldap\LdapErrorThe server did not respond, or it returned an error. $kind says why.

Changelog

VersionChange
0.0.6The output of a child process is tainted bytes. The version text of a database server and the name an LDAP server returns are tainted string. Another program or a server writes this text, so it may contain input from outside.

Examples

The examples below run with this configuration file.

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

Who is logged in

<?nvs
// Shows how to ask the directory which account the connection is logged in as.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
// The `corp` block logs in as the account `Reader` in the domain `EXAMPLE`.
// This prints `u:EXAMPLE\Reader`.
echo $corp->whoami(), "\n";
Output
u:EXAMPLE\Reader

Only the login name

<?nvs
// Shows how to take only the login name out of the result of `whoami`.
// requires: samba-ad
class Login
{
// The result starts with `u:` and a login name, or with `dn:` and a DN.
// A login name can start with a domain and `\`, so the domain is cut off too.
public static function name(tainted string $who): tainted string
{
if (Core\Str::startsWith($who, 'u:')) {
tainted string $login = Core\Str::after($who, 'u:') ?? '';
return Core\Str::after($login, "\\", { last: true }) ?? $login;
}
if (Core\Str::startsWith($who, 'dn:')) {
return Core\Str::after($who, 'dn:') ?? '';
}
return '(not logged in)';
}
}
var $corp = Core\Ldap::connect('corp');
// This prints `Reader`.
echo Login::name($corp->whoami()), "\n";
// These two lines show the other forms. They print `CN=Reader,DC=example,DC=test`
// and `(not logged in)`.
echo Login::name('dn:CN=Reader,DC=example,DC=test'), "\n";
echo Login::name(''), "\n";
Output
Reader
CN=Reader,DC=example,DC=test
(not logged in)

Check the account before a job

<?nvs
// Shows how a job checks that it uses the right directory account before it starts.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
string $expected = "u:EXAMPLE\\Reader";
// The server may return the name in other upper and lower case,
// so both names are compared in lower case.
tainted string $who = $corp->whoami();
if (Core\Str::lower($who) != Core\Str::lower($expected)) {
echo 'The job stops: the directory account is ', $who, "\n";
exit(1);
}
// The account is the expected one, so this prints `The job can start.`
echo "The job can start.\n";
Output
The job can start.