Skip to content

Commit 5c52521

Browse files
authored
Add backend.user_can_authenticate() to allow for additional checks (#254)
This is the same pattern used in django's auth backend - in fact, I just copied the method verbatim: https://github.com/django/django/blob/master/django/contrib/auth/backends.py#L51-L57
1 parent 5ab385b commit 5c52521

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
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

0 commit comments

Comments
 (0)