authentik.sources.saml.tests.test_api

SAML Source API tests

 1"""SAML Source API tests"""
 2
 3from unittest import skipIf
 4
 5from cryptography.hazmat.backends.openssl.backend import backend
 6from django.urls import reverse
 7from rest_framework.test import APITestCase
 8
 9from authentik.core.tests.utils import create_test_admin_user, create_test_cert, create_test_flow
10from authentik.crypto.builder import PrivateKeyAlg
11from authentik.lib.generators import generate_id
12
13
14class TestSAMLSourceAPI(APITestCase):
15    """SAML Source API tests"""
16
17    def setUp(self) -> None:
18        super().setUp()
19        self.client.force_login(create_test_admin_user())
20
21    @skipIf(backend._fips_enabled, "The FIPS provider has no Ed25519")
22    def test_create_validate_unsupported_key_type(self):
23        """Test validation rejects key types without an XML-DSIG transform (Ed25519)"""
24        ed25519_cert = create_test_cert(PrivateKeyAlg.ED25519)
25
26        response = self.client.post(
27            reverse("authentik_api:samlsource-list"),
28            data={
29                "name": generate_id(),
30                "slug": generate_id(),
31                "sso_url": "http://localhost",
32                "pre_authentication_flow": create_test_flow().pk,
33                "signing_kp": ed25519_cert.pk,
34                "verification_kp": ed25519_cert.pk,
35                "encryption_kp": ed25519_cert.pk,
36                "signed_assertion": True,
37            },
38        )
39        self.assertEqual(response.status_code, 400)
40        message = (
41            "Key type Ed25519 is not supported. Supported key types are: RSA, Elliptic Curve, DSA."
42        )
43        self.assertJSONEqual(
44            response.content,
45            {
46                "signing_kp": [message],
47                "verification_kp": [message],
48                "encryption_kp": [message],
49            },
50        )
51
52    def test_create_supported_key_type(self):
53        """Test an RSA keypair is accepted on every keypair field"""
54        cert = create_test_cert()
55
56        response = self.client.post(
57            reverse("authentik_api:samlsource-list"),
58            data={
59                "name": generate_id(),
60                "slug": generate_id(),
61                "sso_url": "http://localhost",
62                "pre_authentication_flow": create_test_flow().pk,
63                "signing_kp": cert.pk,
64                "verification_kp": cert.pk,
65                "encryption_kp": cert.pk,
66                "signed_assertion": True,
67            },
68        )
69        self.assertEqual(response.status_code, 201)
class TestSAMLSourceAPI(rest_framework.test.APITestCase):
15class TestSAMLSourceAPI(APITestCase):
16    """SAML Source API tests"""
17
18    def setUp(self) -> None:
19        super().setUp()
20        self.client.force_login(create_test_admin_user())
21
22    @skipIf(backend._fips_enabled, "The FIPS provider has no Ed25519")
23    def test_create_validate_unsupported_key_type(self):
24        """Test validation rejects key types without an XML-DSIG transform (Ed25519)"""
25        ed25519_cert = create_test_cert(PrivateKeyAlg.ED25519)
26
27        response = self.client.post(
28            reverse("authentik_api:samlsource-list"),
29            data={
30                "name": generate_id(),
31                "slug": generate_id(),
32                "sso_url": "http://localhost",
33                "pre_authentication_flow": create_test_flow().pk,
34                "signing_kp": ed25519_cert.pk,
35                "verification_kp": ed25519_cert.pk,
36                "encryption_kp": ed25519_cert.pk,
37                "signed_assertion": True,
38            },
39        )
40        self.assertEqual(response.status_code, 400)
41        message = (
42            "Key type Ed25519 is not supported. Supported key types are: RSA, Elliptic Curve, DSA."
43        )
44        self.assertJSONEqual(
45            response.content,
46            {
47                "signing_kp": [message],
48                "verification_kp": [message],
49                "encryption_kp": [message],
50            },
51        )
52
53    def test_create_supported_key_type(self):
54        """Test an RSA keypair is accepted on every keypair field"""
55        cert = create_test_cert()
56
57        response = self.client.post(
58            reverse("authentik_api:samlsource-list"),
59            data={
60                "name": generate_id(),
61                "slug": generate_id(),
62                "sso_url": "http://localhost",
63                "pre_authentication_flow": create_test_flow().pk,
64                "signing_kp": cert.pk,
65                "verification_kp": cert.pk,
66                "encryption_kp": cert.pk,
67                "signed_assertion": True,
68            },
69        )
70        self.assertEqual(response.status_code, 201)

SAML Source API tests

def setUp(self) -> None:
18    def setUp(self) -> None:
19        super().setUp()
20        self.client.force_login(create_test_admin_user())

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

@skipIf(backend._fips_enabled, 'The FIPS provider has no Ed25519')
def test_create_validate_unsupported_key_type(self):
22    @skipIf(backend._fips_enabled, "The FIPS provider has no Ed25519")
23    def test_create_validate_unsupported_key_type(self):
24        """Test validation rejects key types without an XML-DSIG transform (Ed25519)"""
25        ed25519_cert = create_test_cert(PrivateKeyAlg.ED25519)
26
27        response = self.client.post(
28            reverse("authentik_api:samlsource-list"),
29            data={
30                "name": generate_id(),
31                "slug": generate_id(),
32                "sso_url": "http://localhost",
33                "pre_authentication_flow": create_test_flow().pk,
34                "signing_kp": ed25519_cert.pk,
35                "verification_kp": ed25519_cert.pk,
36                "encryption_kp": ed25519_cert.pk,
37                "signed_assertion": True,
38            },
39        )
40        self.assertEqual(response.status_code, 400)
41        message = (
42            "Key type Ed25519 is not supported. Supported key types are: RSA, Elliptic Curve, DSA."
43        )
44        self.assertJSONEqual(
45            response.content,
46            {
47                "signing_kp": [message],
48                "verification_kp": [message],
49                "encryption_kp": [message],
50            },
51        )

Test validation rejects key types without an XML-DSIG transform (Ed25519)

def test_create_supported_key_type(self):
53    def test_create_supported_key_type(self):
54        """Test an RSA keypair is accepted on every keypair field"""
55        cert = create_test_cert()
56
57        response = self.client.post(
58            reverse("authentik_api:samlsource-list"),
59            data={
60                "name": generate_id(),
61                "slug": generate_id(),
62                "sso_url": "http://localhost",
63                "pre_authentication_flow": create_test_flow().pk,
64                "signing_kp": cert.pk,
65                "verification_kp": cert.pk,
66                "encryption_kp": cert.pk,
67                "signed_assertion": True,
68            },
69        )
70        self.assertEqual(response.status_code, 201)

Test an RSA keypair is accepted on every keypair field