indigo-dc / flaat

FLAsk with Access Tokens - FLAAT
MIT License
11 stars 6 forks source link

FastAPI check_request_authorization() got multiple values for argument 'user_infos' #68

Closed BorjaEst closed 1 year ago

BorjaEst commented 1 year ago

I am unable to create a first example injecting user_infos:

from fastapi import FastAPI, Request
from flaat.fastapi import Flaat

app = FastAPI()
flaat = Flaat()
flaat.set_trusted_OP_list(["https://aai.egi.eu/oidc/"])

@app.get("/info")
@flaat.inject_user_infos()  # Fail if no valid authentication is provided
def info_strict_mode(request: Request, user_infos=None):
    return user_infos.toJSON()

When calling the enpoint using a correct token, I get Internal Server Error due to check_request_authorization() got multiple values for argument 'user_infos'. Maybe I am missing something?

BorjaEst commented 1 year ago

In case someone wonders why not to remove user_infos, I think (by reading the docs) it is a required field. If I remove:

@app.get("/info")
@flaat.inject_user_infos()  # Fail if no valid authentication is provided
def info_strict_mode(request: Request):
    return <something>

Then I get info_strict_mode() got an unexpected keyword argument 'user_infos' as the function does not accept such argument.

dianagudu commented 1 year ago

It's funny, it seems to work if you change the name of the key that is injected:

@app.get("/info")
@flaat.inject_user_infos(key="userinfo")  # Fail if no valid authentication is provided
def info_strict_mode(request: Request, userinfo=None):
    return userinfo.toJSON()
BorjaEst commented 1 year ago

Thanks @dianagudu

Yes, that is an option, however it might affect the generated Swagger docs. In case a user needs them, it is probably better to get the user_infos from the request.

I have updated the example, so I am closing the issue.