-
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.
feat: Add ability for prez to check for a header on all requests such…
… as an API key.
- Loading branch information
1 parent
6d8d539
commit 2df6675
Showing
3 changed files
with
46 additions
and
7 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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 |