Home / Class/ OAuth2PasswordBearer Class — fastapi Architecture

OAuth2PasswordBearer Class — fastapi Architecture

Architecture documentation for the OAuth2PasswordBearer class in oauth2.py from the fastapi codebase.

Entity Profile

Dependency Diagram

graph TD
  530dd5bc_4096_0b36_b4c7_3b84ace8a699["OAuth2PasswordBearer"]
  f0e2889d_18fe_8d6f_c8d5_f009c8384bc4["OAuth2"]
  530dd5bc_4096_0b36_b4c7_3b84ace8a699 -->|extends| f0e2889d_18fe_8d6f_c8d5_f009c8384bc4
  efdd0530_d49f_83d3_f1d5_e8884b1b9602["oauth2.py"]
  530dd5bc_4096_0b36_b4c7_3b84ace8a699 -->|defined in| efdd0530_d49f_83d3_f1d5_e8884b1b9602
  9546380c_4ae9_5a79_392c_8b310d60d875["__init__()"]
  530dd5bc_4096_0b36_b4c7_3b84ace8a699 -->|method| 9546380c_4ae9_5a79_392c_8b310d60d875
  815c76ad_ef57_2c1a_ca86_4ca22d0bf6d5["__call__()"]
  530dd5bc_4096_0b36_b4c7_3b84ace8a699 -->|method| 815c76ad_ef57_2c1a_ca86_4ca22d0bf6d5

Relationship Graph

Source Code

fastapi/security/oauth2.py lines 433–544

class OAuth2PasswordBearer(OAuth2):
    """
    OAuth2 flow for authentication using a bearer token obtained with a password.
    An instance of it would be used as a dependency.

    Read more about it in the
    [FastAPI docs for Simple OAuth2 with Password and Bearer](https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/).
    """

    def __init__(
        self,
        tokenUrl: Annotated[
            str,
            Doc(
                """
                The URL to obtain the OAuth2 token. This would be the *path operation*
                that has `OAuth2PasswordRequestForm` as a dependency.

                Read more about it in the
                [FastAPI docs for Simple OAuth2 with Password and Bearer](https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/).
                """
            ),
        ],
        scheme_name: Annotated[
            Optional[str],
            Doc(
                """
                Security scheme name.

                It will be included in the generated OpenAPI (e.g. visible at `/docs`).
                """
            ),
        ] = None,
        scopes: Annotated[
            Optional[dict[str, str]],
            Doc(
                """
                The OAuth2 scopes that would be required by the *path operations* that
                use this dependency.

                Read more about it in the
                [FastAPI docs for Simple OAuth2 with Password and Bearer](https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/).
                """
            ),
        ] = 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 no HTTP Authorization header is provided, required for
                OAuth2 authentication, it will automatically cancel the request and
                send the client an error.

                If `auto_error` is set to `False`, when the HTTP Authorization header
                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, with OAuth2
                or in a cookie).
                """
            ),
        ] = True,
        refreshUrl: Annotated[
            Optional[str],
            Doc(
                """
                The URL to refresh the token and obtain a new one.
                """

Domain

Extends

Frequently Asked Questions

What is the OAuth2PasswordBearer class?
OAuth2PasswordBearer is a class in the fastapi codebase, defined in fastapi/security/oauth2.py.
Where is OAuth2PasswordBearer defined?
OAuth2PasswordBearer is defined in fastapi/security/oauth2.py at line 433.
What does OAuth2PasswordBearer extend?
OAuth2PasswordBearer extends OAuth2.

Analyze Your Own Codebase

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

Try Supermodel Free