Skip to content

Commit ef9ff07

Browse files
committed
feat: extract ldap search parsing and validation to dejacode_toolkit/ldap.py
Signed-off-by: Nicolas <32845761+nicoklaus@users.noreply.github.com>
1 parent 1410e99 commit ef9ff07

2 files changed

Lines changed: 64 additions & 41 deletions

File tree

‎dejacode/settings.py‎

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import ldap
1818
from django_auth_ldap.config import GroupOfNamesType
1919
from django_auth_ldap.config import LDAPSearch
20+
from dejacode_toolkit.ldap import build_user_search
2021

2122
# The home directory of the dejacode user that owns the installation.
2223
PROJECT_DIR = environ.Path(__file__) - 1
@@ -769,7 +770,7 @@ def get_fake_redis_connection(config, use_strict_redis):
769770

770771
# Optional: Define multiple LDAP user searches using a JSON list.
771772
# When provided, this setting overrides AUTH_LDAP_USER_DN and AUTH_LDAP_USER_FILTERSTR.
772-
#
773+
#
773774
# Example:
774775
# AUTH_LDAP_USER_SEARCHES = """
775776
# [
@@ -793,46 +794,11 @@ def get_fake_redis_connection(config, use_strict_redis):
793794
# All searches are combined using LDAPSearchUnion.
794795
AUTH_LDAP_USER_SEARCHES = env.str("AUTH_LDAP_USER_SEARCHES", default="")
795796

796-
if AUTH_LDAP_USER_SEARCHES:
797-
import json
798-
from django_auth_ldap.config import LDAPSearchUnion
799-
800-
try:
801-
ldap_search_definitions = json.loads(AUTH_LDAP_USER_SEARCHES)
802-
except json.JSONDecodeError as e:
803-
raise ValueError("Invalid JSON in AUTH_LDAP_USER_SEARCHES") from e
804-
805-
if not isinstance(ldap_search_definitions, list):
806-
raise ValueError("AUTH_LDAP_USER_SEARCHES must be a JSON list")
807-
808-
ldap_searches = []
809-
for search_definition in ldap_search_definitions:
810-
if not isinstance(search_definition, dict):
811-
raise ValueError("Each entry must be an object")
812-
813-
base_dn = search_definition.get("base")
814-
filterstr = search_definition.get("filter")
815-
816-
if not base_dn or not filterstr:
817-
raise ValueError("Each LDAP search entry must define 'base' and 'filter'")
818-
819-
ldap_searches.append(
820-
LDAPSearch(base_dn, ldap.SCOPE_SUBTREE, filterstr)
821-
)
822-
823-
if not ldap_searches:
824-
raise ValueError("AUTH_LDAP_USER_SEARCHES cannot be empty")
825-
826-
# Always use LDAPSearchUnion, even for a single search entry.
827-
AUTH_LDAP_USER_SEARCH = LDAPSearchUnion(*ldap_searches)
828-
829-
else:
830-
# Fallback to single LDAP search configuration.
831-
AUTH_LDAP_USER_SEARCH = LDAPSearch(
832-
AUTH_LDAP_USER_DN,
833-
ldap.SCOPE_SUBTREE,
834-
AUTH_LDAP_USER_FILTERSTR,
835-
)
797+
AUTH_LDAP_USER_SEARCH = build_user_search(
798+
AUTH_LDAP_USER_SEARCHES,
799+
AUTH_LDAP_USER_DN,
800+
AUTH_LDAP_USER_FILTERSTR,
801+
)
836802

837803
# When AUTH_LDAP_AUTOCREATE_USER is True (default), a new DejaCode user will be
838804
# created in the database with the minimum permission (a read-only user).

‎dejacode_toolkit/ldap.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# DejaCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: AGPL-3.0-only
5+
# See https://github.com/aboutcode-org/dejacode for support or download.
6+
# See https://aboutcode.org for more information about AboutCode FOSS projects.
7+
#
8+
9+
import json
10+
11+
import ldap
12+
from django.core.exceptions import ImproperlyConfigured
13+
from django_auth_ldap.config import LDAPSearch
14+
from django_auth_ldap.config import LDAPSearchUnion
15+
16+
17+
def build_user_search(user_searches, user_dn, user_filterstr):
18+
"""Return the ``AUTH_LDAP_USER_SEARCH`` object.
19+
20+
When ``user_searches`` (a raw JSON string) is provided, parse and validate
21+
it and return an ``LDAPSearchUnion``. Otherwise, fall back to a single
22+
``LDAPSearch`` built from ``user_dn`` and ``user_filterstr``.
23+
"""
24+
if not user_searches:
25+
return LDAPSearch(user_dn, ldap.SCOPE_SUBTREE, user_filterstr)
26+
27+
try:
28+
definitions = json.loads(user_searches)
29+
except json.JSONDecodeError as e:
30+
raise ImproperlyConfigured(
31+
f"Invalid JSON in AUTH_LDAP_USER_SEARCHES: {e}"
32+
) from e
33+
34+
if not isinstance(definitions, list):
35+
raise ImproperlyConfigured("AUTH_LDAP_USER_SEARCHES must be a JSON list")
36+
37+
if not definitions:
38+
raise ImproperlyConfigured("AUTH_LDAP_USER_SEARCHES cannot be empty")
39+
40+
searches = []
41+
for index, entry in enumerate(definitions):
42+
if not isinstance(entry, dict):
43+
raise ImproperlyConfigured(
44+
f"AUTH_LDAP_USER_SEARCHES[{index}] must be a JSON object"
45+
)
46+
47+
base_dn = entry.get("base")
48+
filterstr = entry.get("filter")
49+
50+
if not base_dn or not filterstr:
51+
raise ImproperlyConfigured(
52+
f"AUTH_LDAP_USER_SEARCHES[{index}] must define 'base' and 'filter'"
53+
)
54+
55+
searches.append(LDAPSearch(base_dn, ldap.SCOPE_SUBTREE, filterstr))
56+
57+
return LDAPSearchUnion(*searches)

0 commit comments

Comments
 (0)