5

I'm very beginner with authlib and trying to understand its concepts.

I try to understand, how can I save and reuse fetched tokens with authlib.

I created small FastAPI project:

from fastapi import FastAPI
from starlette.config import Config
from starlette.middleware.sessions import SessionMiddleware
from starlette.requests import Request
from authlib.integrations.starlette_client import OAuth


app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key="some-random-secret")

config = Config(".env")
oauth = OAuth(config)

oauth.register(
    name="some_service",
    client_id="client_id",
    client_secret="client_secret",
    authorize_url="https://some-service.com/auth",
    access_token_url="https://some-service.com/token",
    client_kwargs={
        "token_endpoint_auth_method": "client_secret_post",
    },
)


@app.get("/login")
async def login(request: Request):
    redirect_uri = "https://myservice.com/auth"
    return await oauth.some_service.authorize_redirect(request, redirect_uri)


@app.get("/auth")
async def auth(request: Request):
    token = await oauth.some_service.authorize_access_token(request)
    # I suppose that I should save somehow token here
    return token


@app.get("/account")
async def get_account(request: Request):
    account_url = "https://some-service.com/account"
    resp = await oauth.some_service.get(account_url)
    return resp.json()

I want to get account info. So, further steps will be:

  1. GET /login

I'm giving access to use my account and will be redirected back to my service.

  1. GET /auth?oauth_params1=foo&oauth_params2=bar

There will be fetched tokens from token provider. I know that I'm wrongly supposing that token will somehow saved somewhere.

  1. GET /account

And there I'm expecting that with OAuth client I can send previously fetched token. But, I'm getting next error:

authlib.integrations.base_client.errors.MissingTokenError: missing_token:

I also know that I should provide token like that:

oauth.some_service.get(account_url, token=previously_fetched_token)

But, I don't want to ask every time token from some-service I want to reuse token. How to do that?

Am I wrong that this issue is the part of authlib scope? Should I find solution with cache or database mechanisms?

p.s.: I'm really beginner with FastAPI too...

catscoolzhyk
  • 675
  • 10
  • 29

1 Answers1

-1

The token is an object with several values-

{
  "oauth_token": "TOKEN ID",
  "oauth_token_secret": "SECRET TOKEN",
  "user_id": "USER ID",
  "screen_name": "USER SCREEN NAME"
}

You have several options-

  • Use a database model that has those values. Use the "user_id" as the primary key, as the "screen_name" can be changed by users.
  • JSON encode the whole object and stash it somewhere.
  • Shove it into a cookie object so it's sent back with each request. The nice part of this is you don't have to worry about storing the oauth token at all, but it means you can't do anything with it outside of user requests.
Robert Hafner
  • 3,364
  • 18
  • 23