File: reference.rst

package info (click to toggle)
django-auth-ldap 5.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 464 kB
  • sloc: python: 2,599; makefile: 23; sh: 12
file content (669 lines) | stat: -rw-r--r-- 21,865 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
Reference
=========

Settings
--------

.. setting:: AUTH_LDAP_ALWAYS_UPDATE_USER

AUTH_LDAP_ALWAYS_UPDATE_USER
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Default: ``True``

If ``True``, the fields of a :class:`~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 :class:`~django.contrib.auth.models.User` object
will only be populated when it is automatically created.


.. setting:: AUTH_LDAP_AUTHORIZE_ALL_USERS

AUTH_LDAP_AUTHORIZE_ALL_USERS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Default: ``False``

If ``True``, :class:`~django_auth_ldap.backend.LDAPBackend` will be able furnish
permissions for any Django user, regardless of which backend authenticated it.


.. setting:: AUTH_LDAP_BIND_AS_AUTHENTICATING_USER

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.
Remember to set :setting:`AUTH_LDAP_USER_DN_TEMPLATE` to avoid initial connection
to LDAP with default bind credentials.


.. setting:: AUTH_LDAP_REFRESH_DN_ON_BIND

AUTH_LDAP_REFRESH_DN_ON_BIND
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Default: ``False``

If ``True`` and :setting:`AUTH_LDAP_BIND_AS_AUTHENTICATING_USER` is ``True`` and
:setting:`AUTH_LDAP_USER_DN_TEMPLATE` is set, after performing bind login it refresh
the DN attribute of the user. This is meant for such cases in which users authenticates
via `userPrincipalName` and in which `distinguishedName` is not inferrable by that
attribute.


.. setting:: AUTH_LDAP_BIND_DN

AUTH_LDAP_BIND_DN
~~~~~~~~~~~~~~~~~

Default: ``''`` (Empty string)

The distinguished name to use when binding to the LDAP server (with
:setting:`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 :setting:`AUTH_LDAP_USER_DN_TEMPLATE` is not set, we'll
use this to search for the user. If :setting:`AUTH_LDAP_FIND_GROUP_PERMS` is
``True``, we'll also use it to determine group membership.


.. setting:: AUTH_LDAP_BIND_PASSWORD

AUTH_LDAP_BIND_PASSWORD
~~~~~~~~~~~~~~~~~~~~~~~

Default: ``''`` (Empty string)

The password to use with :setting:`AUTH_LDAP_BIND_DN`.


.. setting:: AUTH_LDAP_CACHE_TIMEOUT

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.

.. setting:: AUTH_LDAP_CONNECTION_OPTIONS

AUTH_LDAP_CONNECTION_OPTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Default: ``{}``

A dictionary of options to pass to each connection to the LDAP server via
:meth:`LDAPObject.set_option() <ldap.LDAPObject.set_option>`. Keys are
:ref:`ldap.OPT_* <ldap-options>` constants.


.. setting:: AUTH_LDAP_DENY_GROUP

AUTH_LDAP_DENY_GROUP
~~~~~~~~~~~~~~~~~~~~

Default: ``None``

The distinguished name of a group; authentication will fail for any user
that belongs to this group.


.. setting:: AUTH_LDAP_FIND_GROUP_PERMS

AUTH_LDAP_FIND_GROUP_PERMS
~~~~~~~~~~~~~~~~~~~~~~~~~~

Default: ``False``

If ``True``, :class:`~django_auth_ldap.backend.LDAPBackend` looks up Django
:class:`~django.contrib.auth.models.Group`\ s matching LDAP group names, and
assigns user permissions based on the Django
:class:`~django.contrib.auth.models.Group` permissions.

:setting:`AUTH_LDAP_GROUP_SEARCH` and :setting:`AUTH_LDAP_GROUP_TYPE` must also
be set.

.. important:: Users will not be added to the Django
   :class:`~django.contrib.auth.models.Group`. The LDAP groups remain the
   source of truth for group membership.

.. setting:: AUTH_LDAP_GLOBAL_OPTIONS

AUTH_LDAP_GLOBAL_OPTIONS
~~~~~~~~~~~~~~~~~~~~~~~~

Default: ``{}``

A dictionary of options to pass to :func:`ldap.set_option`. Keys are
:ref:`ldap.OPT_* <ldap-options>` constants.

.. note::

    Due to its global nature, this setting ignores the :doc:`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.


.. setting:: AUTH_LDAP_GROUP_SEARCH

AUTH_LDAP_GROUP_SEARCH
~~~~~~~~~~~~~~~~~~~~~~

Default: ``None``

An :class:`~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 :setting:`AUTH_LDAP_GROUP_TYPE` must be set.


.. setting:: AUTH_LDAP_GROUP_TYPE

AUTH_LDAP_GROUP_TYPE
~~~~~~~~~~~~~~~~~~~~

Default: ``None``

An :class:`~django_auth_ldap.config.LDAPGroupType` instance describing the type
of group returned by :setting:`AUTH_LDAP_GROUP_SEARCH`.


