Skip to content

Commit 23e7df7

Browse files
committed
Merge branch 'dev' of https://github.com/knaperek/djangosaml2 into dev
2 parents 86fc56d + 7950d20 commit 23e7df7

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

djangosaml2/backends.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ def authenticate(self, request, session_info=None, attribute_mapping=None, creat
140140
if user is not None:
141141
user = self._update_user(
142142
user, attributes, attribute_mapping, force_save=created)
143-
144-
return user
143+
144+
if self.user_can_authenticate(user):
145+
return user
145146

146147
def _update_user(self, user, attributes: dict, attribute_mapping: dict, force_save: bool = False):
147148
""" Update a user with a set of attributes and returns the updated user.
@@ -197,6 +198,14 @@ def is_authorized(self, attributes: dict, attribute_mapping: dict, idp_entityid:
197198
""" Hook to allow custom authorization policies based on SAML attributes. True by default. """
198199
return True
199200

201+
def user_can_authenticate(self, user) -> bool:
202+
"""
203+
Reject users with is_active=False. Custom user models that don't have
204+
that attribute are allowed.
205+
"""
206+
is_active = getattr(user, 'is_active', None)
207+
return is_active or is_active is None
208+
200209
def clean_user_main_attribute(self, main_attribute: Any) -> Any:
201210
""" Hook to clean the extracted user-identifying value. No-op by default. """
202211
return main_attribute

djangosaml2/views.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,10 @@ def post_login_hook(self, request: HttpRequest, user: settings.AUTH_USER_MODEL,
439439
def build_relay_state(self) -> str:
440440
""" The relay state is a URL used to redirect the user to the view where they came from.
441441
"""
442+
login_redirect_url = get_custom_setting('LOGIN_REDIRECT_URL', '/')
442443
default_relay_state = get_custom_setting(
443-
'ACS_DEFAULT_REDIRECT_URL', settings.LOGIN_REDIRECT_URL)
444-
relay_state = self.request.POST.get('RelayState', '/')
444+
'ACS_DEFAULT_REDIRECT_URL', login_redirect_url)
445+
relay_state = self.request.POST.get('RelayState', default_relay_state)
445446
relay_state = self.customize_relay_state(relay_state)
446447
if not relay_state:
447448
logger.warning('The RelayState parameter exists but is empty')
@@ -505,7 +506,7 @@ def get(self, request, *args, **kwargs):
505506
'Error Handled - SLO not supported by IDP: {}'.format(exp))
506507
auth.logout(request)
507508
state.sync()
508-
return HttpResponseRedirect(getattr(settings, 'LOGOUT_REDIRECT_URL', '/'))
509+
return self.handle_unsupported_slo_exception(request, exp)
509510

510511
auth.logout(request)
511512
state.sync()
@@ -541,6 +542,15 @@ def get(self, request, *args, **kwargs):
541542
'Could not logout because there only the HTTP_REDIRECT is supported')
542543
return HttpResponseServerError('Logout Binding not supported')
543544

545+
def handle_unsupported_slo_exception(self, request, exception, *args, **kwargs):
546+
""" Subclasses may override this method to implement custom logic for
547+
handling logout errors. Redirects to LOGOUT_REDIRECT_URL by default.
548+
549+
For example, a site may want to perform additional logic and redirect
550+
users somewhere other than the LOGOUT_REDIRECT_URL.
551+
"""
552+
return HttpResponseRedirect(getattr(settings, 'LOGOUT_REDIRECT_URL', '/'))
553+
544554

545555
@method_decorator(csrf_exempt, name='dispatch')
546556
class LogoutView(SPConfigMixin, View):

0 commit comments

Comments
 (0)