| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: django-python3-ldap |
| 3 |
Version: 0.15.8 |
| 4 |
Summary: Django LDAP user authentication backend for Python 3. |
| 5 |
Home-page: https://github.com/etianen/django-python3-ldap |
| 6 |
Author: Dave Hall |
| 7 |
Author-email: [email protected] |
| 8 |
License: BSD |
| 9 |
Classifier: Development Status :: 4 - Beta |
| 10 |
Classifier: Environment :: Web Environment |
| 11 |
Classifier: Intended Audience :: Developers |
| 12 |
Classifier: License :: OSI Approved :: BSD License |
| 13 |
Classifier: Operating System :: OS Independent |
| 14 |
Classifier: Programming Language :: Python |
| 15 |
Classifier: Programming Language :: Python :: 3.6 |
| 16 |
Classifier: Programming Language :: Python :: 3.7 |
| 17 |
Classifier: Programming Language :: Python :: 3.8 |
| 18 |
Classifier: Framework :: Django |
| 19 |
License-File: LICENSE |
| 20 |
Requires-Dist: django>=1.11 |
| 21 |
Requires-Dist: ldap3<3,>=2.5 |
| 22 |
Requires-Dist: pyasn1<0.6,>=0.4.6 |
| 23 |
|
| 24 |
django-python3-ldap |
| 25 |
=================== |
| 26 |
|
| 27 |
**django-python3-ldap** provides a Django LDAP user authentication backend. Python 3.6+ is required. |
| 28 |
|
| 29 |
|
| 30 |
Features |
| 31 |
-------- |
| 32 |
|
| 33 |
- Authenticate users with an LDAP server. |
| 34 |
- Sync LDAP users with a local Django database. |
| 35 |
- Supports custom Django user models. |
| 36 |
|
| 37 |
|
| 38 |
Installation |
| 39 |
------------ |
| 40 |
|
| 41 |
1. Install using ``pip install django-python3-ldap``. |
| 42 |
2. Add ``'django_python3_ldap'`` to your ``INSTALLED_APPS`` setting. |
| 43 |
3. Set your ``AUTHENTICATION_BACKENDS`` setting to ``("django_python3_ldap.auth.LDAPBackend",)`` |
| 44 |
4. Configure the settings for your LDAP server(s) (see Available settings, below). |
| 45 |
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. |
| 46 |
6. Optionally, run ``./manage.py ldap_promote <username>`` to grant superuser admin access to a given user. |
| 47 |
|
| 48 |
|
| 49 |
Available settings |
| 50 |
------------------ |
| 51 |
|
| 52 |
**Note**: The settings below show their default values. You only need to add settings to your ``settings.py`` file that you intend to override. |
| 53 |
|
| 54 |
|
| 55 |
.. code:: python |
| 56 |
|
| 57 |
# The URL of the LDAP server(s). List multiple servers for high availability ServerPool connection. |
| 58 |
LDAP_AUTH_URL = ["ldap://localhost:389"] |
| 59 |
|
| 60 |
# Initiate TLS on connection. |
| 61 |
LDAP_AUTH_USE_TLS = False |
| 62 |
|
| 63 |
# Specify which TLS version to use (Python 3.10 requires TLSv1 or higher) |
| 64 |
import ssl |
| 65 |
LDAP_AUTH_TLS_VERSION = ssl.PROTOCOL_TLSv1_2 |
| 66 |
|
| 67 |
# Specify which TLS ciphers to use. |
| 68 |
LDAP_AUTH_TLS_CIPHERS = "ALL" |
| 69 |
|
| 70 |
# Unspecified TLS keyword arguments applied to the connection on the underlying `ldap3` library. |
| 71 |
LDAP_AUTH_TLS_ARGS = {} |
| 72 |
|
| 73 |
# The LDAP search base for looking up users. |
| 74 |
LDAP_AUTH_SEARCH_BASE = "ou=people,dc=example,dc=com" |
| 75 |
|
| 76 |
# The LDAP class that represents a user. |
| 77 |
LDAP_AUTH_OBJECT_CLASS = "inetOrgPerson" |
| 78 |
|
| 79 |
# User model fields mapped to the LDAP |
| 80 |
# attributes that represent them. |
| 81 |
LDAP_AUTH_USER_FIELDS = { |
| 82 |
"username": "uid", |
| 83 |
"first_name": "givenName", |
| 84 |
"last_name": "sn", |
| 85 |
"email": "mail", |
| 86 |
} |
| 87 |
|
| 88 |
# A tuple of django model fields used to uniquely identify a user. |
| 89 |
LDAP_AUTH_USER_LOOKUP_FIELDS = ("username",) |
| 90 |
|
| 91 |
# Path to a callable that takes a dict of {model_field_name: value}, |
| 92 |
# returning a dict of clean model data. |
| 93 |
# Use this to customize how data loaded from LDAP is saved to the User model. |
| 94 |
LDAP_AUTH_CLEAN_USER_DATA = "django_python3_ldap.utils.clean_user_data" |
| 95 |
|
| 96 |
# Path to a callable that takes a user model, a dict of {ldap_field_name: [value]} |
| 97 |
# a LDAP connection object (to allow further lookups), and saves any additional |
| 98 |
# user relationships based on the LDAP data. |
| 99 |
# Use this to customize how data loaded from LDAP is saved to User model relations. |
| 100 |
# For customizing non-related User model fields, use LDAP_AUTH_CLEAN_USER_DATA. |
| 101 |
LDAP_AUTH_SYNC_USER_RELATIONS = "django_python3_ldap.utils.sync_user_relations" |
| 102 |
|
| 103 |
# Path to a callable that takes a dict of {ldap_field_name: value}, |
| 104 |
# returning a list of [ldap_search_filter]. The search filters will then be AND'd |
| 105 |
# together when creating the final search filter. |
| 106 |
LDAP_AUTH_FORMAT_SEARCH_FILTERS = "django_python3_ldap.utils.format_search_filters" |
| 107 |
|
| 108 |
# Path to a callable that takes a dict of {model_field_name: value}, and returns |
| 109 |
# a string of the username to bind to the LDAP server. |
| 110 |
# Use this to support different types of LDAP server. |
| 111 |
LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_openldap" |
| 112 |
|
| 113 |
# Sets the login domain for Active Directory users. |
| 114 |
LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = None |
| 115 |
|
| 116 |
# The LDAP username and password of a user for querying the LDAP database for user |
| 117 |
# details. If None, then the authenticated user will be used for querying, and |
| 118 |
# the `ldap_sync_users`, `ldap_clean_users` commands will perform an anonymous query. |
| 119 |
LDAP_AUTH_CONNECTION_USERNAME = None |
| 120 |
LDAP_AUTH_CONNECTION_PASSWORD = None |
| 121 |
|
| 122 |
# Use SSL on the connection. |
| 123 |
LDAP_AUTH_CONNECT_USE_SSL = False |
| 124 |
|
| 125 |
# Set connection/receive timeouts (in seconds) on the underlying `ldap3` library. |
| 126 |
LDAP_AUTH_CONNECT_TIMEOUT = None |
| 127 |
LDAP_AUTH_RECEIVE_TIMEOUT = None |
| 128 |
|
| 129 |
# Unspecified keyword arguments to apply to the connection in the underlying `ldap3` library. |
| 130 |
LDAP_AUTH_CONNECT_ARGS = {} |
| 131 |
|
| 132 |
# Set connection pool `active` parameter on the underlying `ldap3` library. |
| 133 |
LDAP_AUTH_POOL_ACTIVE = True |
| 134 |
|
| 135 |
Microsoft Active Directory support |
| 136 |
---------------------------------- |
| 137 |
|
| 138 |
django-python3-ldap is configured by default to support login via OpenLDAP. To connect to |
| 139 |
a Microsoft Active Directory, you need to modify your settings file. |
| 140 |
|
| 141 |
For simple usernames (e.g. "username"): |
| 142 |
|
| 143 |
.. code:: python |
| 144 |
|
| 145 |
LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_active_directory" |
| 146 |
|
| 147 |
For down-level login name formats (e.g. "DOMAIN\\username"): |
| 148 |
|
| 149 |
.. code:: python |
| 150 |
|
| 151 |
LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_active_directory" |
| 152 |
LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = "DOMAIN" |
| 153 |
|
| 154 |
For user-principal-name formats (e.g. "[email protected]"): |
| 155 |
|
| 156 |
.. code:: python |
| 157 |
|
| 158 |
LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_active_directory_principal" |
| 159 |
LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = "domain.com" |
| 160 |
|
| 161 |
Depending on how your Active Directory server is configured, the following additional settings may match your server |
| 162 |
better than the defaults used by django-python3-ldap: |
| 163 |
|
| 164 |
.. code:: python |
| 165 |
|
| 166 |
LDAP_AUTH_USER_FIELDS = { |
| 167 |
"username": "sAMAccountName", |
| 168 |
"first_name": "givenName", |
| 169 |
"last_name": "sn", |
| 170 |
"email": "mail", |
| 171 |
} |
| 172 |
|
| 173 |
LDAP_AUTH_OBJECT_CLASS = "user" |
| 174 |
|
| 175 |
|
| 176 |
Sync User Relations |
| 177 |
------------------- |
| 178 |
|
| 179 |
As part of the user authentication process, django-python3-ldap calls a function specified by the |
| 180 |
LDAP_AUTH_SYNC_USER_RELATIONS configuraton item. This function can be used for making additional |
| 181 |
updates to the user database (for example updaing the groups the user is a member of), or getting |
| 182 |
further information from the LDAP server. |
| 183 |
|
| 184 |
The signature of the called function is:- |
| 185 |
|
| 186 |
.. code:: python |
| 187 |
|
| 188 |
def sync_user_relations(user, ldap_attributes, *, connection=None, dn=None): |
| 189 |
|
| 190 |
The parameters are:- |
| 191 |
|
| 192 |
- ``user`` - a Django user model object |
| 193 |
- ``ldap_attributes`` - a dict of LDAP attributes |
| 194 |
- ``connection`` - the LDAP connection object (optional keyword only parameter) |
| 195 |
- ``dn`` - the DN (Distinguished Name) of the LDAP matched user (optional keyword only parameter) |
| 196 |
|
| 197 |
|
| 198 |
Clean User |
| 199 |
---------- |
| 200 |
|
| 201 |
When a LDAP user is removed from server it could be interresting to deactive or delete its local Django account |
| 202 |
to prevent unauthorized access. |
| 203 |
|
| 204 |
To do so run: |
| 205 |
|
| 206 |
``./manage.py ldap_clean_users`` (or ``./manage.py ldap_clean_users --purge``). |
| 207 |
|
| 208 |
It will deactivate all local users non declared on LDAP server. If ``--purge`` is specified, all local users will be deleted. |
| 209 |
|
| 210 |
|
| 211 |
Can't get authentication to work? |
| 212 |
--------------------------------- |
| 213 |
|
| 214 |
LDAP is a very complicated protocol. Enable logging (see below), and see what error messages the LDAP connection is throwing. |
| 215 |
|
| 216 |
|
| 217 |
Logging |
| 218 |
------- |
| 219 |
|
| 220 |
Print information about failed logins to your console by adding the following to your ``settings.py`` file. |
| 221 |
|
| 222 |
.. code:: python |
| 223 |
|
| 224 |
LOGGING = { |
| 225 |
"version": 1, |
| 226 |
"disable_existing_loggers": False, |
| 227 |
"handlers": { |
| 228 |
"console": { |
| 229 |
"class": "logging.StreamHandler", |
| 230 |
}, |
| 231 |
}, |
| 232 |
"loggers": { |
| 233 |
"django_python3_ldap": { |
| 234 |
"handlers": ["console"], |
| 235 |
"level": "INFO", |
| 236 |
}, |
| 237 |
}, |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
Custom user filters |
| 242 |
------------------- |
| 243 |
|
| 244 |
By default, any users within ``LDAP_AUTH_SEARCH_BASE`` and of the correct ``LDAP_AUTH_OBJECT_CLASS`` |
| 245 |
will be considered a valid user. You can apply further filtering by setting a custom ``LDAP_AUTH_FORMAT_SEARCH_FILTERS`` |
| 246 |
callable. |
| 247 |
|
| 248 |
.. code:: python |
| 249 |
|
| 250 |
# settings.py |
| 251 |
LDAP_AUTH_FORMAT_SEARCH_FILTERS = "path.to.your.custom_format_search_filters" |
| 252 |
|
| 253 |
# path/to/your/module.py |
| 254 |
from django_python3_ldap.utils import format_search_filters |
| 255 |
|
| 256 |
def custom_format_search_filters(ldap_fields): |
| 257 |
# Add in simple filters. |
| 258 |
ldap_fields["memberOf"] = "foo" |
| 259 |
# Call the base format callable. |
| 260 |
search_filters = format_search_filters(ldap_fields) |
| 261 |
# Advanced: apply custom LDAP filter logic. |
| 262 |
search_filters.append("(|(memberOf=groupA)(memberOf=GroupB))") |
| 263 |
# All done! |
| 264 |
return search_filters |
| 265 |
|
| 266 |
The returned list of search filters will be AND'd together to make the final search filter. |
| 267 |
|
| 268 |
|
| 269 |
How it works |
| 270 |
------------ |
| 271 |
|
| 272 |
When a user attempts to authenticate, a connection is made to one of the listed LDAP |
| 273 |
servers, and the application attempts to bind using the provided username and password. |
| 274 |
|
| 275 |
If the bind attempt is successful, the user details are loaded from the LDAP server |
| 276 |
and saved in a local Django ``User`` model. The local model is only created once, |
| 277 |
and the details will be kept updated with the LDAP record details on every login. |
| 278 |
|
| 279 |
To perform a full sync of all LDAP users to the local database, run ``./manage.py ldap_sync_users``. |
| 280 |
This is not required, as the authentication backend will create users on demand. Syncing users has |
| 281 |
the advantage of allowing you to assign permissions and groups to the existing users using the Django |
| 282 |
admin interface. |
| 283 |
|
| 284 |
Running ``ldap_sync_users`` as a background cron task is another optional way to |
| 285 |
keep all users in sync on a regular basis. |
| 286 |
|
| 287 |
|
| 288 |
Support and announcements |
| 289 |
------------------------- |
| 290 |
|
| 291 |
Downloads and bug tracking can be found at the `main project |
| 292 |
website <http://github.com/etianen/django-python3-ldap>`_. |
| 293 |
|
| 294 |
|
| 295 |
More information |
| 296 |
---------------- |
| 297 |
|
| 298 |
The django-python3-ldap project was developed by Dave Hall. You can get the code |
| 299 |
from the `django-python3-ldap project site <http://github.com/etianen/django-python3-ldap>`_. |
| 300 |
|
| 301 |
Dave Hall is a freelance web developer, based in Cambridge, UK. You can usually |
| 302 |
find him on the Internet in a number of different places: |
| 303 |
|
| 304 |
- `Website <http://www.etianen.com/>`_ |
| 305 |
- `Twitter <http://twitter.com/etianen>`_ |
| 306 |
- `Google Profile <http://www.google.com/profiles/david.etianen>`_ |