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
fastapi/security/http.py
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import binascii
|
|
2
|
+
from base64 import b64decode
|
|
3
|
+
from typing import Annotated, Optional
|
|
4
|
+
|
|
5
|
+
from annotated_doc import Doc
|
|
6
|
+
from fastapi.exceptions import HTTPException
|
|
7
|
+
from fastapi.openapi.models import HTTPBase as HTTPBaseModel
|
|
8
|
+
from fastapi.openapi.models import HTTPBearer as HTTPBearerModel
|
|
9
|
+
from fastapi.security.base import SecurityBase
|
|
10
|
+
from fastapi.security.utils import get_authorization_scheme_param
|
|
11
|
+
from pydantic import BaseModel
|
|
12
|
+
from starlette.requests import Request
|
|
13
|
+
from starlette.status import HTTP_401_UNAUTHORIZED
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class HTTPBasicCredentials(BaseModel):
|
|
17
|
+
"""
|
|
18
|
+
The HTTP Basic credentials given as the result of using `HTTPBasic` in a
|
|
19
|
+
dependency.
|
|
20
|
+
|
|
21
|
+
Read more about it in the
|
|
22
|
+
[FastAPI docs for HTTP Basic Auth](https://fastapi.tiangolo.com/advanced/security/http-basic-auth/).
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
username: Annotated[str, Doc("The HTTP Basic username.")]
|
|
26
|
+
password: Annotated[str, Doc("The HTTP Basic password.")]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class HTTPAuthorizationCredentials(BaseModel):
|
|
30
|
+
"""
|
|
31
|
+
The HTTP authorization credentials in the result of using `HTTPBearer` or
|
|
32
|
+
`HTTPDigest` in a dependency.
|
|
33
|
+
|
|
34
|
+
The HTTP authorization header value is split by the first space.
|
|
35
|
+
|
|
36
|
+
The first part is the `scheme`, the second part is the `credentials`.
|
|
37
|
+
|
|
38
|
+
For example, in an HTTP Bearer token scheme, the client will send a header
|
|
39
|
+
like:
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
Authorization: Bearer deadbeef12346
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
In this case:
|
|
46
|
+
|
|
47
|
+
* `scheme` will have the value `"Bearer"`
|
|
48
|
+
* `credentials` will have the value `"deadbeef12346"`
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
scheme: Annotated[
|
|
52
|
+
str,
|
|
53
|
+
Doc(
|
|
54
|
+
"""
|
|
55
|
+
The HTTP authorization scheme extracted from the header value.
|
|
56
|
+
"""
|
|
57
|
+
),
|
|
58
|
+
]
|
|
59
|
+
credentials: Annotated[
|
|
60
|
+
str,
|
|
61
|
+
Doc(
|
|
62
|
+
"""
|
|
63
|
+
The HTTP authorization credentials extracted from the header value.
|
|
64
|
+
"""
|
|
65
|
+
),
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class HTTPBase(SecurityBase):
|
|
70
|
+
def __init__(
|
|
71
|
+
self,
|
|
72
|
+
*,
|
|
73
|
+
scheme: str,
|
|
74
|
+
scheme_name: Optional[str] = None,
|
|
75
|
+
description: Optional[str] = None,
|
|
76
|
+
auto_error: bool = True,
|
|
77
|
+
):
|
|
78
|
+
self.model: HTTPBaseModel = HTTPBaseModel(
|
|
79
|
+
scheme=scheme, description=description
|
|
80
|
+
)
|
|
81
|
+
self.scheme_name = scheme_name or self.__class__.__name__
|
|
82
|
+
self.auto_error = auto_error
|
|
83
|
+
|
|
84
|
+
def make_authenticate_headers(self) -> dict[str, str]:
|
|
85
|
+
return {"WWW-Authenticate": f"{self.model.scheme.title()}"}
|
|
86
|
+
|
|
87
|
+
def make_not_authenticated_error(self) -> HTTPException:
|
|
88
|
+
return HTTPException(
|
|
89
|
+
status_code=HTTP_401_UNAUTHORIZED,
|
|
90
|
+
detail="Not authenticated",
|
|
91
|
+
headers=self.make_authenticate_headers(),
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
async def __call__(
|
|
95
|
+
self, request: Request
|
|
96
|
+
) -> Optional[HTTPAuthorizationCredentials]:
|
|
97
|
+
authorization = request.headers.get("Authorization")
|
|
98
|
+
scheme, credentials = get_authorization_scheme_param(authorization)
|
|
99
|
+
if not (authorization and scheme and credentials):
|
|
100
|
+
if self.auto_error:
|
|
101
|
+
raise self.make_not_authenticated_error()
|
|
102
|
+
else:
|
|
103
|
+
return None
|
|
104
|
+
return HTTPAuthorizationCredentials(scheme=scheme, credentials=credentials)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
class HTTPBasic(HTTPBase):
|
|
108
|
+
"""
|
|
109
|
+
HTTP Basic authentication.
|
|
110
|
+
|
|
111
|
+
Ref: https://datatracker.ietf.org/doc/html/rfc7617
|
|
112
|
+
|
|
113
|
+
## Usage
|
|
114
|
+
|
|
115
|
+
Create an instance object and use that object as the dependency in `Depends()`.
|
|
116
|
+
|
|
117
|
+
The dependency result will be an `HTTPBasicCredentials` object containing the
|
|
118
|
+
`username` and the `password`.
|
|
119
|
+
|
|
120
|
+
Read more about it in the
|
|
121
|
+
[FastAPI docs for HTTP Basic Auth](https://fastapi.tiangolo.com/advanced/security/http-basic-auth/).
|
|
122
|
+
|
|
123
|
+
## Example
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from typing import Annotated
|
|
127
|
+
|
|
128
|
+
from fastapi import Depends, FastAPI
|
|
129
|
+
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
|
130
|
+
|
|
131
|
+
app = FastAPI()
|
|
132
|
+
|
|
133
|
+
security = HTTPBasic()
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@app.get("/users/me")
|
|
137
|
+
def read_current_user(credentials: Annotated[HTTPBasicCredentials, Depends(security)]):
|
|
138
|
+
return {"username": credentials.username, "password": credentials.password}
|
|
139
|
+
```
|
|
140
|
+
"""
|
|
141
|
+
|
|
142
|
+
def __init__(
|
|
143
|
+
self,
|
|
144
|
+
*,
|
|
145
|
+
scheme_name: Annotated[
|
|
146
|
+
Optional[str],
|
|
147
|
+
Doc(
|
|
148
|
+
"""
|
|
149
|
+
Security scheme name.
|
|
150
|
+
|
|
151
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
152
|
+
"""
|
|
153
|
+
),
|
|
154
|
+
] = None,
|
|
155
|
+
realm: Annotated[
|
|
156
|
+
Optional[str],
|
|
157
|
+
Doc(
|
|
158
|
+
"""
|
|
159
|
+
HTTP Basic authentication realm.
|
|
160
|
+
"""
|
|
161
|
+
),
|
|
162
|
+
] = None,
|
|
163
|
+
description: Annotated[
|
|
164
|
+
Optional[str],
|
|
165
|
+
Doc(
|
|
166
|
+
"""
|
|
167
|
+
Security scheme description.
|
|
168
|
+
|
|
169
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
170
|
+
"""
|
|
171
|
+
),
|
|
172
|
+
] = None,
|
|
173
|
+
auto_error: Annotated[
|
|
174
|
+
bool,
|
|
175
|
+
Doc(
|
|
176
|
+
"""
|
|
177
|
+
By default, if the HTTP Basic authentication is not provided (a
|
|
178
|
+
header), `HTTPBasic` will automatically cancel the request and send the
|
|
179
|
+
client an error.
|
|
180
|
+
|
|
181
|
+
If `auto_error` is set to `False`, when the HTTP Basic authentication
|
|
182
|
+
is not available, instead of erroring out, the dependency result will
|
|
183
|
+
be `None`.
|
|
184
|
+
|
|
185
|
+
This is useful when you want to have optional authentication.
|
|
186
|
+
|
|
187
|
+
It is also useful when you want to have authentication that can be
|
|
188
|
+
provided in one of multiple optional ways (for example, in HTTP Basic
|
|
189
|
+
authentication or in an HTTP Bearer token).
|
|
190
|
+
"""
|
|
191
|
+
),
|
|
192
|
+
] = True,
|
|
193
|
+
):
|
|
194
|
+
self.model = HTTPBaseModel(scheme="basic", description=description)
|
|
195
|
+
self.scheme_name = scheme_name or self.__class__.__name__
|
|
196
|
+
self.realm = realm
|
|
197
|
+
self.auto_error = auto_error
|
|
198
|
+
|
|
199
|
+
def make_authenticate_headers(self) -> dict[str, str]:
|
|
200
|
+
if self.realm:
|
|
201
|
+
return {"WWW-Authenticate": f'Basic realm="{self.realm}"'}
|
|
202
|
+
return {"WWW-Authenticate": "Basic"}
|
|
203
|
+
|
|
204
|
+
async def __call__( # type: ignore
|
|
205
|
+
self, request: Request
|
|
206
|
+
) -> Optional[HTTPBasicCredentials]:
|
|
207
|
+
authorization = request.headers.get("Authorization")
|
|
208
|
+
scheme, param = get_authorization_scheme_param(authorization)
|
|
209
|
+
if not authorization or scheme.lower() != "basic":
|
|
210
|
+
if self.auto_error:
|
|
211
|
+
raise self.make_not_authenticated_error()
|
|
212
|
+
else:
|
|
213
|
+
return None
|
|
214
|
+
try:
|
|
215
|
+
data = b64decode(param).decode("ascii")
|
|
216
|
+
except (ValueError, UnicodeDecodeError, binascii.Error) as e:
|
|
217
|
+
raise self.make_not_authenticated_error() from e
|
|
218
|
+
username, separator, password = data.partition(":")
|
|
219
|
+
if not separator:
|
|
220
|
+
raise self.make_not_authenticated_error()
|
|
221
|
+
return HTTPBasicCredentials(username=username, password=password)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
class HTTPBearer(HTTPBase):
|
|
225
|
+
"""
|
|
226
|
+
HTTP Bearer token authentication.
|
|
227
|
+
|
|
228
|
+
## Usage
|
|
229
|
+
|
|
230
|
+
Create an instance object and use that object as the dependency in `Depends()`.
|
|
231
|
+
|
|
232
|
+
The dependency result will be an `HTTPAuthorizationCredentials` object containing
|
|
233
|
+
the `scheme` and the `credentials`.
|
|
234
|
+
|
|
235
|
+
## Example
|
|
236
|
+
|
|
237
|
+
```python
|
|
238
|
+
from typing import Annotated
|
|
239
|
+
|
|
240
|
+
from fastapi import Depends, FastAPI
|
|
241
|
+
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
242
|
+
|
|
243
|
+
app = FastAPI()
|
|
244
|
+
|
|
245
|
+
security = HTTPBearer()
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
@app.get("/users/me")
|
|
249
|
+
def read_current_user(
|
|
250
|
+
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)]
|
|
251
|
+
):
|
|
252
|
+
return {"scheme": credentials.scheme, "credentials": credentials.credentials}
|
|
253
|
+
```
|
|
254
|
+
"""
|
|
255
|
+
|
|
256
|
+
def __init__(
|
|
257
|
+
self,
|
|
258
|
+
*,
|
|
259
|
+
bearerFormat: Annotated[Optional[str], Doc("Bearer token format.")] = None,
|
|
260
|
+
scheme_name: Annotated[
|
|
261
|
+
Optional[str],
|
|
262
|
+
Doc(
|
|
263
|
+
"""
|
|
264
|
+
Security scheme name.
|
|
265
|
+
|
|
266
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
267
|
+
"""
|
|
268
|
+
),
|
|
269
|
+
] = None,
|
|
270
|
+
description: Annotated[
|
|
271
|
+
Optional[str],
|
|
272
|
+
Doc(
|
|
273
|
+
"""
|
|
274
|
+
Security scheme description.
|
|
275
|
+
|
|
276
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
277
|
+
"""
|
|
278
|
+
),
|
|
279
|
+
] = None,
|
|
280
|
+
auto_error: Annotated[
|
|
281
|
+
bool,
|
|
282
|
+
Doc(
|
|
283
|
+
"""
|
|
284
|
+
By default, if the HTTP Bearer token is not provided (in an
|
|
285
|
+
`Authorization` header), `HTTPBearer` will automatically cancel the
|
|
286
|
+
request and send the client an error.
|
|
287
|
+
|
|
288
|
+
If `auto_error` is set to `False`, when the HTTP Bearer token
|
|
289
|
+
is not available, instead of erroring out, the dependency result will
|
|
290
|
+
be `None`.
|
|
291
|
+
|
|
292
|
+
This is useful when you want to have optional authentication.
|
|
293
|
+
|
|
294
|
+
It is also useful when you want to have authentication that can be
|
|
295
|
+
provided in one of multiple optional ways (for example, in an HTTP
|
|
296
|
+
Bearer token or in a cookie).
|
|
297
|
+
"""
|
|
298
|
+
),
|
|
299
|
+
] = True,
|
|
300
|
+
):
|
|
301
|
+
self.model = HTTPBearerModel(bearerFormat=bearerFormat, description=description)
|
|
302
|
+
self.scheme_name = scheme_name or self.__class__.__name__
|
|
303
|
+
self.auto_error = auto_error
|
|
304
|
+
|
|
305
|
+
async def __call__(
|
|
306
|
+
self, request: Request
|
|
307
|
+
) -> Optional[HTTPAuthorizationCredentials]:
|
|
308
|
+
authorization = request.headers.get("Authorization")
|
|
309
|
+
scheme, credentials = get_authorization_scheme_param(authorization)
|
|
310
|
+
if not (authorization and scheme and credentials):
|
|
311
|
+
if self.auto_error:
|
|
312
|
+
raise self.make_not_authenticated_error()
|
|
313
|
+
else:
|
|
314
|
+
return None
|
|
315
|
+
if scheme.lower() != "bearer":
|
|
316
|
+
if self.auto_error:
|
|
317
|
+
raise self.make_not_authenticated_error()
|
|
318
|
+
else:
|
|
319
|
+
return None
|
|
320
|
+
return HTTPAuthorizationCredentials(scheme=scheme, credentials=credentials)
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
class HTTPDigest(HTTPBase):
|
|
324
|
+
"""
|
|
325
|
+
HTTP Digest authentication.
|
|
326
|
+
|
|
327
|
+
**Warning**: this is only a stub to connect the components with OpenAPI in FastAPI,
|
|
328
|
+
but it doesn't implement the full Digest scheme, you would need to to subclass it
|
|
329
|
+
and implement it in your code.
|
|
330
|
+
|
|
331
|
+
Ref: https://datatracker.ietf.org/doc/html/rfc7616
|
|
332
|
+
|
|
333
|
+
## Usage
|
|
334
|
+
|
|
335
|
+
Create an instance object and use that object as the dependency in `Depends()`.
|
|
336
|
+
|
|
337
|
+
The dependency result will be an `HTTPAuthorizationCredentials` object containing
|
|
338
|
+
the `scheme` and the `credentials`.
|
|
339
|
+
|
|
340
|
+
## Example
|
|
341
|
+
|
|
342
|
+
```python
|
|
343
|
+
from typing import Annotated
|
|
344
|
+
|
|
345
|
+
from fastapi import Depends, FastAPI
|
|
346
|
+
from fastapi.security import HTTPAuthorizationCredentials, HTTPDigest
|
|
347
|
+
|
|
348
|
+
app = FastAPI()
|
|
349
|
+
|
|
350
|
+
security = HTTPDigest()
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
@app.get("/users/me")
|
|
354
|
+
def read_current_user(
|
|
355
|
+
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)]
|
|
356
|
+
):
|
|
357
|
+
return {"scheme": credentials.scheme, "credentials": credentials.credentials}
|
|
358
|
+
```
|
|
359
|
+
"""
|
|
360
|
+
|
|
361
|
+
def __init__(
|
|
362
|
+
self,
|
|
363
|
+
*,
|
|
364
|
+
scheme_name: Annotated[
|
|
365
|
+
Optional[str],
|
|
366
|
+
Doc(
|
|
367
|
+
"""
|
|
368
|
+
Security scheme name.
|
|
369
|
+
|
|
370
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
371
|
+
"""
|
|
372
|
+
),
|
|
373
|
+
] = None,
|
|
374
|
+
description: Annotated[
|
|
375
|
+
Optional[str],
|
|
376
|
+
Doc(
|
|
377
|
+
"""
|
|
378
|
+
Security scheme description.
|
|
379
|
+
|
|
380
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
381
|
+
"""
|
|
382
|
+
),
|
|
383
|
+
] = None,
|
|
384
|
+
auto_error: Annotated[
|
|
385
|
+
bool,
|
|
386
|
+
Doc(
|
|
387
|
+
"""
|
|
388
|
+
By default, if the HTTP Digest is not provided, `HTTPDigest` will
|
|
389
|
+
automatically cancel the request and send the client an error.
|
|
390
|
+
|
|
391
|
+
If `auto_error` is set to `False`, when the HTTP Digest is not
|
|
392
|
+
available, instead of erroring out, the dependency result will
|
|
393
|
+
be `None`.
|
|
394
|
+
|
|
395
|
+
This is useful when you want to have optional authentication.
|
|
396
|
+
|
|
397
|
+
It is also useful when you want to have authentication that can be
|
|
398
|
+
provided in one of multiple optional ways (for example, in HTTP
|
|
399
|
+
Digest or in a cookie).
|
|
400
|
+
"""
|
|
401
|
+
),
|
|
402
|
+
] = True,
|
|
403
|
+
):
|
|
404
|
+
self.model = HTTPBaseModel(scheme="digest", description=description)
|
|
405
|
+
self.scheme_name = scheme_name or self.__class__.__name__
|
|
406
|
+
self.auto_error = auto_error
|
|
407
|
+
|
|
408
|
+
async def __call__(
|
|
409
|
+
self, request: Request
|
|
410
|
+
) -> Optional[HTTPAuthorizationCredentials]:
|
|
411
|
+
authorization = request.headers.get("Authorization")
|
|
412
|
+
scheme, credentials = get_authorization_scheme_param(authorization)
|
|
413
|
+
if not (authorization and scheme and credentials):
|
|
414
|
+
if self.auto_error:
|
|
415
|
+
raise self.make_not_authenticated_error()
|
|
416
|
+
else:
|
|
417
|
+
return None
|
|
418
|
+
if scheme.lower() != "digest":
|
|
419
|
+
if self.auto_error:
|
|
420
|
+
raise self.make_not_authenticated_error()
|
|
421
|
+
else:
|
|
422
|
+
return None
|
|
423
|
+
return HTTPAuthorizationCredentials(scheme=scheme, credentials=credentials)
|