Skip to content

Commit 7f479f0

Browse files
authored
Merge pull request #241 from hho6643/ServerPool
use serverpool to enable multiple auth servers
2 parents da85abf + 2cfc44f commit 7f479f0

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

README.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Installation
1818
1. Install using ``pip install django-python3-ldap``.
1919
2. Add ``'django_python3_ldap'`` to your ``INSTALLED_APPS`` setting.
2020
3. Set your ``AUTHENTICATION_BACKENDS`` setting to ``("django_python3_ldap.auth.LDAPBackend",)``
21-
4. Configure the settings for your LDAP server (see Available settings, below).
21+
4. Configure the settings for your LDAP server(s) (see Available settings, below).
2222
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.
2323
6. Optionally, run ``./manage.py ldap_promote <username>`` to grant superuser admin access to a given user.
2424

@@ -31,8 +31,8 @@ Available settings
3131

3232
.. code:: python
3333
34-
# The URL of the LDAP server.
35-
LDAP_AUTH_URL = "ldap://localhost:389"
34+
# The URL of the LDAP server(s). List multiple servers for high availability ServerPool connection.
35+
LDAP_AUTH_URL = ["ldap://localhost:389"]
3636
3737
# Initiate TLS on connection.
3838
LDAP_AUTH_USE_TLS = False
@@ -215,8 +215,8 @@ The returned list of search filters will be AND'd together to make the final sea
215215
How it works
216216
------------
217217

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

221221
If the bind attempt is successful, the user details are loaded from the LDAP server
222222
and saved in a local Django ``User`` model. The local model is only created once,

django_python3_ldap/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self, settings):
3535

3636
LDAP_AUTH_URL = LazySetting(
3737
name="LDAP_AUTH_URL",
38-
default="ldap://localhost:389",
38+
default=["ldap://localhost:389"],
3939
)
4040

4141
LDAP_AUTH_USE_TLS = LazySetting(

django_python3_ldap/ldap.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,24 @@ def connection(**kwargs):
150150
if kwargs:
151151
password = kwargs.pop("password")
152152
username = format_username(kwargs)
153-
# Connect.
154-
try:
155-
c = ldap3.Connection(
153+
# Build server pool
154+
server_pool = ldap3.ServerPool(None, ldap3.RANDOM, active=True, exhaust=5)
155+
auth_url = settings.LDAP_AUTH_URL
156+
if not isinstance(auth_url, list):
157+
auth_url = [auth_url]
158+
for u in auth_url:
159+
server_pool.add(
156160
ldap3.Server(
157-
settings.LDAP_AUTH_URL,
161+
u,
158162
allowed_referral_hosts=[("*", True)],
159163
get_info=ldap3.NONE,
160164
connect_timeout=settings.LDAP_AUTH_CONNECT_TIMEOUT,
161-
),
165+
)
166+
)
167+
# Connect.
168+
try:
169+
c = ldap3.Connection(
170+
server_pool,
162171
user=username,
163172
password=password,
164173
auto_bind=False,

tests/django_python3_ldap_test/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
# LDAP auth settings.
3131

32-
LDAP_AUTH_URL = "ldap://ldap.forumsys.com:389"
32+
LDAP_AUTH_URL = ["ldap://ldap.forumsys.com:389"]
3333

3434
LDAP_AUTH_SEARCH_BASE = "dc=example,dc=com"
3535

0 commit comments

Comments
 (0)