authentik.sources.saml.tests.test_property_mappings

SAML Source tests

  1"""SAML Source tests"""
  2
  3from base64 import b64encode
  4from unittest.mock import MagicMock, patch
  5
  6from defusedxml.lxml import fromstring
  7from django.test import TestCase
  8from freezegun import freeze_time
  9
 10from authentik.common.saml.constants import NS_SAML_ASSERTION
 11from authentik.core.tests.utils import RequestFactory, create_test_flow
 12from authentik.lib.generators import generate_id
 13from authentik.lib.tests.utils import load_fixture
 14from authentik.sources.saml.models import SAMLSource, SAMLSourcePropertyMapping
 15from authentik.sources.saml.processors.response import ResponseProcessor
 16
 17ROOT = fromstring(load_fixture("fixtures/response_success.xml").encode())
 18ROOT_GROUPS = fromstring(load_fixture("fixtures/response_success_groups.xml").encode())
 19NAME_ID = (
 20    ROOT.find(f"{{{NS_SAML_ASSERTION}}}Assertion")
 21    .find(f"{{{NS_SAML_ASSERTION}}}Subject")
 22    .find(f"{{{NS_SAML_ASSERTION}}}NameID")
 23)
 24
 25
 26@patch(
 27    "authentik.sources.saml.processors.response.reverse",
 28    MagicMock(return_value="https://127.0.0.1:9443/source/saml/google/acs/"),
 29)
 30class TestPropertyMappings(TestCase):
 31    """Test Property Mappings"""
 32
 33    def setUp(self):
 34        self.factory = RequestFactory()
 35        self.source = SAMLSource.objects.create(
 36            name=generate_id(),
 37            slug=generate_id(),
 38            issuer_override="https://accounts.google.com/o/saml2?idpid=",
 39            allow_idp_initiated=True,
 40            pre_authentication_flow=create_test_flow(),
 41        )
 42
 43    @freeze_time("2022-10-14T14:15:00")
 44    def test_user_base_properties(self):
 45        """Test user base properties"""
 46        properties = self.source.get_base_user_properties(
 47            root=ROOT, assertion=ROOT.find(f"{{{NS_SAML_ASSERTION}}}Assertion"), name_id=NAME_ID
 48        )
 49        self.assertEqual(
 50            properties,
 51            {
 52                "email": "foo@bar.baz",
 53                "name": "foo",
 54                "sn": "bar",
 55                "username": "jens@goauthentik.io",
 56            },
 57        )
 58
 59    def test_group_base_properties(self):
 60        """Test group base properties"""
 61        properties = self.source.get_base_user_properties(
 62            root=ROOT_GROUPS,
 63            assertion=ROOT_GROUPS.find(f"{{{NS_SAML_ASSERTION}}}Assertion"),
 64            name_id=NAME_ID,
 65        )
 66        self.assertEqual(properties["groups"], ["group 1", "group 2"])
 67        for group_id in ["group 1", "group 2"]:
 68            properties = self.source.get_base_group_properties(root=ROOT, group_id=group_id)
 69            self.assertEqual(properties, {"name": group_id})
 70
 71    @freeze_time("2022-10-14T14:15:00")
 72    def test_user_property_mappings(self):
 73        """Test user property mappings"""
 74        self.source.user_property_mappings.add(
 75            SAMLSourcePropertyMapping.objects.create(
 76                name="test",
 77                expression="return {'attributes': {'department': 'Engineering'}, 'sn': None}",
 78            )
 79        )
 80        request = self.factory.post(
 81            "/",
 82            data={
 83                "SAMLResponse": b64encode(
 84                    load_fixture("fixtures/response_success.xml").encode()
 85                ).decode()
 86            },
 87        )
 88
 89        parser = ResponseProcessor(self.source, request)
 90        parser.parse()
 91        sfm = parser.prepare_flow_manager()
 92        self.assertEqual(
 93            sfm.user_properties,
 94            {
 95                "email": "foo@bar.baz",
 96                "name": "foo",
 97                "username": "jens@goauthentik.io",
 98                "attributes": {
 99                    "department": "Engineering",
100                },
101                "path": self.source.get_user_path(),
102            },
103        )
104
105    @freeze_time("2022-10-14T14:15:00")
106    def test_group_property_mappings(self):
107        """Test group property mappings"""
108        self.source.group_property_mappings.add(
109            SAMLSourcePropertyMapping.objects.create(
110                name="test",
111                expression="return {'attributes': {'id': group_id}}",
112            )
113        )
114        request = self.factory.post(
115            "/",
116            data={
117                "SAMLResponse": b64encode(
118                    load_fixture("fixtures/response_success_groups.xml").encode()
119                ).decode()
120            },
121        )
122
123        parser = ResponseProcessor(self.source, request)
124        parser.parse()
125        sfm = parser.prepare_flow_manager()
126        self.assertEqual(
127            sfm.groups_properties,
128            {
129                "group 1": {
130                    "name": "group 1",
131                    "attributes": {
132                        "id": "group 1",
133                    },
134                },
135                "group 2": {
136                    "name": "group 2",
137                    "attributes": {
138                        "id": "group 2",
139                    },
140                },
141            },
142        )
ROOT = <Element {urn:oasis:names:tc:SAML:2.0:protocol}Response>
ROOT_GROUPS = <Element {urn:oasis:names:tc:SAML:2.0:protocol}Response>
NAME_ID = <Element {urn:oasis:names:tc:SAML:2.0:assertion}NameID>
@patch('authentik.sources.saml.processors.response.reverse', MagicMock(return_value='https://127.0.0.1:9443/source/saml/google/acs/'))
class TestPropertyMappings(django.test.testcases.TestCase):
 27@patch(
 28    "authentik.sources.saml.processors.response.reverse",
 29    MagicMock(return_value="https://127.0.0.1:9443/source/saml/google/acs/"),
 30)
 31class TestPropertyMappings(TestCase):
 32    """Test Property Mappings"""
 33
 34    def setUp(self):
 35        self.factory = RequestFactory()
 36        self.source = SAMLSource.objects.create(
 37            name=generate_id(),
 38            slug=generate_id(),
 39            issuer_override="https://accounts.google.com/o/saml2?idpid=",
 40            allow_idp_initiated=True,
 41            pre_authentication_flow=create_test_flow(),
 42        )
 43
 44    @freeze_time("2022-10-14T14:15:00")
 45    def test_user_base_properties(self):
 46        """Test user base properties"""
 47        properties = self.source.get_base_user_properties(
 48            root=ROOT, assertion=ROOT.find(f"{{{NS_SAML_ASSERTION}}}Assertion"), name_id=NAME_ID
 49        )
 50        self.assertEqual(
 51            properties,
 52            {
 53                "email": "foo@bar.baz",
 54                "name": "foo",
 55                "sn": "bar",
 56                "username": "jens@goauthentik.io",
 57            },
 58        )
 59
 60    def test_group_base_properties(self):
 61        """Test group base properties"""
 62        properties = self.source.get_base_user_properties(
 63            root=ROOT_GROUPS,
 64            assertion=ROOT_GROUPS.find(f"{{{NS_SAML_ASSERTION}}}Assertion"),
 65            name_id=NAME_ID,
 66        )
 67        self.assertEqual(properties["groups"], ["group 1", "group 2"])
 68        for group_id in ["group 1", "group 2"]:
 69            properties = self.source.get_base_group_properties(root=ROOT, group_id=group_id)
 70            self.assertEqual(properties, {"name": group_id})
 71
 72    @freeze_time("2022-10-14T14:15:00")
 73    def test_user_property_mappings(self):
 74        """Test user property mappings"""
 75        self.source.user_property_mappings.add(
 76            SAMLSourcePropertyMapping.objects.create(
 77                name="test",
 78                expression="return {'attributes': {'department': 'Engineering'}, 'sn': None}",
 79            )
 80        )
 81        request = self.factory.post(
 82            "/",
 83            data={
 84                "SAMLResponse": b64encode(
 85                    load_fixture("fixtures/response_success.xml").encode()
 86                ).decode()
 87            },
 88        )
 89
 90        parser = ResponseProcessor(self.source, request)
 91        parser.parse()
 92        sfm = parser.prepare_flow_manager()
 93        self.assertEqual(
 94            sfm.user_properties,
 95            {
 96                "email": "foo@bar.baz",
 97                "name": "foo",
 98                "username": "jens@goauthentik.io",
 99                "attributes": {
100                    "department": "Engineering",
101                },
102                "path": self.source.get_user_path(),
103            },
104        )
105
106    @freeze_time("2022-10-14T14:15:00")
107    def test_group_property_mappings(self):
108        """Test group property mappings"""
109        self.source.group_property_mappings.add(
110            SAMLSourcePropertyMapping.objects.create(
111                name="test",
112                expression="return {'attributes': {'id': group_id}}",
113            )
114        )
115        request = self.factory.post(
116            "/",
117            data={
118                "SAMLResponse": b64encode(
119                    load_fixture("fixtures/response_success_groups.xml").encode()
120                ).decode()
121            },
122        )
123
124        parser = ResponseProcessor(self.source, request)
125        parser.parse()
126        sfm = parser.prepare_flow_manager()
127        self.assertEqual(
128            sfm.groups_properties,
129            {
130                "group 1": {
131                    "name": "group 1",
132                    "attributes": {
133                        "id": "group 1",
134                    },
135                },
136                "group 2": {
137                    "name": "group 2",
138                    "attributes": {
139                        "id": "group 2",
140                    },
141                },
142            },
143        )

