authentik.sources.ldap.tests.test_password

LDAP Source tests

  1"""LDAP Source tests"""
  2
  3from unittest.mock import MagicMock, patch
  4
  5from django.test import TestCase
  6from ldap3.core.exceptions import LDAPUnwillingToPerformResult
  7from rest_framework.serializers import ValidationError
  8
  9from authentik.core.models import User
 10from authentik.events.models import Event, EventAction
 11from authentik.lib.generators import generate_id
 12from authentik.sources.ldap.models import LDAPSource, LDAPSourcePropertyMapping
 13from authentik.sources.ldap.password import LDAPPasswordChanger
 14from authentik.sources.ldap.tests.mock_ad import mock_ad_connection
 15from authentik.stages.password.models import PasswordDevice
 16
 17LDAP_CONNECTION_PATCH = MagicMock(return_value=mock_ad_connection())
 18
 19
 20class LDAPPasswordTests(TestCase):
 21    """LDAP Password tests"""
 22
 23    def setUp(self):
 24        self.source = LDAPSource.objects.create(
 25            name="ldap",
 26            slug="ldap",
 27            base_dn="dc=t,dc=goauthentik,dc=io",
 28            additional_user_dn="",
 29            additional_group_dn="",
 30        )
 31        self.source.user_property_mappings.set(LDAPSourcePropertyMapping.objects.all())
 32        self.source.save()
 33
 34    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
 35    def test_password_complexity(self):
 36        """Test password without user"""
 37        pwc = LDAPPasswordChanger(self.source)
 38        self.assertFalse(pwc.ad_password_complexity("test"))  # 1 category
 39        self.assertFalse(pwc.ad_password_complexity("test1"))  # 2 categories
 40        self.assertTrue(pwc.ad_password_complexity("test1!"))  # 2 categories
 41
 42    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
 43    def test_password_complexity_user(self):
 44        """test password with user"""
 45        pwc = LDAPPasswordChanger(self.source)
 46        user = User.objects.create(
 47            username="test",
 48            attributes={
 49                "distinguishedName": "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io"
 50            },
 51        )
 52        self.assertFalse(pwc.ad_password_complexity("test", user))  # 1 category
 53        self.assertFalse(pwc.ad_password_complexity("test1", user))  # 2 categories
 54        self.assertTrue(pwc.ad_password_complexity("test1!", user))  # 2 categories
 55
 56    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
 57    def test_password_complexity_user_name_tokens(self):
 58        """Test passwords containing display-name tokens"""
 59        pwc = LDAPPasswordChanger(self.source)
 60        user = User.objects.create(
 61            username="test",
 62            attributes={
 63                "distinguishedName": "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io"
 64            },
 65        )
 66        user_attributes = {
 67            "sAMAccountName": "erin.h",
 68            "displayName": ["Erin M. Hagens"],
 69        }
 70        with patch.object(
 71            pwc._connection.extend.standard,
 72            "paged_search",
 73            return_value=[{"attributes": user_attributes}],
 74        ):
 75            self.assertFalse(pwc.ad_password_complexity("ErinA1!", user))
 76            self.assertFalse(pwc.ad_password_complexity("HagensA1!", user))
 77            self.assertTrue(pwc.ad_password_complexity("ExampleA1!", user))
 78
 79    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
 80    def test_ad_check_password_existing_sam_account_name(self):
 81        """Test case-insensitive sAMAccountName containment"""
 82        pwc = LDAPPasswordChanger(self.source)
 83        user_attributes = {"sAMAccountName": "account123", "displayName": []}
 84        with patch.object(
 85            pwc._connection.extend.standard,
 86            "paged_search",
 87            return_value=[{"attributes": user_attributes}],
 88        ):
 89            self.assertFalse(pwc._ad_check_password_existing("XXaccount123A1!", "user"))
 90            self.assertFalse(pwc._ad_check_password_existing("XXAccount123A1!", "user"))
 91            self.assertTrue(pwc._ad_check_password_existing("XXaccount12A1!", "user"))
 92
 93    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
 94    def test_ad_check_password_existing_display_name_separators(self):
 95        """Test display-name tokens are split on each documented separator"""
 96        pwc = LDAPPasswordChanger(self.source)
 97        for separator in [",", ".", "-", "_", " ", "#", "\t"]:
 98            with self.subTest(separator=repr(separator)):
 99                user_attributes = {
100                    "sAMAccountName": "account123",
101                    "displayName": [f"Erin{separator}Hagens"],
102                }
103                with patch.object(
104                    pwc._connection.extend.standard,
105                    "paged_search",
106                    return_value=[{"attributes": user_attributes}],
107                ):
108                    self.assertFalse(pwc._ad_check_password_existing("ErinA1!", "user"))
109                    self.assertFalse(pwc._ad_check_password_existing("HagensA1!", "user"))
110
111    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
112    def test_ad_check_password_existing_ignores_short_display_name_tokens(self):
113        """Test display-name tokens shorter than three characters are ignored"""
114        pwc = LDAPPasswordChanger(self.source)
115        user_attributes = {"sAMAccountName": "account123", "displayName": ["AB"]}
116        with patch.object(
117            pwc._connection.extend.standard,
118            "paged_search",
119            return_value=[{"attributes": user_attributes}],
120        ):
121            self.assertTrue(pwc._ad_check_password_existing("AB1!", "user"))
122
123    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
124    def test_sync_password_ad_error(self):
125        """Test that a rejected password is reported with the correct error"""
126        user = User.objects.create(
127            username="test",
128            attributes={
129                "distinguishedName": "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io"
130            },
131        )
132        with (
133            patch.object(
134                LDAP_CONNECTION_PATCH.return_value.extend.microsoft,
135                "modify_password",
136                side_effect=LDAPUnwillingToPerformResult(
137                    result=53,
138                    description="unwillingToPerform",
139                    message=(
140                        "0000052D: SvcErr: DSID-031A124C, problem 5003 (WILL_NOT_PERFORM), "
141                        "data 0\n\x00"
142                    ),
143                ),
144            ),
145            self.assertRaises(ValidationError),
146        ):
147            PasswordDevice.set_password(user, generate_id())
148
149        event = Event.objects.filter(
150            action=EventAction.CONFIGURATION_ERROR, user__pk=user.pk
151        ).first()
152        self.assertIn("0000052D", event.context["message"])
LDAP_CONNECTION_PATCH = <MagicMock id='123519827187360'>
class LDAPPasswordTests(django.test.testcases.TestCase):
 21class LDAPPasswordTests(TestCase):
 22    """LDAP Password tests"""
 23
 24    def setUp(self):
 25        self.source = LDAPSource.objects.create(
 26            name="ldap",
 27            slug="ldap",
 28            base_dn="dc=t,dc=goauthentik,dc=io",
 29            additional_user_dn="",
 30            additional_group_dn="",
 31        )
 32        self.source.user_property_mappings.set(LDAPSourcePropertyMapping.objects.all())
 33        self.source.save()
 34
 35    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
 36    def test_password_complexity(self):
 37        """Test password without user"""
 38        pwc = LDAPPasswordChanger(self.source)
 39        self.assertFalse(pwc.ad_password_complexity("test"))  # 1 category
 40        self.assertFalse(pwc.ad_password_complexity("test1"))  # 2 categories
 41        self.assertTrue(pwc.ad_password_complexity("test1!"))  # 2 categories
 42
 43    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
 44    def test_password_complexity_user(self):
 45        """test password with user"""
 46        pwc = LDAPPasswordChanger(self.source)
 47        user = User.objects.create(
 48            username="test",
 49            attributes={
 50                "distinguishedName": "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io"
 51            },
 52        )
 53        self.assertFalse(pwc.ad_password_complexity("test", user))  # 1 category
 54        self.assertFalse(pwc.ad_password_complexity("test1", user))  # 2 categories
 55        self.assertTrue(pwc.ad_password_complexity("test1!", user))  # 2 categories
 56
 57    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
 58    def test_password_complexity_user_name_tokens(self):
 59        """Test passwords containing display-name tokens"""
 60        pwc = LDAPPasswordChanger(self.source)
 61        user = User.objects.create(
 62            username="test",
 63            attributes={
 64                "distinguishedName": "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io"
 65            },
 66        )
 67        user_attributes = {
 68            "sAMAccountName": "erin.h",
 69            "displayName": ["Erin M. Hagens"],
 70        }
 71        with patch.object(
 72            pwc._connection.extend.standard,
 73            "paged_search",
 74            return_value=[{"attributes": user_attributes}],
 75        ):
 76            self.assertFalse(pwc.ad_password_complexity("ErinA1!", user))
 77            self.assertFalse(pwc.ad_password_complexity("HagensA1!", user))
 78            self.assertTrue(pwc.ad_password_complexity("ExampleA1!", user))
 79
 80    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
 81    def test_ad_check_password_existing_sam_account_name(self):
 82        """Test case-insensitive sAMAccountName containment"""
 83        pwc = LDAPPasswordChanger(self.source)
 84        user_attributes = {"sAMAccountName": "account123", "displayName": []}
 85        with patch.object(
 86            pwc._connection.extend.standard,
 87            "paged_search",
 88            return_value=[{"attributes": user_attributes}],
 89        ):
 90            self.assertFalse(pwc._ad_check_password_existing("XXaccount123A1!", "user"))
 91            self.assertFalse(pwc._ad_check_password_existing("XXAccount123A1!", "user"))
 92            self.assertTrue(pwc._ad_check_password_existing("XXaccount12A1!", "user"))
 93
 94    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
 95    def test_ad_check_password_existing_display_name_separators(self):
 96        """Test display-name tokens are split on each documented separator"""
 97        pwc = LDAPPasswordChanger(self.source)
 98        for separator in [",", ".", "-", "_", " ", "#", "\t"]:
 99            with self.subTest(separator=repr(separator)):
