fastapi 0.128.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi/__init__.py +25 -0
- fastapi/__main__.py +3 -0
- fastapi/_compat/__init__.py +41 -0
- fastapi/_compat/shared.py +206 -0
- fastapi/_compat/v2.py +568 -0
- fastapi/applications.py +4669 -0
- fastapi/background.py +60 -0
- fastapi/cli.py +13 -0
- fastapi/concurrency.py +41 -0
- fastapi/datastructures.py +183 -0
- fastapi/dependencies/__init__.py +0 -0
- fastapi/dependencies/models.py +193 -0
- fastapi/dependencies/utils.py +1021 -0
- fastapi/encoders.py +346 -0
- fastapi/exception_handlers.py +34 -0
- fastapi/exceptions.py +246 -0
- fastapi/logger.py +3 -0
- fastapi/middleware/__init__.py +1 -0
- fastapi/middleware/asyncexitstack.py +18 -0
- fastapi/middleware/cors.py +1 -0
- fastapi/middleware/gzip.py +1 -0
- fastapi/middleware/httpsredirect.py +3 -0
- fastapi/middleware/trustedhost.py +3 -0
- fastapi/middleware/wsgi.py +1 -0
- fastapi/openapi/__init__.py +0 -0
- fastapi/openapi/constants.py +3 -0
- fastapi/openapi/docs.py +344 -0
- fastapi/openapi/models.py +438 -0
- fastapi/openapi/utils.py +567 -0
- fastapi/param_functions.py +2369 -0
- fastapi/params.py +755 -0
- fastapi/py.typed +0 -0
- fastapi/requests.py +2 -0
- fastapi/responses.py +48 -0
- fastapi/routing.py +4508 -0
- fastapi/security/__init__.py +15 -0
- fastapi/security/api_key.py +318 -0
- fastapi/security/base.py +6 -0
- fastapi/security/http.py +423 -0
- fastapi/security/oauth2.py +663 -0
- fastapi/security/open_id_connect_url.py +94 -0
- fastapi/security/utils.py +10 -0
- fastapi/staticfiles.py +1 -0
- fastapi/templating.py +1 -0
- fastapi/testclient.py +1 -0
- fastapi/types.py +11 -0
- fastapi/utils.py +164 -0
- fastapi/websockets.py +3 -0
- fastapi-0.128.0.dist-info/METADATA +645 -0
- fastapi-0.128.0.dist-info/RECORD +53 -0
- fastapi-0.128.0.dist-info/WHEEL +4 -0
- fastapi-0.128.0.dist-info/entry_points.txt +5 -0
- fastapi-0.128.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .api_key import APIKeyCookie as APIKeyCookie
|
|
2
|
+
from .api_key import APIKeyHeader as APIKeyHeader
|
|
3
|
+
from .api_key import APIKeyQuery as APIKeyQuery
|
|
4
|
+
from .http import HTTPAuthorizationCredentials as HTTPAuthorizationCredentials
|
|
5
|
+
from .http import HTTPBasic as HTTPBasic
|
|
6
|
+
from .http import HTTPBasicCredentials as HTTPBasicCredentials
|
|
7
|
+
from .http import HTTPBearer as HTTPBearer
|
|
8
|
+
from .http import HTTPDigest as HTTPDigest
|
|
9
|
+
from .oauth2 import OAuth2 as OAuth2
|
|
10
|
+
from .oauth2 import OAuth2AuthorizationCodeBearer as OAuth2AuthorizationCodeBearer
|
|
11
|
+
from .oauth2 import OAuth2PasswordBearer as OAuth2PasswordBearer
|
|
12
|
+
from .oauth2 import OAuth2PasswordRequestForm as OAuth2PasswordRequestForm
|
|
13
|
+
from .oauth2 import OAuth2PasswordRequestFormStrict as OAuth2PasswordRequestFormStrict
|
|
14
|
+
from .oauth2 import SecurityScopes as SecurityScopes
|
|
15
|
+
from .open_id_connect_url import OpenIdConnect as OpenIdConnect
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
from typing import Annotated, Optional, Union
|
|
2
|
+
|
|
3
|
+
from annotated_doc import Doc
|
|
4
|
+
from fastapi.openapi.models import APIKey, APIKeyIn
|
|
5
|
+
from fastapi.security.base import SecurityBase
|
|
6
|
+
from starlette.exceptions import HTTPException
|
|
7
|
+
from starlette.requests import Request
|
|
8
|
+
from starlette.status import HTTP_401_UNAUTHORIZED
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class APIKeyBase(SecurityBase):
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
location: APIKeyIn,
|
|
15
|
+
name: str,
|
|
16
|
+
description: Union[str, None],
|
|
17
|
+
scheme_name: Union[str, None],
|
|
18
|
+
auto_error: bool,
|
|
19
|
+
):
|
|
20
|
+
self.auto_error = auto_error
|
|
21
|
+
|
|
22
|
+
self.model: APIKey = APIKey(
|
|
23
|
+
**{"in": location},
|
|
24
|
+
name=name,
|
|
25
|
+
description=description,
|
|
26
|
+
)
|
|
27
|
+
self.scheme_name = scheme_name or self.__class__.__name__
|
|
28
|
+
|
|
29
|
+
def make_not_authenticated_error(self) -> HTTPException:
|
|
30
|
+
"""
|
|
31
|
+
The WWW-Authenticate header is not standardized for API Key authentication but
|
|
32
|
+
the HTTP specification requires that an error of 401 "Unauthorized" must
|
|
33
|
+
include a WWW-Authenticate header.
|
|
34
|
+
|
|
35
|
+
Ref: https://datatracker.ietf.org/doc/html/rfc9110#name-401-unauthorized
|
|
36
|
+
|
|
37
|
+
For this, this method sends a custom challenge `APIKey`.
|
|
38
|
+
"""
|
|
39
|
+
return HTTPException(
|
|
40
|
+
status_code=HTTP_401_UNAUTHORIZED,
|
|
41
|
+
detail="Not authenticated",
|
|
42
|
+
headers={"WWW-Authenticate": "APIKey"},
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
def check_api_key(self, api_key: Optional[str]) -> Optional[str]:
|
|
46
|
+
if not api_key:
|
|
47
|
+
if self.auto_error:
|
|
48
|
+
raise self.make_not_authenticated_error()
|
|
49
|
+
return None
|
|
50
|
+
return api_key
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class APIKeyQuery(APIKeyBase):
|
|
54
|
+
"""
|
|
55
|
+
API key authentication using a query parameter.
|
|
56
|
+
|
|
57
|
+
This defines the name of the query parameter that should be provided in the request
|
|
58
|
+
with the API key and integrates that into the OpenAPI documentation. It extracts
|
|
59
|
+
the key value sent in the query parameter automatically and provides it as the
|
|
60
|
+
dependency result. But it doesn't define how to send that API key to the client.
|
|
61
|
+
|
|
62
|
+
## Usage
|
|
63
|
+
|
|
64
|
+
Create an instance object and use that object as the dependency in `Depends()`.
|
|
65
|
+
|
|
66
|
+
The dependency result will be a string containing the key value.
|
|
67
|
+
|
|
68
|
+
## Example
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from fastapi import Depends, FastAPI
|
|
72
|
+
from fastapi.security import APIKeyQuery
|
|
73
|
+
|
|
74
|
+
app = FastAPI()
|
|
75
|
+
|
|
76
|
+
query_scheme = APIKeyQuery(name="api_key")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@app.get("/items/")
|
|
80
|
+
async def read_items(api_key: str = Depends(query_scheme)):
|
|
81
|
+
return {"api_key": api_key}
|
|
82
|
+
```
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
def __init__(
|
|
86
|
+
self,
|
|
87
|
+
*,
|
|
88
|
+
name: Annotated[
|
|
89
|
+
str,
|
|
90
|
+
Doc("Query parameter name."),
|
|
91
|
+
],
|
|
92
|
+
scheme_name: Annotated[
|
|
93
|
+
Optional[str],
|
|
94
|
+
Doc(
|
|
95
|
+
"""
|
|
96
|
+
Security scheme name.
|
|
97
|
+
|
|
98
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
99
|
+
"""
|
|
100
|
+
),
|
|
101
|
+
] = None,
|
|
102
|
+
description: Annotated[
|
|
103
|
+
Optional[str],
|
|
104
|
+
Doc(
|
|
105
|
+
"""
|
|
106
|
+
Security scheme description.
|
|
107
|
+
|
|
108
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
109
|
+
"""
|
|
110
|
+
),
|
|
111
|
+
] = None,
|
|
112
|
+
auto_error: Annotated[
|
|
113
|
+
bool,
|
|
114
|
+
Doc(
|
|
115
|
+
"""
|
|
116
|
+
By default, if the query parameter is not provided, `APIKeyQuery` will
|
|
117
|
+
automatically cancel the request and send the client an error.
|
|
118
|
+
|
|
119
|
+
If `auto_error` is set to `False`, when the query parameter is not
|
|
120
|
+
available, instead of erroring out, the dependency result will be
|
|
121
|
+
`None`.
|
|
122
|
+
|
|
123
|
+
This is useful when you want to have optional authentication.
|
|
124
|
+
|
|
125
|
+
It is also useful when you want to have authentication that can be
|
|
126
|
+
provided in one of multiple optional ways (for example, in a query
|
|
127
|
+
parameter or in an HTTP Bearer token).
|
|
128
|
+
"""
|
|
129
|
+
),
|
|
130
|
+
] = True,
|
|
131
|
+
):
|
|
132
|
+
super().__init__(
|
|
133
|
+
location=APIKeyIn.query,
|
|
134
|
+
name=name,
|
|
135
|
+
scheme_name=scheme_name,
|
|
136
|
+
description=description,
|
|
137
|
+
auto_error=auto_error,
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
async def __call__(self, request: Request) -> Optional[str]:
|
|
141
|
+
api_key = request.query_params.get(self.model.name)
|
|
142
|
+
return self.check_api_key(api_key)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class APIKeyHeader(APIKeyBase):
|
|
146
|
+
"""
|
|
147
|
+
API key authentication using a header.
|
|
148
|
+
|
|
149
|
+
This defines the name of the header that should be provided in the request with
|
|
150
|
+
the API key and integrates that into the OpenAPI documentation. It extracts
|
|
151
|
+
the key value sent in the header automatically and provides it as the dependency
|
|
152
|
+
result. But it doesn't define how to send that key to the client.
|
|
153
|
+
|
|
154
|
+
## Usage
|
|
155
|
+
|
|
156
|
+
Create an instance object and use that object as the dependency in `Depends()`.
|
|
157
|
+
|
|
158
|
+
The dependency result will be a string containing the key value.
|
|
159
|
+
|
|
160
|
+
## Example
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from fastapi import Depends, FastAPI
|
|
164
|
+
from fastapi.security import APIKeyHeader
|
|
165
|
+
|
|
166
|
+
app = FastAPI()
|
|
167
|
+
|
|
168
|
+
header_scheme = APIKeyHeader(name="x-key")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
@app.get("/items/")
|
|
172
|
+
async def read_items(key: str = Depends(header_scheme)):
|
|
173
|
+
return {"key": key}
|
|
174
|
+
```
|
|
175
|
+
"""
|
|
176
|
+
|
|
177
|
+
def __init__(
|
|
178
|
+
self,
|
|
179
|
+
*,
|
|
180
|
+
name: Annotated[str, Doc("Header name.")],
|
|
181
|
+
scheme_name: Annotated[
|
|
182
|
+
Optional[str],
|
|
183
|
+
Doc(
|
|
184
|
+
"""
|
|
185
|
+
Security scheme name.
|
|
186
|
+
|
|
187
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
188
|
+
"""
|
|
189
|
+
),
|
|
190
|
+
] = None,
|
|
191
|
+
description: Annotated[
|
|
192
|
+
Optional[str],
|
|
193
|
+
Doc(
|
|
194
|
+
"""
|
|
195
|
+
Security scheme description.
|
|
196
|
+
|
|
197
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
198
|
+
"""
|
|
199
|
+
),
|
|
200
|
+
] = None,
|
|
201
|
+
auto_error: Annotated[
|
|
202
|
+
bool,
|
|
203
|
+
Doc(
|
|
204
|
+
"""
|
|
205
|
+
By default, if the header is not provided, `APIKeyHeader` will
|
|
206
|
+
automatically cancel the request and send the client an error.
|
|
207
|
+
|
|
208
|
+
If `auto_error` is set to `False`, when the header is not available,
|
|
209
|
+
instead of erroring out, the dependency result will be `None`.
|
|
210
|
+
|
|
211
|
+
This is useful when you want to have optional authentication.
|
|
212
|
+
|
|
213
|
+
It is also useful when you want to have authentication that can be
|
|
214
|
+
provided in one of multiple optional ways (for example, in a header or
|
|
215
|
+
in an HTTP Bearer token).
|
|
216
|
+
"""
|
|
217
|
+
),
|
|
218
|
+
] = True,
|
|
219
|
+
):
|
|
220
|
+
super().__init__(
|
|
221
|
+
location=APIKeyIn.header,
|
|
222
|
+
name=name,
|
|
223
|
+
scheme_name=scheme_name,
|
|
224
|
+
description=description,
|
|
225
|
+
auto_error=auto_error,
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
async def __call__(self, request: Request) -> Optional[str]:
|
|
229
|
+
api_key = request.headers.get(self.model.name)
|
|
230
|
+
return self.check_api_key(api_key)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
class APIKeyCookie(APIKeyBase):
|
|
234
|
+
"""
|
|
235
|
+
API key authentication using a cookie.
|
|
236
|
+
|
|
237
|
+
This defines the name of the cookie that should be provided in the request with
|
|
238
|
+
the API key and integrates that into the OpenAPI documentation. It extracts
|
|
239
|
+
the key value sent in the cookie automatically and provides it as the dependency
|
|
240
|
+
result. But it doesn't define how to set that cookie.
|
|
241
|
+
|
|
242
|
+
## Usage
|
|
243
|
+
|
|
244
|
+
Create an instance object and use that object as the dependency in `Depends()`.
|
|
245
|
+
|
|
246
|
+
The dependency result will be a string containing the key value.
|
|
247
|
+
|
|
248
|
+
## Example
|
|
249
|
+
|
|
250
|
+
```python
|
|
251
|
+
from fastapi import Depends, FastAPI
|
|
252
|
+
from fastapi.security import APIKeyCookie
|
|
253
|
+
|
|
254
|
+
app = FastAPI()
|
|
255
|
+
|
|
256
|
+
cookie_scheme = APIKeyCookie(name="session")
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
@app.get("/items/")
|
|
260
|
+
async def read_items(session: str = Depends(cookie_scheme)):
|
|
261
|
+
return {"session": session}
|
|
262
|
+
```
|
|
263
|
+
"""
|
|
264
|
+
|
|
265
|
+
def __init__(
|
|
266
|
+
self,
|
|
267
|
+
*,
|
|
268
|
+
name: Annotated[str, Doc("Cookie name.")],
|
|
269
|
+
scheme_name: Annotated[
|
|
270
|
+
Optional[str],
|
|
271
|
+
Doc(
|
|
272
|
+
"""
|
|
273
|
+
Security scheme name.
|
|
274
|
+
|
|
275
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
276
|
+
"""
|
|
277
|
+
),
|
|
278
|
+
] = None,
|
|
279
|
+
description: Annotated[
|
|
280
|
+
Optional[str],
|
|
281
|
+
Doc(
|
|
282
|
+
"""
|
|
283
|
+
Security scheme description.
|
|
284
|
+
|
|
285
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
286
|
+
"""
|
|
287
|
+
),
|
|
288
|
+
] = None,
|
|
289
|
+
auto_error: Annotated[
|
|
290
|
+
bool,
|
|
291
|
+
Doc(
|
|
292
|
+
"""
|
|
293
|
+
By default, if the cookie is not provided, `APIKeyCookie` will
|
|
294
|
+
automatically cancel the request and send the client an error.
|
|
295
|
+
|
|
296
|
+
If `auto_error` is set to `False`, when the cookie is not available,
|
|
297
|
+
instead of erroring out, the dependency result will be `None`.
|
|
298
|
+
|
|
299
|
+
This is useful when you want to have optional authentication.
|
|
300
|
+
|
|
301
|
+
It is also useful when you want to have authentication that can be
|
|
302
|
+
provided in one of multiple optional ways (for example, in a cookie or
|
|
303
|
+
in an HTTP Bearer token).
|
|
304
|
+
"""
|
|
305
|
+
),
|
|
306
|
+
] = True,
|
|
307
|
+
):
|
|
308
|
+
super().__init__(
|
|
309
|
+
location=APIKeyIn.cookie,
|
|
310
|
+
name=name,
|
|
311
|
+
scheme_name=scheme_name,
|
|
312
|
+
description=description,
|
|
313
|
+
auto_error=auto_error,
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
async def __call__(self, request: Request) -> Optional[str]:
|
|
317
|
+
api_key = request.cookies.get(self.model.name)
|
|
318
|
+
return self.check_api_key(api_key)
|