mirror of https://github.com/tiangolo/fastapi.git
📝 Update includes for `docs/en/docs/tutorial/security/get-current-user.md` (#12645)
This commit is contained in:
parent
f5b8f39ed7
commit
f26a1b6385
|
|
@ -2,35 +2,7 @@
|
||||||
|
|
||||||
In the previous chapter the security system (which is based on the dependency injection system) was giving the *path operation function* a `token` as a `str`:
|
In the previous chapter the security system (which is based on the dependency injection system) was giving the *path operation function* a `token` as a `str`:
|
||||||
|
|
||||||
//// tab | Python 3.9+
|
{* ../../docs_src/security/tutorial001_an_py39.py hl[12] *}
|
||||||
|
|
||||||
```Python hl_lines="12"
|
|
||||||
{!> ../../docs_src/security/tutorial001_an_py39.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+
|
|
||||||
|
|
||||||
```Python hl_lines="11"
|
|
||||||
{!> ../../docs_src/security/tutorial001_an.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+ non-Annotated
|
|
||||||
|
|
||||||
/// tip
|
|
||||||
|
|
||||||
Prefer to use the `Annotated` version if possible.
|
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
```Python hl_lines="10"
|
|
||||||
{!> ../../docs_src/security/tutorial001.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
But that is still not that useful.
|
But that is still not that useful.
|
||||||
|
|
||||||
|
|
@ -42,57 +14,7 @@ First, let's create a Pydantic user model.
|
||||||
|
|
||||||
The same way we use Pydantic to declare bodies, we can use it anywhere else:
|
The same way we use Pydantic to declare bodies, we can use it anywhere else:
|
||||||
|
|
||||||
//// tab | Python 3.10+
|
{* ../../docs_src/security/tutorial002_an_py310.py hl[5,12:6] *}
|
||||||
|
|
||||||
```Python hl_lines="5 12-16"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an_py310.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.9+
|
|
||||||
|
|
||||||
```Python hl_lines="5 12-16"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an_py39.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+
|
|
||||||
|
|
||||||
```Python hl_lines="5 13-17"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.10+ non-Annotated
|
|
||||||
|
|
||||||
/// tip
|
|
||||||
|
|
||||||
Prefer to use the `Annotated` version if possible.
|
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
```Python hl_lines="3 10-14"
|
|
||||||
{!> ../../docs_src/security/tutorial002_py310.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+ non-Annotated
|
|
||||||
|
|
||||||
/// tip
|
|
||||||
|
|
||||||
Prefer to use the `Annotated` version if possible.
|
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
```Python hl_lines="5 12-16"
|
|
||||||
{!> ../../docs_src/security/tutorial002.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
## Create a `get_current_user` dependency
|
## Create a `get_current_user` dependency
|
||||||
|
|
||||||
|
|
@ -104,169 +26,19 @@ Remember that dependencies can have sub-dependencies?
|
||||||
|
|
||||||
The same as we were doing before in the *path operation* directly, our new dependency `get_current_user` will receive a `token` as a `str` from the sub-dependency `oauth2_scheme`:
|
The same as we were doing before in the *path operation* directly, our new dependency `get_current_user` will receive a `token` as a `str` from the sub-dependency `oauth2_scheme`:
|
||||||
|
|
||||||
//// tab | Python 3.10+
|
{* ../../docs_src/security/tutorial002_an_py310.py hl[25] *}
|
||||||
|
|
||||||
```Python hl_lines="25"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an_py310.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.9+
|
|
||||||
|
|
||||||
```Python hl_lines="25"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an_py39.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+
|
|
||||||
|
|
||||||
```Python hl_lines="26"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.10+ non-Annotated
|
|
||||||
|
|
||||||
/// tip
|
|
||||||
|
|
||||||
Prefer to use the `Annotated` version if possible.
|
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
```Python hl_lines="23"
|
|
||||||
{!> ../../docs_src/security/tutorial002_py310.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+ non-Annotated
|
|
||||||
|
|
||||||
/// tip
|
|
||||||
|
|
||||||
Prefer to use the `Annotated` version if possible.
|
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
```Python hl_lines="25"
|
|
||||||
{!> ../../docs_src/security/tutorial002.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
## Get the user
|
## Get the user
|
||||||
|
|
||||||
`get_current_user` will use a (fake) utility function we created, that takes a token as a `str` and returns our Pydantic `User` model:
|
`get_current_user` will use a (fake) utility function we created, that takes a token as a `str` and returns our Pydantic `User` model:
|
||||||
|
|
||||||
//// tab | Python 3.10+
|
{* ../../docs_src/security/tutorial002_an_py310.py hl[19:22,26:27] *}
|
||||||
|
|
||||||
```Python hl_lines="19-22 26-27"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an_py310.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.9+
|
|
||||||
|
|
||||||
```Python hl_lines="19-22 26-27"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an_py39.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+
|
|
||||||
|
|
||||||
```Python hl_lines="20-23 27-28"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.10+ non-Annotated
|
|
||||||
|
|
||||||
/// tip
|
|
||||||
|
|
||||||
Prefer to use the `Annotated` version if possible.
|
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
```Python hl_lines="17-20 24-25"
|
|
||||||
{!> ../../docs_src/security/tutorial002_py310.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+ non-Annotated
|
|
||||||
|
|
||||||
/// tip
|
|
||||||
|
|
||||||
Prefer to use the `Annotated` version if possible.
|
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
```Python hl_lines="19-22 26-27"
|
|
||||||
{!> ../../docs_src/security/tutorial002.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
## Inject the current user
|
## Inject the current user
|
||||||
|
|
||||||
So now we can use the same `Depends` with our `get_current_user` in the *path operation*:
|
So now we can use the same `Depends` with our `get_current_user` in the *path operation*:
|
||||||
|
|
||||||
//// tab | Python 3.10+
|
{* ../../docs_src/security/tutorial002_an_py310.py hl[31] *}
|
||||||
|
|
||||||
```Python hl_lines="31"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an_py310.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.9+
|
|
||||||
|
|
||||||
```Python hl_lines="31"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an_py39.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+
|
|
||||||
|
|
||||||
```Python hl_lines="32"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.10+ non-Annotated
|
|
||||||
|
|
||||||
/// tip
|
|
||||||
|
|
||||||
Prefer to use the `Annotated` version if possible.
|
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
```Python hl_lines="29"
|
|
||||||
{!> ../../docs_src/security/tutorial002_py310.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+ non-Annotated
|
|
||||||
|
|
||||||
/// tip
|
|
||||||
|
|
||||||
Prefer to use the `Annotated` version if possible.
|
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
```Python hl_lines="31"
|
|
||||||
{!> ../../docs_src/security/tutorial002.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
Notice that we declare the type of `current_user` as the Pydantic model `User`.
|
Notice that we declare the type of `current_user` as the Pydantic model `User`.
|
||||||
|
|
||||||
|
|
@ -320,57 +92,7 @@ And all of them (or any portion of them that you want) can take advantage of re-
|
||||||
|
|
||||||
And all these thousands of *path operations* can be as small as 3 lines:
|
And all these thousands of *path operations* can be as small as 3 lines:
|
||||||
|
|
||||||
//// tab | Python 3.10+
|
{* ../../docs_src/security/tutorial002_an_py310.py hl[30:32] *}
|
||||||
|
|
||||||
```Python hl_lines="30-32"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an_py310.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.9+
|
|
||||||
|
|
||||||
```Python hl_lines="30-32"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an_py39.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+
|
|
||||||
|
|
||||||
```Python hl_lines="31-33"
|
|
||||||
{!> ../../docs_src/security/tutorial002_an.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.10+ non-Annotated
|
|
||||||
|
|
||||||
/// tip
|
|
||||||
|
|
||||||
Prefer to use the `Annotated` version if possible.
|
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
```Python hl_lines="28-30"
|
|
||||||
{!> ../../docs_src/security/tutorial002_py310.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
//// tab | Python 3.8+ non-Annotated
|
|
||||||
|
|
||||||
/// tip
|
|
||||||
|
|
||||||
Prefer to use the `Annotated` version if possible.
|
|
||||||
|
|
||||||
///
|
|
||||||
|
|
||||||
```Python hl_lines="30-32"
|
|
||||||
{!> ../../docs_src/security/tutorial002.py!}
|
|
||||||
```
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
## Recap
|
## Recap
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue