您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

654 行
22 KiB

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