Core\Ldap::open
Core\Ldap::open({…} $settings): Core\Ldap\ConnectionCore\Ldap::open( {…} $settings, ): Core\Ldap\Connection
Opens an LDAP directory at a URL your program gives, with the account your program gives. An LDAP directory is a server with users and groups, such as Active Directory.
Use it when the directory is not known in advance, for example when each customer sets up their own. Core\Ldap::open takes the url, the user and the password, and logs in. It returns a Core\Ldap\Connection, which is closed when the request ends.
The connection is encrypted. ldaps:// encrypts it from the start. tls: Tls::None sends the password and every answer as plain text. It works only for a host in [capabilities.ldap] cleartext. If a TLS connection fails, the error names the cause and the fix (TLS connections).
The url cannot be tainted (a value from a request that is not checked yet), because the password is sent to that host. An empty password, or a password with a zero byte (\x00), throws an error before anything is sent.
Good to know: [capabilities.ldap] open in nvs.toml lists the hosts a program may open. Any other host throws a RuntimeError. A private address, such as 10.0.0.5, also throws, unless the operator allows it.
Parameters
$settings{ url: string, user?: tainted string, password?: secret tainted string, tls?: Core\Ldap::Tls, tlsCa?: string, tlsPin?: string|array<string>, tlsVerifyHost?: bool, tlsVerify?: bool, tlsMinVersion?: string, timeout?: Core\Time\Duration, base?: Core\Ldap\Dn|string }The directory to open and the account to log in with.urlstringOneldaps://orldap://URL. It cannot betainted, because the password is sent to this host.usertainted stringThe account to log in as, as a DN,user@example.testorEXAMPLE\user. Left out, the connection does not log in.passwordsecret tainted stringThe account's password. An empty password throws before anything is sent. A password with a zero byte (\x00) throws before it is sent.tlsTlsTls::Required, the default, encrypts the connection.Tls::Nonesends everything as plain text, and needs the host in[capabilities.ldap] cleartext.tlsCastringPEM certificates to trust for this connection, in place of[tls.client] roots. It needs thetls.anchorscapability for the host.tlsPinstring|array<string>One or moresha256//hashes of the server's public key. A server with a matching key is accepted, and its certificate chain is not checked. It needs thetls.pincapability for the host.tlsVerifyHostboolfalsechecks the certificate chain, but not the host name in the certificate. It needs thetls.any_namecapability for the host. The default istrue.tlsVerifyboolfalsechecks neither the certificate chain nor the host name. The connection is still encrypted. Another machine on the network can still pretend to be the server and read the password. Use a real certificate andtlsCawhere you can. It needs thetls.insecurecapability for the host. The default istrue.tlsMinVersionstringThe lowest TLS version to accept:"1.2"or"1.3". It can only raise[tls.client] min_version, so it needs no capability.timeoutDurationHow long one operation may take. Left out, it is 30 seconds.baseDn|stringThe DN a search starts from when it does not give its ownbase. Text cannot betainted.
Return value
Returns Core\Ldap\Connection. A Core\Ldap\Connection. It is closed when the request ends.
Errors
RuntimeErrorurlis not anldap://orldaps://URL,ldap.opendoes not grant its host, or the host's address is in a private range thatnet.internaldoes not allow.LogicErrorA TLS setting makes the certificate check less strict, and its grant does not list the host.tlsCaneedstls.anchors,tlsPinneedstls.pin,tlsVerifyHost: falseneedstls.any_nameandtlsVerify: falseneedstls.insecure. It is also thrown whentlsMinVersionis not"1.2"or"1.3", or is lower than[tls.client] min_version. Nothing is connected.Core\Ldap\LdapErrorThe server did not respond, or the server did not accept the user and password. It is also thrown fortls: Core\Ldap::Tls::Nonewhenldap.cleartextdoes not list the host. The error'skindproperty says which case it is.
Changelog
| Version | Change |
|---|---|
0.0.6 | A failed TLS connection names its cause, who can fix it and how, as TLS connection errors explains. For an expired certificate, the message gives the date it expired and the time on this host's clock. For a wrong pin, it gives the sha256// hash of the server's public key. |
0.0.6 | Core\Ldap::open has the TLS settings of Core\Http\Client: tlsCa, tlsPin, tlsVerifyHost, tlsVerify and tlsMinVersion. A setting that makes the certificate check less strict needs the host in a list under [capabilities.tls], or the call throws a LogicError. The [ldap.<name>], [mail.<name>], [imap.<name>] and [cache.shared] blocks have the same keys as a [db.<name>] block, such as tls_verify. |
Examples
The examples below run with this configuration file.
# The program may open a directory on `127.0.0.1` only.[capabilities.ldap]open = ["127.0.0.1"]Open a directory the program names
<?nvs// Shows how to open a directory at an address the program chooses.// `Core\Ldap::open` needs the `ldap.open` capability for the host in the URL.// No directory server runs at this address, so the call throws a `Core\Ldap\LdapError`.
try { var $directory = Core\Ldap::open({ url: 'ldaps://127.0.0.1:1', user: 'CN=Reader,CN=Users,DC=example,DC=test', password: 'Example-Pw1', timeout: Core\Time\Duration::seconds(5), }); echo 'Logged in as ', $directory->whoami(), "\n";} catch (Core\Ldap\LdapError $error) { echo "The directory did not answer.\n";}The directory did not answer.A host the operator did not allow
<?nvs// Shows what happens when a program opens a directory on a host the operator did// not allow. `[capabilities.ldap] open` in `nvs.toml` lists the hosts. This// program may open `127.0.0.1` only, so the call throws a `RuntimeError`.
try { Core\Ldap::open({ url: 'ldaps://dc1.example.test', user: 'CN=Reader,CN=Users,DC=example,DC=test', password: 'Example-Pw1', }); echo "The directory is open.\n";} catch (RuntimeError $error) { echo $error->message, "\n";}Core\Ldap::open needs the capability `ldap.open` for dc1.example.test, which is not grantedhelp: add `open = ["dc1.example.test"]` under `[capabilities.ldap]` in nvs.tomlPlain text needs its own grant
<?nvs// Shows that a connection without encryption needs its own permission.// `Tls::None` sends the password as plain text. The host must also be listed in// `[capabilities.ldap] cleartext` in `nvs.toml`, and here it is not.
try { Core\Ldap::open({ url: 'ldap://127.0.0.1:1', user: 'CN=Reader,CN=Users,DC=example,DC=test', password: 'Example-Pw1', tls: Core\Ldap::Tls::None, }); echo "The directory is open.\n";} catch (Core\Ldap\LdapError $error) { // This prints `EncryptionRequired`. echo $error->kind == Core\Ldap\LdapError::Kind::EncryptionRequired ? 'EncryptionRequired' : 'another reason', "\n";}EncryptionRequired