Skip to content

Commit

Permalink
Merge pull request #374 from uktrade/master
Browse files Browse the repository at this point in the history
Split authenticate into separate overridable function
  • Loading branch information
Giuseppe De Marco authored May 1, 2023
2 parents f1aa8e0 + 2434322 commit 6c7f760
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
47 changes: 35 additions & 12 deletions djangosaml2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,40 @@ def post(self, request, attribute_mapping=None, create_unknown_user=None):
if callable(create_unknown_user):
create_unknown_user = create_unknown_user()

try:
user = self.authenticate_user(
request,
session_info,
attribute_mapping,
create_unknown_user,
assertion_info
)
except PermissionDenied as e:
return self.handle_acs_failure(
request,
exception=e,
session_info=session_info,
)

relay_state = self.build_relay_state()
custom_redirect_url = self.custom_redirect(user, relay_state, session_info)
if custom_redirect_url:
return HttpResponseRedirect(custom_redirect_url)
relay_state = validate_referral_url(request, relay_state)
logger.debug("Redirecting to the RelayState: %s", relay_state)
return HttpResponseRedirect(relay_state)

def authenticate_user(
self,
request,
session_info,
attribute_mapping,
create_unknown_user,
assertion_info
):
"""Calls Django's authenticate method after the SAML response is verified"""
logger.debug("Trying to authenticate the user. Session info: %s", session_info)

user = auth.authenticate(
request=request,
session_info=session_info,
Expand All @@ -563,11 +596,7 @@ def post(self, request, attribute_mapping=None, create_unknown_user=None):
"Could not authenticate user received in SAML Assertion. Session info: %s",
session_info,
)
return self.handle_acs_failure(
request,
exception=PermissionDenied("No user could be authenticated."),
session_info=session_info,
)
raise PermissionDenied("No user could be authenticated.")

auth.login(self.request, user)
_set_subject_id(request.saml_session, session_info["name_id"])
Expand All @@ -576,13 +605,7 @@ def post(self, request, attribute_mapping=None, create_unknown_user=None):
self.post_login_hook(request, user, session_info)
self.customize_session(user, session_info)

relay_state = self.build_relay_state()
custom_redirect_url = self.custom_redirect(user, relay_state, session_info)
if custom_redirect_url:
return HttpResponseRedirect(custom_redirect_url)
relay_state = validate_referral_url(request, relay_state)
logger.debug("Redirecting to the RelayState: %s", relay_state)
return HttpResponseRedirect(relay_state)
return user

def post_login_hook(
self, request: HttpRequest, user: settings.AUTH_USER_MODEL, session_info: dict
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def read(*rnames):

setup(
name="djangosaml2",
version="1.5.6",
version="1.5.7",
description="pysaml2 integration for Django",
long_description=read("README.md"),
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 6c7f760

Please sign in to comment.