-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54da1f9
commit e2b17b4
Showing
7 changed files
with
654 additions
and
627 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,50 @@ | ||
# from fastapi import Request | ||
# from fastapi.responses import JSONResponse | ||
# | ||
# | ||
# def create_validate_header_middleware(required_header: dict[str, str] | None): | ||
# async def validate_header(request: Request, call_next): | ||
# if required_header: | ||
# header_name, expected_value = next(iter(required_header.items())) | ||
# if ( | ||
# header_name not in request.headers | ||
# or request.headers[header_name] != expected_value | ||
# ): | ||
# return JSONResponse( # attempted to use Exception and although it was caught it did not propagate | ||
# status_code=400, | ||
# content={ | ||
# "error": "Header Validation Error", | ||
# "message": f"Missing or invalid header: {header_name}", | ||
# "code": "HEADER_VALIDATION_ERROR", | ||
# }, | ||
# ) | ||
# return await call_next(request) | ||
# | ||
# return validate_header | ||
|
||
|
||
from fastapi import Request | ||
from fastapi.responses import JSONResponse | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
from starlette.types import ASGIApp | ||
|
||
|
||
def create_validate_header_middleware(required_header: dict[str, str] | None): | ||
async def validate_header(request: Request, call_next): | ||
if required_header: | ||
header_name, expected_value = next(iter(required_header.items())) | ||
if ( | ||
header_name not in request.headers | ||
or request.headers[header_name] != expected_value | ||
): | ||
return JSONResponse( # attempted to use Exception and although it was caught it did not propagate | ||
class ValidateHeaderMiddleware(BaseHTTPMiddleware): | ||
def __init__(self, app: ASGIApp, required_header: dict[str, str] | None): | ||
super().__init__(app) | ||
self.required_header = required_header | ||
|
||
async def dispatch(self, request: Request, call_next): | ||
if self.required_header: | ||
header_name, expected_value = next(iter(self.required_header.items())) | ||
if header_name not in request.headers or request.headers[header_name] != expected_value: | ||
return JSONResponse( | ||
status_code=400, | ||
content={ | ||
"error": "Header Validation Error", | ||
"message": f"Missing or invalid header: {header_name}", | ||
"code": "HEADER_VALIDATION_ERROR", | ||
}, | ||
"code": "HEADER_VALIDATION_ERROR" | ||
} | ||
) | ||
return await call_next(request) | ||
|
||
return validate_header | ||
response = await call_next(request) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters