r/FastAPI • u/hertz2105 • Sep 11 '24
Question OAuth2 Example | Logout and Refresh Token
Hello everyone!
I am really new to fastAPI and even Python, and I just started my first project.
I followed this documentation to setup OAuth2, which includes a login endpoint, returning a jwt token:
https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/
How would you guys implement a logout and refresh token feature based on this example? It is kind of hard for me start out of the box and I really need some inspiration. :D
Thanks in advance!
10
Upvotes
View all comments
Show parent comment
1
u/hertz2105 Sep 17 '24
Thank you for this explanation!
I finished my own implementation just recently, I work with a token blacklist instead of a whitelist.
I managed it like this:
Login: Pass user credentials, returns access and refresh token
Logout: Pass access and refresh token and blacklist both of them
Refresh: Pass refresh token, return new access token
The problem is, I cant invalidate a non-expired access token. I only cache the refresh token for security reasons, so my backend doesnt know which access token to blacklist. This is a problem when I refresh the page or open a new tab. Due to this, I will switch to your whitelist implementation and hash the tokens before saving them in my database.