100                user_attributes = {
101                    "sAMAccountName": "account123",
102                    "displayName": [f"Erin{separator}Hagens"],
103                }
104                with patch.object(
105                    pwc._connection.extend.standard,
106                    "paged_search",
107                    return_value=[{"attributes": user_attributes}],
108                ):
109                    self.assertFalse(pwc._ad_check_password_existing("ErinA1!", "user"))
110                    self.assertFalse(pwc._ad_check_password_existing("HagensA1!", "user"))
111
112    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
113    def test_ad_check_password_existing_ignores_short_display_name_tokens(self):
114        """Test display-name tokens shorter than three characters are ignored"""
115        pwc = LDAPPasswordChanger(self.source)
116        user_attributes = {"sAMAccountName": "account123", "displayName": ["AB"]}
117        with patch.object(
118            pwc._connection.extend.standard,
119            "paged_search",
120            return_value=[{"attributes": user_attributes}],
121        ):
122            self.assertTrue(pwc._ad_check_password_existing("AB1!", "user"))
123
124    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
125    def test_sync_password_ad_error(self):
126        """Test that a rejected password is reported with the correct error"""
127        user = User.objects.create(
128            username="test",
129            attributes={
130                "distinguishedName": "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io"
131            },
132        )
133        with (
134            patch.object(
135                LDAP_CONNECTION_PATCH.return_value.extend.microsoft,
136                "modify_password",
137                side_effect=LDAPUnwillingToPerformResult(
138                    result=53,
139                    description="unwillingToPerform",
140                    message=(
141                        "0000052D: SvcErr: DSID-031A124C, problem 5003 (WILL_NOT_PERFORM), "
142                        "data 0\n\x00"
143                    ),
144                ),
145            ),
146            self.assertRaises(ValidationError),
147        ):
148            PasswordDevice.set_password(user, generate_id())
149
150        event = Event.objects.filter(
151            action=EventAction.CONFIGURATION_ERROR, user__pk=user.pk
152        ).first()
153        self.assertIn("0000052D", event.context["message"])

