I would like to manage a custom authorization system in my FastAPI app.
To achieve this, I would like to use group path operations dependencies on concerned routes to ensure the auth token is present and valid. I followed this guide in documentation that works well.
But since my business logic is more complicated, I also would like to be able to use the API token in my controlers to perform additional rules.
The tip in this section states that Note that, much like dependencies in path operation decorators, no value will be passed to your path operation function.
Which means, if the auth token is defined as a dependency for a group of path operations, there's no way to retrieve its value in the controlers.
Does it mean that I necessarily have to use single dependencies in each path operation function by adding them as a parameter, or is there another way of doing this in a more DRY way ? I know I can retrieve the header within request.headers, but is it how it's supposed to be done ?
One way would look like this :
router = APIRouter()
@router.get('/test')
async def test(request: Request, token: str = Depends(check_token)):
token # directly available
While the other is :
router = APIRouter(dependencies=[Depends(check_token)])
@router.get('/test')
async def test(request: Request):
token = request.headers['auth'] # need some work around to retrieve token
...