Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand All @@ -35,6 +31,30 @@ jobs:
django-version: '>=2.1,<2.2'
- 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
- name: Set up Python ${{ matrix.python-version }}
Expand Down
13 changes: 12 additions & 1 deletion django_python3_ldap/auth.py
Original file line number Diff line number Diff line change
@@ -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):

"""
Expand All @@ -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)
14 changes: 13 additions & 1 deletion django_python3_ldap/tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down