-
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.
Merge pull request #294 from RDFLib/david/check-header-clean
David/check header
- Loading branch information
Showing
7 changed files
with
650 additions
and
612 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 |
---|---|---|
@@ -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 |
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