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,663 @@
|
|
|
1
|
+
from typing import Annotated, Any, Optional, Union, cast
|
|
2
|
+
|
|
3
|
+
from annotated_doc import Doc
|
|
4
|
+
from fastapi.exceptions import HTTPException
|
|
5
|
+
from fastapi.openapi.models import OAuth2 as OAuth2Model
|
|
6
|
+
from fastapi.openapi.models import OAuthFlows as OAuthFlowsModel
|
|
7
|
+
from fastapi.param_functions import Form
|
|
8
|
+
from fastapi.security.base import SecurityBase
|
|
9
|
+
from fastapi.security.utils import get_authorization_scheme_param
|
|
10
|
+
from starlette.requests import Request
|
|
11
|
+
from starlette.status import HTTP_401_UNAUTHORIZED
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class OAuth2PasswordRequestForm:
|
|
15
|
+
"""
|
|
16
|
+
This is a dependency class to collect the `username` and `password` as form data
|
|
17
|
+
for an OAuth2 password flow.
|
|
18
|
+
|
|
19
|
+
The OAuth2 specification dictates that for a password flow the data should be
|
|
20
|
+
collected using form data (instead of JSON) and that it should have the specific
|
|
21
|
+
fields `username` and `password`.
|
|
22
|
+
|
|
23
|
+
All the initialization parameters are extracted from the request.
|
|
24
|
+
|
|
25
|
+
Read more about it in the
|
|
26
|
+
[FastAPI docs for Simple OAuth2 with Password and Bearer](https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/).
|
|
27
|
+
|
|
28
|
+
## Example
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from typing import Annotated
|
|
32
|
+
|
|
33
|
+
from fastapi import Depends, FastAPI
|
|
34
|
+
from fastapi.security import OAuth2PasswordRequestForm
|
|
35
|
+
|
|
36
|
+
app = FastAPI()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@app.post("/login")
|
|
40
|
+
def login(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]):
|
|
41
|
+
data = {}
|
|
42
|
+
data["scopes"] = []
|
|
43
|
+
for scope in form_data.scopes:
|
|
44
|
+
data["scopes"].append(scope)
|
|
45
|
+
if form_data.client_id:
|
|
46
|
+
data["client_id"] = form_data.client_id
|
|
47
|
+
if form_data.client_secret:
|
|
48
|
+
data["client_secret"] = form_data.client_secret
|
|
49
|
+
return data
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Note that for OAuth2 the scope `items:read` is a single scope in an opaque string.
|
|
53
|
+
You could have custom internal logic to separate it by colon characters (`:`) or
|
|
54
|
+
similar, and get the two parts `items` and `read`. Many applications do that to
|
|
55
|
+
group and organize permissions, you could do it as well in your application, just
|
|
56
|
+
know that that it is application specific, it's not part of the specification.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
def __init__(
|
|
60
|
+
self,
|
|
61
|
+
*,
|
|
62
|
+
grant_type: Annotated[
|
|
63
|
+
Union[str, None],
|
|
64
|
+
Form(pattern="^password$"),
|
|
65
|
+
Doc(
|
|
66
|
+
"""
|
|
67
|
+
The OAuth2 spec says it is required and MUST be the fixed string
|
|
68
|
+
"password". Nevertheless, this dependency class is permissive and
|
|
69
|
+
allows not passing it. If you want to enforce it, use instead the
|
|
70
|
+
`OAuth2PasswordRequestFormStrict` dependency.
|
|
71
|
+
"""
|
|
72
|
+
),
|
|
73
|
+
] = None,
|
|
74
|
+
username: Annotated[
|
|
75
|
+
str,
|
|
76
|
+
Form(),
|
|
77
|
+
Doc(
|
|
78
|
+
"""
|
|
79
|
+
`username` string. The OAuth2 spec requires the exact field name
|
|
80
|
+
`username`.
|
|
81
|
+
"""
|
|
82
|
+
),
|
|
83
|
+
],
|
|
84
|
+
password: Annotated[
|
|
85
|
+
str,
|
|
86
|
+
Form(json_schema_extra={"format": "password"}),
|
|
87
|
+
Doc(
|
|
88
|
+
"""
|
|
89
|
+
`password` string. The OAuth2 spec requires the exact field name
|
|
90
|
+
`password`.
|
|
91
|
+
"""
|
|
92
|
+
),
|
|
93
|
+
],
|
|
94
|
+
scope: Annotated[
|
|
95
|
+
str,
|
|
96
|
+
Form(),
|
|
97
|
+
Doc(
|
|
98
|
+
"""
|
|
99
|
+
A single string with actually several scopes separated by spaces. Each
|
|
100
|
+
scope is also a string.
|
|
101
|
+
|
|
102
|
+
For example, a single string with:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
"items:read items:write users:read profile openid"
|
|
106
|
+
````
|
|
107
|
+
|
|
108
|
+
would represent the scopes:
|
|
109
|
+
|
|
110
|
+
* `items:read`
|
|
111
|
+
* `items:write`
|
|
112
|
+
* `users:read`
|
|
113
|
+
* `profile`
|
|
114
|
+
* `openid`
|
|
115
|
+
"""
|
|
116
|
+
),
|
|
117
|
+
] = "",
|
|
118
|
+
client_id: Annotated[
|
|
119
|
+
Union[str, None],
|
|
120
|
+
Form(),
|
|
121
|
+
Doc(
|
|
122
|
+
"""
|
|
123
|
+
If there's a `client_id`, it can be sent as part of the form fields.
|
|
124
|
+
But the OAuth2 specification recommends sending the `client_id` and
|
|
125
|
+
`client_secret` (if any) using HTTP Basic auth.
|
|
126
|
+
"""
|
|
127
|
+
),
|
|
128
|
+
] = None,
|
|
129
|
+
client_secret: Annotated[
|
|
130
|
+
Union[str, None],
|
|
131
|
+
Form(json_schema_extra={"format": "password"}),
|
|
132
|
+
Doc(
|
|
133
|
+
"""
|
|
134
|
+
If there's a `client_password` (and a `client_id`), they can be sent
|
|
135
|
+
as part of the form fields. But the OAuth2 specification recommends
|
|
136
|
+
sending the `client_id` and `client_secret` (if any) using HTTP Basic
|
|
137
|
+
auth.
|
|
138
|
+
"""
|
|
139
|
+
),
|
|
140
|
+
] = None,
|
|
141
|
+
):
|
|
142
|
+
self.grant_type = grant_type
|
|
143
|
+
self.username = username
|
|
144
|
+
self.password = password
|
|
145
|
+
self.scopes = scope.split()
|
|
146
|
+
self.client_id = client_id
|
|
147
|
+
self.client_secret = client_secret
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class OAuth2PasswordRequestFormStrict(OAuth2PasswordRequestForm):
|
|
151
|
+
"""
|
|
152
|
+
This is a dependency class to collect the `username` and `password` as form data
|
|
153
|
+
for an OAuth2 password flow.
|
|
154
|
+
|
|
155
|
+
The OAuth2 specification dictates that for a password flow the data should be
|
|
156
|
+
collected using form data (instead of JSON) and that it should have the specific
|
|
157
|
+
fields `username` and `password`.
|
|
158
|
+
|
|
159
|
+
All the initialization parameters are extracted from the request.
|
|
160
|
+
|
|
161
|
+
The only difference between `OAuth2PasswordRequestFormStrict` and
|
|
162
|
+
`OAuth2PasswordRequestForm` is that `OAuth2PasswordRequestFormStrict` requires the
|
|
163
|
+
client to send the form field `grant_type` with the value `"password"`, which
|
|
164
|
+
is required in the OAuth2 specification (it seems that for no particular reason),
|
|
165
|
+
while for `OAuth2PasswordRequestForm` `grant_type` is optional.
|
|
166
|
+
|
|
167
|
+
Read more about it in the
|
|
168
|
+
[FastAPI docs for Simple OAuth2 with Password and Bearer](https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/).
|
|
169
|
+
|
|
170
|
+
## Example
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
from typing import Annotated
|
|
174
|
+
|
|
175
|
+
from fastapi import Depends, FastAPI
|
|
176
|
+
from fastapi.security import OAuth2PasswordRequestForm
|
|
177
|
+
|
|
178
|
+
app = FastAPI()
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@app.post("/login")
|
|
182
|
+
def login(form_data: Annotated[OAuth2PasswordRequestFormStrict, Depends()]):
|
|
183
|
+
data = {}
|
|
184
|
+
data["scopes"] = []
|
|
185
|
+
for scope in form_data.scopes:
|
|
186
|
+
data["scopes"].append(scope)
|
|
187
|
+
if form_data.client_id:
|
|
188
|
+
data["client_id"] = form_data.client_id
|
|
189
|
+
if form_data.client_secret:
|
|
190
|
+
data["client_secret"] = form_data.client_secret
|
|
191
|
+
return data
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Note that for OAuth2 the scope `items:read` is a single scope in an opaque string.
|
|
195
|
+
You could have custom internal logic to separate it by colon characters (`:`) or
|
|
196
|
+
similar, and get the two parts `items` and `read`. Many applications do that to
|
|
197
|
+
group and organize permissions, you could do it as well in your application, just
|
|
198
|
+
know that that it is application specific, it's not part of the specification.
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
grant_type: the OAuth2 spec says it is required and MUST be the fixed string "password".
|
|
202
|
+
This dependency is strict about it. If you want to be permissive, use instead the
|
|
203
|
+
OAuth2PasswordRequestForm dependency class.
|
|
204
|
+
username: username string. The OAuth2 spec requires the exact field name "username".
|
|
205
|
+
password: password string. The OAuth2 spec requires the exact field name "password".
|
|
206
|
+
scope: Optional string. Several scopes (each one a string) separated by spaces. E.g.
|
|
207
|
+
"items:read items:write users:read profile openid"
|
|
208
|
+
client_id: optional string. OAuth2 recommends sending the client_id and client_secret (if any)
|
|
209
|
+
using HTTP Basic auth, as: client_id:client_secret
|
|
210
|
+
client_secret: optional string. OAuth2 recommends sending the client_id and client_secret (if any)
|
|
211
|
+
using HTTP Basic auth, as: client_id:client_secret
|
|
212
|
+
"""
|
|
213
|
+
|
|
214
|
+
def __init__(
|
|
215
|
+
self,
|
|
216
|
+
grant_type: Annotated[
|
|
217
|
+
str,
|
|
218
|
+
Form(pattern="^password$"),
|
|
219
|
+
Doc(
|
|
220
|
+
"""
|
|
221
|
+
The OAuth2 spec says it is required and MUST be the fixed string
|
|
222
|
+
"password". This dependency is strict about it. If you want to be
|
|
223
|
+
permissive, use instead the `OAuth2PasswordRequestForm` dependency
|
|
224
|
+
class.
|
|
225
|
+
"""
|
|
226
|
+
),
|
|
227
|
+
],
|
|
228
|
+
username: Annotated[
|
|
229
|
+
str,
|
|
230
|
+
Form(),
|
|
231
|
+
Doc(
|
|
232
|
+
"""
|
|
233
|
+
`username` string. The OAuth2 spec requires the exact field name
|
|
234
|
+
`username`.
|
|
235
|
+
"""
|
|
236
|
+
),
|
|
237
|
+
],
|
|
238
|
+
password: Annotated[
|
|
239
|
+
str,
|
|
240
|
+
Form(),
|
|
241
|
+
Doc(
|
|
242
|
+
"""
|
|
243
|
+
`password` string. The OAuth2 spec requires the exact field name
|
|
244
|
+
`password`.
|
|
245
|
+
"""
|
|
246
|
+
),
|
|
247
|
+
],
|
|
248
|
+
scope: Annotated[
|
|
249
|
+
str,
|
|
250
|
+
Form(),
|
|
251
|
+
Doc(
|
|
252
|
+
"""
|
|
253
|
+
A single string with actually several scopes separated by spaces. Each
|
|
254
|
+
scope is also a string.
|
|
255
|
+
|
|
256
|
+
For example, a single string with:
|
|
257
|
+
|
|
258
|
+
```python
|
|
259
|
+
"items:read items:write users:read profile openid"
|
|
260
|
+
````
|
|
261
|
+
|
|
262
|
+
would represent the scopes:
|
|
263
|
+
|
|
264
|
+
* `items:read`
|
|
265
|
+
* `items:write`
|
|
266
|
+
* `users:read`
|
|
267
|
+
* `profile`
|
|
268
|
+
* `openid`
|
|
269
|
+
"""
|
|
270
|
+
),
|
|
271
|
+
] = "",
|
|
272
|
+
client_id: Annotated[
|
|
273
|
+
Union[str, None],
|
|
274
|
+
Form(),
|
|
275
|
+
Doc(
|
|
276
|
+
"""
|
|
277
|
+
If there's a `client_id`, it can be sent as part of the form fields.
|
|
278
|
+
But the OAuth2 specification recommends sending the `client_id` and
|
|
279
|
+
`client_secret` (if any) using HTTP Basic auth.
|
|
280
|
+
"""
|
|
281
|
+
),
|
|
282
|
+
] = None,
|
|
283
|
+
client_secret: Annotated[
|
|
284
|
+
Union[str, None],
|
|
285
|
+
Form(),
|
|
286
|
+
Doc(
|
|
287
|
+
"""
|
|
288
|
+
If there's a `client_password` (and a `client_id`), they can be sent
|
|
289
|
+
as part of the form fields. But the OAuth2 specification recommends
|
|
290
|
+
sending the `client_id` and `client_secret` (if any) using HTTP Basic
|
|
291
|
+
auth.
|
|
292
|
+
"""
|
|
293
|
+
),
|
|
294
|
+
] = None,
|
|
295
|
+
):
|
|
296
|
+
super().__init__(
|
|
297
|
+
grant_type=grant_type,
|
|
298
|
+
username=username,
|
|
299
|
+
password=password,
|
|
300
|
+
scope=scope,
|
|
301
|
+
client_id=client_id,
|
|
302
|
+
client_secret=client_secret,
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
class OAuth2(SecurityBase):
|
|
307
|
+
"""
|
|
308
|
+
This is the base class for OAuth2 authentication, an instance of it would be used
|
|
309
|
+
as a dependency. All other OAuth2 classes inherit from it and customize it for
|
|
310
|
+
each OAuth2 flow.
|
|
311
|
+
|
|
312
|
+
You normally would not create a new class inheriting from it but use one of the
|
|
313
|
+
existing subclasses, and maybe compose them if you want to support multiple flows.
|
|
314
|
+
|
|
315
|
+
Read more about it in the
|
|
316
|
+
[FastAPI docs for Security](https://fastapi.tiangolo.com/tutorial/security/).
|
|
317
|
+
"""
|
|
318
|
+
|
|
319
|
+
def __init__(
|
|
320
|
+
self,
|
|
321
|
+
*,
|
|
322
|
+
flows: Annotated[
|
|
323
|
+
Union[OAuthFlowsModel, dict[str, dict[str, Any]]],
|
|
324
|
+
Doc(
|
|
325
|
+
"""
|
|
326
|
+
The dictionary of OAuth2 flows.
|
|
327
|
+
"""
|
|
328
|
+
),
|
|
329
|
+
] = OAuthFlowsModel(),
|
|
330
|
+
scheme_name: Annotated[
|
|
331
|
+
Optional[str],
|
|
332
|
+
Doc(
|
|
333
|
+
"""
|
|
334
|
+
Security scheme name.
|
|
335
|
+
|
|
336
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
337
|
+
"""
|
|
338
|
+
),
|
|
339
|
+
] = None,
|
|
340
|
+
description: Annotated[
|
|
341
|
+
Optional[str],
|
|
342
|
+
Doc(
|
|
343
|
+
"""
|
|
344
|
+
Security scheme description.
|
|
345
|
+
|
|
346
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
347
|
+
"""
|
|
348
|
+
),
|
|
349
|
+
] = None,
|
|
350
|
+
auto_error: Annotated[
|
|
351
|
+
bool,
|
|
352
|
+
Doc(
|
|
353
|
+
"""
|
|
354
|
+
By default, if no HTTP Authorization header is provided, required for
|
|
355
|
+
OAuth2 authentication, it will automatically cancel the request and
|
|
356
|
+
send the client an error.
|
|
357
|
+
|
|
358
|
+
If `auto_error` is set to `False`, when the HTTP Authorization header
|
|
359
|
+
is not available, instead of erroring out, the dependency result will
|
|
360
|
+
be `None`.
|
|
361
|
+
|
|
362
|
+
This is useful when you want to have optional authentication.
|
|
363
|
+
|
|
364
|
+
It is also useful when you want to have authentication that can be
|
|
365
|
+
provided in one of multiple optional ways (for example, with OAuth2
|
|
366
|
+
or in a cookie).
|
|
367
|
+
"""
|
|
368
|
+
),
|
|
369
|
+
] = True,
|
|
370
|
+
):
|
|
371
|
+
self.model = OAuth2Model(
|
|
372
|
+
flows=cast(OAuthFlowsModel, flows), description=description
|
|
373
|
+
)
|
|
374
|
+
self.scheme_name = scheme_name or self.__class__.__name__
|
|
375
|
+
self.auto_error = auto_error
|
|
376
|
+
|
|
377
|
+
def make_not_authenticated_error(self) -> HTTPException:
|
|
378
|
+
"""
|
|
379
|
+
The OAuth 2 specification doesn't define the challenge that should be used,
|
|
380
|
+
because a `Bearer` token is not really the only option to authenticate.
|
|
381
|
+
|
|
382
|
+
But declaring any other authentication challenge would be application-specific
|
|
383
|
+
as it's not defined in the specification.
|
|
384
|
+
|
|
385
|
+
For practical reasons, this method uses the `Bearer` challenge by default, as
|
|
386
|
+
it's probably the most common one.
|
|
387
|
+
|
|
388
|
+
If you are implementing an OAuth2 authentication scheme other than the provided
|
|
389
|
+
ones in FastAPI (based on bearer tokens), you might want to override this.
|
|
390
|
+
|
|
391
|
+
Ref: https://datatracker.ietf.org/doc/html/rfc6749
|
|
392
|
+
"""
|
|
393
|
+
return HTTPException(
|
|
394
|
+
status_code=HTTP_401_UNAUTHORIZED,
|
|
395
|
+
detail="Not authenticated",
|
|
396
|
+
headers={"WWW-Authenticate": "Bearer"},
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
async def __call__(self, request: Request) -> Optional[str]:
|
|
400
|
+
authorization = request.headers.get("Authorization")
|
|
401
|
+
if not authorization:
|
|
402
|
+
if self.auto_error:
|
|
403
|
+
raise self.make_not_authenticated_error()
|
|
404
|
+
else:
|
|
405
|
+
return None
|
|
406
|
+
return authorization
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
class OAuth2PasswordBearer(OAuth2):
|
|
410
|
+
"""
|
|
411
|
+
OAuth2 flow for authentication using a bearer token obtained with a password.
|
|
412
|
+
An instance of it would be used as a dependency.
|
|
413
|
+
|
|
414
|
+
Read more about it in the
|
|
415
|
+
[FastAPI docs for Simple OAuth2 with Password and Bearer](https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/).
|
|
416
|
+
"""
|
|
417
|
+
|
|
418
|
+
def __init__(
|
|
419
|
+
self,
|
|
420
|
+
tokenUrl: Annotated[
|
|
421
|
+
str,
|
|
422
|
+
Doc(
|
|
423
|
+
"""
|
|
424
|
+
The URL to obtain the OAuth2 token. This would be the *path operation*
|
|
425
|
+
that has `OAuth2PasswordRequestForm` as a dependency.
|
|
426
|
+
"""
|
|
427
|
+
),
|
|
428
|
+
],
|
|
429
|
+
scheme_name: Annotated[
|
|
430
|
+
Optional[str],
|
|
431
|
+
Doc(
|
|
432
|
+
"""
|
|
433
|
+
Security scheme name.
|
|
434
|
+
|
|
435
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
436
|
+
"""
|
|
437
|
+
),
|
|
438
|
+
] = None,
|
|
439
|
+
scopes: Annotated[
|
|
440
|
+
Optional[dict[str, str]],
|
|
441
|
+
Doc(
|
|
442
|
+
"""
|
|
443
|
+
The OAuth2 scopes that would be required by the *path operations* that
|
|
444
|
+
use this dependency.
|
|
445
|
+
"""
|
|
446
|
+
),
|
|
447
|
+
] = None,
|
|
448
|
+
description: Annotated[
|
|
449
|
+
Optional[str],
|
|
450
|
+
Doc(
|
|
451
|
+
"""
|
|
452
|
+
Security scheme description.
|
|
453
|
+
|
|
454
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
455
|
+
"""
|
|
456
|
+
),
|
|
457
|
+
] = None,
|
|
458
|
+
auto_error: Annotated[
|
|
459
|
+
bool,
|
|
460
|
+
Doc(
|
|
461
|
+
"""
|
|
462
|
+
By default, if no HTTP Authorization header is provided, required for
|
|
463
|
+
OAuth2 authentication, it will automatically cancel the request and
|
|
464
|
+
send the client an error.
|
|
465
|
+
|
|
466
|
+
If `auto_error` is set to `False`, when the HTTP Authorization header
|
|
467
|
+
is not available, instead of erroring out, the dependency result will
|
|
468
|
+
be `None`.
|
|
469
|
+
|
|
470
|
+
This is useful when you want to have optional authentication.
|
|
471
|
+
|
|
472
|
+
It is also useful when you want to have authentication that can be
|
|
473
|
+
provided in one of multiple optional ways (for example, with OAuth2
|
|
474
|
+
or in a cookie).
|
|
475
|
+
"""
|
|
476
|
+
),
|
|
477
|
+
] = True,
|
|
478
|
+
refreshUrl: Annotated[
|
|
479
|
+
Optional[str],
|
|
480
|
+
Doc(
|
|
481
|
+
"""
|
|
482
|
+
The URL to refresh the token and obtain a new one.
|
|
483
|
+
"""
|
|
484
|
+
),
|
|
485
|
+
] = None,
|
|
486
|
+
):
|
|
487
|
+
if not scopes:
|
|
488
|
+
scopes = {}
|
|
489
|
+
flows = OAuthFlowsModel(
|
|
490
|
+
password=cast(
|
|
491
|
+
Any,
|
|
492
|
+
{
|
|
493
|
+
"tokenUrl": tokenUrl,
|
|
494
|
+
"refreshUrl": refreshUrl,
|
|
495
|
+
"scopes": scopes,
|
|
496
|
+
},
|
|
497
|
+
)
|
|
498
|
+
)
|
|
499
|
+
super().__init__(
|
|
500
|
+
flows=flows,
|
|
501
|
+
scheme_name=scheme_name,
|
|
502
|
+
description=description,
|
|
503
|
+
auto_error=auto_error,
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
async def __call__(self, request: Request) -> Optional[str]:
|
|
507
|
+
authorization = request.headers.get("Authorization")
|
|
508
|
+
scheme, param = get_authorization_scheme_param(authorization)
|
|
509
|
+
if not authorization or scheme.lower() != "bearer":
|
|
510
|
+
if self.auto_error:
|
|
511
|
+
raise self.make_not_authenticated_error()
|
|
512
|
+
else:
|
|
513
|
+
return None
|
|
514
|
+
return param
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
class OAuth2AuthorizationCodeBearer(OAuth2):
|
|
518
|
+
"""
|
|
519
|
+
OAuth2 flow for authentication using a bearer token obtained with an OAuth2 code
|
|
520
|
+
flow. An instance of it would be used as a dependency.
|
|
521
|
+
"""
|
|
522
|
+
|
|
523
|
+
def __init__(
|
|
524
|
+
self,
|
|
525
|
+
authorizationUrl: str,
|
|
526
|
+
tokenUrl: Annotated[
|
|
527
|
+
str,
|
|
528
|
+
Doc(
|
|
529
|
+
"""
|
|
530
|
+
The URL to obtain the OAuth2 token.
|
|
531
|
+
"""
|
|
532
|
+
),
|
|
533
|
+
],
|
|
534
|
+
refreshUrl: Annotated[
|
|
535
|
+
Optional[str],
|
|
536
|
+
Doc(
|
|
537
|
+
"""
|
|
538
|
+
The URL to refresh the token and obtain a new one.
|
|
539
|
+
"""
|
|
540
|
+
),
|
|
541
|
+
] = None,
|
|
542
|
+
scheme_name: Annotated[
|
|
543
|
+
Optional[str],
|
|
544
|
+
Doc(
|
|
545
|
+
"""
|
|
546
|
+
Security scheme name.
|
|
547
|
+
|
|
548
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
549
|
+
"""
|
|
550
|
+
),
|
|
551
|
+
] = None,
|
|
552
|
+
scopes: Annotated[
|
|
553
|
+
Optional[dict[str, str]],
|
|
554
|
+
Doc(
|
|
555
|
+
"""
|
|
556
|
+
The OAuth2 scopes that would be required by the *path operations* that
|
|
557
|
+
use this dependency.
|
|
558
|
+
"""
|
|
559
|
+
),
|
|
560
|
+
] = None,
|
|
561
|
+
description: Annotated[
|
|
562
|
+
Optional[str],
|
|
563
|
+
Doc(
|
|
564
|
+
"""
|
|
565
|
+
Security scheme description.
|
|
566
|
+
|
|
567
|
+
It will be included in the generated OpenAPI (e.g. visible at `/docs`).
|
|
568
|
+
"""
|
|
569
|
+
),
|
|
570
|
+
] = None,
|
|
571
|
+
auto_error: Annotated[
|
|
572
|
+
bool,
|
|
573
|
+
Doc(
|
|
574
|
+
"""
|
|
575
|
+
By default, if no HTTP Authorization header is provided, required for
|
|
576
|
+
OAuth2 authentication, it will automatically cancel the request and
|
|
577
|
+
send the client an error.
|
|
578
|
+
|
|
579
|
+
If `auto_error` is set to `False`, when the HTTP Authorization header
|
|
580
|
+
is not available, instead of erroring out, the dependency result will
|
|
581
|
+
be `None`.
|
|
582
|
+
|
|
583
|
+
This is useful when you want to have optional authentication.
|
|
584
|
+
|
|
585
|
+
It is also useful when you want to have authentication that can be
|
|
586
|
+
provided in one of multiple optional ways (for example, with OAuth2
|
|
587
|
+
or in a cookie).
|
|
588
|
+
"""
|
|
589
|
+
),
|
|
590
|
+
] = True,
|
|
591
|
+
):
|
|
592
|
+
if not scopes:
|
|
593
|
+
scopes = {}
|
|
594
|
+
flows = OAuthFlowsModel(
|
|
595
|
+
authorizationCode=cast(
|
|
596
|
+
Any,
|
|
597
|
+
{
|
|
598
|
+
"authorizationUrl": authorizationUrl,
|
|
599
|
+
"tokenUrl": tokenUrl,
|
|
600
|
+
"refreshUrl": refreshUrl,
|
|
601
|
+
"scopes": scopes,
|
|
602
|
+
},
|
|
603
|
+
)
|
|
604
|
+
)
|
|
605
|
+
super().__init__(
|
|
606
|
+
flows=flows,
|
|
607
|
+
scheme_name=scheme_name,
|
|
608
|
+
description=description,
|
|
609
|
+
auto_error=auto_error,
|
|
610
|
+
)
|
|
611
|
+
|
|
612
|
+
async def __call__(self, request: Request) -> Optional[str]:
|
|
613
|
+
authorization = request.headers.get("Authorization")
|
|
614
|
+
scheme, param = get_authorization_scheme_param(authorization)
|
|
615
|
+
if not authorization or scheme.lower() != "bearer":
|
|
616
|
+
if self.auto_error:
|
|
617
|
+
raise self.make_not_authenticated_error()
|
|
618
|
+
else:
|
|
619
|
+
return None # pragma: nocover
|
|
620
|
+
return param
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
class SecurityScopes:
|
|
624
|
+
"""
|
|
625
|
+
This is a special class that you can define in a parameter in a dependency to
|
|
626
|
+
obtain the OAuth2 scopes required by all the dependencies in the same chain.
|
|
627
|
+
|
|
628
|
+
This way, multiple dependencies can have different scopes, even when used in the
|
|
629
|
+
same *path operation*. And with this, you can access all the scopes required in
|
|
630
|
+
all those dependencies in a single place.
|
|
631
|
+
|
|
632
|
+
Read more about it in the
|
|
633
|
+
[FastAPI docs for OAuth2 scopes](https://fastapi.tiangolo.com/advanced/security/oauth2-scopes/).
|
|
634
|
+
"""
|
|
635
|
+
|
|
636
|
+
def __init__(
|
|
637
|
+
self,
|
|
638
|
+
scopes: Annotated[
|
|
639
|
+
Optional[list[str]],
|
|
640
|
+
Doc(
|
|
641
|
+
"""
|
|
642
|
+
This will be filled by FastAPI.
|
|
643
|
+
"""
|
|
644
|
+
),
|
|
645
|
+
] = None,
|
|
646
|
+
):
|
|
647
|
+
self.scopes: Annotated[
|
|
648
|
+
list[str],
|
|
649
|
+
Doc(
|
|
650
|
+
"""
|
|
651
|
+
The list of all the scopes required by dependencies.
|
|
652
|
+
"""
|
|
653
|
+
),
|
|
654
|
+
] = scopes or []
|
|
655
|
+
self.scope_str: Annotated[
|
|
656
|
+
str,
|
|
657
|
+
Doc(
|
|
658
|
+
"""
|
|
659
|
+
All the scopes required by all the dependencies in a single string
|
|
660
|
+
separated by spaces, as defined in the OAuth2 specification.
|
|
661
|
+
"""
|
|
662
|
+
),
|
|
663
|
+
] = " ".join(self.scopes)
|