Skip to content

Add backend.user_can_authenticate() to allow for additional checks #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions djangosaml2/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ def authenticate(self, request, session_info=None, attribute_mapping=None, creat
if user is not None:
user = self._update_user(
user, attributes, attribute_mapping, force_save=created)

return user

if self.user_can_authenticate(user):
return user

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

def user_can_authenticate(self, user) -> bool:
"""
Reject users with is_active=False. Custom user models that don't have
that attribute are allowed.
"""
is_active = getattr(user, 'is_active', None)
return is_active or is_active is None

def clean_user_main_attribute(self, main_attribute: Any) -> Any:
""" Hook to clean the extracted user-identifying value. No-op by default. """
return main_attribute
Expand Down