authentik.sources.saml.tests.test_views

SAML Source tests

  1"""SAML Source tests"""
  2
  3from base64 import b64encode
  4from unittest.mock import MagicMock, patch
  5
  6from django.test import RequestFactory, TestCase
  7from django.urls import reverse
  8from freezegun import freeze_time
  9
 10from authentik.core.tests.utils import create_test_flow
 11from authentik.flows.planner import PLAN_CONTEXT_REDIRECT, FlowPlan
 12from authentik.flows.views.executor import SESSION_KEY_PLAN
 13from authentik.lib.generators import generate_id
 14from authentik.lib.tests.utils import load_fixture
 15from authentik.sources.saml.models import SAMLSource
 16
 17
 18@patch(
 19    "authentik.sources.saml.processors.response.reverse",
 20    MagicMock(return_value="https://127.0.0.1:9443/source/saml/google/acs/"),
 21)
 22class TestViews(TestCase):
 23    """Test SAML Views"""
 24
 25    def setUp(self):
 26        self.factory = RequestFactory()
 27        self.source = SAMLSource.objects.create(
 28            name=generate_id(),
 29            slug=generate_id(),
 30            issuer_override="https://accounts.google.com/o/saml2?idpid=",
 31            allow_idp_initiated=True,
 32            pre_authentication_flow=create_test_flow(),
 33        )
 34
 35    @freeze_time("2022-10-14T14:15:00")
 36    def test_enroll(self):
 37        """Enroll"""
 38        flow = create_test_flow()
 39        self.source.enrollment_flow = flow
 40        self.source.save()
 41
 42        response = self.client.post(
 43            reverse(
 44                "authentik_sources_saml:acs",
 45                kwargs={
 46                    "source_slug": self.source.slug,
 47                },
 48            ),
 49            data={
 50                "SAMLResponse": b64encode(
 51                    load_fixture("fixtures/response_success.xml").encode()
 52                ).decode()
 53            },
 54        )
 55        self.assertEqual(response.status_code, 302)
 56        self.assertRedirects(
 57            response, reverse("authentik_core:if-flow", kwargs={"flow_slug": flow.slug})
 58        )
 59        plan: FlowPlan = self.client.session.get(SESSION_KEY_PLAN)
 60        self.assertIsNotNone(plan)
 61
 62    @freeze_time("2022-10-14T15:00:00")
 63    def test_expired_assertion(self):
 64        """An expired assertion should return a 400, not an unhandled error"""
 65        response = self.client.post(
 66            reverse(
 67                "authentik_sources_saml:acs",
 68                kwargs={
 69                    "source_slug": self.source.slug,
 70                },
 71            ),
 72            data={
 73                "SAMLResponse": b64encode(
 74                    load_fixture("fixtures/response_success.xml").encode()
 75                ).decode()
 76            },
 77        )
 78        self.assertEqual(response.status_code, 400)
 79        self.assertIn(b"The SAML assertion is not valid yet or has expired.", response.content)
 80
 81    def test_error_status_generic_message(self):
 82        """An unsuccessful IdP status shows the user a generic message, not the IdP's details"""
 83        response = self.client.post(
 84            reverse(
 85                "authentik_sources_saml:acs",
 86                kwargs={
 87                    "source_slug": self.source.slug,
 88                },
 89            ),
 90            data={
 91                "SAMLResponse": b64encode(
 92                    load_fixture("fixtures/response_error_with_detail.xml").encode()
 93                ).decode()
 94            },
 95        )
 96        self.assertEqual(response.status_code, 400)
 97        self.assertIn(
 98            b"The identity provider did not complete the authentication request.",
 99            response.content,
100        )
101        self.assertNotIn(b"User account is disabled", response.content)
102        self.assertNotIn(b"Authentication failed", response.content)
103
104    @freeze_time("2022-10-14T14:15:00")
105    def test_enroll_redirect(self):
106        """Enroll when attempting to access a provider"""
107        initial_redirect = f"http://{generate_id()}"
108
109        session = self.client.session
110        old_plan = FlowPlan(generate_id())
111        old_plan.context[PLAN_CONTEXT_REDIRECT] = initial_redirect
112        session[SESSION_KEY_PLAN] = old_plan
113        session.save()
114
115        flow = create_test_flow()
116        self.source.enrollment_flow = flow
117        self.source.save()
118
119        response = self.client.post(
120            reverse(
121                "authentik_sources_saml:acs",
122                kwargs={
123                    "source_slug": self.source.slug,
124                },
125            ),
126            data={
127                "SAMLResponse": b64encode(
128                    load_fixture("fixtures/response_success.xml").encode()
129                ).decode()
130            },
131        )
132        self.assertEqual(response.status_code, 302)
133        self.assertRedirects(
134            response, reverse("authentik_core:if-flow", kwargs={"flow_slug": flow.slug})
135        )
136        plan: FlowPlan = self.client.session.get(SESSION_KEY_PLAN)
137        self.assertIsNotNone(plan)
138        self.assertEqual(plan.context.get(PLAN_CONTEXT_REDIRECT), initial_redirect)
@patch('authentik.sources.saml.processors.response.reverse', MagicMock(return_value='https://127.0.0.1:9443/source/saml/google/acs/'))
class TestViews(django.test.testcases.TestCase):
 19@patch(
 20    "authentik.sources.saml.processors.response.reverse",
 21    MagicMock(return_value="https://127.0.0.1:9443/source/saml/google/acs/"),
 22)
 23class TestViews(TestCase):
 24    """Test SAML Views"""
 25
 26    def setUp(self):
 27        self.factory = RequestFactory()
 28        self.source = SAMLSource.objects.create(
 29            name=generate_id(),
 30            slug=generate_id(),
 31            issuer_override="https://accounts.google.com/o/saml2?idpid=",
 32            allow_idp_initiated=True,
 33            pre_authentication_flow=create_test_flow(),
 34        )
 35
 36    @freeze_time("2022-10-14T14:15:00")
 37    def test_enroll(self):
 38        """Enroll"""
 39        flow = create_test_flow()
 40        self.source.enrollment_flow = flow
 41        self.source.save()
 42
 43        response = self.client.post(
 44            reverse(
 45                "authentik_sources_saml:acs",
 46                kwargs={
 47                    "source_slug": self.source.slug,
 48                },
 49            ),
 50            data={
 51                "SAMLResponse": b64encode(
 52                    load_fixture("fixtures/response_success.xml").encode()
 53                ).decode()
 54            },
 55        )
 56        self.assertEqual(response.status_code, 302)
 57        self.assertRedirects(
 58            response, reverse("authentik_core:if-flow", kwargs={"flow_slug": flow.slug})
 59        )
 60        plan: FlowPlan = self.client.session.get(SESSION_KEY_PLAN)
 61        self.assertIsNotNone(plan)
 62
 63    @freeze_time("2022-10-14T15:00:00")
 64    def test_expired_assertion(self):
 65        """An expired assertion should return a 400, not an unhandled error"""
 66        response = self.client.post(
 67            reverse(
 68                "authentik_sources_saml:acs",
 69                kwargs={
 70                    "source_slug": self.source.slug,
 71                },
 72            ),
 73            data={
 74                "SAMLResponse": b64encode(
 75                    load_fixture("fixtures/response_success.xml").encode()
 76                ).decode()
 77            },
 78        )
 79        self.assertEqual(response.status_code, 400)
 80        self.assertIn(b"The SAML assertion is not valid yet or has expired.", response.content)
 81
 82    def test_error_status_generic_message(self):
 83        """An unsuccessful IdP status shows the user a generic message, not the IdP's details"""
 84        response = self.client.post(
 85            reverse(
 86                "authentik_sources_saml:acs",
 87                kwargs={
 88                    "source_slug": self.source.slug,
 89                },
 90            ),
 91            data={
 92                "SAMLResponse": b64encode(
 93                    load_fixture("fixtures/response_error_with_detail.xml").encode()
 94                ).decode()
 95            },
 96        )
 97        self.assertEqual(response.status_code, 400)
 98        self.assertIn(
 99            b"The identity provider did not complete the authentication request.",
100            response.content,
101        )
102        self.assertNotIn(b"User account is disabled", response.content)
103        self.assertNotIn(b"Authentication failed", response.content)
104
105    @freeze_time("2022-10-14T14:15:00")
106    def test_enroll_redirect(self):
107        """Enroll when attempting to access a provider"""
108        initial_redirect = f"http://{generate_id()}"
109
110        session = self.client.session
111        old_plan = FlowPlan(generate_id())
112        old_plan.context[PLAN_CONTEXT_REDIRECT] = initial_redirect
113        session[SESSION_KEY_PLAN] = old_plan
114        session.save()
115
116        flow = create_test_flow()
117        self.source.enrollment_flow = flow
118        self.source.save()
119
120        response = self.client.post(
121            reverse(
122                "authentik_sources_saml:acs",
123                kwargs={
124                    "source_slug": self.source.slug,
125                },
126            ),
127            data={
128                "SAMLResponse": b64encode(
129                    load_fixture("fixtures/response_success.xml").encode()
130                ).decode()
131            },
132        )
133        self.assertEqual(response.status_code, 302)
134        self.assertRedirects(
135            response, reverse("authentik_core:if-flow", kwargs={"flow_slug": flow.slug})
136        )
137        plan: FlowPlan = self.client.session.get(SESSION_KEY_PLAN)
138        self.assertIsNotNone(plan)
139        self.assertEqual(plan.context.get(PLAN_CONTEXT_REDIRECT), initial_redirect)

