Skip to main content

HTTPBase

This class provides a base for HTTP security schemes, handling common authentication logic such as parsing authorization headers and generating authentication challenges. It supports various HTTP authentication schemes and can automatically raise HTTP exceptions for unauthenticated requests.

Attributes

AttributeTypeDescription
modelHTTPBaseModelThis attribute stores an instance of HTTPBaseModel, which encapsulates the authentication scheme and description for the security scheme.

Constructor

Signature

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

Parameters

NameTypeDescription
schemestr = nullThe authentication scheme.
scheme_name`strNone` = null
description`strNone` = null
auto_errorbool = TrueIf True, an HTTPException will be raised automatically on authentication failure.

Methods


make_authenticate_headers()

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

Generates the WWW-Authenticate header dictionary required for HTTP 401 Unauthorized responses. Callers use this to inform clients about the expected authentication scheme.

Returns

TypeDescription
dict[str, str]A dictionary containing the 'WWW-Authenticate' header with the scheme name.

make_not_authenticated_error()

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

Creates an HTTPException instance configured for an HTTP 401 Unauthorized response. This method is used to raise a standardized error when authentication fails.

Returns

TypeDescription
[HTTPException](../../exceptions/httpexception.md?sid=fastapi_exceptions_httpexception)An HTTPException object with a 401 status code, 'Not authenticated' detail, and appropriate WWW-Authenticate headers.