Skip to main content

HTTPBase

This class serves as a base for implementing HTTP-based security schemes by extracting and validating authorization credentials from request headers. It provides mechanisms for generating authentication headers and handling unauthorized access errors based on a specified security scheme. The class supports optional automatic error raising when credentials are missing or malformed.

Attributes​

AttributeTypeDescription
modelHTTPBaseModelThe underlying data model containing the security scheme and description for the HTTP authentication.
scheme_namestringThe name of the security scheme, defaulting to the class name if not explicitly provided.
auto_errorboolean = TrueDetermines whether to raise an HTTP 401 error automatically if authentication credentials are missing.

Constructor​

Signature​

def HTTPBase(
scheme: str,
scheme_name: str | None = None,
description: str | None = None,
auto_error: bool = True
)

Parameters​

NameTypeDescription
schemestrThe HTTP authorization scheme to be used (e.g., 'bearer', 'basic').
scheme_name`strNone` = None
description`strNone` = None
auto_errorbool = TrueWhether to automatically raise an HTTPException if authentication fails.

Signature​

def HTTPBase(
scheme: str,
scheme_name: str | None = None,
description: str | None = None,
auto_error: bool = True
)

Parameters​

NameTypeDescription
schemestrThe name of the HTTP authentication scheme to be used, such as 'bearer' or 'basic'
scheme_name`strNone` = None
description`strNone` = None
auto_errorbool = TrueDetermines whether to automatically raise an HTTPException when authentication fails or is missing

Methods​


make_authenticate_headers()​

@classmethod
def make_authenticate_headers() - > dict[str, str]

Creates the WWW-Authenticate header required for 401 Unauthorized responses.

Returns​

TypeDescription
dict[str, str]A dictionary containing the WWW-Authenticate key and the formatted authentication scheme value

make_not_authenticated_error()​

@classmethod
def make_not_authenticated_error() - > [HTTPException](../../exceptions/httpexception.md?sid=fastapi_exceptions_httpexception)

Constructs an HTTPException for unauthenticated requests including the appropriate challenge headers.

Returns​

TypeDescription
[HTTPException](../../exceptions/httpexception.md?sid=fastapi_exceptions_httpexception)An exception object with a 401 status code and authentication headers