Skip to main content

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

AttributeTypeDescription
appThe FastAPI application (this is a mandatory framework parameter)
header_extractorsList of functions to extract headers from requests
excluded_pathsSet 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

NameTypeDescription
appAny = nullThe 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

NameTypeDescription
requestRequestThe incoming HTTP request
call_nextAnyThe next middleware or route handler to call

Returns

TypeDescription
ResponseThe 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

NameTypeDescription
requestRequestThe FastAPI/Starlette request object

Returns

TypeDescription
`tuple[str, str]None`

@classmethod
def extract_cookie_header(
request: Request
) - > tuple[str, str]| None

Extract the Cookie header from the request.

Parameters

NameTypeDescription
requestRequestThe FastAPI/Starlette request object

Returns

TypeDescription
`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

NameTypeDescription
header_namestrThe name of the header to extract (case-insensitive)

Returns

TypeDescription
HeaderExtractorA header extractor function that extracts the specified header