codemation / easyauth

Create a centralized Authentication and Authorization token server. Easily secure FastAPI endpoints based on Users, Groups, Roles or Permissions with very little database usage.
https://easyauth.readthedocs.io/en/latest/
MIT License
553 stars 52 forks source link

Update token expiry or refresh token #104

Open aghure opened 1 year ago

aghure commented 1 year ago

How i can refresh google auth token ? If the user is active , i want to refresh the token and update the token expiry. If in get_user i check the token time and call issue token again , will it work?

` def get_user_handler(request: Request): if "token" not in request.cookies and "Authorization" not in request.headers: return None

    if "token" in request.cookies and request.cookies['token'] != "INVALID":
        token = request.cookies['token']
        user_id = get_user_from_token(token)
        //Here i will check the expiry and if the expiry is less than 5 mins will  call **self.issue_token(permissions)**
        return  user_id

    elif "Authorization" in request.headers:
        # header should be separated by 'Bearer <tokenstr>'
        decoded_token = jwt.decode(
            request.headers["Authorization"].split(" ")[1],
            options={"verify_signature": False},
        )
        return decoded_token["permissions"]["users"][0]
    return None

return Depends(get_user_handler)`
codemation commented 11 months ago

@aghure that is one way to accomplish this manually, but if you want this to automatically be updated in cookies, you'll need to specify that in the response headers manually. I think adapting a simple refresh token endpoint could also be quite easy,