Search code examples
pythonflaskauthlib

Python authlib flask - how to handle refresh token?


I only need oauth2 for login so far, but I feel that to be reasonably complete, my app should still handle refresh tokens.

I'm fairly new to oauth, so here is my understanding:
By keeping access token lifetime short, but refresh token lifetime long, you force the client to "check in" regularly to renew the access token, and thereby maintain more control.

But how to actually do that using authlib and the flask integration?
There seems to be no refresh_token() on the FlaskRemoteApp, and I have not been able to find any example code showing this.

This seems to work to get a new token

res = oauth.myOauth2.fetch_access_token(refresh_token=session['tok_res']['refresh_token'])
session['tok_res'].update(res)

But fails when subsequently using the new access_token for an api call.
Could be a server error I suppose, or maybe I need to wait for the old token to expire before using the new one? The expires_at time, keeps updating and that makes no sense to me in that case.

It would be great if somebody could explain how refresh_token is intended to be used with authlib and flask.


Solution

  • Well, unless @lepture drops by with the answer, I have at least an answer.
    I was simply missing grant_type='refresh_token' from my call.
    So this now works as expected for me.

    if session['oatoken']['expires_at'] - 2 < now:  # refresh 2 seconds early
        oatoken = oauth.myOauth2.fetch_access_token(
            refresh_token=session['oatoken']['refresh_token'],
            grant_type='refresh_token')
        session['oatoken'].update(oatoken)
    

    I played around a little with setting the update_token function in the registry, but was not able to make it work. In any case I believe this is only triggered if an api call using the token receives an "expired token" reply. In my case I'm only using Oauth for login, so I can just watch the expires_at time, and only hit the auth server when necessary.