Test Property Mappings

def setUp(self):
34    def setUp(self):
35        self.factory = RequestFactory()
36        self.source = SAMLSource.objects.create(
37            name=generate_id(),
38            slug=generate_id(),
39            issuer_override="https://accounts.google.com/o/saml2?idpid=",
40            allow_idp_initiated=True,
41            pre_authentication_flow=create_test_flow(),
42        )

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

@freeze_time('2022-10-14T14:15:00')
def test_user_base_properties(self):
44    @freeze_time("2022-10-14T14:15:00")
45    def test_user_base_properties(self):
46        """Test user base properties"""
47        properties = self.source.get_base_user_properties(
48            root=ROOT, assertion=ROOT.find(f"{{{NS_SAML_ASSERTION}}}Assertion"), name_id=NAME_ID
49        )
50        self.assertEqual(
51            properties,
52            {
53                "email": "foo@bar.baz",
54                "name": "foo",
55                "sn": "bar",
56                "username": "jens@goauthentik.io",
57            },
58        )

Test user base properties

def test_group_base_properties(self):
60    def test_group_base_properties(self):
61        """Test group base properties"""
62        properties = self.source.get_base_user_properties(
63            root=ROOT_GROUPS,
64            assertion=ROOT_GROUPS.find(f"{{{NS_SAML_ASSERTION}}}Assertion"),
65            name_id=NAME_ID,
66        )
67        self.assertEqual(properties["groups"], ["group 1", "group 2"])
68        for group_id in ["group 1", "group 2"]:
69            properties = self.source.get_base_group_properties(root=ROOT, group_id=group_id)
70            self.assertEqual(properties, {"name": group_id})

