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
| Attribute | Type | Description |
|---|---|---|
| model | HTTPBaseModel | The underlying data model containing the security scheme and description for the HTTP authentication. |
| scheme_name | string | The name of the security scheme, defaulting to the class name if not explicitly provided. |
| auto_error | boolean = True | Determines 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
| Name | Type | Description |
|---|---|---|
| scheme | str | The HTTP authorization scheme to be used (e.g., 'bearer', 'basic'). |
| scheme_name | `str | None` = None |
| description | `str | None` = None |
| auto_error | bool = True | Whether 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
| Name | Type | Description |
|---|---|---|
| scheme | str | The name of the HTTP authentication scheme to be used, such as 'bearer' or 'basic' |
| scheme_name | `str | None` = None |
| description | `str | None` = None |
| auto_error | bool = True | Determines 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
| Type | Description |
|---|---|
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
| Type | Description |
|---|---|
[HTTPException](../../exceptions/httpexception.md?sid=fastapi_exceptions_httpexception) | An exception object with a 401 status code and authentication headers |