-
-
Notifications
You must be signed in to change notification settings - Fork 212
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
Ensure old tokens continue to work by removing token_key
#362
Conversation
token_key
@giovannicimolin @johnraz This ended up being a bigger change than expected but this gives the project both a performance boost and solves the issue of invalidated tokens. It's my first contribution here so happy to address any feedback. |
@jmsmkn Adding this to my review queue for next week. Thanks for the contribution and for the bug fix! 🚀 |
@jmsmkn Hey! I reviewed your PR and it's mostly looking good, but I wasn't able to properly test it because there's a few things messed up in the library right now. I'll get back to this and merge it as soon as I can figure out what's wrong. |
This would be great to add in as token invalidation is what's stopping me from upgrading to v5. Is this fix still viable? |
Echoing @Fireclunge 's comment, the invalidation of existing tokens is a non-starter for us when upgrading, unfortunately. tldr; it seems like I agree with @jmsmkn 's assessment that Before #272 , this is what hash_token looked like:
The important bit is the
In #272 , the addition of a token prefix resulted in an additional step of ensuring the token string is hex-compatible. This is reasonable, as tokens like
However, the actual implementation of
And with a token prefix present, it does work, but it's still just the bytes() version of the token str:
All that said,
Again, this still works, because
NOTE: This of course assumes you are using the default (Sorry for the long reply, here's my proposed solution, which I think could slot nicely into this PR) Update
Adding this check should be lightweight performance-wise and it maintains backwards compatibility for existing tokens (while supporting newer tokens that might use the token_prefix option). @giovannicimolin let me know what you think, happy to help support this effort in this PR (if @jmsmkn is interested?) or break it out separately. Also happy to help write tests and whatnot. |
Great find, @mr-niche! I'm unsure of the best way to proceed with this. Even though I was given the old "PRs welcome" treatment when I first raised the backward compatibility issue, it's been two months since I submitted a PR, and I haven't received any feedback. I spent a weekend working on improving the library, but without input from the maintainers, it's hard to move forward. If you'd like to make a PR to my branch to consolidate our efforts, I'd be happy to merge it. However, we ultimately need some guidance from the maintainers to get this resolved. |
Thanks @jmsmkn ! I'll give @giovannicimolin some time to weigh in on how to proceed. If we get a thumbs up, I'll go ahead and submit a PR to your branch and we can get this ball rolling again. I'll also include some documentation around "Upgrading from v4 -> v5" so that folks who have raised this concern before (i.e. in #356 ) can see if the upgrade path is viable for their use case. |
@mr-niche Thanks for the great in-depth investigation of the issue! @jmsmkn Can you incorporate @mr-niche's changes into your PR and resolve the PR conflicts? Let's make this the 5.1 version - lots of folks will be happy with the library being backwards compatible. |
Thanks @giovannicimolin ! @jmsmkn , I'll take a stab at adding this to your PR, I have a little bit of time today. |
Thanks @giovannicimolin ! I have fixed the conflicts. One question: we could still keep around |
@jmsmkn That makes sense, can you keep it around for now? I'll take some time to test it out tomorrow. :) |
@giovannicimolin my changes are in this PR (to be merged into @jmsmkn 's PR) https://github.com/jmsmkn/django-rest-knox/pull/1 We need some guidance on one last consideration: if we switch back to the original I'm not sure what the right thing to do here is, we could either:
Option 2 feels risky. But option 1 is yet another breaking change (albeit, a potentially smaller one - if folks have already upgraded to 5.0.*, they might not be as concerned about breaking changes, but I don't necessarily want to make that assumption). Let me know what you think/if you have any other ideas here! |
Thank you both for your efforts with this. I pieced together bits of your work and created a custom authentication class to allow this to work for me. A quick summary for anyone that wants to upgrade but has doubts that it will be merged into main. from knox.auth import TokenAuthentication
class TokenAuthenticationOverride(TokenAuthentication):
"""
Extension of the TokenAuthentication class
"""
def _legacy_make_hex_compatible(self, token: str) -> bytes:
"""
Ensure a token, which may contain a TOKEN_PREFIX, is hex-compatible before hashing.
Reduce this down to return binascii.unhexlify(token) if you did not use a prefix
"""
try:
# this supports tokens generated in v4.2 and any tokens which do not contain a TOKEN_PREFIX
return binascii.unhexlify(token)
except (binascii.Error, ValueError):
# if a token has a prefix, encode it so that it's hex-compatible and can be hashed
return binascii.hexlify(token.encode('utf-8'))
def _authenticate_legacy_credentials(self, token):
# Allows backward compatability with 4.2.0 tokens
# Tokens that have expired will be deleted and skipped
msg = _('Invalid token.')
token = token.decode("utf-8")
try:
digest = hash_func()
digest.update(self._legacy_make_hex_compatible(token))
digest = digest.hexdigest()
except (TypeError, binascii.Error):
raise exceptions.AuthenticationFailed(msg)
try:
auth_token = get_token_model().objects.get(digest=digest)
except get_token_model().DoesNotExist:
raise exceptions.AuthenticationFailed(msg)
if self._cleanup_token(auth_token):
raise exceptions.AuthenticationFailed(msg)
if knox_settings.AUTO_REFRESH and auth_token.expiry:
self.renew_token(auth_token)
return self.validate_user(auth_token)
def authenticate_credentials(self, token):
# Allows backward compatability with 4.2.0 tokens
# https://github.com/jazzband/django-rest-knox/pull/362/files
# Tokens that have expired will be deleted and skipped
try:
# Use new method first - All new tokens after upgrade will use this
return super().authenticate_credentials(token) # Knox 5.0.0+ tokens
except AuthenticationFailed:
return self._authenticate_legacy_credentials(token) # Old 4.2.0 tokens I was able to reduce make_hex_compatible(token) to binascii.unhexlify(token) in my application to remove the need for the extra function. The idea is to deprecate 4.2.0 tokens when an acceptable level of users are using the new tokens. Unfortunately there will be a small performance hit while this is churning through. I would suggest that this thread should be more visible and not be closed, but it doesn't appear like many people actually seem to care about this? 🫠😂 It's a huge L for the library IMO - Just casually logging out all your users, no big deal. Absolutely nightmarish for B2C websites with a large volume of users |
Sweet, thanks for following up on this @Fireclunge ! This is a nice option for rolling forward - the performance hit is a bummer, but I'm glad you found an option that works for you. @giovannicimolin , if this work isn't going to be supported/merged, I think the snippet above should be visible or linked in the README or CHANGELOG to highlight this as an "upgrade path" for 4.2 -> 5. Additionally, the performance improvement @jmsmkn suggested (removing the Thanks! |
While I appreciate the work done here and understand the concern, I think taking a step back is important. Asking customers to re-login or even integrators of an API to update their token (with a reasonable delay to do so ofc) is by no means the end of the world. Rotating a token on a regular basis should be considered a good practice anyway - I mean, this all process shouldn’t be « nightmarish » and if it is you should consider discussing this with your product / customer success team more than with us IMO 😉 Sorry if this sound like a rant, it’s really not, but the « drama » around change in our field is sometimes getting under my skin 😂 Cheers ! |
Of course token rotation is a good practice, no one is suggesting otherwise. However, the moment that occurs should be dictated by a library as it requires synchronisation across the application. It certainly shouldn't occur simultaneously with a dependency update, especially without a migration strategy in place. It would be absurd if Django invalidated everyone's passwords after a minor version update, and the same principle applies here. |
I’m not discussing the fact that we screwed up by introducing a breaking change by mistake without doing a major bump and that it should be avoided in the future. I’m trying to bring some nuance to the topic which looking at your answer is going to be a difficult task 🤣 Also comparing this lib to Django is … well a bit absurd on its own… We don’t have the same kind of community to back the project or QA process… We don’t even have a decent test suite to begin with. I just wanted to underline that the situation is not THAT bad, that’s all. |
With all due respect we've all gotten together in this thread precisisely so that the "process isn't nightmarish" and to share solutions so that we tackle this as a community. This might shock you but a lot of people don't know or trust themselves enough in hashing and crytography knowledge to go around poking and amending internals of an authentication library that is to be used in production. Thank god to @jmsmkn and @mr-niche for the work they put into this because I honestly would'nt even know where to start - all I did was provide a quick copy-pastable overview because I think we got the hint that this was never going to be merged in. You talk as though key rotation is a good practice and should be discussed with your "product / customer success team", completely forgetting that many of us ARE the ****ing product team. - and we have decided that invalidating all tokens in one swift move is a complete no go and we've bashed heads here to provide a gradual solution. It;s quite frustrating especially because this library is specifically recommended by the Django Rest Framework community ( https://www.django-rest-framework.org/api-guide/authentication/#third-party-packages ) and there's a lot of people that put this library in high regard. Appreciation to yourself too for all the work you've put into this - but goddam it highly encapsulates why developers wind business people up. |
Did I say I value the work being done in this thread? Yes, I’m happy to see people looking for a work around. Do I think people are overreacting a bit? Yes, as I said I just wanted to bring some nuance to the topic as I (as in my own opinion, don’t have to be yours) don’t think it’s such a big deal. At no point did I try to start a flame war… I would be happy to discuss such cases where it really so badly impact the business to a point where it’s a massive nogo and increase my knowledge there. Now on a more ethical side which has nothing to do with the original discussion but I just can’t let it slip: « developers wind business people up » It is really offensive. This type of comment and your general tone in your message is the reason why people get burned out with open source, and no this is not me being sensitive, please read about it, it is a real curse. |
This PR adds a test that ensures old tokens continue to work by removing
token_key
. See discussion in #358 and #356.When each token had its own salt
hash_token
needed to be done per token withhash_token(token, auth_token.salt)
.token_key
was introduced in 913336d so that the comparison could then be done on a smaller number of tokens.However, the per-token salt was removed in 51a5204. That means that we can now move
hash_token
out of the filter loop ofauthenticate
as we no longer have to passauth_token.salt
, we only pass thetoken
sent by the user. That gives us thedigest
straight away, meaning that we can do a direct lookup ondigest
(the primary key, so unique) to find the relevant token.This also has the benefit of improving performance as multiple hashes and comparisons no longer need to be made, and solves the problem of tokens being invalidated between versions 4.2.0 -> 5.0.0. Additionally,
MAXIMUM_TOKEN_PREFIX_LENGTH
is no longer bound byTOKEN_KEY_LENGTH
so could be increased if you wish.