Test group base properties

@freeze_time('2022-10-14T14:15:00')
def test_user_property_mappings(self):
 72    @freeze_time("2022-10-14T14:15:00")
 73    def test_user_property_mappings(self):
 74        """Test user property mappings"""
 75        self.source.user_property_mappings.add(
 76            SAMLSourcePropertyMapping.objects.create(
 77                name="test",
 78                expression="return {'attributes': {'department': 'Engineering'}, 'sn': None}",
 79            )
 80        )
 81        request = self.factory.post(
 82            "/",
 83            data={
 84                "SAMLResponse": b64encode(
 85                    load_fixture("fixtures/response_success.xml").encode()
 86                ).decode()
 87            },
 88        )
 89
 90        parser = ResponseProcessor(self.source, request)
 91        parser.parse()
 92        sfm = parser.prepare_flow_manager()
 93        self.assertEqual(
 94            sfm.user_properties,
 95            {
 96                "email": "foo@bar.baz",
 97                "name": "foo",
 98                "username": "jens@goauthentik.io",
 99                "attributes": {
100                    "department": "Engineering",
101                },
102                "path": self.source.get_user_path(),
103            },
104        )

Test user property mappings

@freeze_time('2022-10-14T14:15:00')
def test_group_property_mappings(self):
106    @freeze_time("2022-10-14T14:15:00")
107    def test_group_property_mappings(self):
108        """Test group property mappings"""
109        self.source.group_property_mappings.add(
110            SAMLSourcePropertyMapping.objects.create(
111                name="test",
112                expression="return {'attributes': {'id': group_id}}",
113            )
114        )
115        request = self.factory.post(
116            "/",
117            data={
118                "SAMLResponse": b64encode(
119                    load_fixture("fixtures/response_success_groups.xml").encode()
120                ).decode()
121            },
122        )
123
124        parser = ResponseProcessor(self.source, request)
125        parser.parse()
126        sfm = parser.prepare_flow_manager()
127        self.assertEqual(
128            sfm.groups_properties,
129            {
130                "group 1": {
131                    "name": "group 1",
132                    "attributes": {
133                        "id": "group 1",
134                    },
135                },
136                "group 2": {
137                    "name": "group 2",
138                    "attributes": {
139                        "id": "group 2",
140                    },
141                },
142            },
143        )

Test group property mappings