There are 4 ways to authenticate/authorize requests:
AbstractAuthenticationMiddleware
middlewareAbstractSecurityConfig
middlewareThere are certain shortcomings:
AbstractAuthenticationMiddleware
does not add security
and securityScheme
(but you can define it per layer)
AbstractSecurityConfig
injects security
and securityScheme
globally instead of per operation (#3013). Also, you can only define it per application, not per layer (ie. routers, controllers, handlers). Even if #3013 gets fixed, it's inconvenient having to exclude routes per path (string values) and not being able to define this per layer.
Usual DI logic works just fine, but if you don't actually need the value it injects, linters may nag you and IDEs render the parameter as grayed out (because it's not used). Plus, it's not possible to adjust the security
/ securityScheme
via it (at least not without hacks).
Guards don't support DI, and cannot alter security
/ securityScheme
What's needed is a mechanism that:
security
per layer and securityScheme
(when needed, to the global level)scheme = SecurityScheme(type="apiKey", description="...", ...)
async def callable_auth(
scheme: Annotated[Any, scheme],
):
... # the usual auth checks
# Or maybe
class ClassAuth(Security): # Security being some Litestar provided base class
security_scheme = scheme
async def __call__(self):
... # the usual auth checks
@get()
async def handler1(auth1: User) -> str: ...
@get(guards=["auth2"])
async def handler2() -> str: ...
app = Litestar(
[handler1, handler2],
dependencies={
"auth1": Provide(callable_auth),
"auth2": Provide(ClassAuth),
},
)
So, with the idea that:
security
and securityScheme
are filled automatically and only where neededSomething like that?
No response
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too