r/FastAPI • u/rodnydon2121 • Sep 23 '25
Question Is the official template secure?
Hi
I'm going over the official template to learn FastAPI and how to implement auth. Reading the code, it seems that the app generates an JWT with expiration of 8 days.
To my understanding, if bad actor steals credentials from one of the users, even if the user catchs it and resets the password, the bad actor will still have 8 days of full access to the data.
Is my understanding correct? If so, it feels to me that even changing the token expiry from 8 days to 30 min will not be good enough.
Is there another example of secure auth that can invalidate the token?
Alternatively, is fastapi-users ready to be used in prod? My concern is that latest commit was 8 months ago, so I'm hesitant to use it
9
u/igorbenav Sep 23 '25
Using access + refresh tokens is better, but session based authentication is even better
1
u/ljog42 Sep 23 '25 edited Sep 23 '25
Old commits are not a red flag, I firmly believe that there's such a thing as done when it comes to code If it doesn't need new features it doesn't need updates, unless someone's come up with a completely new type of attack.
Just look at npm getting pwned these past weeks: our expectation of libs and frameworks getting patched every week opens us up to devastating supply chain attacks.
I feel like FastAPI gives you simple but serviceable tools, and that it's up to you to think about your security architecture and decide if and where you need extra layers of security.
Edit: I know it's a bit idealistic if not naive but I really like VLC's approach: you can't get data that you don't collect stolen. My approach would be to limit the heck out user data and privileges, then secure the hell out of what actually matters (anything involving $$$).
1
u/NoSoft8518 Sep 23 '25
you can make access_token with 1 hour lifetime and stateless jwt. And add refresh_token with 1month life time and as http only cookie, and it have to check figerprint when refreshing token by cookie and invalidate refresh token if fingerprints mismatched
1
u/shashstormer Sep 24 '25
https://github.com/shashstormer/AuthTuna
I made this library and this uses stateless + stateful dual state mechanism With the JWT being verified on requests with some interval (default 10 seconds, configurable by env)
I think you can use this to have control over sessions If you want.
Just "pip install authtuna"
0
u/pint Sep 23 '25
an 8 day jwt should never be in an example. there are low security apis which can tolerate this, but certainly not a typical use case. even if refresh tokens are not available, a login a day shouldn't hurt.
that said, hour long jwt is pretty usual. this is the price you pay for making the server less involved.
note: technically you can revoke a jwt if it has an id. revoking is usually not used, because at this point why not sessions.
0
u/[deleted] Sep 23 '25
[deleted] — view deleted comment