-
Notifications
You must be signed in to change notification settings - Fork 672
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
Add option to allow inactive user authentication and token generation #834
Merged
Andrew-Chen-Wang
merged 8 commits into
jazzband:master
from
zxkeyy:feature/allow-inactive-user-authentication
Dec 23, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
14bc650
Feat: Add authentication class and authentication rule that allows in…
zxkeyy 731d489
Test: Add tests for inactive user authentication and serializer valid…
zxkeyy 6d22c21
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] df7c68a
Add check if user is active setting,
zxkeyy 7789c87
Add test for retrieving inactive user in JWT authentication
zxkeyy adefffe
Use new setting in serializer test
zxkeyy 18f62dc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8ab5c92
Update get_user to use new setting and remove unnecessary class
zxkeyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,7 +131,8 @@ | |
except self.user_model.DoesNotExist: | ||
raise AuthenticationFailed(_("User not found"), code="user_not_found") | ||
|
||
if not user.is_active: | ||
# Ensure authentication rule passes | ||
if not api_settings.USER_AUTHENTICATION_RULE(user): | ||
raise AuthenticationFailed(_("User is inactive"), code="user_inactive") | ||
|
||
if api_settings.CHECK_REVOKE_TOKEN: | ||
|
@@ -164,6 +165,37 @@ | |
return api_settings.TOKEN_USER_CLASS(validated_token) | ||
|
||
|
||
class JWTInactiveUserAuthentication(JWTAuthentication): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class shouldn't be needed given the new settings There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops, i missed it. should be good now |
||
""" | ||
An authentication plugin that authenticates requests through a JSON web | ||
token provided in a request header, allowing inactive users to authenticate. | ||
""" | ||
|
||
def get_user(self, validated_token: Token) -> AuthUser: | ||
""" | ||
Attempts to find and return a user using the given validated token. | ||
""" | ||
try: | ||
user_id = validated_token[api_settings.USER_ID_CLAIM] | ||
except KeyError: | ||
raise InvalidToken(_("Token contained no recognizable user identification")) | ||
|
||
try: | ||
user = self.user_model.objects.get(**{api_settings.USER_ID_FIELD: user_id}) | ||
except self.user_model.DoesNotExist: | ||
raise AuthenticationFailed(_("User not found"), code="user_not_found") | ||
|
||
if api_settings.CHECK_REVOKE_TOKEN: | ||
if validated_token.get( | ||
api_settings.REVOKE_TOKEN_CLAIM | ||
) != get_md5_hash_password(user.password): | ||
raise AuthenticationFailed( | ||
_("The user's password has been changed."), code="password_changed" | ||
) | ||
|
||
return user | ||
|
||
|
||
JWTTokenUserAuthentication = JWTStatelessUserAuthentication | ||
|
||
|
||
|
@@ -175,4 +207,6 @@ | |
# `AllowAllUsersModelBackend`. However, we explicitly prevent inactive | ||
# users from authenticating to enforce a reasonable policy and provide | ||
# sensible backwards compatibility with older Django versions. | ||
return user is not None and user.is_active | ||
return user is not None and ( | ||
not api_settings.CHECK_USER_IS_ACTIVE or user.is_active | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we want to only validate the user is active, not run the authentication rule, since this function is just looking to fetch a given user. Good forward thinking, but yes this would break backwards compatibility
We can revisit this if you open a GitHub issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking about it harder i see how it could cause a problem if a user made their own rule, addressed in latest commit.