Session

class pyldap_orm.session.LDAPSession(backend, mode=0, cert=None, key=None, cacertdir='/etc/ssl/certs')

Create a LDAPSession by connecting to the LDAP server.

Tested servers:
  • OpenDJ
  • OpenLDAP

A basic usage looks like:

>>> session = LDAPSession(backend='ldap://localhost:389', mode=LDAPSession.STARTTLS)
>>> session.authenticate('cn=admin,dc=example,dc=com', 'password')

You can also bind as anonymous:

>>> session.authenticate()
Parameters:
  • backend – a LDAP URI like ldaps?://host(:port)?
  • mode – Transport mode, must be LDAPSession.PLAIN (the default), LDAPSession.STARTTLS or LDAPSession.LDAPS
  • cert – An optional client certificate, in PEM format
  • key – The client certificate related private key, in PEM format with no password
  • cacertdir – Directory of CA certificates, default is /etc/ssl/certs
authenticate(bind_dn=None, credential=None, mode=0)

Perform LDAP authentication and parse schema. This method is mandatory.

Parameters:
  • bind_dn – optional string to perform a bind
  • credential – optional string with the password of bind_dn
  • mode – Can se LDAPSession.AUTH_SIMPLE_BIND (the default) or LDAPSession.AUTH_SASL_EXTERNAL
parse_schema()

Create self.schema['attributes'] dictionary where values are a tuple holding the syntax oid and a boolean (true if the attribute is single valued).

search(base, scope=<Mock id='140401136869672'>, ldap_filter='(objectClass=*)', attributes=None, serverctrls=None)

Perform a low level LDAP search (synchronous) using the given arguments.

Parameters:
  • base – Base DN of the search
  • scope – Scope of the search, default is SCOPE_SUBTREE
  • ldap_filter – ldap filter, default is ‘(objectClass=*)’
  • attributes – An array of attributes to return, default is [‘*’]
  • serverctrls – An array server extended controls
Returns:

a list of tuples (dn, attributes)