.. setting:: AUTH_LDAP_MIRROR_GROUPS

AUTH_LDAP_MIRROR_GROUPS
~~~~~~~~~~~~~~~~~~~~~~~

Default: ``None``

If ``True``, :class:`~django_auth_ldap.backend.LDAPBackend` will mirror a
user's LDAP group membership in the Django database. Any time a user
authenticates through the :class:`~django_auth_ldap.backend.LDAPBackend`, 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
:setting:`AUTH_LDAP_MIRROR_GROUPS_EXCEPT` is set.

.. note:: Users authenticating through another authentication backend, such as
   :class:`~django.contrib.auth.backends.ModelBackend` will **not** have their
   group membership and permissions refreshed from the LDAP server.


.. setting:: AUTH_LDAP_MIRROR_GROUPS_EXCEPT

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. :setting:`AUTH_LDAP_MIRROR_GROUPS` is ignored in this
case.


.. setting:: AUTH_LDAP_PERMIT_EMPTY_PASSWORD

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``.


.. setting:: AUTH_LDAP_REQUIRE_GROUP

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
:class:`~django_auth_ldap.config.LDAPGroupQuery` instance.


.. setting:: AUTH_LDAP_NO_NEW_USERS

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.


.. setting:: AUTH_LDAP_SERVER_URI

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``.

.. versionchanged:: 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.


.. setting:: AUTH_LDAP_START_TLS

AUTH_LDAP_START_TLS
~~~~~~~~~~~~~~~~~~~

Default: ``False``

If ``True``, each connection to the LDAP server will call
:meth:`~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
:setting:`AUTH_LDAP_GLOBAL_OPTIONS` that affect the TLS connection. For example,
:data:`ldap.OPT_X_TLS_REQUIRE_CERT` can be set to :data:`ldap.OPT_X_TLS_NEVER`
to disable certificate verification, perhaps to allow self-signed certificates.


.. setting:: AUTH_LDAP_USER_QUERY_FIELD

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
:setting:`AUTH_LDAP_USER_ATTR_MAP`. For example, setting :setting:`AUTH_LDAP_USER_QUERY_FIELD`
to ``username`` and adding ``"username": "sAMAccountName",`` to :setting:`AUTH_LDAP_USER_ATTR_MAP`
will cause django to query local database using ``username`` column and LDAP using
``sAMAccountName`` attribute.


.. setting:: AUTH_LDAP_USER_ATTRLIST

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:

.. code-block:: python

    AUTH_LDAP_USER_ATTRLIST = ["*", "+"]


.. setting:: AUTH_LDAP_USER_ATTR_MAP

AUTH_LDAP_USER_ATTR_MAP
~~~~~~~~~~~~~~~~~~~~~~~

Default: ``{}``

A mapping from :class:`~django.contrib.auth.models.User` field names to LDAP
attribute names. A users's :class:`~django.contrib.auth.models.User` object will
be populated from his LDAP attributes at login.


.. setting:: AUTH_LDAP_USER_DN_TEMPLATE

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``.


.. setting:: AUTH_LDAP_USER_FLAGS_BY_GROUP

AUTH_LDAP_USER_FLAGS_BY_GROUP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Default: ``{}``

A mapping from boolean :class:`~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
:class:`~django_auth_ldap.config.LDAPGroupQuery` instances for more complex
cases.


.. setting:: AUTH_LDAP_USER_SEARCH

AUTH_LDAP_USER_SEARCH
~~~~~~~~~~~~~~~~~~~~~

Default: ``None``

