]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
๐Ÿ“ Update usage of Token model in security docs (#9313)
authorPiotr Szaciล‚owski <44623605+piotrszacilowski@users.noreply.github.com>
Thu, 11 Jan 2024 21:21:35 +0000 (22:21 +0100)
committerGitHub <noreply@github.com>
Thu, 11 Jan 2024 21:21:35 +0000 (16:21 -0500)
Co-authored-by: Alejandra Sรกnchez <ing.alejandrasanchezv@gmail.com>
Co-authored-by: Alejandra <90076947+alejsdev@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
17 files changed:
docs/em/docs/advanced/security/oauth2-scopes.md
docs/em/docs/tutorial/security/oauth2-jwt.md
docs/en/docs/advanced/security/oauth2-scopes.md
docs/en/docs/tutorial/security/oauth2-jwt.md
docs/ja/docs/tutorial/security/oauth2-jwt.md
docs/zh/docs/tutorial/security/oauth2-jwt.md
docs_src/security/tutorial004.py
docs_src/security/tutorial004_an.py
docs_src/security/tutorial004_an_py310.py
docs_src/security/tutorial004_an_py39.py
docs_src/security/tutorial004_py310.py
docs_src/security/tutorial005.py
docs_src/security/tutorial005_an.py
docs_src/security/tutorial005_an_py310.py
docs_src/security/tutorial005_an_py39.py
docs_src/security/tutorial005_py310.py
docs_src/security/tutorial005_py39.py

index a4684352ccd4dedb2180af87f67522f5818e3da4..d82fe152bef3141fcff6352dab88facd7dd9531f 100644 (file)
@@ -56,7 +56,7 @@ Oauth2๏ธโƒฃ ๐Ÿ”ง ๐Ÿ”ฌ "โ†”" ๐Ÿ“‡ ๐ŸŽป ๐ŸŽ ๐Ÿš€.
 
 ๐Ÿฅ‡, โžก๏ธ ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ• ๐Ÿ‘ˆ ๐Ÿ”€ โšช๏ธโžก๏ธ ๐Ÿ–ผ ๐Ÿ‘‘ **๐Ÿ”ฐ - ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿฆฎ** [Oauth2๏ธโƒฃ โฎ๏ธ ๐Ÿ” (&amp; ๐Ÿ”), ๐Ÿ“จ โฎ๏ธ ๐Ÿฅ™ ๐Ÿค](../../tutorial/security/oauth2-jwt.md){.internal-link target=_blank}. ๐Ÿ”œ โš™๏ธ Oauth2๏ธโƒฃ โ†”:
 
-```Python hl_lines="2  4  8  12  46  64  105  107-115  121-124  128-134  139  153"
+```Python hl_lines="2  4  8  12  46  64  105  107-115  121-124  128-134  139  155"
 {!../../../docs_src/security/tutorial005.py!}
 ```
 
@@ -93,7 +93,7 @@ Oauth2๏ธโƒฃ ๐Ÿ”ง ๐Ÿ”ฌ "โ†”" ๐Ÿ“‡ ๐ŸŽป ๐ŸŽ ๐Ÿš€.
 
     โœ‹๏ธ ๐Ÿ‘† ๐Ÿˆธ, ๐Ÿ’‚โ€โ™‚, ๐Ÿ‘† ๐Ÿ”œ โš’ ๐Ÿ’ญ ๐Ÿ‘† ๐Ÿ•ด ๐Ÿšฎ โ†” ๐Ÿ‘ˆ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿค™ ๐Ÿ’ช โœ”๏ธ, โš–๏ธ ๐Ÿ• ๐Ÿ‘† โœ”๏ธ ๐Ÿ”.
 
-```Python hl_lines="153"
+```Python hl_lines="155"
 {!../../../docs_src/security/tutorial005.py!}
 ```
 
@@ -118,7 +118,7 @@ Oauth2๏ธโƒฃ ๐Ÿ”ง ๐Ÿ”ฌ "โ†”" ๐Ÿ“‡ ๐ŸŽป ๐ŸŽ ๐Ÿš€.
 
     ๐Ÿ‘ฅ ๐Ÿ”จ โšซ๏ธ ๐Ÿ“ฅ ๐ŸŽฆ โ” **FastAPI** ๐Ÿต โ†” ๐Ÿ“ฃ ๐ŸŽ ๐ŸŽš.
 
-```Python hl_lines="4  139  166"
+```Python hl_lines="4  139  168"
 {!../../../docs_src/security/tutorial005.py!}
 ```
 
index bc207c5666d90423ea005975081f9d3e5b5618c2..bc3c943f86dc66ca4bd82b74ff4a0eed28da0f5b 100644 (file)
@@ -192,13 +192,13 @@ $ openssl rand -hex 32
 
 === "๐Ÿ 3๏ธโƒฃ.6๏ธโƒฃ &amp; ๐Ÿ”›"
 
-    ```Python hl_lines="115-128"
+    ```Python hl_lines="115-130"
     {!> ../../../docs_src/security/tutorial004.py!}
     ```
 
 === "๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ &amp; ๐Ÿ”›"
 
-    ```Python hl_lines="114-127"
+    ```Python hl_lines="114-129"
     {!> ../../../docs_src/security/tutorial004_py310.py!}
     ```
 
index 304a46090e1d7008a148b96deccda9ff1bca91c8..b93d2991c4dcb88d8636ad613961d886a424be66 100644 (file)
@@ -79,7 +79,7 @@ First, let's quickly see the parts that change from the examples in the main **T
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="3  7  11  45  63  104  106-114  120-123  127-133  138  152"
+    ```Python hl_lines="3  7  11  45  63  104  106-114  120-123  127-133  138  154"
     {!> ../../../docs_src/security/tutorial005_py310.py!}
     ```
 
@@ -88,7 +88,7 @@ First, let's quickly see the parts that change from the examples in the main **T
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="2  4  8  12  46  64  105  107-115  121-124  128-134  139  153"
+    ```Python hl_lines="2  4  8  12  46  64  105  107-115  121-124  128-134  139  155"
     {!> ../../../docs_src/security/tutorial005_py39.py!}
     ```
 
@@ -97,7 +97,7 @@ First, let's quickly see the parts that change from the examples in the main **T
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="2  4  8  12  46  64  105  107-115  121-124  128-134  139  153"
+    ```Python hl_lines="2  4  8  12  46  64  105  107-115  121-124  128-134  139  155"
     {!> ../../../docs_src/security/tutorial005.py!}
     ```
 
@@ -199,7 +199,7 @@ And we return the scopes as part of the JWT token.
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="152"
+    ```Python hl_lines="154"
     {!> ../../../docs_src/security/tutorial005_py310.py!}
     ```
 
@@ -208,7 +208,7 @@ And we return the scopes as part of the JWT token.
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="153"
+    ```Python hl_lines="155"
     {!> ../../../docs_src/security/tutorial005_py39.py!}
     ```
 
@@ -217,7 +217,7 @@ And we return the scopes as part of the JWT token.
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="153"
+    ```Python hl_lines="155"
     {!> ../../../docs_src/security/tutorial005.py!}
     ```
 
@@ -265,7 +265,7 @@ In this case, it requires the scope `me` (it could require more than one scope).
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="3  138  165"
+    ```Python hl_lines="3  138  167"
     {!> ../../../docs_src/security/tutorial005_py310.py!}
     ```
 
@@ -274,7 +274,7 @@ In this case, it requires the scope `me` (it could require more than one scope).
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="4  139  166"
+    ```Python hl_lines="4  139  168"
     {!> ../../../docs_src/security/tutorial005_py39.py!}
     ```
 
@@ -283,7 +283,7 @@ In this case, it requires the scope `me` (it could require more than one scope).
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="4  139  166"
+    ```Python hl_lines="4  139  168"
     {!> ../../../docs_src/security/tutorial005.py!}
     ```
 
index 4159b365919424adbdd2d604472590f2747a8996..1c792e3d9e5993c024dfc61752608ed5faafa620 100644 (file)
@@ -285,7 +285,7 @@ Create a real JWT access token and return it
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="114-127"
+    ```Python hl_lines="114-129"
     {!> ../../../docs_src/security/tutorial004_py310.py!}
     ```
 
@@ -294,7 +294,7 @@ Create a real JWT access token and return it
     !!! tip
         Prefer to use the `Annotated` version if possible.
 
-    ```Python hl_lines="115-128"
+    ```Python hl_lines="115-130"
     {!> ../../../docs_src/security/tutorial004.py!}
     ```
 
index 348ffda0163e9b0359f4fd7d27c3b8e8f662056a..d5b179aa05abff23aabb6e2acce0a17f1ad9b6fe 100644 (file)
@@ -167,7 +167,7 @@ JWTใƒˆใƒผใ‚ฏใƒณใฎ็ฝฒๅใซไฝฟ็”จใ™ใ‚‹ใ‚ขใƒซใ‚ดใƒชใ‚บใƒ `"HS256"`ใ‚’ๆŒ‡ๅฎšใ—
 
 JWTใ‚ขใ‚ฏใ‚ปใ‚นใƒˆใƒผใ‚ฏใƒณใ‚’ไฝœๆˆใ—ใ€ใใ‚Œใ‚’่ฟ”ใ—ใพใ™ใ€‚
 
-```Python hl_lines="115-128"
+```Python hl_lines="115-130"
 {!../../../docs_src/security/tutorial004.py!}
 ```
 
index 054198545ef8e233c0b826854b21c596155c4a74..33a4d7fc76171a3d498bea26a006422e69539c40 100644 (file)
@@ -170,7 +170,7 @@ $ openssl rand -hex 32
 
 ๅˆ›ๅปบๅนถ่ฟ”ๅ›ž็œŸๆญฃ็š„ JWT ่ฎฟ้—ฎไปค็‰Œใ€‚
 
-```Python hl_lines="115-128"
+```Python hl_lines="115-130"
 {!../../../docs_src/security/tutorial004.py!}
 ```
 
index 134c15c5a036994c7753b1ca87d79d8ed2749d4e..044eec70037da6ab2bbf0e7999f7295b6f785335 100644 (file)
@@ -112,8 +112,10 @@ async def get_current_active_user(current_user: User = Depends(get_current_user)
     return current_user
 
 
-@app.post("/token", response_model=Token)
-async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
+@app.post("/token")
+async def login_for_access_token(
+    form_data: OAuth2PasswordRequestForm = Depends()
+) -> Token:
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     if not user:
         raise HTTPException(
@@ -125,7 +127,7 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
     access_token = create_access_token(
         data={"sub": user.username}, expires_delta=access_token_expires
     )
-    return {"access_token": access_token, "token_type": "bearer"}
+    return Token(access_token=access_token, token_type="bearer")
 
 
 @app.get("/users/me/", response_model=User)
index 204151a566485cecfec1b9e93c146629acdd3343..c78e8496c642772b8310b29f00068a7b5c58eceb 100644 (file)
@@ -115,10 +115,10 @@ async def get_current_active_user(
     return current_user
 
 
-@app.post("/token", response_model=Token)
+@app.post("/token")
 async def login_for_access_token(
     form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
-):
+) -> Token:
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     if not user:
         raise HTTPException(
@@ -130,7 +130,7 @@ async def login_for_access_token(
     access_token = create_access_token(
         data={"sub": user.username}, expires_delta=access_token_expires
     )
-    return {"access_token": access_token, "token_type": "bearer"}
+    return Token(access_token=access_token, token_type="bearer")
 
 
 @app.get("/users/me/", response_model=User)
index 64dfa15c62718f2baa7160c7e4cb0208de49cd0d..36dbc677e06389af925261d934da467eba54cd3f 100644 (file)
@@ -114,10 +114,10 @@ async def get_current_active_user(
     return current_user
 
 
-@app.post("/token", response_model=Token)
+@app.post("/token")
 async def login_for_access_token(
     form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
-):
+) -> Token:
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     if not user:
         raise HTTPException(
@@ -129,7 +129,7 @@ async def login_for_access_token(
     access_token = create_access_token(
         data={"sub": user.username}, expires_delta=access_token_expires
     )
-    return {"access_token": access_token, "token_type": "bearer"}
+    return Token(access_token=access_token, token_type="bearer")
 
 
 @app.get("/users/me/", response_model=User)
index 631a8366eb81b7fef562a383c1c7b2ae93ba5c63..23fc04a72143329d8149240fe67aff14981aa16a 100644 (file)
@@ -114,10 +114,10 @@ async def get_current_active_user(
     return current_user
 
 
-@app.post("/token", response_model=Token)
+@app.post("/token")
 async def login_for_access_token(
     form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
-):
+) -> Token:
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     if not user:
         raise HTTPException(
@@ -129,7 +129,7 @@ async def login_for_access_token(
     access_token = create_access_token(
         data={"sub": user.username}, expires_delta=access_token_expires
     )
-    return {"access_token": access_token, "token_type": "bearer"}
+    return Token(access_token=access_token, token_type="bearer")
 
 
 @app.get("/users/me/", response_model=User)
index 470f22e29f03bd0d8bfd692770ee075fea89f1c9..8363d45ab534987cc8b0d21a8aae142318965831 100644 (file)
@@ -111,8 +111,10 @@ async def get_current_active_user(current_user: User = Depends(get_current_user)
     return current_user
 
 
-@app.post("/token", response_model=Token)
-async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
+@app.post("/token")
+async def login_for_access_token(
+    form_data: OAuth2PasswordRequestForm = Depends()
+) -> Token:
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     if not user:
         raise HTTPException(
@@ -124,7 +126,7 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
     access_token = create_access_token(
         data={"sub": user.username}, expires_delta=access_token_expires
     )
-    return {"access_token": access_token, "token_type": "bearer"}
+    return Token(access_token=access_token, token_type="bearer")
 
 
 @app.get("/users/me/", response_model=User)
index ece461bc8ac36d36c215487895d72c00491a4c1b..b16bf440a51c1bb69339a49b02b536e3e7d3f4ee 100644 (file)
@@ -143,8 +143,10 @@ async def get_current_active_user(
     return current_user
 
 
-@app.post("/token", response_model=Token)
-async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
+@app.post("/token")
+async def login_for_access_token(
+    form_data: OAuth2PasswordRequestForm = Depends()
+) -> Token:
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     if not user:
         raise HTTPException(status_code=400, detail="Incorrect username or password")
@@ -153,7 +155,7 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
         data={"sub": user.username, "scopes": form_data.scopes},
         expires_delta=access_token_expires,
     )
-    return {"access_token": access_token, "token_type": "bearer"}
+    return Token(access_token=access_token, token_type="bearer")
 
 
 @app.get("/users/me/", response_model=User)
index c5b5609e525e01179e8a1a1d015867b427de77ee..95e406b32f74837790e25fab8bf1f1c7347d1037 100644 (file)
@@ -144,10 +144,10 @@ async def get_current_active_user(
     return current_user
 
 
-@app.post("/token", response_model=Token)
+@app.post("/token")
 async def login_for_access_token(
     form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
-):
+) -> Token:
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     if not user:
         raise HTTPException(status_code=400, detail="Incorrect username or password")
@@ -156,7 +156,7 @@ async def login_for_access_token(
         data={"sub": user.username, "scopes": form_data.scopes},
         expires_delta=access_token_expires,
     )
-    return {"access_token": access_token, "token_type": "bearer"}
+    return Token(access_token=access_token, token_type="bearer")
 
 
 @app.get("/users/me/", response_model=User)
index 5e81a50e12d6390716e3aaa0583686e46577cbdd..c6116a5ed120f3f42f634a94ef52372cddeeb19a 100644 (file)
@@ -143,10 +143,10 @@ async def get_current_active_user(
     return current_user
 
 
-@app.post("/token", response_model=Token)
+@app.post("/token")
 async def login_for_access_token(
     form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
-):
+) -> Token:
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     if not user:
         raise HTTPException(status_code=400, detail="Incorrect username or password")
@@ -155,7 +155,7 @@ async def login_for_access_token(
         data={"sub": user.username, "scopes": form_data.scopes},
         expires_delta=access_token_expires,
     )
-    return {"access_token": access_token, "token_type": "bearer"}
+    return Token(access_token=access_token, token_type="bearer")
 
 
 @app.get("/users/me/", response_model=User)
index ae9811c689f5bb95f87ebe05c365cbe2cd742dc3..af51c08b5081d87288ae619b30a1aa269d782374 100644 (file)
@@ -143,10 +143,10 @@ async def get_current_active_user(
     return current_user
 
 
-@app.post("/token", response_model=Token)
+@app.post("/token")
 async def login_for_access_token(
     form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
-):
+) -> Token:
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     if not user:
         raise HTTPException(status_code=400, detail="Incorrect username or password")
@@ -155,7 +155,7 @@ async def login_for_access_token(
         data={"sub": user.username, "scopes": form_data.scopes},
         expires_delta=access_token_expires,
     )
-    return {"access_token": access_token, "token_type": "bearer"}
+    return Token(access_token=access_token, token_type="bearer")
 
 
 @app.get("/users/me/", response_model=User)
index 0fcdda4c004c627c5412860e268ce6ac217eefc5..37a22c70907f628f5cc9288b703f093961ada09f 100644 (file)
@@ -142,8 +142,10 @@ async def get_current_active_user(
     return current_user
 
 
-@app.post("/token", response_model=Token)
-async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
+@app.post("/token")
+async def login_for_access_token(
+    form_data: OAuth2PasswordRequestForm = Depends()
+) -> Token:
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     if not user:
         raise HTTPException(status_code=400, detail="Incorrect username or password")
@@ -152,7 +154,7 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
         data={"sub": user.username, "scopes": form_data.scopes},
         expires_delta=access_token_expires,
     )
-    return {"access_token": access_token, "token_type": "bearer"}
+    return Token(access_token=access_token, token_type="bearer")
 
 
 @app.get("/users/me/", response_model=User)
index d756c0b6b87f97469c83071ba10724e03234bfe4..c275807636c4bd347755264b3a6bf7d5ea595efb 100644 (file)
@@ -143,8 +143,10 @@ async def get_current_active_user(
     return current_user
 
 
-@app.post("/token", response_model=Token)
-async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
+@app.post("/token")
+async def login_for_access_token(
+    form_data: OAuth2PasswordRequestForm = Depends()
+) -> Token:
     user = authenticate_user(fake_users_db, form_data.username, form_data.password)
     if not user:
         raise HTTPException(status_code=400, detail="Incorrect username or password")
@@ -153,7 +155,7 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
         data={"sub": user.username, "scopes": form_data.scopes},
         expires_delta=access_token_expires,
     )
-    return {"access_token": access_token, "token_type": "bearer"}
+    return Token(access_token=access_token, token_type="bearer")
 
 
 @app.get("/users/me/", response_model=User)