r/FastAPI • u/netyaco • Jan 06 '25
Question Validate only one of two security options
Hello!
I'm developing an API with FastAPI, and I have 2 types of security: oauth2 and api_key (from headers).
Some endpoint use oauth2 (basically interactions from frontend), and others use api_key (for some automations), and all works fine.
My question is: is it possible to combine these two options, but be enough that one of them is fulfilled?
I have tried several approaches, but I can't get it to work (at least via Postman). I imagine that one type of authorization “overrides” the other (I have to use either oauth2 or api_key when I make the request, but check both).
Any idea?
Thanks a lot!
View all comments
Show parent comment
2
u/netyaco Jan 06 '25
I have a workaround, but it's pretty tricky. I have a group of endopoints used by a bot for the automations, and this group has "the same" endopints I use via frontend, but with an extra path (
/mysuperbot). For example,/usershas/mysuperbot/users,/movieshas/mysuperbot/movies...The difference is that these endpoints use the API Key auth, and then call directly to the main endpoint.
Yes, it works, but I need to create 2 endpoints of those of that I need to consume from the 2 auth options.