An :class:`~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.


Module Properties
-----------------

.. module:: django_auth_ldap

.. data:: version

    The library's current version number as a 3-tuple.

.. data:: version_string

    The library's current version number as a string.


Configuration
-------------

.. module:: django_auth_ldap.config

.. class:: LDAPSearch

    .. method:: __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.


.. class:: LDAPSearchUnion

    .. versionadded:: 1.1

    .. method:: __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: :class:`LDAPSearch`


.. class:: LDAPGroupType

    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.

    .. method:: __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``.


.. class:: PosixGroupType

    A concrete subclass of :class:`~django_auth_ldap.config.LDAPGroupType` that
    handles the ``posixGroup`` object class. This checks for both primary group
    and group membership.

    .. method:: __init__(name_attr='cn')


.. class:: MemberDNGroupType

    A concrete subclass of
    :class:`~django_auth_ldap.config.LDAPGroupType` that handles grouping
    mechanisms wherein the group object contains a list of its member DNs.

    .. method:: __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.


.. class:: NestedMemberDNGroupType

    Similar to :class:`~django_auth_ldap.config.MemberDNGroupType`, except this
    allows groups to contain other groups as members. Group hierarchies will be
    traversed to determine membership.

    .. method:: __init__(member_attr, name_attr='cn')

        As above.


.. class:: GroupOfNamesType

    A concrete subclass of :class:`~django_auth_ldap.config.MemberDNGroupType`
    that handles the ``groupOfNames`` object class. Equivalent to
    ``MemberDNGroupType('member')``.

    .. method:: __init__(name_attr='cn')


.. class:: NestedGroupOfNamesType

    A concrete subclass of
    :class:`~django_auth_ldap.config.NestedMemberDNGroupType` that handles the
    ``groupOfNames`` object class. Equivalent to
    ``NestedMemberDNGroupType('member')``.

    .. method:: __init__(name_attr='cn')


.. class:: GroupOfUniqueNamesType

    A concrete subclass of :class:`~django_auth_ldap.config.MemberDNGroupType`
    that handles the ``groupOfUniqueNames`` object class. Equivalent to
    ``MemberDNGroupType('uniqueMember')``.

    .. method:: __init__(name_attr='cn')


.. class:: NestedGroupOfUniqueNamesType

    A concrete subclass of
    :class:`~django_auth_ldap.config.NestedMemberDNGroupType` that handles the
    ``groupOfUniqueNames`` object class. Equivalent to
    ``NestedMemberDNGroupType('uniqueMember')``.

    .. method:: __init__(name_attr='cn')


.. class:: ActiveDirectoryGroupType

    A concrete subclass of :class:`~django_auth_ldap.config.MemberDNGroupType`
    that handles Active Directory groups. Equivalent to
    ``MemberDNGroupType('member')``.

    .. method:: __init__(name_attr='cn')


.. class:: NestedActiveDirectoryGroupType

    A concrete subclass of
    :class:`~django_auth_ldap.config.NestedMemberDNGroupType` that handles
    Active Directory groups. Equivalent to
    ``NestedMemberDNGroupType('member')``.

    .. method:: __init__(name_attr='cn')


.. class:: OrganizationalRoleGroupType

    A concrete subclass of :class:`~django_auth_ldap.config.MemberDNGroupType`
    that handles the ``organizationalRole`` object class. Equivalent to
    ``MemberDNGroupType('roleOccupant')``.

    .. method:: __init__(name_attr='cn')


.. class:: NestedOrganizationalRoleGroupType

    A concrete subclass of
    :class:`~django_auth_ldap.config.NestedMemberDNGroupType` that handles the
    ``organizationalRole`` object class. Equivalent to
    ``NestedMemberDNGroupType('roleOccupant')``.

    .. method:: __init__(name_attr='cn')


.. class:: LDAPGroupQuery

    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
    :setting:`AUTH_LDAP_REQUIRE_GROUP` and
    :setting:`AUTH_LDAP_USER_FLAGS_BY_GROUP`. An example is shown in
    :ref:`limiting-access`.

    .. method:: __init__(group_dn)

        :param str group_dn: The distinguished name of a group to test for
            membership.


Backend
-------

.. module:: django_auth_ldap.backend

.. data:: populate_user

    This is a Django signal that is sent when clients should perform additional
    customization of a :class:`~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
    :class:`~django.contrib.auth.models.User` object and ``ldap_user`` is the
    same as ``user.ldap_user``. The sender is the
    :class:`~django_auth_ldap.backend.LDAPBackend` class.

.. data:: ldap_error

    This is a Django signal that is sent when we receive an
    :exc:`ldap.LDAPError` exception. The signal has four keyword arguments:

    - ``context``: one of ``'authenticate'``, ``'get_group_permissions'``, or
      ``'populate_user'``, ``'search_for_user_dn'`` or ``'mirror_groups'``,
      indicating which API was being called when the exception was caught.
    - ``user``: the Django user being processed (if available) or ``None``.
    - ``request``: the Django request object associated with the
      authentication attempt (if available) or ``None``.
    - ``exception``: the :exc:`~ldap.LDAPError` object itself.

    The sender is the :class:`~django_auth_ldap.backend.LDAPBackend` class (or
    subclass).

    By default, LDAP errors are be handled by ``django_auth_ldap`` by failing
    the authentication. If instead you wish to propagate the error to up
    application code, then raise an exception from the signal handler.

.. class:: LDAPBackend

    :class:`~django_auth_ldap.backend.LDAPBackend` has one method that may be
    called directly and several that may be overridden in subclasses.

    .. data:: 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.

    .. data:: default_settings

        A dictionary of default settings. This is empty in
        :class:`~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.

    .. method:: 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.
        :setting:`AUTH_LDAP_ALWAYS_UPDATE_USER` is ignored (assumed ``True``).

    .. method:: get_user_model(self)

        Returns the user model that
        :meth:`~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
        :ref:`proxy model <proxy-models>`.

    .. method:: authenticate_ldap_user(self, ldap_user, password)

       Given an LDAP user object and password, authenticates the user and
       returns a Django user object. See :ref:`customizing-authentication`.

    .. method:: 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
        :meth:`~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
        :meth:`~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 :data:`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.

    .. method:: 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.

    .. method:: django_to_ldap_username(username)

        The inverse of
        :meth:`~django_auth_ldap.backend.LDAPBackend.ldap_to_django_username`.
        If this is not symmetrical to
        :meth:`~django_auth_ldap.backend.LDAPBackend.ldap_to_django_username`,
        the behavior is undefined.