Browse Source

🔥 Remove manual type annotations in JWT tutorial to avoid typing expectations (JWT doesn't provide more types) (#13378)

pull/13379/head
Sebastián Ramírez 2 months ago
committed by GitHub
parent
commit
08b817a842
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      docs_src/security/tutorial004.py
  2. 2
      docs_src/security/tutorial004_an.py
  3. 2
      docs_src/security/tutorial004_an_py310.py
  4. 2
      docs_src/security/tutorial004_an_py39.py
  5. 2
      docs_src/security/tutorial004_py310.py
  6. 2
      docs_src/security/tutorial005_an.py
  7. 2
      docs_src/security/tutorial005_an_py310.py
  8. 6
      docs_src/security/tutorial005_an_py39.py

2
docs_src/security/tutorial004.py

@ -95,7 +95,7 @@ async def get_current_user(token: str = Depends(oauth2_scheme)):
) )
try: try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub") username = payload.get("sub")
if username is None: if username is None:
raise credentials_exception raise credentials_exception
token_data = TokenData(username=username) token_data = TokenData(username=username)

2
docs_src/security/tutorial004_an.py

@ -96,7 +96,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
) )
try: try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub") username = payload.get("sub")
if username is None: if username is None:
raise credentials_exception raise credentials_exception
token_data = TokenData(username=username) token_data = TokenData(username=username)

2
docs_src/security/tutorial004_an_py310.py

@ -95,7 +95,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
) )
try: try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub") username = payload.get("sub")
if username is None: if username is None:
raise credentials_exception raise credentials_exception
token_data = TokenData(username=username) token_data = TokenData(username=username)

2
docs_src/security/tutorial004_an_py39.py

@ -95,7 +95,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
) )
try: try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub") username = payload.get("sub")
if username is None: if username is None:
raise credentials_exception raise credentials_exception
token_data = TokenData(username=username) token_data = TokenData(username=username)

2
docs_src/security/tutorial004_py310.py

@ -94,7 +94,7 @@ async def get_current_user(token: str = Depends(oauth2_scheme)):
) )
try: try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub") username = payload.get("sub")
if username is None: if username is None:
raise credentials_exception raise credentials_exception
token_data = TokenData(username=username) token_data = TokenData(username=username)

2
docs_src/security/tutorial005_an.py

@ -117,7 +117,7 @@ async def get_current_user(
) )
try: try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub") username = payload.get("sub")
if username is None: if username is None:
raise credentials_exception raise credentials_exception
token_scopes = payload.get("scopes", []) token_scopes = payload.get("scopes", [])

2
docs_src/security/tutorial005_an_py310.py

@ -116,7 +116,7 @@ async def get_current_user(
) )
try: try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub") username = payload.get("sub")
if username is None: if username is None:
raise credentials_exception raise credentials_exception
token_scopes = payload.get("scopes", []) token_scopes = payload.get("scopes", [])

6
docs_src/security/tutorial005_an_py39.py

@ -1,5 +1,5 @@
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from typing import Annotated, List, Union from typing import Annotated, Union
import jwt import jwt
from fastapi import Depends, FastAPI, HTTPException, Security, status from fastapi import Depends, FastAPI, HTTPException, Security, status
@ -44,7 +44,7 @@ class Token(BaseModel):
class TokenData(BaseModel): class TokenData(BaseModel):
username: Union[str, None] = None username: Union[str, None] = None
scopes: List[str] = [] scopes: list[str] = []
class User(BaseModel): class User(BaseModel):
@ -116,7 +116,7 @@ async def get_current_user(
) )
try: try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub") username = payload.get("sub")
if username is None: if username is None:
raise credentials_exception raise credentials_exception
token_scopes = payload.get("scopes", []) token_scopes = payload.get("scopes", [])

Loading…
Cancel
Save