Skip to content
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

Change token in session #653

Open
devmitrandir opened this issue Jun 2, 2024 · 2 comments
Open

Change token in session #653

devmitrandir opened this issue Jun 2, 2024 · 2 comments

Comments

@devmitrandir
Copy link

Hello!

I don't need to add a token to the store.
I want to set the updated token in request.session.
Is it possible?

I use this demo https://github.com/authlib/demo-oauth-client/blob/master/fastapi-twitter-login/app.py

async def update_token(token, refresh_token=None, access_token=None):
    # I want this
    # request.session['token'] = token
    await asyncio.sleep(0)

oauth = OAuth()
oauth.register(
    ...,
    update_token=update_token
)
@codespearhead
Copy link

I don't need to add a token to the store.

Does this answer your question?

Accessing OAuth Resources
-------------------------
.. note::
If your application ONLY needs login via 3rd party services like
Twitter, Google, Facebook and GitHub to login, you DON'T need to
create the token database.
There are also chances that you need to access your user's 3rd party
OAuth provider resources. For instance, you want to display the logged
in user's twitter time line and GitHub repositories. You will use
**access token** to fetch the resources::
def get_twitter_tweets(request):
token = OAuth1Token.find(
name='twitter',
user=request.user
)
# API URL: https://api.twitter.com/1.1/statuses/user_timeline.json
resp = oauth.twitter.get('statuses/user_timeline.json', token=token.to_token())
resp.raise_for_status()
return resp.json()
def get_github_repositories(request):
token = OAuth2Token.find(
name='github',
user=request.user
)
# API URL: https://api.github.com/user/repos
resp = oauth.github.get('user/repos', token=token.to_token())
resp.raise_for_status()
return resp.json()
In this case, we need a place to store the access token in order to use
it later. Usually we will save the token into database. In the previous
**Routes for Authorization** ``authorize`` part, we can save the token into
database.

Not having a single source of truth will make handling the token, especially its lifecycle management, very hard, unless you make that session a global variable, which is an equally bad practice.

@devmitrandir
Copy link
Author

@codespearhead

Thanks for the answer!

I'm using only gitlab oauth2.
I just need to submit requests to gitlab through my application.

This is a simple application that should create reports for the user.
Is it possible to manage without a database?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants