NAME

Web::Session::LDAP - An implementation of Apache::Session::LDAP

SYNOPSIS

use Web::Session::LDAP;
tie %hash, 'Web::Session::LDAP', $id, {
  ldapServer           => 'ldap://localhost:389',
  ldapConfBase         => 'dmdName=applications,dc=example,dc=com',
  ldapBindDN           => 'cn=admin,dc=example,dc=com',
  ldapBindPassword     => 'pass',
  Index                => 'uid ipAddr',
  ldapObjectClass      => 'applicationProcess',
  ldapAttributeId      => 'cn',
  ldapAttributeContent => 'description',
  ldapAttributeIndex   => 'ou',
  ldapVerify           => 'require',
  ldapCAFile           => '/etc/ssl/certs/ca-certificates.crt',
  ldapTimeout          => 10,
};

DESCRIPTION

This module is an implementation of Apache::Session. It uses an LDAP directory to store datas.

See Web::Session::Store::LDAP for the available options.

Each session is an entry named ldapAttributeId=id,ldapConfBase (default objectClass applicationProcess, available in the core schema). The session is serialized in JSON into ldapAttributeContent, and each field listed in Index and set in the session is stored as a field_value value of ldapAttributeIndex. Fields whose value is empty or 0 are not indexed.

The directory should index ldapAttributeIndex for equality and substring searches (OpenLDAP: index ou eq,sub).

searchLt() and searchGt()

# Refresh tokens not updated since 1 hour
my $hash = Web::Session::LDAP->searchLt( $args,
    '_oidcRtUpdate', time - 3600 );
my $hash = Web::Session::LDAP->searchGt( $args,
    '_utime', time - 3600, 'uid', 'ipAddr' );

Return sessions whose field is strictly lower (or greater) than the given number, like searchOn(). Sessions without the field, or where it isn't a number, are skipped. The value must be a decimal number (/^-?[0-9]+(?:\.[0-9]+)?$/), otherwise an empty result is returned with a warning. LDAP errors also return an empty result with a warning.

The index attribute has no ordering matching rule, and substring filters can't bound the length of a number, so the directory can't select the lower values. When the field is indexed, only sessions having it are read; otherwise all sessions are read. Numbers are then compared in Perl. Lemonldap::NG purge calls searchLt() on _oidcRtUpdate when a refresh token activity timeout is set: add it to Index.

When the field is indexed, sessions are found through their index values: sessions whose value is 0 or empty, or written before the field was added to Index, are missed until they are rewritten.

Differences with the SQL backends (searchLt(), searchGt() and deleteIfLowerThan()):

  • Values which aren't numbers are ignored here, whereas SQL backends compare them as 0 or by their numeric prefix (12abc is 12).

  • Fields whose value is 0 or empty are not indexed, so they are missed when the field is indexed, whereas SQL backends find them.

deleteIfLowerThan()

my ( $ok, $count ) = Web::Session::LDAP->deleteIfLowerThan(
    $args,
    {
        not => { _session_kind => 'Persistent' },
        or  => { _utime => time - 7200, _lastSeen => time - 3600 },
    }
);

Deletes sessions where one (or) or all (and) of the given fields are strictly lower than their threshold, and where each not field is missing or has another value. Exactly one of or and and must be given, and thresholds must be decimal numbers (surrounding spaces are ignored). A missing field, or one which isn't a number, is never lower.

Only the index is used: every field of the rule must be listed in Index. For Lemonldap::NG sessions purge, index _session_kind and _utime, plus _lastSeen when "timeoutActivity" is set and _oidcRtUpdate for searchLt() (see above), for example:

Index => '_whatToTrace _session_kind _utime _lastSeen _oidcRtUpdate ipAddr'

The directory selects the sessions having the or/and fields and none of the not values, for example:

(&(objectClass=applicationProcess)(|(ou=_lastSeen_*)(ou=_utime_*))
  (!(ou=_session_kind_Persistent)))

Only their DN and index values are read; numbers are compared in Perl, then the matching entries are deleted. As with searchLt(), no filter can select lower numbers: ou has no ordering rule, and a substring filter can't tell 99 from 9900000000. So the index values of all the candidate sessions are read, but much less data than the full scan used otherwise.

The serialized session is only read when a matching session has no index value for a not field (written before this field was added to Index): it is kept if its content has the not value.

Each deletion carries an assertion control (RFC 4528) checking that the index values seen by the search haven't changed: a session updated in the meantime is not deleted. The control is critical: if the server (or a proxy) doesn't support it, the deletion fails with LDAP error 12, and deleteIfLowerThan() stops and returns false (see below) so that no refreshed session is deleted.

Limits:

  • The rule fields are read from the index: after adding a field to Index, sessions written before are ignored until they are rewritten.

  • not values are compared with the equality rule of the index attribute: case-insensitive for ou, so persistent is also kept.

  • Fields whose value is empty or 0 aren't indexed: they are considered missing, and a not value empty or 0 is refused.

Returns false, deleting nothing (0 in list context), if the rule can't be handled this way (non indexed field, invalid threshold...) or if the connection or the search fails: Lemonldap::NG then falls back to reading all sessions. Otherwise returns true and, in list context, the number of deleted sessions (sessions removed or updated meanwhile aren't counted). If a deletion fails, stops and returns false, and in list context the number of sessions already deleted.

Size limits

Searches returning many sessions (get_key_from_all_sessions(), searchOn(), searchOnExpr(), searchLt(), searchGt(), deleteIfLowerThan()) use the paged results control. With OpenLDAP, if the bind DN isn't the rootdn, allow it to read all sessions, for example with limits dn.exact="<bind DN>" size=unlimited (this covers paged and unpaged searches).

COPYRIGHT AND LICENSE

2009-2026 by Xavier Guimard
2013-2025 by Clément Oudot
2019-2025 by Maxime Besson
2013-2025 by Worteks
2023-2026 by Linagora

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.

SEE ALSO

Apache::Session