Test SAML Views

def setUp(self):
26    def setUp(self):
27        self.factory = RequestFactory()
28        self.source = SAMLSource.objects.create(
29            name=generate_id(),
30            slug=generate_id(),
31            issuer_override="https://accounts.google.com/o/saml2?idpid=",
32            allow_idp_initiated=True,
33            pre_authentication_flow=create_test_flow(),
34        )

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

@freeze_time('2022-10-14T14:15:00')
def test_enroll(self):
36    @freeze_time("2022-10-14T14:15:00")
37    def test_enroll(self):
38        """Enroll"""
39        flow = create_test_flow()
40        self.source.enrollment_flow = flow
41        self.source.save()
42
43        response = self.client.post(
44            reverse(
45                "authentik_sources_saml:acs",
46                kwargs={
47                    "source_slug": self.source.slug,
48                },
49            ),
50            data={
51                "SAMLResponse": b64encode(
52                    load_fixture("fixtures/response_success.xml").encode()
53                ).decode()
54            },
55        )
56        self.assertEqual(response.status_code, 302)
57        self.assertRedirects(
58            response, reverse("authentik_core:if-flow", kwargs={"flow_slug": flow.slug})
59        )
60        plan: FlowPlan = self.client.session.get(SESSION_KEY_PLAN)
61        self.assertIsNotNone(plan)

