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
| Attribute | Type | Description |
|---|---|---|
| model | HTTPBaseModel | This 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
| Name | Type | Description |
|---|---|---|
| scheme | str = null | The authentication scheme. |
| scheme_name | `str | None` = null |
| description | `str | None` = null |
| auto_error | bool = True | If 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
| Type | Description |
|---|---|
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
| Type | Description |
|---|---|
[HTTPException](../../exceptions/httpexception.md?sid=fastapi_exceptions_httpexception) | An HTTPException object with a 401 status code, 'Not authenticated' detail, and appropriate WWW-Authenticate headers. |