Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation, tests and docs of pull request #104 #172

Merged
merged 2 commits into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion django_auth_adfs/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ def update_user_flags(self, user, claims):

for flag, group in settings.GROUP_TO_FLAG_MAPPING.items():
if hasattr(user, flag):
if group in access_token_groups:
if not isinstance(group, list):
group = [group]

if any(group_list_item in access_token_groups for group_list_item in group):
value = True
else:
value = False
Expand Down
4 changes: 2 additions & 2 deletions docs/settings_ref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,14 @@ For example, if a user is a member of the group ``Django Staff``, you can automa
field of the user to ``True``.

The **key** represents the boolean user model field (e.g. ``is_staff``)
and the **value** represents the group name (e.g. ``Django Staff``).
and the **value**, which can either be a single String or an array of Strings, represents the group(s) name (e.g. ``Django Staff``).

example

.. code-block:: python

AUTH_ADFS = {
"GROUP_TO_FLAG_MAPPING": {"is_staff": "Django Staff",
"GROUP_TO_FLAG_MAPPING": {"is_staff": ["Django Staff", "Other Django Staff"],
"is_superuser": "Django Admins"},
}

Expand Down
36 changes: 36 additions & 0 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,42 @@ def test_group_removal_overlap(self):
self.assertEqual(user.groups.all()[0].name, "group1")
self.assertEqual(user.groups.all()[1].name, "group2")

@mock_adfs("2016")
def test_group_to_flag_mapping(self):
group_to_flag_mapping = {
"is_staff": ["group1", "group4"],
"is_superuser": "group2",
}
with patch("django_auth_adfs.backend.settings.GROUP_TO_FLAG_MAPPING", group_to_flag_mapping):
with patch("django_auth_adfs.backend.settings.BOOLEAN_CLAIM_MAPPING", {}):
backend = AdfsAuthCodeBackend()

user = backend.authenticate(self.request, authorization_code="dummycode")
self.assertIsInstance(user, User)
self.assertEqual(user.first_name, "John")
self.assertEqual(user.last_name, "Doe")
self.assertEqual(user.email, "john.doe@example.com")
self.assertEqual(len(user.groups.all()), 2)
self.assertTrue(user.is_staff)
self.assertTrue(user.is_superuser)

@mock_adfs("2016")
def test_boolean_claim_mapping(self):
boolean_claim_mapping = {
"is_superuser": "user_is_superuser",
}
with patch("django_auth_adfs.backend.settings.BOOLEAN_CLAIM_MAPPING", boolean_claim_mapping):
backend = AdfsAuthCodeBackend()

user = backend.authenticate(self.request, authorization_code="dummycode")
self.assertIsInstance(user, User)
self.assertEqual(user.first_name, "John")
self.assertEqual(user.last_name, "Doe")
self.assertEqual(user.email, "john.doe@example.com")
self.assertEqual(len(user.groups.all()), 2)
self.assertFalse(user.is_staff)
self.assertTrue(user.is_superuser)

@mock_adfs("2016")
def test_authentication(self):
response = self.client.get("/oauth2/callback", {'code': 'testcode'})
Expand Down