LDAP Password tests

def setUp(self):
24    def setUp(self):
25        self.source = LDAPSource.objects.create(
26            name="ldap",
27            slug="ldap",
28            base_dn="dc=t,dc=goauthentik,dc=io",
29            additional_user_dn="",
30            additional_group_dn="",
31        )
32        self.source.user_property_mappings.set(LDAPSourcePropertyMapping.objects.all())
33        self.source.save()

Hook method for setting up the test fixture before exercising it.

@patch('authentik.sources.ldap.models.LDAPSource.connection', LDAP_CONNECTION_PATCH)
def test_password_complexity(self):
35    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
36    def test_password_complexity(self):
37        """Test password without user"""
38        pwc = LDAPPasswordChanger(self.source)
39        self.assertFalse(pwc.ad_password_complexity("test"))  # 1 category
40        self.assertFalse(pwc.ad_password_complexity("test1"))  # 2 categories
41        self.assertTrue(pwc.ad_password_complexity("test1!"))  # 2 categories

Test password without user

@patch('authentik.sources.ldap.models.LDAPSource.connection', LDAP_CONNECTION_PATCH)
def test_password_complexity_user(self):
43    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
44    def test_password_complexity_user(self):
45        """test password with user"""
46        pwc = LDAPPasswordChanger(self.source)
47        user = User.objects.create(
48            username="test",
49            attributes={
50                "distinguishedName": "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io"
51            },
52        )
53        self.assertFalse(pwc.ad_password_complexity("test", user))  # 1 category
54        self.assertFalse(pwc.ad_password_complexity("test1", user))  # 2 categories
55        self.assertTrue(pwc.ad_password_complexity("test1!", user))  # 2 categories

test password with user

