Skip to content

Commit

Permalink
Add ldap interface to get uid data
Browse files Browse the repository at this point in the history
  • Loading branch information
cc-a committed Nov 21, 2024
1 parent 51e46ff commit dc52707
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
8 changes: 8 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ pluggy==1.5.0
# via pytest
pre-commit==4.0.1
# via imperial_coldfront_plugin (pyproject.toml)
pyasn1==0.6.1
# via
# pyasn1-modules
# python-ldap
pyasn1-modules==0.4.1
# via python-ldap
pycparser==2.22
# via cffi
pyopenssl==24.2.1
Expand Down Expand Up @@ -186,6 +192,8 @@ python-dateutil==2.9.0.post0
# arrow
# coldfront
# faker
python-ldap==3.4.4
# via imperial_coldfront_plugin (pyproject.toml)
python-memcached==1.62
# via coldfront
pytz==2024.1
Expand Down
8 changes: 8 additions & 0 deletions doc-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ platformdirs==4.3.6
# via
# mkdocs-get-deps
# mkdocstrings
pyasn1==0.6.1
# via
# pyasn1-modules
# python-ldap
pyasn1-modules==0.4.1
# via python-ldap
pycparser==2.22
# via cffi
pygments==2.18.0
Expand All @@ -205,6 +211,8 @@ python-dateutil==2.9.0.post0
# coldfront
# faker
# ghp-import
python-ldap==3.4.4
# via imperial_coldfront_plugin (pyproject.toml)
python-memcached==1.62
# via coldfront
pytz==2024.1
Expand Down
23 changes: 23 additions & 0 deletions imperial_coldfront_plugin/ldap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Module for interacting with LDAP identity provider."""

import ldap
from django.conf import settings


def get_uid_from_ldap(username: str) -> int:
"""Retrieve the UID from LDAP for a given username.
Args:
username: The username to search for in LDAP.
Returns:
The UID number associated with the given username.
"""
conn = ldap.initialize(settings.LDAP_SERVER_URI)
conn.set_option(ldap.OPT_REFERRALS, 0)
conn.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
conn.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)
result = conn.search_s(
settings.LDAP_SEARCH_BASE, ldap.SCOPE_SUBTREE, f"(cn={username})", ["uidNumber"]
)
return int(result[0][1]["uidNumber"][0].decode("utf-8"))
6 changes: 6 additions & 0 deletions imperial_coldfront_plugin/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Plugin settings that are imported into the global settings namespace of Coldfront."""

from coldfront.config.env import ENV

LDAP_SERVER_URI = ENV.str("LDAP_SERVER_URI", default="")
LDAP_SEARCH_BASE = ENV.str("LDAP_SEARCH_BASE", default="")
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies = [
"django",
"mozilla_django_oidc",
"django-stubs-ext",
"python-ldap",
]

[project.optional-dependencies]
Expand Down Expand Up @@ -60,7 +61,7 @@ module = "tests.*"
disallow_untyped_defs = false

[[tool.mypy.overrides]]
module = ["mozilla_django_oidc.*"]
module = ["mozilla_django_oidc.*", "ldap.*"]
ignore_missing_imports = true

[[tool.mypy.overrides]]
Expand Down Expand Up @@ -92,4 +93,4 @@ pydocstyle.convention = "google"
] # Missing docstring in public module, Missing docstring in public package

[tool.django-stubs]
django_settings_module = "imperial_coldfront_plugin" # a lie but seems to satisfy
django_settings_module = "imperial_coldfront_plugin.settings"
8 changes: 8 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ josepy==1.14.0
# via mozilla-django-oidc
mozilla-django-oidc==4.0.1
# via imperial_coldfront_plugin (pyproject.toml)
pyasn1==0.6.1
# via
# pyasn1-modules
# python-ldap
pyasn1-modules==0.4.1
# via python-ldap
pycparser==2.22
# via cffi
pyopenssl==24.2.1
Expand All @@ -121,6 +127,8 @@ python-dateutil==2.9.0.post0
# arrow
# coldfront
# faker
python-ldap==3.4.4
# via imperial_coldfront_plugin (pyproject.toml)
python-memcached==1.62
# via coldfront
pytz==2024.1
Expand Down

0 comments on commit dc52707

Please sign in to comment.