-
Notifications
You must be signed in to change notification settings - Fork 695
feat(auth): Revoke refresh token on password change #928
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
Changes from all commits
14f4e61
71f6d7d
c6708d1
6e484a7
c1fd2ea
670baff
7d2d28a
b6ed585
ea764f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| from .models import TokenUser | ||
| from .settings import api_settings | ||
| from .tokens import RefreshToken, SlidingToken, Token, UntypedToken | ||
| from .utils import get_md5_hash_password | ||
|
|
||
| AuthUser = TypeVar("AuthUser", AbstractBaseUser, TokenUser) | ||
|
|
||
|
|
@@ -105,27 +106,51 @@ class TokenRefreshSerializer(serializers.Serializer): | |
| token_class = RefreshToken | ||
|
|
||
| default_error_messages = { | ||
| "no_active_account": _("No active account found for the given token.") | ||
| "no_active_account": _("No active account found for the given token."), | ||
| "password_changed": _("The user's password has been changed."), | ||
| } | ||
|
|
||
| def validate(self, attrs: dict[str, Any]) -> dict[str, str]: | ||
| refresh = self.token_class(attrs["refresh"]) | ||
|
|
||
| user_id = refresh.payload.get(api_settings.USER_ID_CLAIM, None) | ||
| if user_id: | ||
| user = ( | ||
| get_user_model() | ||
| .objects.filter(**{api_settings.USER_ID_FIELD: user_id}) | ||
| .first() | ||
| ) | ||
| if not user or not api_settings.USER_AUTHENTICATION_RULE(user): | ||
| try: | ||
| user = get_user_model().objects.get( | ||
| **{api_settings.USER_ID_FIELD: user_id} | ||
| ) | ||
| except get_user_model().DoesNotExist: | ||
| # This handles the case where the user has been deleted. | ||
| raise AuthenticationFailed( | ||
| self.error_messages["no_active_account"], | ||
| "no_active_account", | ||
| self.error_messages["no_active_account"], "no_active_account" | ||
| ) | ||
|
|
||
| data = {"access": str(refresh.access_token)} | ||
| if not api_settings.USER_AUTHENTICATION_RULE(user): | ||
| raise AuthenticationFailed( | ||
| self.error_messages["no_active_account"], "no_active_account" | ||
| ) | ||
|
Comment on lines
-116
to
+131
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. I think your changes are the try/except; please ensure it uses the code from #861 to be more DRY 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. Hi Andrew, Thanks for the feedback. I considered the suggestion to use In contrast, |
||
|
|
||
| if api_settings.CHECK_REVOKE_TOKEN: | ||
| if refresh.payload.get( | ||
| api_settings.REVOKE_TOKEN_CLAIM | ||
| ) != get_md5_hash_password(user.password): | ||
| # If the password has changed, we blacklist the token | ||
| # to prevent any further use. | ||
| if ( | ||
| "rest_framework_simplejwt.token_blacklist" | ||
| in settings.INSTALLED_APPS | ||
| ): | ||
| try: | ||
| refresh.blacklist() | ||
| except AttributeError: | ||
| pass | ||
|
|
||
| raise AuthenticationFailed( | ||
| self.error_messages["password_changed"], | ||
| code="password_changed", | ||
| ) | ||
|
|
||
| data = {"access": str(refresh.access_token)} | ||
| if api_settings.ROTATE_REFRESH_TOKENS: | ||
| if api_settings.BLACKLIST_AFTER_ROTATION: | ||
| try: | ||
|
|
@@ -150,8 +175,49 @@ class TokenRefreshSlidingSerializer(serializers.Serializer): | |
| token = serializers.CharField() | ||
| token_class = SlidingToken | ||
|
|
||
| default_error_messages = { | ||
| "no_active_account": _("No active account found for the given token."), | ||
| "password_changed": _("The user's password has been changed."), | ||
| } | ||
|
|
||
| def validate(self, attrs: dict[str, Any]) -> dict[str, str]: | ||
| token = self.token_class(attrs["token"]) | ||
| user_id = token.payload.get(api_settings.USER_ID_CLAIM, None) | ||
| if user_id: | ||
| try: | ||
| user = get_user_model().objects.get( | ||
| **{api_settings.USER_ID_FIELD: user_id} | ||
| ) | ||
| except get_user_model().DoesNotExist: | ||
| # This handles the case where the user has been deleted. | ||
| raise AuthenticationFailed( | ||
| self.error_messages["no_active_account"], "no_active_account" | ||
| ) | ||
|
|
||
| if not api_settings.USER_AUTHENTICATION_RULE(user): | ||
| raise AuthenticationFailed( | ||
| self.error_messages["no_active_account"], "no_active_account" | ||
| ) | ||
|
|
||
| if api_settings.CHECK_REVOKE_TOKEN: | ||
| if token.payload.get( | ||
| api_settings.REVOKE_TOKEN_CLAIM | ||
| ) != get_md5_hash_password(user.password): | ||
| # If the password has changed, we blacklist the token | ||
| # to prevent any further use. | ||
| if ( | ||
| "rest_framework_simplejwt.token_blacklist" | ||
| in settings.INSTALLED_APPS | ||
| ): | ||
| try: | ||
| token.blacklist() | ||
| except AttributeError: | ||
| pass | ||
mahdirahimi1999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| raise AuthenticationFailed( | ||
| self.error_messages["password_changed"], | ||
| code="password_changed", | ||
| ) | ||
|
|
||
| # Check that the timestamp in the "refresh_exp" claim has not | ||
| # passed | ||
|
|
||
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.
re-run the locale generation since this is a new gettext (i.e. the line where the gettext is written has moved)
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.
I noticed that the project uses a custom script (scripts/i18n_updater.py) for locale updates, likely to avoid unnecessary changes in translation files. Since this is a multi-language project, I won't run makemessages or compilemessages directly. Please let me know if you prefer me to run the custom script or if locale updates are handled separately by maintainers.
Uh oh!
There was an error while loading. Please reload this page.
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.
Update: I ran the
i18n_updater.pyscript, but it generated no changes.This is because the
The user's password has been changed.string already exists in the translation catalog from a previous PR (originating fromauthentication.py:146), somakemessagescorrectly finds no new strings to add. No further action should be needed.