Home / File/ open_id_connect_url.py — fastapi Source File

open_id_connect_url.py — fastapi Source File

Architecture documentation for open_id_connect_url.py, a python file in the fastapi codebase. 9 imports, 4 dependents.

File python Security Schemes 9 imports 4 dependents 1 classes

Entity Profile

Dependency Diagram

graph LR
  41fcb02b_3db0_cf0b_0554_183bc646a21d["open_id_connect_url.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  41fcb02b_3db0_cf0b_0554_183bc646a21d --> 0dda2280_3359_8460_301c_e98c77e78185
  5efacb44_5373_ceb9_9579_6e6603820488["annotated_doc"]
  41fcb02b_3db0_cf0b_0554_183bc646a21d --> 5efacb44_5373_ceb9_9579_6e6603820488
  7f688779_6b22_3c15_6514_97dec91c3c30["models.py"]
  41fcb02b_3db0_cf0b_0554_183bc646a21d --> 7f688779_6b22_3c15_6514_97dec91c3c30
  2b7bda22_d59a_0185_2564_df90a3648bc0["OpenIdConnect"]
  41fcb02b_3db0_cf0b_0554_183bc646a21d --> 2b7bda22_d59a_0185_2564_df90a3648bc0
  81864ff4_3194_3d8a_0ac8_a6193fbdc833["base.py"]
  41fcb02b_3db0_cf0b_0554_183bc646a21d --> 81864ff4_3194_3d8a_0ac8_a6193fbdc833
  1aa22226_1c87_90b6_c61d_502d10cd0315["SecurityBase"]
  41fcb02b_3db0_cf0b_0554_183bc646a21d --> 1aa22226_1c87_90b6_c61d_502d10cd0315
  72a586ac_ceef_ac9f_17ec_0ed0d645b635["starlette.exceptions"]
  41fcb02b_3db0_cf0b_0554_183bc646a21d --> 72a586ac_ceef_ac9f_17ec_0ed0d645b635
  7ba41d57_5222_3320_290a_ff23f8a854b4["starlette.requests"]
  41fcb02b_3db0_cf0b_0554_183bc646a21d --> 7ba41d57_5222_3320_290a_ff23f8a854b4
  26ce4316_d64e_8f75_fd3c_7f3b4f109cff["starlette.status"]
  41fcb02b_3db0_cf0b_0554_183bc646a21d --> 26ce4316_d64e_8f75_fd3c_7f3b4f109cff
  35c4ea20_c454_5afd_6cda_f0818bbcc650["__init__.py"]
  35c4ea20_c454_5afd_6cda_f0818bbcc650 --> 41fcb02b_3db0_cf0b_0554_183bc646a21d
  61470476_48f7_a937_be0d_6d256741a034["test_security_openid_connect.py"]
  61470476_48f7_a937_be0d_6d256741a034 --> 41fcb02b_3db0_cf0b_0554_183bc646a21d
  66cc3715_e8ab_57ab_b56a_7a46fdf9b2d7["test_security_openid_connect_description.py"]
  66cc3715_e8ab_57ab_b56a_7a46fdf9b2d7 --> 41fcb02b_3db0_cf0b_0554_183bc646a21d
  68e8df50_067f_7811_9244_652b9ff1ee74["test_security_openid_connect_optional.py"]
  68e8df50_067f_7811_9244_652b9ff1ee74 --> 41fcb02b_3db0_cf0b_0554_183bc646a21d
  style 41fcb02b_3db0_cf0b_0554_183bc646a21d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Annotated, Optional

from annotated_doc import Doc
from fastapi.openapi.models import OpenIdConnect as OpenIdConnectModel
from fastapi.security.base import SecurityBase
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.status import HTTP_401_UNAUTHORIZED


class OpenIdConnect(SecurityBase):
    """
    OpenID Connect authentication class. An instance of it would be used as a
    dependency.

    **Warning**: this is only a stub to connect the components with OpenAPI in FastAPI,
    but it doesn't implement the full OpenIdConnect scheme, for example, it doesn't use
    the OpenIDConnect URL. You would need to to subclass it and implement it in your
    code.
    """

    def __init__(
        self,
        *,
        openIdConnectUrl: Annotated[
            str,
            Doc(
                """
            The OpenID Connect URL.
            """
            ),
        ],
        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 no HTTP Authorization header is provided, required for
                OpenID Connect 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 OpenID
                Connect or in a cookie).
                """
            ),
        ] = True,
    ):
        self.model = OpenIdConnectModel(
            openIdConnectUrl=openIdConnectUrl, description=description
        )
        self.scheme_name = scheme_name or self.__class__.__name__
        self.auto_error = auto_error

    def make_not_authenticated_error(self) -> HTTPException:
        return HTTPException(
            status_code=HTTP_401_UNAUTHORIZED,
            detail="Not authenticated",
            headers={"WWW-Authenticate": "Bearer"},
        )

    async def __call__(self, request: Request) -> Optional[str]:
        authorization = request.headers.get("Authorization")
        if not authorization:
            if self.auto_error:
                raise self.make_not_authenticated_error()
            else:
                return None
        return authorization

Domain

Subdomains

Classes

Dependencies

Frequently Asked Questions

What does open_id_connect_url.py do?
open_id_connect_url.py is a source file in the fastapi codebase, written in python. It belongs to the Security domain, Schemes subdomain.
What does open_id_connect_url.py depend on?
open_id_connect_url.py imports 9 module(s): OpenIdConnect, SecurityBase, annotated_doc, base.py, models.py, starlette.exceptions, starlette.requests, starlette.status, and 1 more.
What files import open_id_connect_url.py?
open_id_connect_url.py is imported by 4 file(s): __init__.py, test_security_openid_connect.py, test_security_openid_connect_description.py, test_security_openid_connect_optional.py.
Where is open_id_connect_url.py in the architecture?
open_id_connect_url.py is located at fastapi/security/open_id_connect_url.py (domain: Security, subdomain: Schemes, directory: fastapi/security).

Analyze Your Own Codebase

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

Try Supermodel Free