Browse Source

📝 Update usage of Token model in security docs (#9313)

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>
pull/10941/head
Piotr Szaciłowski 1 year ago
committed by GitHub
parent
commit
fd97e8efe4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      docs/em/docs/advanced/security/oauth2-scopes.md
  2. 4
      docs/em/docs/tutorial/security/oauth2-jwt.md
  3. 18
      docs/en/docs/advanced/security/oauth2-scopes.md
  4. 4
      docs/en/docs/tutorial/security/oauth2-jwt.md
  5. 2
      docs/ja/docs/tutorial/security/oauth2-jwt.md
  6. 2
      docs/zh/docs/tutorial/security/oauth2-jwt.md
  7. 8
      docs_src/security/tutorial004.py
  8. 6
      docs_src/security/tutorial004_an.py
  9. 6
      docs_src/security/tutorial004_an_py310.py
  10. 6
      docs_src/security/tutorial004_an_py39.py
  11. 8
      docs_src/security/tutorial004_py310.py
  12. 8
      docs_src/security/tutorial005.py
  13. 6
      docs_src/security/tutorial005_an.py
  14. 6
      docs_src/security/tutorial005_an_py310.py
  15. 6
      docs_src/security/tutorial005_an_py39.py
  16. 8
      docs_src/security/tutorial005_py310.py
  17. 8
      docs_src/security/tutorial005_py39.py

6
docs/em/docs/advanced/security/oauth2-scopes.md

@ -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!}
```

4
docs/em/docs/tutorial/security/oauth2-jwt.md

@ -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!}
```

18
docs/en/docs/advanced/security/oauth2-scopes.md

@ -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!}
```

4
docs/en/docs/tutorial/security/oauth2-jwt.md

@ -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!}
```

2
docs/ja/docs/tutorial/security/oauth2-jwt.md

@ -167,7 +167,7 @@ JWTトークンの署名に使用するアルゴリズム`"HS256"`を指定し
JWTアクセストークンを作成し、それを返します。
```Python hl_lines="115-128"
```Python hl_lines="115-130"
{!../../../docs_src/security/tutorial004.py!}
```

2
docs/zh/docs/tutorial/security/oauth2-jwt.md

@ -170,7 +170,7 @@ $ openssl rand -hex 32
创建并返回真正的 JWT 访问令牌。
```Python hl_lines="115-128"
```Python hl_lines="115-130"
{!../../../docs_src/security/tutorial004.py!}
```

8
docs_src/security/tutorial004.py

@ -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)

6
docs_src/security/tutorial004_an.py

@ -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)

6
docs_src/security/tutorial004_an_py310.py

@ -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)

6
docs_src/security/tutorial004_an_py39.py

@ -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)

8
docs_src/security/tutorial004_py310.py

@ -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)

8
docs_src/security/tutorial005.py

@ -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)

6
docs_src/security/tutorial005_an.py

@ -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)

6
docs_src/security/tutorial005_an_py310.py

@ -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)

6
docs_src/security/tutorial005_an_py39.py

@ -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)

8
docs_src/security/tutorial005_py310.py

@ -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)

8
docs_src/security/tutorial005_py39.py

@ -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)

Loading…
Cancel
Save