File: PKG-INFO

package info (click to toggle)
django-python3-ldap 0.15.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 224 kB
  • sloc: python: 975; sh: 6; makefile: 4
file content (306 lines) | stat: -rw-r--r-- 10,987 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
Metadata-Version: 2.1
Name: django-python3-ldap
Version: 0.15.8
Summary: Django LDAP user authentication backend for Python 3.
Home-page: https://github.com/etianen/django-python3-ldap
Author: Dave Hall
Author-email: dave@etianen.com
License: BSD
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Framework :: Django
License-File: LICENSE
Requires-Dist: django>=1.11
Requires-Dist: ldap3<3,>=2.5
Requires-Dist: pyasn1<0.6,>=0.4.6

django-python3-ldap
===================

**django-python3-ldap** provides a Django LDAP user authentication backend. Python 3.6+ is required.


Features
--------

- Authenticate users with an LDAP server.
- Sync LDAP users with a local Django database.
- Supports custom Django user models.


Installation
------------

1. Install using ``pip install django-python3-ldap``.
2. Add ``'django_python3_ldap'`` to your ``INSTALLED_APPS`` setting.
3. Set your ``AUTHENTICATION_BACKENDS`` setting to ``("django_python3_ldap.auth.LDAPBackend",)``
4. Configure the settings for your LDAP server(s) (see Available settings, below).
5. Optionally, run ``./manage.py ldap_sync_users`` (or ``./manage.py ldap_sync_users <list of user lookups>``) to perform an initial sync of LDAP users.
6. Optionally, run ``./manage.py ldap_promote <username>`` to grant superuser admin access to a given user.


Available settings
------------------

**Note**: The settings below show their default values. You only need to add settings to your ``settings.py`` file that you intend to override.


.. code:: python

    # The URL of the LDAP server(s).  List multiple servers for high availability ServerPool connection.
    LDAP_AUTH_URL = ["ldap://localhost:389"]

    # Initiate TLS on connection.
    LDAP_AUTH_USE_TLS = False

    # Specify which TLS version to use (Python 3.10 requires TLSv1 or higher)
    import ssl
    LDAP_AUTH_TLS_VERSION = ssl.PROTOCOL_TLSv1_2

    # Specify which TLS ciphers to use.
    LDAP_AUTH_TLS_CIPHERS = "ALL"

    # Unspecified TLS keyword arguments applied to the connection on the underlying `ldap3` library.
    LDAP_AUTH_TLS_ARGS = {}

    # The LDAP search base for looking up users.
    LDAP_AUTH_SEARCH_BASE = "ou=people,dc=example,dc=com"

    # The LDAP class that represents a user.
    LDAP_AUTH_OBJECT_CLASS = "inetOrgPerson"

    # User model fields mapped to the LDAP
    # attributes that represent them.
    LDAP_AUTH_USER_FIELDS = {
        "username": "uid",
        "first_name": "givenName",
        "last_name": "sn",
        "email": "mail",
    }

    # A tuple of django model fields used to uniquely identify a user.
    LDAP_AUTH_USER_LOOKUP_FIELDS = ("username",)

    # Path to a callable that takes a dict of {model_field_name: value},
    # returning a dict of clean model data.
    # Use this to customize how data loaded from LDAP is saved to the User model.
    LDAP_AUTH_CLEAN_USER_DATA = "django_python3_ldap.utils.clean_user_data"

    # Path to a callable that takes a user model, a dict of {ldap_field_name: [value]}
    # a LDAP connection object (to allow further lookups), and saves any additional
    # user relationships based on the LDAP data.
    # Use this to customize how data loaded from LDAP is saved to User model relations.
    # For customizing non-related User model fields, use LDAP_AUTH_CLEAN_USER_DATA.
    LDAP_AUTH_SYNC_USER_RELATIONS = "django_python3_ldap.utils.sync_user_relations"

    # Path to a callable that takes a dict of {ldap_field_name: value},
    # returning a list of [ldap_search_filter]. The search filters will then be AND'd
    # together when creating the final search filter.
    LDAP_AUTH_FORMAT_SEARCH_FILTERS = "django_python3_ldap.utils.format_search_filters"

    # Path to a callable that takes a dict of {model_field_name: value}, and returns
    # a string of the username to bind to the LDAP server.
    # Use this to support different types of LDAP server.
    LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_openldap"

    # Sets the login domain for Active Directory users.
    LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = None

    # The LDAP username and password of a user for querying the LDAP database for user
    # details. If None, then the authenticated user will be used for querying, and
    # the `ldap_sync_users`, `ldap_clean_users` commands will perform an anonymous query.
    LDAP_AUTH_CONNECTION_USERNAME = None
    LDAP_AUTH_CONNECTION_PASSWORD = None

    # Use SSL on the connection.
    LDAP_AUTH_CONNECT_USE_SSL = False

    # Set connection/receive timeouts (in seconds) on the underlying `ldap3` library.
    LDAP_AUTH_CONNECT_TIMEOUT = None
    LDAP_AUTH_RECEIVE_TIMEOUT = None

    # Unspecified keyword arguments to apply to the connection in the underlying `ldap3` library.
    LDAP_AUTH_CONNECT_ARGS = {}

    # Set connection pool `active` parameter on the underlying `ldap3` library.
    LDAP_AUTH_POOL_ACTIVE = True

