FastAPIPassthroughAuthMiddleware
FastAPI middleware that automatically sets Flyte auth metadata from request headers. This middleware extracts authentication headers from incoming HTTP requests and sets them in the Flyte context using the auth_metadata() context manager. This eliminates the need to manually wrap endpoint handlers with auth_metadata(). The middleware is highly configurable: Custom header extractors can be provided, Specific paths can be excluded from auth requirements, Auth can be optional or required.
Attributes
| Attribute | Type | Description |
|---|---|---|
| app | The FastAPI application (this is a mandatory framework parameter) | |
| header_extractors | List of functions to extract headers from requests | |
| excluded_paths | Set of URL paths that bypass auth extraction |
Constructor
Signature
def FastAPIPassthroughAuthMiddleware(
app: Any = null,
header_extractors: list[HeaderExtractor]| None = None,
excluded_paths: set[str]| None = None
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| app | Any = null | The FastAPI/Starlette application |
| header_extractors | `list[HeaderExtractor] | None` = None |
| excluded_paths | `set[str] | None` = None |
Methods
dispatch()
@classmethod
def dispatch(
request: Request,
call_next: Any
) - > Response
Process each request, extracting auth headers and setting Flyte context.
Parameters
| Name | Type | Description |
|---|---|---|
| request | Request | The incoming HTTP request |
| call_next | Any | The next middleware or route handler to call |
Returns
| Type | Description |
|---|---|
Response | The HTTP response from the handler |
extract_authorization_header()
@classmethod
def extract_authorization_header(
request: Request
) - > tuple[str, str]| None
Extract the Authorization header from the request.
Parameters
| Name | Type | Description |
|---|---|---|
| request | Request | The FastAPI/Starlette request object |
Returns
| Type | Description |
|---|---|
| `tuple[str, str] | None` |
extract_cookie_header()
@classmethod
def extract_cookie_header(
request: Request
) - > tuple[str, str]| None
Extract the Cookie header from the request.
Parameters
| Name | Type | Description |
|---|---|---|
| request | Request | The FastAPI/Starlette request object |
Returns
| Type | Description |
|---|---|
| `tuple[str, str] | None` |
extract_custom_header()
@classmethod
def extract_custom_header(
header_name: str
) - > HeaderExtractor
Create a header extractor for a custom header name.
Parameters
| Name | Type | Description |
|---|---|---|
| header_name | str | The name of the header to extract (case-insensitive) |
Returns
| Type | Description |
|---|---|
HeaderExtractor | A header extractor function that extracts the specified header |