Use pyjwt for API tokens instead of itsdangerous's deprecated code #6267
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.
Status
Ready for review
Description of Changes
itsdangerous deprecated TimedJSONWebSignatureSerializer in 2.0 and has
entirely removed it in 2.1, so we need to switch to something else. They
recommended authlib, except that's a pretty large general-purpose
authentication library.
pyjwt is a smallish (1k LOC) library that just provides JWT/JWS
functionality. For the HS512 algorithm, it uses the same implementation
from hashlib in the Python standard library as itsdangerous did.
Tests are provided to verify successful operation as well as different
failure modes, such as invalid tokens, incorrect secret key, wrong
algorithm, and lack of an expiry.
Tokens generated by itsdangerous are not decodable by pyjwt because
itsdangerous passed the expiry as a header while pyjwt includes the
expiry in the body/payload itself. This shouldn't be a significant issue
since these tokens already expire after 8 hours.
Fixes #6224.
Testing
curl -X POST -d '{"username":"journalist", "passphrase":"correct horse battery staple profanity oil chewy","one_time_code":"XXXXXX"}' -H "Content-Type: application/json" http://127.0.0.1:8081/api/v1/token
. Then try it against an API endpoint:curl -H 'Authorization: Token {token}' http://127.0.0.1:8081/api/v1/users
and get a proper response back.revoked_tokens
database table and try to use it against the API, e.g.curl -H 'Authorization: Token {token}' http://127.0.0.1:8081/api/v1/users
. You should get an error message saying the token is invalid or expired.10
(seconds). Get a valid token using the first curl command, then wait 10 seconds. Try making an API request using that token, and get a response back saying the token is invalid or expired.Deployment
Any special considerations for deployment? Yes-ish.
Existing API tokens will no longer be valid and need to be reissued. This shouldn't be a big deal since they only live for 8 hours anyways.
Checklist
If you made changes to the server application code:
make lint
) and tests (make test
) pass in the development containerIf you added or updated a production code dependency: