-
We have a use case where an optional callback to the CORSMiddleware is hugely useful. Would a PR for something like this be (eventually) accepted? class CORSMiddleware:
def __init__(
self,
app: ASGIApp,
allow_origins: typing.Sequence[str] = (),
allow_methods: typing.Sequence[str] = ("GET",),
allow_headers: typing.Sequence[str] = (),
allow_credentials: bool = False,
allow_origin_regex: str | None = None,
expose_headers: typing.Sequence[str] = (),
max_age: int = 600,
callback: typing.Optional[typing.Callable[[str], bool]] = None,
) -> None:
...
self.callback = callback
...
async def is_allowed_origin(self, origin: str) -> bool:
if self.allow_all_origins:
return True
if self.allow_origin_regex is not None and self.allow_origin_regex.fullmatch(
origin
):
return True
# Check static origins first
if origin in self.allow_origins:
return True
if self.callback is not None:
return await self.callback(origin)
return False |
Beta Was this translation helpful? Give feedback.
Answered by
Kludex
Apr 15, 2024
Replies: 1 comment 2 replies
-
You can subclass CORSMiddleware, and overwrite the |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
Kludex
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can subclass CORSMiddleware, and overwrite the
is_allowed_origin
method.