-
Notifications
You must be signed in to change notification settings - Fork 56
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
Added automatic access_token refresh when they expire. #346
Conversation
9d75d30
to
8b92cf5
Compare
Since checks are turning red due to the missing jwt library, am trying to add the correct one. |
I added https://pyjwt.readthedocs.io , which I import as jwt - I'm not sure how to declare it properly in the requirements file. |
Thank you for your contribution @stevenpince. We are pretty busy at the moment but your code will be merged as soon as we have availability. @EvanBldy please include this merge in your todo-list. |
Hi @stevenpince, good idea thanks! For pyjwt: |
Ok, I am traveling at the moment - will incorporate these changes. |
13adf31
to
8409b35
Compare
Ok, looks like Python 2.7 is breaking because datetime.datetime.timestamp is not available. Let me check if there's a clean way to resolve. Last version of 2.7 is 5.5 years old at this point. What software packages still ship it? |
We need to keep Gazu compatible with Python 2.7 for now, some of our clients use some DCC/pipeline that need it. |
I understand you want to stay Python 2.7 compatible, but datetime.datetime.timestamp was added in 3.3. I tried running a local Python 2.7 environment, but it was just breaking too much for me (requests_mock depends on urllib.parse, which is also not available). I've gated the functionality for Python3 only. It won't break Python 2. |
@EvanBldy Can you review the last changes? |
1ea8413
to
c55e0a7
Compare
I'm sorry, but I finally don't get the point of parsing the JWT expiration date. It breaks what I made before and knowing from the server if the JWT is expired is not a problem (see here: https://datatracker.ietf.org/doc/html/rfc6749#section-1.5). Also, this is not compatible with Python 2.7 and it will parse the token / check for expiration at each Gazu request. Nevertheless I kept some interesting part of the code. |
c55e0a7
to
0430871
Compare
0430871
to
0f4b692
Compare
0f4b692
to
09fe0f8
Compare
Problem
The added functionality in #326 (automatic refresh after specific HTTP status code in Zou's response) is wasteful as it puts the responsibility of tracking the access_token's expiration on the Zou server.
Solution
I added a properties to the Client object, that makes access to "access_token" and "refresh_token" easier, as well as the property "access_token_has_expired", that decodes the given JWT access token to determine it's expiration date.
The property is checked in the make_auth_header call, which I have also move to the Client object for a more clear separation of concerns.
If the client's automatic_refresh_token is set to True, the refresh_token (renamed to "refresh_authentication_tokens") method is called to refresh authentication before creating the header - thus theoretically bypassing any possible future NotAuthenticatedError.
This does add the python module "jwt" to the requirements of Gazu - which I haven't added yet to this change.
As far as I can tell, this module should be available from Python 3.6+ - let's check the options
Unittests have been added to showcase the use of these changes and all checks are green.