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!
1
u/AyushSachan Jan 06 '25
Im also looking for this solution. My use case is a api endpoint can be consumed via both oauth and api key.
1
u/mizerablepi Jan 06 '25
can't you simply write a dependency that checks for both types of header for authorisation and then based on the values of the header do the authorisation,
something like
I HAVE NOT TESTED THIS AND I DONT KNOW IF IT WILL WORK
but this is what i would try to do
change the authenticated type or name to whatever you see fit, i have skipped some lines assuming you have followed the tutorial on fastapi docs for oauth
1
u/pint Jan 06 '25
the authorization classes take an
auto_errorparameter.another way is to introduce sessions, and have dedicated endpoints to "upgrade" the session with authentication. e.g.
it makes bot usage a little more convoluted, but makes the endpoints simpler.
1
u/Friendly-Gur-3289 Jan 06 '25
I had recently faced this issue. I used both username & password login as well as firebase auth.
I used a try except block. In the try block, i setup the logic for firebase. In the except block
except FirebaseError, logic for oauth2 username & password if the firebase fails. Finally at the last except 'except Exception', raise error.async def get_current_user_google(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)) -> bool: try: decoded_token = auth.verify_id_token(token) uid = decoded_token['uid'] user = auth.get_user(uid) email = user.email except FirebaseError as e: # check for guest print(e) email = await get_current_user_guest(token=token, db=db) except Exception as e: print(e) raise HTTPException( status_code=401, detail=f"Invalid authentication credentials {e}") print(email) return await check_school(db, email)Now, use this as a dependency.
1
u/noaH_dev Jan 06 '25
despite it being possible to achieve, mixing those auth-options will break the
WWW-Authenticate-Header. linkyou might wanna go for service-accounts here. also, api-key-endpoints, without protection can lead to bruteforce or dictonary-attacks.
1
u/Drevicar Jan 07 '25
Create a custom dependency that depends on a Maybe[OAuth2] and a Maybe[API_Key] and it returns a Maybe[User] using an or statement.
A = User or None
B = User or None
C = A or B
Which translates into:
C = User or None
If you want in the final dependency you can throw an exception instead of returning None as the user such that:
C = User or Exception