Skip to content

Commit f9711a8

Browse files
slyngshedenijel
authored andcommitted
CAS: Support group whitelisting
Allow the user to configure a group whitelist. Any user not a member of the one or more of the whitelisted groups will not allowed to authenticate, if the social_auth.auth_allowed is enabled.
1 parent 146a634 commit f9711a8

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

social_core/backends/cas.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class CASOpenIdConnectAuth(OpenIdConnectAuth):
2525
SOCIAL_AUTH_CAS_OIDC_ENDPOINT = 'https://.....' # endpoint without /.well-known/openid-configuration
2626
SOCIAL_AUTH_CAS_KEY = '<client_id>'
2727
SOCIAL_AUTH_CAS_SECRET = '<client_secret>'
28+
SOCIAL_AUTH_CAS_ALLOW_GROUPS = []
2829
"""
2930

3031
name = "cas"
@@ -59,3 +60,17 @@ def get_user_details(self, response):
5960
"first_name": attributes.get("given_name"),
6061
"last_name": attributes.get("family_name"),
6162
}
63+
64+
def auth_allowed(self, response, details):
65+
if not super().auth_allowed(response, details):
66+
return False
67+
68+
allow_groups = self.setting("ALLOW_GROUPS", [])
69+
if not allow_groups:
70+
return True
71+
72+
groups = response.get('groups', [])
73+
for group in groups:
74+
if group in allow_groups:
75+
return True
76+
return False

social_core/tests/backends/test_cas.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def extra_settings(self):
4747
settings.update(
4848
{
4949
f"SOCIAL_AUTH_{self.name}_OIDC_ENDPOINT": f"{ROOT_URL}oidc",
50+
f"SOCIAL_AUTH_{self.name}_ALLOW_GROUPS": ["users",],
51+
f"SOCIAL_AUTH_{self.name}_WHITELISTED_DOMAINS": ["example.net",]
5052
}
5153
)
5254
return settings

0 commit comments

Comments
 (0)