authentik.providers.scim.tests.test_auth
SCIM authentication tests
1"""SCIM authentication tests""" 2 3from base64 import b64encode 4 5from django.core.cache import cache 6from django.test import TestCase 7from requests_mock import Mocker 8 9from authentik.lib.generators import generate_id 10from authentik.providers.scim.clients.base import SCIMClient 11from authentik.providers.scim.models import SCIMAuthenticationMode, SCIMProvider 12 13 14class SCIMAuthTests(TestCase): 15 """SCIM authentication tests""" 16 17 def setUp(self) -> None: 18 cache.clear() 19 20 def request_authorization_header(self, provider: SCIMProvider) -> str: 21 with Mocker() as mock: 22 mock.get("https://localhost/ServiceProviderConfig", json={}) 23 SCIMClient(provider) 24 return mock.last_request.headers["Authorization"] 25 26 def test_token(self): 27 """Test token authentication""" 28 token = generate_id() 29 provider = SCIMProvider.objects.create( 30 name=generate_id(), 31 url="https://localhost", 32 auth_mode=SCIMAuthenticationMode.TOKEN, 33 token=token, 34 ) 35 self.assertEqual(self.request_authorization_header(provider), f"Bearer {token}") 36 37 def test_basic(self): 38 """Test Basic authentication""" 39 user = generate_id() 40 password = generate_id() 41 provider = SCIMProvider.objects.create( 42 name=generate_id(), 43 url="https://localhost", 44 auth_mode=SCIMAuthenticationMode.BASIC, 45 auth_basic_user=user, 46 auth_basic_password=password, 47 ) 48 credentials = b64encode(f"{user}:{password}".encode()).decode() 49 self.assertEqual(self.request_authorization_header(provider), f"Basic {credentials}") 50 51 def test_basic_non_ascii(self): 52 """Test Basic authentication encodes non-ASCII credentials as UTF-8""" 53 provider = SCIMProvider.objects.create( 54 name=generate_id(), 55 url="https://localhost", 56 auth_mode=SCIMAuthenticationMode.BASIC, 57 auth_basic_user="ünity", 58 auth_basic_password="pässwörd", 59 ) 60 credentials = b64encode("ünity:pässwörd".encode()).decode() 61 self.assertEqual(self.request_authorization_header(provider), f"Basic {credentials}")
class
SCIMAuthTests(django.test.testcases.TestCase):
15class SCIMAuthTests(TestCase): 16 """SCIM authentication tests""" 17 18 def setUp(self) -> None: 19 cache.clear() 20 21 def request_authorization_header(self, provider: SCIMProvider) -> str: 22 with Mocker() as mock: 23 mock.get("https://localhost/ServiceProviderConfig", json={}) 24 SCIMClient(provider) 25 return mock.last_request.headers["Authorization"] 26 27 def test_token(self): 28 """Test token authentication""" 29 token = generate_id() 30 provider = SCIMProvider.objects.create( 31 name=generate_id(), 32 url="https://localhost", 33 auth_mode=SCIMAuthenticationMode.TOKEN, 34 token=token, 35 ) 36 self.assertEqual(self.request_authorization_header(provider), f"Bearer {token}") 37 38 def test_basic(self): 39 """Test Basic authentication""" 40 user = generate_id() 41 password = generate_id() 42 provider = SCIMProvider.objects.create( 43 name=generate_id(), 44 url="https://localhost", 45 auth_mode=SCIMAuthenticationMode.BASIC, 46 auth_basic_user=user, 47 auth_basic_password=password, 48 ) 49 credentials = b64encode(f"{user}:{password}".encode()).decode() 50 self.assertEqual(self.request_authorization_header(provider), f"Basic {credentials}") 51 52 def test_basic_non_ascii(self): 53 """Test Basic authentication encodes non-ASCII credentials as UTF-8""" 54 provider = SCIMProvider.objects.create( 55 name=generate_id(), 56 url="https://localhost", 57 auth_mode=SCIMAuthenticationMode.BASIC, 58 auth_basic_user="ünity", 59 auth_basic_password="pässwörd", 60 ) 61 credentials = b64encode("ünity:pässwörd".encode()).decode() 62 self.assertEqual(self.request_authorization_header(provider), f"Basic {credentials}")
SCIM authentication tests
def
test_token(self):
27 def test_token(self): 28 """Test token authentication""" 29 token = generate_id() 30 provider = SCIMProvider.objects.create( 31 name=generate_id(), 32 url="https://localhost", 33 auth_mode=SCIMAuthenticationMode.TOKEN, 34 token=token, 35 ) 36 self.assertEqual(self.request_authorization_header(provider), f"Bearer {token}")
Test token authentication
def
test_basic(self):
38 def test_basic(self): 39 """Test Basic authentication""" 40 user = generate_id() 41 password = generate_id() 42 provider = SCIMProvider.objects.create( 43 name=generate_id(), 44 url="https://localhost", 45 auth_mode=SCIMAuthenticationMode.BASIC, 46 auth_basic_user=user, 47 auth_basic_password=password, 48 ) 49 credentials = b64encode(f"{user}:{password}".encode()).decode() 50 self.assertEqual(self.request_authorization_header(provider), f"Basic {credentials}")
Test Basic authentication
def
test_basic_non_ascii(self):
52 def test_basic_non_ascii(self): 53 """Test Basic authentication encodes non-ASCII credentials as UTF-8""" 54 provider = SCIMProvider.objects.create( 55 name=generate_id(), 56 url="https://localhost", 57 auth_mode=SCIMAuthenticationMode.BASIC, 58 auth_basic_user="ünity", 59 auth_basic_password="pässwörd", 60 ) 61 credentials = b64encode("ünity:pässwörd".encode()).decode() 62 self.assertEqual(self.request_authorization_header(provider), f"Basic {credentials}")
Test Basic authentication encodes non-ASCII credentials as UTF-8