AUTH_LDAP_ALWAYS_UPDATE_USER
Default: True
If True
, the fields of a ~django.contrib.auth.models.User
object will be updated with the latest values from the LDAP directory every time the user logs in. Otherwise the ~django.contrib.auth.models.User
object will only be populated when it is automatically created.
AUTH_LDAP_AUTHORIZE_ALL_USERS
Default: False
If True
, ~django_auth_ldap.backend.LDAPBackend
will be able furnish permissions for any Django user, regardless of which backend authenticated it.
AUTH_LDAP_BIND_AS_AUTHENTICATING_USER
Default: False
If True
, authentication will leave the LDAP connection bound as the authenticating user, rather than forcing it to re-bind with the default credentials after authentication succeeds. This may be desirable if you do not have global credentials that are able to access the user's attributes. django-auth-ldap never stores the user's password, so this only applies to requests where the user is authenticated. Thus, the downside to this setting is that LDAP results may vary based on whether the user was authenticated earlier in the Django view, which could be surprising to code not directly concerned with authentication.
AUTH_LDAP_BIND_DN
Default: ''
(Empty string)
The distinguished name to use when binding to the LDAP server (with AUTH_LDAP_BIND_PASSWORD
). Use the empty string (the default) for an anonymous bind. To authenticate a user, we will bind with that user's DN and password, but for all other LDAP operations, we will be bound as the DN in this setting. For example, if AUTH_LDAP_USER_DN_TEMPLATE
is not set, we'll use this to search for the user. If AUTH_LDAP_FIND_GROUP_PERMS
is True
, we'll also use it to determine group membership.
AUTH_LDAP_BIND_PASSWORD
Default: ''
(Empty string)
The password to use with AUTH_LDAP_BIND_DN
.
AUTH_LDAP_CACHE_TIMEOUT
Default: 0
The value determines the amount of time, in seconds, a user's group memberships and distinguished name are cached. The value 0
, the default, disables caching entirely.
AUTH_LDAP_CONNECTION_OPTIONS
Default: {}
A dictionary of options to pass to each connection to the LDAP server via LDAPObject.set_option() <ldap.LDAPObject.set_option>
. Keys are ldap.OPT_* <ldap-options>
constants.
AUTH_LDAP_DENY_GROUP
Default: None
The distinguished name of a group; authentication will fail for any user that belongs to this group.
AUTH_LDAP_FIND_GROUP_PERMS
Default: False
If True
, ~django_auth_ldap.backend.LDAPBackend
will furnish group permissions based on the LDAP groups the authenticated user belongs to. AUTH_LDAP_GROUP_SEARCH
and AUTH_LDAP_GROUP_TYPE
must also be set.
AUTH_LDAP_GLOBAL_OPTIONS
Default: {}
A dictionary of options to pass to ldap.set_option
. Keys are ldap.OPT_* <ldap-options>
constants.
Note
Due to its global nature, this setting ignores the settings prefix
<multiconfig>
. Regardless of how many backends are installed, this setting is referenced once by its default name at the time we load the ldap module.
AUTH_LDAP_GROUP_SEARCH
Default: None
An ~django_auth_ldap.config.LDAPSearch
object that finds all LDAP groups that users might belong to. If your configuration makes any references to LDAP groups, this and AUTH_LDAP_GROUP_TYPE
must be set.
AUTH_LDAP_GROUP_TYPE
Default: None
An ~django_auth_ldap.config.LDAPGroupType
instance describing the type of group returned by AUTH_LDAP_GROUP_SEARCH
.
AUTH_LDAP_MIRROR_GROUPS
Default: None
If True
, ~django_auth_ldap.backend.LDAPBackend
will mirror a user's LDAP group membership in the Django database. Any time a user authenticates, we will create all of their LDAP groups as Django groups and update their Django group membership to exactly match their LDAP group membership. If the LDAP server has nested groups, the Django database will end up with a flattened representation.
This can also be a list or other collection of group names, in which case we'll only mirror those groups and leave the rest alone. This is ignored if AUTH_LDAP_MIRROR_GROUPS_EXCEPT
is set.
AUTH_LDAP_MIRROR_GROUPS_EXCEPT
Default: None
If this is not None
, it must be a list or other collection of group names. This will enable group mirroring, except that we'll never change the membership of the indicated groups. AUTH_LDAP_MIRROR_GROUPS
is ignored in this case.
AUTH_LDAP_PERMIT_EMPTY_PASSWORD
Default: False
If False
(the default), authentication with an empty password will fail immediately, without any LDAP communication. This is a secure default, as some LDAP servers are configured to allow binds to succeed with no password, perhaps at a reduced level of access. If you need to make use of this LDAP feature, you can change this setting to True
.
AUTH_LDAP_REQUIRE_GROUP
Default: None
The distinguished name of a group; authentication will fail for any user that does not belong to this group. This can also be an ~django_auth_ldap.config.LDAPGroupQuery
instance.
AUTH_LDAP_NO_NEW_USERS
Default: False
Prevent the creation of new users during authentication. Any users not already in the Django user database will not be able to login.
AUTH_LDAP_SERVER_URI
Default: 'ldap://localhost'
The URI of the LDAP server. This can be any URI that is supported by your underlying LDAP libraries. Can also be a callable that returns the URI. The callable is passed a single positional argument: request
.
1.7.0
When AUTH_LDAP_SERVER_URI
is set to a callable, it is now passed a positional request
argument. Support for no arguments will continue for backwards compatibility but will be removed in a future version.
AUTH_LDAP_START_TLS
Default: False
If True
, each connection to the LDAP server will call ~ldap.LDAPObject.start_tls_s
to enable TLS encryption over the standard LDAP port. There are a number of configuration options that can be given to AUTH_LDAP_GLOBAL_OPTIONS
that affect the TLS connection. For example, ldap.OPT_X_TLS_REQUIRE_CERT
can be set to ldap.OPT_X_TLS_NEVER
to disable certificate verification, perhaps to allow self-signed certificates.
AUTH_LDAP_USER_QUERY_FIELD
Default: None
The field on the user model used to query the authenticating user in the database. If unset, uses the value of USERNAME_FIELD
of the model class. When set, the value used to query is obtained through the AUTH_LDAP_USER_ATTR_MAP
. For example, setting AUTH_LDAP_USER_QUERY_FIELD
to username
and adding "username": "sAMAccountName",
to AUTH_LDAP_USER_ATTR_MAP
will cause django to query local database using username
column and LDAP using sAMAccountName
attribute.
AUTH_LDAP_USER_ATTRLIST
Default: None
A list of attribute names to load for the authenticated user. Normally, you can ignore this and the LDAP server will send back all of the attributes of the directory entry. One reason you might need to override this is to get operational attributes, which are not normally included:
AUTH_LDAP_USER_ATTRLIST = ["*", "+"]
AUTH_LDAP_USER_ATTR_MAP
Default: {}
A mapping from ~django.contrib.auth.models.User
field names to LDAP attribute names. A users's ~django.contrib.auth.models.User
object will be populated from his LDAP attributes at login.
AUTH_LDAP_USER_DN_TEMPLATE
Default: None
A string template that describes any user's distinguished name based on the username. This must contain the placeholder %(user)s
.
AUTH_LDAP_USER_FLAGS_BY_GROUP
Default: {}
A mapping from boolean ~django.contrib.auth.models.User
field names to distinguished names of LDAP groups. The corresponding field is set to True
or False
according to whether the user is a member of the group.
Values may be strings for simple group membership tests or ~django_auth_ldap.config.LDAPGroupQuery
instances for more complex cases.
AUTH_LDAP_USER_SEARCH
Default: None
An ~django_auth_ldap.config.LDAPSearch
object that will locate a user in the directory. The filter parameter should contain the placeholder %(user)s
for the username. It must return exactly one result for authentication to succeed.
django_auth_ldap
version
The library's current version number as a 3-tuple.
version_string
The library's current version number as a string.
django_auth_ldap.config
__init__(base_dn, scope, filterstr='(objectClass=*)')
- param str base_dn
The distinguished name of the search base.
- param int scope
One of
ldap.SCOPE_*
.- param str filterstr
An optional filter string (e.g. '(objectClass=person)'). In order to be valid,
filterstr
must be enclosed in parentheses.
1.1
__init__(*searches)
- param searches
Zero or more LDAPSearch objects. The result of the overall search is the union (by DN) of the results of the underlying searches. The precedence of the underlying results and the ordering of the final results are both undefined.
- type searches
LDAPSearch
The base class for objects that will determine group membership for various LDAP grouping mechanisms. Implementations are provided for common group types or you can write your own. See the source code for subclassing notes.
__init__(name_attr='cn')
By default, LDAP groups will be mapped to Django groups by taking the first value of the cn attribute. You can specify a different attribute with name_attr
.
A concrete subclass of ~django_auth_ldap.config.LDAPGroupType
that handles the posixGroup
object class. This checks for both primary group and group membership.
__init__(name_attr='cn')
A concrete subclass of ~django_auth_ldap.config.LDAPGroupType
that handles grouping mechanisms wherein the group object contains a list of its member DNs.
__init__(member_attr, name_attr='cn')
- param str member_attr
The attribute on the group object that contains a list of member DNs. 'member' and 'uniqueMember' are common examples.
Similar to ~django_auth_ldap.config.MemberDNGroupType
, except this allows groups to contain other groups as members. Group hierarchies will be traversed to determine membership.
__init__(member_attr, name_attr='cn')
As above.
A concrete subclass of ~django_auth_ldap.config.MemberDNGroupType
that handles the groupOfNames
object class. Equivalent to MemberDNGroupType('member')
.
__init__(name_attr='cn')
A concrete subclass of ~django_auth_ldap.config.NestedMemberDNGroupType
that handles the groupOfNames
object class. Equivalent to NestedMemberDNGroupType('member')
.
__init__(name_attr='cn')
A concrete subclass of ~django_auth_ldap.config.MemberDNGroupType
that handles the groupOfUniqueNames
object class. Equivalent to MemberDNGroupType('uniqueMember')
.
__init__(name_attr='cn')
A concrete subclass of ~django_auth_ldap.config.NestedMemberDNGroupType
that handles the groupOfUniqueNames
object class. Equivalent to NestedMemberDNGroupType('uniqueMember')
.
__init__(name_attr='cn')
A concrete subclass of ~django_auth_ldap.config.MemberDNGroupType
that handles Active Directory groups. Equivalent to MemberDNGroupType('member')
.
__init__(name_attr='cn')
A concrete subclass of ~django_auth_ldap.config.NestedMemberDNGroupType
that handles Active Directory groups. Equivalent to NestedMemberDNGroupType('member')
.
__init__(name_attr='cn')
A concrete subclass of ~django_auth_ldap.config.MemberDNGroupType
that handles the organizationalRole
object class. Equivalent to MemberDNGroupType('roleOccupant')
.
__init__(name_attr='cn')
A concrete subclass of ~django_auth_ldap.config.NestedMemberDNGroupType
that handles the organizationalRole
object class. Equivalent to NestedMemberDNGroupType('roleOccupant')
.
__init__(name_attr='cn')
Represents a compound query for group membership.
This can be used to construct an arbitrarily complex group membership query with AND, OR, and NOT logical operators. Construct primitive queries with a group DN as the only argument. These queries can then be combined with the &
, |
, and ~
operators.
This is used by certain settings, including AUTH_LDAP_REQUIRE_GROUP
and AUTH_LDAP_USER_FLAGS_BY_GROUP
. An example is shown in limiting-access
.
__init__(group_dn)
- param str group_dn
The distinguished name of a group to test for membership.
django_auth_ldap.backend
populate_user
This is a Django signal that is sent when clients should perform additional customization of a ~django.contrib.auth.models.User
object. It is sent after a user has been authenticated and the backend has finished populating it, and just before it is saved. The client may take this opportunity to populate additional model fields, perhaps based on ldap_user.attrs
. This signal has two keyword arguments: user
is the ~django.contrib.auth.models.User
object and ldap_user
is the same as user.ldap_user
. The sender is the ~django_auth_ldap.backend.LDAPBackend
class.
ldap_error
This is a Django signal that is sent when we receive an ldap.LDAPError
exception. The signal has three keyword arguments:
context
: one of'authenticate'
,'get_group_permissions'
, or'populate_user'
, indicating which API was being called when the exception was caught.user
: the Django user being processed (if available).exception
: the~ldap.LDAPError
object itself.
The sender is the ~django_auth_ldap.backend.LDAPBackend
class (or subclass).
~django_auth_ldap.backend.LDAPBackend
has one method that may be called directly and several that may be overridden in subclasses.
settings_prefix
A prefix for all of our Django settings. By default, this is 'AUTH_LDAP_'
, but subclasses can override this. When different subclasses use different prefixes, they can both be installed and operate independently.
default_settings
A dictionary of default settings. This is empty in ~django_auth_ldap.backend.LDAPBackend
, but subclasses can populate this with values that will override the built-in defaults. Note that the keys should omit the 'AUTH_LDAP_'
prefix.
populate_user(username)
Populates the Django user for the given LDAP username. This connects to the LDAP directory with the default credentials and attempts to populate the indicated Django user as if they had just logged in. AUTH_LDAP_ALWAYS_UPDATE_USER
is ignored (assumed True
).
get_user_model(self)
Returns the user model that ~django_auth_ldap.backend.LDAPBackend.get_or_build_user
will instantiate. By default, custom user models will be respected. Subclasses would most likely override this in order to substitute a proxy model <proxy-models>
.
authenticate_ldap_user(self, ldap_user, password)
Given an LDAP user object and password, authenticates the user and returns a Django user object. See customizing-authentication
.
get_or_build_user(self, username, ldap_user)
Given a username and an LDAP user object, this must return a valid Django user model instance. The username
argument has already been passed through ~django_auth_ldap.backend.LDAPBackend.ldap_to_django_username
. You can get information about the LDAP user via ldap_user.dn
and ldap_user.attrs
. The return value must be an (instance, created) two-tuple. The instance does not need to be saved.
The default implementation looks for the username with a case-insensitive query; if it's not found, the model returned by ~django_auth_ldap.backend.LDAPBackend.get_user_model
will be created with the lowercased username. New users will not be saved to the database until after the django_auth_ldap.backend.populate_user
signal has been sent.
A subclass may override this to associate LDAP users to Django users any way it likes.
ldap_to_django_username(username)
Returns a valid Django username based on the given LDAP username (which is what the user enters). By default, username
is returned unchanged. This can be overridden by subclasses.
django_to_ldap_username(username)
The inverse of ~django_auth_ldap.backend.LDAPBackend.ldap_to_django_username
. If this is not symmetrical to ~django_auth_ldap.backend.LDAPBackend.ldap_to_django_username
, the behavior is undefined.