From 6d3700c07896f145e9497797c268dfa803ae1f05 Mon Sep 17 00:00:00 2001 From: Cristopher Hernandez <22552070+CristopherH95@users.noreply.github.com> Date: Thu, 29 May 2025 11:48:26 -0700 Subject: [PATCH 1/5] Add async method to LDAPBackend --- django_python3_ldap/auth.py | 13 ++++++++++++- setup.py | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/django_python3_ldap/auth.py b/django_python3_ldap/auth.py index cba93fd..96f2c14 100644 --- a/django_python3_ldap/auth.py +++ b/django_python3_ldap/auth.py @@ -1,12 +1,20 @@ """ Django authentication backend. """ - +from asgiref.sync import sync_to_async from django.contrib.auth.backends import ModelBackend from django_python3_ldap import ldap +@sync_to_async +def run_authentication_async(*args, **kwargs): + """ + Executes the ldap.authenticate function, wrapped in asynchronous execution. + """ + return ldap.authenticate(*args, **kwargs) + + class LDAPBackend(ModelBackend): """ @@ -21,3 +29,6 @@ class LDAPBackend(ModelBackend): def authenticate(self, *args, **kwargs): return ldap.authenticate(*args, **kwargs) + + async def aauthenticate(self, *args, **kwargs): + return await run_authentication_async(*args, **kwargs) diff --git a/setup.py b/setup.py index 60ddfdc..2d1c8c8 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,7 @@ packages=find_packages(), install_requires=[ "django>=1.11", + "asgiref>=2.0.0", "ldap3>=2.5,<3", "pyasn1>=0.4.6,<0.6", ], From 2d2ef0629132d341aad6c57c48980bcb668df628 Mon Sep 17 00:00:00 2001 From: Cristopher Hernandez <22552070+CristopherH95@users.noreply.github.com> Date: Thu, 29 May 2025 12:26:45 -0700 Subject: [PATCH 2/5] Add unit test for async runner --- django_python3_ldap/tests.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/django_python3_ldap/tests.py b/django_python3_ldap/tests.py index 207ae91..17032c1 100644 --- a/django_python3_ldap/tests.py +++ b/django_python3_ldap/tests.py @@ -1,15 +1,17 @@ # encoding=utf-8 from __future__ import unicode_literals -from unittest import skipUnless, skip +from unittest import skipUnless, skip, mock from io import StringIO +from asgiref.sync import async_to_sync from django.test import TestCase, override_settings from django.contrib.auth import authenticate from django.contrib.auth.models import User from django.conf import settings as django_settings from django.core.management import call_command, CommandError +from django_python3_ldap.auth import run_authentication_async from django_python3_ldap.conf import settings from django_python3_ldap.ldap import connection from django_python3_ldap.utils import clean_ldap_name, import_func @@ -161,6 +163,16 @@ def testAuthenticateWithLimitedRetries(self): ) self.assertEqual(user, None) + def testAuthenticateAsyncRunner(self): + async_runner = async_to_sync(run_authentication_async) + + with mock.patch("django_python3_ldap.ldap.authenticate") as mocked_authenticate_call: + async_runner( + username=settings.LDAP_AUTH_TEST_USER_USERNAME, + password=settings.LDAP_AUTH_TEST_USER_PASSWORD, + ) + mocked_authenticate_call.assert_called() + # User synchronisation. def testSyncUsersCreatesUsers(self): From 302d1f382f486e51f6d524c0790492b21254f8e3 Mon Sep 17 00:00:00 2001 From: Cristopher Hernandez <22552070+CristopherH95@users.noreply.github.com> Date: Sun, 6 Jul 2025 13:11:40 -0700 Subject: [PATCH 3/5] Update python-package.yml --- .github/workflows/python-package.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 49c9ba9..0470260 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -9,7 +9,7 @@ jobs: PYTHONDEVMODE: 1 strategy: matrix: - python-version: [3.7, 3.8, 3.9, '3.10'] + python-version: ['3.9', '3.10', '3.11', '3.12'] django-version: - '>=4.0a1,<4.1' - '>=3.2,<4.0' @@ -19,10 +19,6 @@ jobs: - '>=2.1,<2.2' - '>=2.0,<2.1' exclude: - - python-version: 3.6 - django-version: '>=4.0a1,<4.1' - - python-version: 3.7 - django-version: '>=4.0a1,<4.1' - python-version: '3.10' django-version: '>=3.2,<4.0' - python-version: '3.10' From 1fe006a7fb1dd18736b00f3dfbcbe31f3f464651 Mon Sep 17 00:00:00 2001 From: Cristopher Hernandez <22552070+CristopherH95@users.noreply.github.com> Date: Sun, 6 Jul 2025 13:28:16 -0700 Subject: [PATCH 4/5] Update exclusions in matrix --- .github/workflows/python-package.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 0470260..68f9deb 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -19,17 +19,17 @@ jobs: - '>=2.1,<2.2' - '>=2.0,<2.1' exclude: - - python-version: '3.10' + - python-version: '>=3.10' django-version: '>=3.2,<4.0' - - python-version: '3.10' + - python-version: '>=3.10' django-version: '>=3.1,<3.2' - - python-version: '3.10' + - python-version: '>=3.10' django-version: '>=3.0,<3.1' - - python-version: '3.10' + - python-version: '>=3.10' django-version: '>=2.2,<3.0' - - python-version: '3.10' + - python-version: '>=3.10' django-version: '>=2.1,<2.2' - - python-version: '3.10' + - python-version: '>=3.10' django-version: '>=2.0,<2.1' steps: - uses: actions/checkout@v2 From 606c9a4433008d00fe178c94caf50239a9485ec3 Mon Sep 17 00:00:00 2001 From: Cristopher Hernandez <22552070+CristopherH95@users.noreply.github.com> Date: Sun, 6 Jul 2025 13:51:05 -0700 Subject: [PATCH 5/5] Fix matrix syntax --- .github/workflows/python-package.yml | 36 +++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 68f9deb..332e980 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -19,17 +19,41 @@ jobs: - '>=2.1,<2.2' - '>=2.0,<2.1' exclude: - - python-version: '>=3.10' + - python-version: '3.10' django-version: '>=3.2,<4.0' - - python-version: '>=3.10' + - python-version: '3.10' django-version: '>=3.1,<3.2' - - python-version: '>=3.10' + - python-version: '3.10' django-version: '>=3.0,<3.1' - - python-version: '>=3.10' + - python-version: '3.10' django-version: '>=2.2,<3.0' - - python-version: '>=3.10' + - python-version: '3.10' django-version: '>=2.1,<2.2' - - python-version: '>=3.10' + - python-version: '3.10' + django-version: '>=2.0,<2.1' + - python-version: '3.11' + django-version: '>=3.2,<4.0' + - python-version: '3.11' + django-version: '>=3.1,<3.2' + - python-version: '3.11' + django-version: '>=3.0,<3.1' + - python-version: '3.11' + django-version: '>=2.2,<3.0' + - python-version: '3.11' + django-version: '>=2.1,<2.2' + - python-version: '3.11' + django-version: '>=2.0,<2.1' + - python-version: '3.12' + django-version: '>=3.2,<4.0' + - python-version: '3.12' + django-version: '>=3.1,<3.2' + - python-version: '3.12' + django-version: '>=3.0,<3.1' + - python-version: '3.12' + django-version: '>=2.2,<3.0' + - python-version: '3.12' + django-version: '>=2.1,<2.2' + - python-version: '3.12' django-version: '>=2.0,<2.1' steps: - uses: actions/checkout@v2