Is your feature request related to a problem? Please describe.
I want to be able to specify different permission levels on endpoints but I can't because FastAPI is not compatible with the default Starlette Authentication starlette.authentication.requires wrapper.
Describe the solution you'd like
The default Starlette starlette.authentication.requires looks for the request keyword in the argument list of the endpoint, but the @route wrappers of FastAPI strip this out. It would be great to have a keyword in the @route wrappers to allow a user to specify the list of strings (i.e. Permissions) following the original Starlette documentation. Something like:
from fastapi import Depends, FastAPI
@app = FastAPI()
@app.get(
path='/',
requires=['secret_permission']
)
def my_endpoint(current_user: User = Security(custom_auth_handler)) -> str:
return f'{current_user} is authorized!'
When a given user does not have the specified permission, a 401 should be raised. Additionally, if the user is not authenticated, it should redirect to the login endpoint before throwing a 401.
Describe alternatives you've considered
When using FastAPI, manually specifying the request: starlette.requests.Request in the endpoint as a required input parameter in addition to the starlette.authentication.requires wrapper from Starlette results in an IndexError exception (because FastAPI is messing with the order of arguments when it attempts to parse out the other parameters). The only other solution is to strip out components of FastAPI for pydantic parsing and use the default Starlette library to have access to permissions per endpoint.
Additional context
Working sample in Starlette can be found in their documentation here.
Is your feature request related to a problem? Please describe.
I want to be able to specify different permission levels on endpoints but I can't because FastAPI is not compatible with the default Starlette Authentication starlette.authentication.requires wrapper.
Describe the solution you'd like
The default Starlette starlette.authentication.requires looks for the request keyword in the argument list of the endpoint, but the @route wrappers of FastAPI strip this out. It would be great to have a keyword in the @route wrappers to allow a user to specify the list of strings (i.e. Permissions) following the original Starlette documentation. Something like:
When a given user does not have the specified permission, a 401 should be raised. Additionally, if the user is not authenticated, it should redirect to the login endpoint before throwing a 401.
Describe alternatives you've considered
When using FastAPI, manually specifying the request: starlette.requests.Request in the endpoint as a required input parameter in addition to the starlette.authentication.requires wrapper from Starlette results in an IndexError exception (because FastAPI is messing with the order of arguments when it attempts to parse out the other parameters). The only other solution is to strip out components of FastAPI for pydantic parsing and use the default Starlette library to have access to permissions per endpoint.
Additional context
Working sample in Starlette can be found in their documentation here.