Enroll

@freeze_time('2022-10-14T15:00:00')
def test_expired_assertion(self):
63    @freeze_time("2022-10-14T15:00:00")
64    def test_expired_assertion(self):
65        """An expired assertion should return a 400, not an unhandled error"""
66        response = self.client.post(
67            reverse(
68                "authentik_sources_saml:acs",
69                kwargs={
70                    "source_slug": self.source.slug,
71                },
72            ),
73            data={
74                "SAMLResponse": b64encode(
75                    load_fixture("fixtures/response_success.xml").encode()
76                ).decode()
77            },
78        )
79        self.assertEqual(response.status_code, 400)
80        self.assertIn(b"The SAML assertion is not valid yet or has expired.", response.content)

An expired assertion should return a 400, not an unhandled error

def test_error_status_generic_message(self):
 82    def test_error_status_generic_message(self):
 83        """An unsuccessful IdP status shows the user a generic message, not the IdP's details"""
 84        response = self.client.post(
 85            reverse(
 86                "authentik_sources_saml:acs",
 87                kwargs={
 88                    "source_slug": self.source.slug,
 89                },
 90            ),
 91            data={
 92                "SAMLResponse": b64encode(
 93                    load_fixture("fixtures/response_error_with_detail.xml").encode()
 94                ).decode()
 95            },
 96        )
 97        self.assertEqual(response.status_code, 400)
 98        self.assertIn(
 99            b"The identity provider did not complete the authentication request.",
100            response.content,
101        )
102        self.assertNotIn(b"User account is disabled", response.content)
103        self.assertNotIn(b"Authentication failed", response.content)

An unsuccessful IdP status shows the user a generic message, not the IdP's details

@freeze_time('2022-10-14T14:15:00')
def test_enroll_redirect(self):
105    @freeze_time("2022-10-14T14:15:00")
106    def test_enroll_redirect(self):
107        """Enroll when attempting to access a provider"""
108        initial_redirect = f"http://{generate_id()}"
109
110        session = self.client.session
111        old_plan = FlowPlan(generate_id())
112        old_plan.context[PLAN_CONTEXT_REDIRECT] = initial_redirect
113        session[SESSION_KEY_PLAN] = old_plan
114        session.save()
115
116        flow = create_test_flow()
117        self.source.enrollment_flow = flow
118        self.source.save()
119
120        response = self.client.post(
121            reverse(
122                "authentik_sources_saml:acs",
123                kwargs={
124                    "source_slug": self.source.slug,
125                },
126            ),
127            data={
128                "SAMLResponse": b64encode(
129                    load_fixture("fixtures/response_success.xml").encode()
130                ).decode()
131            },
132        )
133        self.assertEqual(response.status_code, 302)
134        self.assertRedirects(
135            response, reverse("authentik_core:if-flow", kwargs={"flow_slug": flow.slug})
136        )
137        plan: FlowPlan = self.client.session.get(SESSION_KEY_PLAN)
138        self.assertIsNotNone(plan)
139        self.assertEqual(plan.context.get(PLAN_CONTEXT_REDIRECT), initial_redirect)

Enroll when attempting to access a provider