@patch('authentik.sources.ldap.models.LDAPSource.connection', LDAP_CONNECTION_PATCH)
def test_password_complexity_user_name_tokens(self):
57    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
58    def test_password_complexity_user_name_tokens(self):
59        """Test passwords containing display-name tokens"""
60        pwc = LDAPPasswordChanger(self.source)
61        user = User.objects.create(
62            username="test",
63            attributes={
64                "distinguishedName": "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io"
65            },
66        )
67        user_attributes = {
68            "sAMAccountName": "erin.h",
69            "displayName": ["Erin M. Hagens"],
70        }
71        with patch.object(
72            pwc._connection.extend.standard,
73            "paged_search",
74            return_value=[{"attributes": user_attributes}],
75        ):
76            self.assertFalse(pwc.ad_password_complexity("ErinA1!", user))
77            self.assertFalse(pwc.ad_password_complexity("HagensA1!", user))
78            self.assertTrue(pwc.ad_password_complexity("ExampleA1!", user))

Test passwords containing display-name tokens

@patch('authentik.sources.ldap.models.LDAPSource.connection', LDAP_CONNECTION_PATCH)
def test_ad_check_password_existing_sam_account_name(self):
80    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
81    def test_ad_check_password_existing_sam_account_name(self):
82        """Test case-insensitive sAMAccountName containment"""
83        pwc = LDAPPasswordChanger(self.source)
84        user_attributes = {"sAMAccountName": "account123", "displayName": []}
85        with patch.object(
86            pwc._connection.extend.standard,
87            "paged_search",
88            return_value=[{"attributes": user_attributes}],
89        ):
90            self.assertFalse(pwc._ad_check_password_existing("XXaccount123A1!", "user"))
91            self.assertFalse(pwc._ad_check_password_existing("XXAccount123A1!", "user"))
92            self.assertTrue(pwc._ad_check_password_existing("XXaccount12A1!", "user"))

Test case-insensitive sAMAccountName containment

@patch('authentik.sources.ldap.models.LDAPSource.connection', LDAP_CONNECTION_PATCH)
def test_ad_check_password_existing_display_name_separators(self):
 94    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
 95    def test_ad_check_password_existing_display_name_separators(self):
 96        """Test display-name tokens are split on each documented separator"""
 97        pwc = LDAPPasswordChanger(self.source)
 98        for separator in [",", ".", "-", "_", " ", "#", "\t"]:
 99            with self.subTest(separator=repr(separator)):
100                user_attributes = {
101                    "sAMAccountName": "account123",
102                    "displayName": [f"Erin{separator}Hagens"],
103                }
104                with patch.object(
105                    pwc._connection.extend.standard,
106                    "paged_search",
107                    return_value=[{"attributes": user_attributes}],
108                ):
109                    self.assertFalse(pwc._ad_check_password_existing("ErinA1!", "user"))
110                    self.assertFalse(pwc._ad_check_password_existing("HagensA1!", "user"))

Test display-name tokens are split on each documented separator

@patch('authentik.sources.ldap.models.LDAPSource.connection', LDAP_CONNECTION_PATCH)
def test_ad_check_password_existing_ignores_short_display_name_tokens(self):
112    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
113    def test_ad_check_password_existing_ignores_short_display_name_tokens(self):
114        """Test display-name tokens shorter than three characters are ignored"""
115        pwc = LDAPPasswordChanger(self.source)
116        user_attributes = {"sAMAccountName": "account123", "displayName": ["AB"]}
117        with patch.object(
118            pwc._connection.extend.standard,
119            "paged_search",
120            return_value=[{"attributes": user_attributes}],
121        ):
122            self.assertTrue(pwc._ad_check_password_existing("AB1!", "user"))

Test display-name tokens shorter than three characters are ignored

@patch('authentik.sources.ldap.models.LDAPSource.connection', LDAP_CONNECTION_PATCH)
def test_sync_password_ad_error(self):
124    @patch("authentik.sources.ldap.models.LDAPSource.connection", LDAP_CONNECTION_PATCH)
125    def test_sync_password_ad_error(self):
126        """Test that a rejected password is reported with the correct error"""
127        user = User.objects.create(
128            username="test",
129            attributes={
130                "distinguishedName": "CN=Erin M. Hagens,OU=ak-test,DC=t,DC=goauthentik,DC=io"
131            },
132        )
133        with (
134            patch.object(
135                LDAP_CONNECTION_PATCH.return_value.extend.microsoft,
136                "modify_password",
137                side_effect=LDAPUnwillingToPerformResult(
138                    result=53,
139                    description="unwillingToPerform",
140                    message=(
141                        "0000052D: SvcErr: DSID-031A124C, problem 5003 (WILL_NOT_PERFORM), "
142                        "data 0\n\x00"
143                    ),
144                ),
145            ),
146            self.assertRaises(ValidationError),
147        ):
148            PasswordDevice.set_password(user, generate_id())
149
150        event = Event.objects.filter(
151            action=EventAction.CONFIGURATION_ERROR, user__pk=user.pk
152        ).first()
153        self.assertIn("0000052D", event.context["message"])

Test that a rejected password is reported with the correct error