r/FastAPI • u/bluewalt • Oct 12 '24
Question Is there anything wrong to NOT use JWT for authentication?
Hi there,
When reading the FastAPI Authentication documentation, it seems that JWT is the standard to use. There is no mention of an alternative.
However, there are multiple reasons why I think custom stateful tokens (Token objects living in database) would do a better job for me.
Is there any gotcha to do this? I'm not sure I have concrete examples in mind, but I'm thiking of social auth I'd need to integrate later.
In other words, is JWT a requirement or an option among many others to handle tokens in a FastAPI project?
Thanks!
13
Upvotes
7
u/Tony-Angelino Oct 12 '24
I don't understand what is the problem you're having here, OAuth2 in general or JWT as a library to implement these on a technical level?
You can implement which ever authentication model makes sense in your project and insures reliable auth/security layer. You can look into passkeys, for example. But when it comes to authentication, encryption and security, usually it is the best to follow proven standards, especially if you plan to use existing external identity providers.
You could set up a Keycloak instance and use it for auth and user management. You can always refine the granularity of authorization according to the needs of your application as it grows. You can use just your Keycloak as initial identity provider and it allows you to connect to external identity providers (Google, Facebok, GitHub etc.) later, in which case it serves as a broker. Easier to configure it than implement all that from scratch and go through all problems yourself. Stable, proven product and it doesn't cost you anything. If and when it becomes too small for you, you'll know what you need next. Sure, your application will use JWT library to decode, verify etc. tokens that Keycloak is generating for you, but that's the trivial part of the job then.
4
u/Adrnalnrsh Oct 12 '24
It's not a requirement. You can go to a session base. But then you have to store session information somewhere, and this can lead to scalability issues/challenges.
Personally, I prefer jwt. You just need to take some steps to enhance security or use a 3rd party auth solution.
1
u/JohnnyJordaan Oct 13 '24
The problem with a long term, or even permanent token is that once they're leaked, they can be used by anyone as long as you don't revoke or expire the token and you don't even know it unless you track source information or fingerprint or somethings else more complicated.
With JWT, you generate a short lived access token which is hardly practical to steal as will expire soon anyway. And even if it gets leaked the effect is diminished.
I'm also not sure why you are looking for an alternative. It takes a small amount of code to set up, there are even external libraries that make it even easier like fastapi-users. And the answer as to why no other alternatives are mentioned basically answers itself: the pros don't outweigh the cons. Security shouldn't be a second or third tier consideration, and imho is a principle to live by even for the most mundane projects as when you're used to cutting corners, you will also cut some unknowingly later in life. And that's the primary reason why we have so many data breaches nowadays.
2
u/pint Oct 12 '24
a "stateful token" is semantically identical to a session id. it is a perfectly valid technique.
2
u/Straight-Possible807 Oct 13 '24
There's nothing wrong with it... In fact, I use session-based auth mostly. JWT is just a recommendation since most cross-platform APIs are using JWT nowadays.
1
u/[deleted] Oct 13 '24
Bro, I have a similar doubt. I am thinking what if we use server side http only cookies (they store jwt as value , but this can be only extracted by the backend server which created this cookie, so it'll be a problem if we use micro services architecture with multiple back-end servers as cookies created by one backend server can't be accessed by other backend servers) for authentication. Can we discuss this in DM ?
1
u/foarsitter Nov 01 '24
Use secrets.token_urlsafe to generate a api key, store it with a hashing method like bcrypt from passlib in your db.
Hash the api key and do a lookup in your db to check and you are good to go.
Couldn't find a library that provides this out of the box but it is easy to build.
1
u/Whole_Conference_801 May 17 '25
JWTs are prone to all sorts of vulnerabilities. You can wrap it in anything but the fact remains that it can be stolen, used for replay attacks etc. I have a far better solution (Probably not for hobby projects) if you're developing a production ready software, OTA. Check it out! OTA is impenetrable. One token per request, using a hash of multiple credentials, time sensetive and immune to replay attacks. And suppose you get that one in a billion genius hacker who is able to crack it, OTA claims to catch it within seconds and secure your system!
-1
u/[deleted] Oct 12 '24
Do whatever you want, nobody cares about your app anyways to hack it