api_key.py — fastapi Source File
Architecture documentation for api_key.py, a python file in the fastapi codebase. 10 imports, 1 dependents.
Entity Profile
Dependency Diagram
graph LR fac63c1f_ae18_a88d_487c_0fd6cfe587f0["api_key.py"] 0dda2280_3359_8460_301c_e98c77e78185["typing"] fac63c1f_ae18_a88d_487c_0fd6cfe587f0 --> 0dda2280_3359_8460_301c_e98c77e78185 5efacb44_5373_ceb9_9579_6e6603820488["annotated_doc"] fac63c1f_ae18_a88d_487c_0fd6cfe587f0 --> 5efacb44_5373_ceb9_9579_6e6603820488 7f688779_6b22_3c15_6514_97dec91c3c30["models.py"] fac63c1f_ae18_a88d_487c_0fd6cfe587f0 --> 7f688779_6b22_3c15_6514_97dec91c3c30 77038145_43f7_619e_b494_3ad3bbbd92c9["APIKey"] fac63c1f_ae18_a88d_487c_0fd6cfe587f0 --> 77038145_43f7_619e_b494_3ad3bbbd92c9 a6d1a718_f075_8139_2a71_95f3d1eeac35["APIKeyIn"] fac63c1f_ae18_a88d_487c_0fd6cfe587f0 --> a6d1a718_f075_8139_2a71_95f3d1eeac35 81864ff4_3194_3d8a_0ac8_a6193fbdc833["base.py"] fac63c1f_ae18_a88d_487c_0fd6cfe587f0 --> 81864ff4_3194_3d8a_0ac8_a6193fbdc833 1aa22226_1c87_90b6_c61d_502d10cd0315["SecurityBase"] fac63c1f_ae18_a88d_487c_0fd6cfe587f0 --> 1aa22226_1c87_90b6_c61d_502d10cd0315 72a586ac_ceef_ac9f_17ec_0ed0d645b635["starlette.exceptions"] fac63c1f_ae18_a88d_487c_0fd6cfe587f0 --> 72a586ac_ceef_ac9f_17ec_0ed0d645b635 7ba41d57_5222_3320_290a_ff23f8a854b4["starlette.requests"] fac63c1f_ae18_a88d_487c_0fd6cfe587f0 --> 7ba41d57_5222_3320_290a_ff23f8a854b4 26ce4316_d64e_8f75_fd3c_7f3b4f109cff["starlette.status"] fac63c1f_ae18_a88d_487c_0fd6cfe587f0 --> 26ce4316_d64e_8f75_fd3c_7f3b4f109cff 35c4ea20_c454_5afd_6cda_f0818bbcc650["__init__.py"] 35c4ea20_c454_5afd_6cda_f0818bbcc650 --> fac63c1f_ae18_a88d_487c_0fd6cfe587f0 style fac63c1f_ae18_a88d_487c_0fd6cfe587f0 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
from typing import Annotated, Optional, Union
from annotated_doc import Doc
from fastapi.openapi.models import APIKey, APIKeyIn
from fastapi.security.base import SecurityBase
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.status import HTTP_401_UNAUTHORIZED
class APIKeyBase(SecurityBase):
def __init__(
self,
location: APIKeyIn,
name: str,
description: Union[str, None],
scheme_name: Union[str, None],
auto_error: bool,
):
self.auto_error = auto_error
self.model: APIKey = APIKey(
**{"in": location},
name=name,
description=description,
)
self.scheme_name = scheme_name or self.__class__.__name__
def make_not_authenticated_error(self) -> HTTPException:
"""
The WWW-Authenticate header is not standardized for API Key authentication but
the HTTP specification requires that an error of 401 "Unauthorized" must
include a WWW-Authenticate header.
Ref: https://datatracker.ietf.org/doc/html/rfc9110#name-401-unauthorized
For this, this method sends a custom challenge `APIKey`.
"""
return HTTPException(
status_code=HTTP_401_UNAUTHORIZED,
detail="Not authenticated",
headers={"WWW-Authenticate": "APIKey"},
)
def check_api_key(self, api_key: Optional[str]) -> Optional[str]:
if not api_key:
if self.auto_error:
raise self.make_not_authenticated_error()
return None
return api_key
class APIKeyQuery(APIKeyBase):
"""
API key authentication using a query parameter.
This defines the name of the query parameter that should be provided in the request
with the API key and integrates that into the OpenAPI documentation. It extracts
the key value sent in the query parameter automatically and provides it as the
dependency result. But it doesn't define how to send that API key to the client.
// ... (259 more lines)
Domain
Subdomains
Dependencies
- APIKey
- APIKeyIn
- SecurityBase
- annotated_doc
- base.py
- models.py
- starlette.exceptions
- starlette.requests
- starlette.status
- typing
Imported By
Source
Frequently Asked Questions
What does api_key.py do?
api_key.py is a source file in the fastapi codebase, written in python. It belongs to the Security domain, Schemes subdomain.
What does api_key.py depend on?
api_key.py imports 10 module(s): APIKey, APIKeyIn, SecurityBase, annotated_doc, base.py, models.py, starlette.exceptions, starlette.requests, and 2 more.
What files import api_key.py?
api_key.py is imported by 1 file(s): __init__.py.
Where is api_key.py in the architecture?
api_key.py is located at fastapi/security/api_key.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