Home / Class/ HTTPBearer Class — fastapi Architecture

HTTPBearer Class — fastapi Architecture

Architecture documentation for the HTTPBearer class in http.py from the fastapi codebase.

Entity Profile

Dependency Diagram

graph TD
  9620b194_84a3_fbe6_126b_b915d25890fd["HTTPBearer"]
  7b709818_b832_ceea_aedf_a621ca961213["HTTPBase"]
  9620b194_84a3_fbe6_126b_b915d25890fd -->|extends| 7b709818_b832_ceea_aedf_a621ca961213
  96278b4c_a391_681f_b974_563be8af72ce["http.py"]
  9620b194_84a3_fbe6_126b_b915d25890fd -->|defined in| 96278b4c_a391_681f_b974_563be8af72ce
  fa4f2890_492f_9992_1dcf_a2bb5b0b2a66["__init__()"]
  9620b194_84a3_fbe6_126b_b915d25890fd -->|method| fa4f2890_492f_9992_1dcf_a2bb5b0b2a66
  b744049d_60fe_b80e_7c2a_c093693fd243["__call__()"]
  9620b194_84a3_fbe6_126b_b915d25890fd -->|method| b744049d_60fe_b80e_7c2a_c093693fd243

Relationship Graph

Source Code

fastapi/security/http.py lines 224–320

class HTTPBearer(HTTPBase):
    """
    HTTP Bearer token authentication.

    ## Usage

    Create an instance object and use that object as the dependency in `Depends()`.

    The dependency result will be an `HTTPAuthorizationCredentials` object containing
    the `scheme` and the `credentials`.

    ## Example

    ```python
    from typing import Annotated

    from fastapi import Depends, FastAPI
    from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer

    app = FastAPI()

    security = HTTPBearer()


    @app.get("/users/me")
    def read_current_user(
        credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)]
    ):
        return {"scheme": credentials.scheme, "credentials": credentials.credentials}
    ```
    """

    def __init__(
        self,
        *,
        bearerFormat: Annotated[Optional[str], Doc("Bearer token format.")] = None,
        scheme_name: Annotated[
            Optional[str],
            Doc(
                """
                Security scheme name.

                It will be included in the generated OpenAPI (e.g. visible at `/docs`).
                """
            ),
        ] = None,
        description: Annotated[
            Optional[str],
            Doc(
                """
                Security scheme description.

                It will be included in the generated OpenAPI (e.g. visible at `/docs`).
                """
            ),
        ] = None,
        auto_error: Annotated[
            bool,
            Doc(
                """
                By default, if the HTTP Bearer token is not provided (in an
                `Authorization` header), `HTTPBearer` will automatically cancel the
                request and send the client an error.

                If `auto_error` is set to `False`, when the HTTP Bearer token
                is not available, instead of erroring out, the dependency result will
                be `None`.

                This is useful when you want to have optional authentication.

                It is also useful when you want to have authentication that can be
                provided in one of multiple optional ways (for example, in an HTTP
                Bearer token or in a cookie).
                """
            ),
        ] = True,
    ):
        self.model = HTTPBearerModel(bearerFormat=bearerFormat, description=description)
        self.scheme_name = scheme_name or self.__class__.__name__
        self.auto_error = auto_error

Domain

Extends

Frequently Asked Questions

What is the HTTPBearer class?
HTTPBearer is a class in the fastapi codebase, defined in fastapi/security/http.py.
Where is HTTPBearer defined?
HTTPBearer is defined in fastapi/security/http.py at line 224.
What does HTTPBearer extend?
HTTPBearer extends HTTPBase.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free