authentik.sources.ldap.tests.test_models
LDAP Source model tests
1"""LDAP Source model tests""" 2 3from unittest.mock import MagicMock, Mock, call, patch 4 5from django.test import TestCase 6from ldap3 import EXTERNAL, SASL, SIMPLE 7from ldap3.core.exceptions import LDAPConfigurationError 8 9from authentik.core.tests.utils import create_test_cert 10from authentik.lib.generators import generate_id 11from authentik.sources.ldap.models import LDAPSource, LDAPSourceBindMethod 12 13 14class LDAPModelTests(TestCase): 15 """LDAP Source model tests""" 16 17 def create_source(self, **kwargs) -> LDAPSource: 18 """Create an LDAP source with common required fields.""" 19 source_kwargs = { 20 "name": generate_id(), 21 "slug": generate_id(), 22 "server_uri": "ldaps://ldap.example.com", 23 "base_dn": "dc=example,dc=com", 24 } 25 source_kwargs.update(kwargs) 26 return LDAPSource.objects.create( 27 **source_kwargs, 28 ) 29 30 def test_server_sni(self): 31 """Test that the SNI name is the bare hostname, not the full server URI""" 32 source = self.create_source( 33 server_uri="ldaps://ldap.example.com:636", 34 sni=True, 35 ) 36 pool = source.server() 37 self.assertEqual([server.tls.sni for server in pool.servers], ["ldap.example.com"]) 38 39 def test_server_sni_multiple(self): 40 """Test that each server in a pool gets its own hostname as the SNI name""" 41 source = self.create_source( 42 server_uri="ldaps://ldap1.example.com,ldaps://ldap2.example.com:636", 43 sni=True, 44 ) 45 pool = source.server() 46 self.assertEqual( 47 [server.tls.sni for server in pool.servers], 48 ["ldap1.example.com", "ldap2.example.com"], 49 ) 50 51 def test_server_sni_disabled(self): 52 """Test that no SNI name is set when the SNI option is disabled""" 53 source = self.create_source( 54 server_uri="ldaps://ldap.example.com", 55 sni=False, 56 ) 57 pool = source.server() 58 for server in pool.servers: 59 self.assertIsNone(server.tls.sni) 60 61 def test_simple_service_connection(self): 62 """The default service connection preserves simple bind credentials.""" 63 source = self.create_source(bind_cn="cn=service", bind_password="password") 64 with patch.object(source, "_connect_and_bind") as connection: 65 source.connection() 66 connection.assert_called_once_with( 67 None, 68 None, 69 {"user": "cn=service", "password": "password"}, 70 ) 71 72 def test_anonymous_service_connection(self): 73 """Blank credentials continue to let ldap3 select anonymous authentication.""" 74 source = self.create_source() 75 with patch.object(source, "_connect_and_bind") as connection: 76 source.connection() 77 connection.assert_called_once_with(None, None, {"user": "", "password": ""}) 78 79 def test_sasl_external_service_connection(self): 80 """SASL EXTERNAL derives authorization from the TLS client certificate.""" 81 source = self.create_source( 82 client_certificate=create_test_cert(), 83 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 84 ) 85 with patch.object(source, "_connect_and_bind") as connection: 86 source.connection(connection_kwargs={"user": "ignored", "password": "ignored"}) 87 connection.assert_called_once_with( 88 None, 89 None, 90 { 91 "authentication": SASL, 92 "sasl_mechanism": EXTERNAL, 93 "sasl_credentials": "", 94 }, 95 ) 96 97 def test_sasl_external_requires_client_certificate(self): 98 """SASL EXTERNAL cannot run without a TLS client identity.""" 99 source = self.create_source( 100 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 101 ) 102 with self.assertRaises(LDAPConfigurationError): 103 source.connection() 104 105 def test_sasl_external_requires_private_key(self): 106 """SASL EXTERNAL rejects a certificate without its private key.""" 107 certificate = create_test_cert() 108 certificate.key_data = "" 109 certificate.save(update_fields=["key_data"]) 110 source = self.create_source( 111 client_certificate=certificate, 112 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 113 ) 114 with self.assertRaises(LDAPConfigurationError): 115 source.connection() 116 117 def test_sasl_external_requires_secure_transport(self): 118 """SASL EXTERNAL rejects a plaintext LDAP connection.""" 119 source = self.create_source( 120 server_uri="ldap://ldap.example.com", 121 client_certificate=create_test_cert(), 122 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 123 ) 124 with self.assertRaises(LDAPConfigurationError): 125 source.connection() 126 127 def test_sasl_external_start_tls(self): 128 """SASL EXTERNAL accepts LDAP when StartTLS is enabled.""" 129 source = self.create_source( 130 server_uri="ldap://ldap.example.com", 131 start_tls=True, 132 client_certificate=create_test_cert(), 133 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 134 ) 135 with patch.object(source, "_connect_and_bind") as connection: 136 source.connection() 137 connection.assert_called_once_with( 138 None, 139 None, 140 { 141 "authentication": SASL, 142 "sasl_mechanism": EXTERNAL, 143 "sasl_credentials": "", 144 }, 145 ) 146 147 def test_user_connection_always_uses_simple_bind(self): 148 """User password verification never inherits the service bind method.""" 149 source = self.create_source( 150 client_certificate=create_test_cert(), 151 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 152 ) 153 with patch.object(source, "_connect_and_bind") as connection: 154 source.connection_as_user("cn=user", "submitted-password") 155 connection.assert_called_once_with( 156 connection_kwargs={ 157 "user": "cn=user", 158 "password": "submitted-password", 159 "authentication": SIMPLE, 160 } 161 ) 162 163 def test_start_tls_before_bind(self): 164 """StartTLS secures the connection before credentials are bound.""" 165 source = self.create_source( 166 server_uri="ldap://ldap.example.com", 167 start_tls=True, 168 ) 169 server = MagicMock() 170 connection = MagicMock() 171 connection.bind.return_value = True 172 connection.server.tls.certificate_file = None 173 calls = Mock() 174 calls.attach_mock(connection.start_tls, "start_tls") 175 calls.attach_mock(connection.bind, "bind") 176 with patch("ldap3.Connection", return_value=connection): 177 source._connect_and_bind(server=server) 178 self.assertEqual( 179 calls.mock_calls, 180 [call.start_tls(read_server_info=False), call.bind()], 181 )
15class LDAPModelTests(TestCase): 16 """LDAP Source model tests""" 17 18 def create_source(self, **kwargs) -> LDAPSource: 19 """Create an LDAP source with common required fields.""" 20 source_kwargs = { 21 "name": generate_id(), 22 "slug": generate_id(), 23 "server_uri": "ldaps://ldap.example.com", 24 "base_dn": "dc=example,dc=com", 25 } 26 source_kwargs.update(kwargs) 27 return LDAPSource.objects.create( 28 **source_kwargs, 29 ) 30 31 def test_server_sni(self): 32 """Test that the SNI name is the bare hostname, not the full server URI""" 33 source = self.create_source( 34 server_uri="ldaps://ldap.example.com:636", 35 sni=True, 36 ) 37 pool = source.server() 38 self.assertEqual([server.tls.sni for server in pool.servers], ["ldap.example.com"]) 39 40 def test_server_sni_multiple(self): 41 """Test that each server in a pool gets its own hostname as the SNI name""" 42 source = self.create_source( 43 server_uri="ldaps://ldap1.example.com,ldaps://ldap2.example.com:636", 44 sni=True, 45 ) 46 pool = source.server() 47 self.assertEqual( 48 [server.tls.sni for server in pool.servers], 49 ["ldap1.example.com", "ldap2.example.com"], 50 ) 51 52 def test_server_sni_disabled(self): 53 """Test that no SNI name is set when the SNI option is disabled""" 54 source = self.create_source( 55 server_uri="ldaps://ldap.example.com", 56 sni=False, 57 ) 58 pool = source.server() 59 for server in pool.servers: 60 self.assertIsNone(server.tls.sni) 61 62 def test_simple_service_connection(self): 63 """The default service connection preserves simple bind credentials.""" 64 source = self.create_source(bind_cn="cn=service", bind_password="password") 65 with patch.object(source, "_connect_and_bind") as connection: 66 source.connection() 67 connection.assert_called_once_with( 68 None, 69 None, 70 {"user": "cn=service", "password": "password"}, 71 ) 72 73 def test_anonymous_service_connection(self): 74 """Blank credentials continue to let ldap3 select anonymous authentication.""" 75 source = self.create_source() 76 with patch.object(source, "_connect_and_bind") as connection: 77 source.connection() 78 connection.assert_called_once_with(None, None, {"user": "", "password": ""}) 79 80 def test_sasl_external_service_connection(self): 81 """SASL EXTERNAL derives authorization from the TLS client certificate.""" 82 source = self.create_source( 83 client_certificate=create_test_cert(), 84 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 85 ) 86 with patch.object(source, "_connect_and_bind") as connection: 87 source.connection(connection_kwargs={"user": "ignored", "password": "ignored"}) 88 connection.assert_called_once_with( 89 None, 90 None, 91 { 92 "authentication": SASL, 93 "sasl_mechanism": EXTERNAL, 94 "sasl_credentials": "", 95 }, 96 ) 97 98 def test_sasl_external_requires_client_certificate(self): 99 """SASL EXTERNAL cannot run without a TLS client identity.""" 100 source = self.create_source( 101 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 102 ) 103 with self.assertRaises(LDAPConfigurationError): 104 source.connection() 105 106 def test_sasl_external_requires_private_key(self): 107 """SASL EXTERNAL rejects a certificate without its private key.""" 108 certificate = create_test_cert() 109 certificate.key_data = "" 110 certificate.save(update_fields=["key_data"]) 111 source = self.create_source( 112 client_certificate=certificate, 113 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 114 ) 115 with self.assertRaises(LDAPConfigurationError): 116 source.connection() 117 118 def test_sasl_external_requires_secure_transport(self): 119 """SASL EXTERNAL rejects a plaintext LDAP connection.""" 120 source = self.create_source( 121 server_uri="ldap://ldap.example.com", 122 client_certificate=create_test_cert(), 123 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 124 ) 125 with self.assertRaises(LDAPConfigurationError): 126 source.connection() 127 128 def test_sasl_external_start_tls(self): 129 """SASL EXTERNAL accepts LDAP when StartTLS is enabled.""" 130 source = self.create_source( 131 server_uri="ldap://ldap.example.com", 132 start_tls=True, 133 client_certificate=create_test_cert(), 134 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 135 ) 136 with patch.object(source, "_connect_and_bind") as connection: 137 source.connection() 138 connection.assert_called_once_with( 139 None, 140 None, 141 { 142 "authentication": SASL, 143 "sasl_mechanism": EXTERNAL, 144 "sasl_credentials": "", 145 }, 146 ) 147 148 def test_user_connection_always_uses_simple_bind(self): 149 """User password verification never inherits the service bind method.""" 150 source = self.create_source( 151 client_certificate=create_test_cert(), 152 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 153 ) 154 with patch.object(source, "_connect_and_bind") as connection: 155 source.connection_as_user("cn=user", "submitted-password") 156 connection.assert_called_once_with( 157 connection_kwargs={ 158 "user": "cn=user", 159 "password": "submitted-password", 160 "authentication": SIMPLE, 161 } 162 ) 163 164 def test_start_tls_before_bind(self): 165 """StartTLS secures the connection before credentials are bound.""" 166 source = self.create_source( 167 server_uri="ldap://ldap.example.com", 168 start_tls=True, 169 ) 170 server = MagicMock() 171 connection = MagicMock() 172 connection.bind.return_value = True 173 connection.server.tls.certificate_file = None 174 calls = Mock() 175 calls.attach_mock(connection.start_tls, "start_tls") 176 calls.attach_mock(connection.bind, "bind") 177 with patch("ldap3.Connection", return_value=connection): 178 source._connect_and_bind(server=server) 179 self.assertEqual( 180 calls.mock_calls, 181 [call.start_tls(read_server_info=False), call.bind()], 182 )
LDAP Source model tests
18 def create_source(self, **kwargs) -> LDAPSource: 19 """Create an LDAP source with common required fields.""" 20 source_kwargs = { 21 "name": generate_id(), 22 "slug": generate_id(), 23 "server_uri": "ldaps://ldap.example.com", 24 "base_dn": "dc=example,dc=com", 25 } 26 source_kwargs.update(kwargs) 27 return LDAPSource.objects.create( 28 **source_kwargs, 29 )
Create an LDAP source with common required fields.
31 def test_server_sni(self): 32 """Test that the SNI name is the bare hostname, not the full server URI""" 33 source = self.create_source( 34 server_uri="ldaps://ldap.example.com:636", 35 sni=True, 36 ) 37 pool = source.server() 38 self.assertEqual([server.tls.sni for server in pool.servers], ["ldap.example.com"])
Test that the SNI name is the bare hostname, not the full server URI
40 def test_server_sni_multiple(self): 41 """Test that each server in a pool gets its own hostname as the SNI name""" 42 source = self.create_source( 43 server_uri="ldaps://ldap1.example.com,ldaps://ldap2.example.com:636", 44 sni=True, 45 ) 46 pool = source.server() 47 self.assertEqual( 48 [server.tls.sni for server in pool.servers], 49 ["ldap1.example.com", "ldap2.example.com"], 50 )
Test that each server in a pool gets its own hostname as the SNI name
52 def test_server_sni_disabled(self): 53 """Test that no SNI name is set when the SNI option is disabled""" 54 source = self.create_source( 55 server_uri="ldaps://ldap.example.com", 56 sni=False, 57 ) 58 pool = source.server() 59 for server in pool.servers: 60 self.assertIsNone(server.tls.sni)
Test that no SNI name is set when the SNI option is disabled
62 def test_simple_service_connection(self): 63 """The default service connection preserves simple bind credentials.""" 64 source = self.create_source(bind_cn="cn=service", bind_password="password") 65 with patch.object(source, "_connect_and_bind") as connection: 66 source.connection() 67 connection.assert_called_once_with( 68 None, 69 None, 70 {"user": "cn=service", "password": "password"}, 71 )
The default service connection preserves simple bind credentials.
73 def test_anonymous_service_connection(self): 74 """Blank credentials continue to let ldap3 select anonymous authentication.""" 75 source = self.create_source() 76 with patch.object(source, "_connect_and_bind") as connection: 77 source.connection() 78 connection.assert_called_once_with(None, None, {"user": "", "password": ""})
Blank credentials continue to let ldap3 select anonymous authentication.
80 def test_sasl_external_service_connection(self): 81 """SASL EXTERNAL derives authorization from the TLS client certificate.""" 82 source = self.create_source( 83 client_certificate=create_test_cert(), 84 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 85 ) 86 with patch.object(source, "_connect_and_bind") as connection: 87 source.connection(connection_kwargs={"user": "ignored", "password": "ignored"}) 88 connection.assert_called_once_with( 89 None, 90 None, 91 { 92 "authentication": SASL, 93 "sasl_mechanism": EXTERNAL, 94 "sasl_credentials": "", 95 }, 96 )
SASL EXTERNAL derives authorization from the TLS client certificate.
98 def test_sasl_external_requires_client_certificate(self): 99 """SASL EXTERNAL cannot run without a TLS client identity.""" 100 source = self.create_source( 101 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 102 ) 103 with self.assertRaises(LDAPConfigurationError): 104 source.connection()
SASL EXTERNAL cannot run without a TLS client identity.
106 def test_sasl_external_requires_private_key(self): 107 """SASL EXTERNAL rejects a certificate without its private key.""" 108 certificate = create_test_cert() 109 certificate.key_data = "" 110 certificate.save(update_fields=["key_data"]) 111 source = self.create_source( 112 client_certificate=certificate, 113 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 114 ) 115 with self.assertRaises(LDAPConfigurationError): 116 source.connection()
SASL EXTERNAL rejects a certificate without its private key.
118 def test_sasl_external_requires_secure_transport(self): 119 """SASL EXTERNAL rejects a plaintext LDAP connection.""" 120 source = self.create_source( 121 server_uri="ldap://ldap.example.com", 122 client_certificate=create_test_cert(), 123 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 124 ) 125 with self.assertRaises(LDAPConfigurationError): 126 source.connection()
SASL EXTERNAL rejects a plaintext LDAP connection.
128 def test_sasl_external_start_tls(self): 129 """SASL EXTERNAL accepts LDAP when StartTLS is enabled.""" 130 source = self.create_source( 131 server_uri="ldap://ldap.example.com", 132 start_tls=True, 133 client_certificate=create_test_cert(), 134 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 135 ) 136 with patch.object(source, "_connect_and_bind") as connection: 137 source.connection() 138 connection.assert_called_once_with( 139 None, 140 None, 141 { 142 "authentication": SASL, 143 "sasl_mechanism": EXTERNAL, 144 "sasl_credentials": "", 145 }, 146 )
SASL EXTERNAL accepts LDAP when StartTLS is enabled.
148 def test_user_connection_always_uses_simple_bind(self): 149 """User password verification never inherits the service bind method.""" 150 source = self.create_source( 151 client_certificate=create_test_cert(), 152 service_bind_method=LDAPSourceBindMethod.SASL_EXTERNAL, 153 ) 154 with patch.object(source, "_connect_and_bind") as connection: 155 source.connection_as_user("cn=user", "submitted-password") 156 connection.assert_called_once_with( 157 connection_kwargs={ 158 "user": "cn=user", 159 "password": "submitted-password", 160 "authentication": SIMPLE, 161 } 162 )
User password verification never inherits the service bind method.
164 def test_start_tls_before_bind(self): 165 """StartTLS secures the connection before credentials are bound.""" 166 source = self.create_source( 167 server_uri="ldap://ldap.example.com", 168 start_tls=True, 169 ) 170 server = MagicMock() 171 connection = MagicMock() 172 connection.bind.return_value = True 173 connection.server.tls.certificate_file = None 174 calls = Mock() 175 calls.attach_mock(connection.start_tls, "start_tls") 176 calls.attach_mock(connection.bind, "bind") 177 with patch("ldap3.Connection", return_value=connection): 178 source._connect_and_bind(server=server) 179 self.assertEqual( 180 calls.mock_calls, 181 [call.start_tls(read_server_info=False), call.bind()], 182 )
StartTLS secures the connection before credentials are bound.