Skip to content

Commit

Permalink
Merge pull request #376 from uktrade/skip-login-view-logic
Browse files Browse the repository at this point in the history
Separate out the condition for skipping login
  • Loading branch information
Giuseppe De Marco authored May 29, 2023
2 parents e2e06d6 + 2366a92 commit fb5554c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions djangosaml2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,23 @@ def load_sso_kwargs(self, sso_kwargs):
def add_idp_hinting(self, http_response):
return add_idp_hinting(self.request, http_response) or http_response

def get(self, request, *args, **kwargs):
logger.debug("Login process started")
next_path = self.get_next_path(request)

# if the user is already authenticated that maybe because of two reasons:
def should_prevent_auth(self, request) -> bool:
# If the user is already authenticated that maybe because of two reasons:
# A) He has this URL in two browser windows and in the other one he
# has already initiated the authenticated session.
# B) He comes from a view that (incorrectly) send him here because
# he does not have enough permissions. That view should have shown
# an authorization error in the first place.
# We can only make one thing here and that is configurable with the
# SAML_IGNORE_AUTHENTICATED_USERS_ON_LOGIN setting. If that setting
# is True (default value) we will redirect him to the next_path path.
# Otherwise, we will show an (configurable) authorization error.
if request.user.is_authenticated:
return request.user.is_authenticated

def get(self, request, *args, **kwargs):
logger.debug("Login process started")
next_path = self.get_next_path(request)

if self.should_prevent_auth(request):
# If the SAML_IGNORE_AUTHENTICATED_USERS_ON_LOGIN setting is True
# (default value), redirect to the next_path. Otherwise, show a
# configurable authorization error.
if get_custom_setting("SAML_IGNORE_AUTHENTICATED_USERS_ON_LOGIN", True):
return HttpResponseRedirect(next_path)
logger.debug("User is already logged in")
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.7",
version="1.5.8",
description="pysaml2 integration for Django",
long_description=read("README.md"),
long_description_content_type="text/markdown",
Expand Down

0 comments on commit fb5554c

Please sign in to comment.