Skip to main content

FastAPIPassthroughAuthMiddleware

FastAPI middleware that automatically sets Flyte auth metadata from request headers.

Attributes

AttributeTypeDescription
appFastAPIThe FastAPI application (this is a mandatory framework parameter)
header_extractorslist[HeaderExtractor] = [self.extract_authorization_header, self.extract_cookie_header]List of functions to extract headers from requests
excluded_pathsset[str] = set()Set of URL paths that bypass auth extraction

Constructor

Signature

def FastAPIPassthroughAuthMiddleware(
app: Any,
header_extractors: list[HeaderExtractor]| None = None,
excluded_paths: set[str]| None = None
) - > None

Parameters

NameTypeDescription
appAnyThe FastAPI/Starlette application instance.
header_extractors`list[HeaderExtractor]None` = None
excluded_paths`set[str]None` = None

Signature

def FastAPIPassthroughAuthMiddleware(
app: Any,
header_extractors: list[HeaderExtractor]| None = None,
excluded_paths: set[str]| None = None
)

Parameters

NameTypeDescription
appAnyThe FastAPI/Starlette application instance being wrapped by the middleware
header_extractors`list[HeaderExtractor]None` = None
excluded_paths`set[str]None` = None

Methods


dispatch()

@classmethod
def dispatch(
request: Request,
call_next: Callable
) - > Response

Process each request, extracting auth headers and setting Flyte context.

Parameters

NameTypeDescription
requestRequestThe incoming HTTP request
call_nextCallableThe 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