Microsoft Active Directory support
----------------------------------

django-python3-ldap is configured by default to support login via OpenLDAP. To connect to
a Microsoft Active Directory, you need to modify your settings file.

For simple usernames (e.g. "username"):

.. code:: python

    LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_active_directory"

For down-level login name formats (e.g. "DOMAIN\\username"):

.. code:: python

    LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_active_directory"
    LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = "DOMAIN"

For user-principal-name formats (e.g. "user@domain.com"):

.. code:: python

    LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_active_directory_principal"
    LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = "domain.com"

Depending on how your Active Directory server is configured, the following additional settings may match your server
better than the defaults used by django-python3-ldap:

.. code:: python

    LDAP_AUTH_USER_FIELDS = {
        "username": "sAMAccountName",
        "first_name": "givenName",
        "last_name": "sn",
        "email": "mail",
    }

    LDAP_AUTH_OBJECT_CLASS = "user"


Sync User Relations
-------------------

As part of the user authentication process, django-python3-ldap calls a function specified by the
LDAP_AUTH_SYNC_USER_RELATIONS configuraton item.  This function can be used for making additional
updates to the user database (for example updaing the groups the user is a member of), or getting
further information from the LDAP server.

The signature of the called function is:-

.. code:: python

    def sync_user_relations(user, ldap_attributes, *, connection=None, dn=None):

The parameters are:-

- ``user`` - a Django user model object
- ``ldap_attributes`` - a dict of LDAP attributes
- ``connection`` - the LDAP connection object (optional keyword only parameter)
- ``dn`` - the DN (Distinguished Name) of the LDAP matched user (optional keyword only parameter)


Clean User
----------

When a LDAP user is removed from server it could be interresting to deactive or delete its local Django account
to prevent unauthorized access.

To do so run:

    ``./manage.py ldap_clean_users`` (or ``./manage.py ldap_clean_users --purge``).

It will deactivate all local users non declared on LDAP server. If ``--purge`` is specified, all local users will be deleted.


Can't get authentication to work?
---------------------------------

LDAP is a very complicated protocol. Enable logging (see below), and see what error messages the LDAP connection is throwing.


Logging
-------

Print information about failed logins to your console by adding the following to your ``settings.py`` file.

.. code:: python

    LOGGING = {
        "version": 1,
        "disable_existing_loggers": False,
        "handlers": {
            "console": {
                "class": "logging.StreamHandler",
            },
        },
        "loggers": {
            "django_python3_ldap": {
                "handlers": ["console"],
                "level": "INFO",
            },
        },
    }


Custom user filters
-------------------

By default, any users within ``LDAP_AUTH_SEARCH_BASE`` and of the correct ``LDAP_AUTH_OBJECT_CLASS``
will be considered a valid user. You can apply further filtering by setting a custom ``LDAP_AUTH_FORMAT_SEARCH_FILTERS``
callable.

.. code:: python

    # settings.py
    LDAP_AUTH_FORMAT_SEARCH_FILTERS = "path.to.your.custom_format_search_filters"

    # path/to/your/module.py
    from django_python3_ldap.utils import format_search_filters

    def custom_format_search_filters(ldap_fields):
        # Add in simple filters.
        ldap_fields["memberOf"] = "foo"
        # Call the base format callable.
        search_filters = format_search_filters(ldap_fields)
        # Advanced: apply custom LDAP filter logic.
        search_filters.append("(|(memberOf=groupA)(memberOf=GroupB))")
        # All done!
        return search_filters

The returned list of search filters will be AND'd together to make the final search filter.


How it works
------------

When a user attempts to authenticate, a connection is made to one of the listed LDAP
servers, and the application attempts to bind using the provided username and password.

If the bind attempt is successful, the user details are loaded from the LDAP server
and saved in a local Django ``User`` model. The local model is only created once,
and the details will be kept updated with the LDAP record details on every login.

To perform a full sync of all LDAP users to the local database, run ``./manage.py ldap_sync_users``.
This is not required, as the authentication backend will create users on demand. Syncing users has
the advantage of allowing you to assign permissions and groups to the existing users using the Django
admin interface.

Running ``ldap_sync_users`` as a background cron task is another optional way to
keep all users in sync on a regular basis.


Support and announcements
-------------------------

Downloads and bug tracking can be found at the `main project
website <http://github.com/etianen/django-python3-ldap>`_.


More information
----------------

The django-python3-ldap project was developed by Dave Hall. You can get the code
from the `django-python3-ldap project site <http://github.com/etianen/django-python3-ldap>`_.

Dave Hall is a freelance web developer, based in Cambridge, UK. You can usually
find him on the Internet in a number of different places:

-  `Website <http://www.etianen.com/>`_
-  `Twitter <http://twitter.com/etianen>`_
-  `Google Profile <http://www.google.com/profiles/david.etianen>`_