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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with
10 additions and
10 deletions
-
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_an.py
-
docs_src/security/tutorial005_an_py310.py
-
docs_src/security/tutorial005_an_py39.py
|
|
@ -95,7 +95,7 @@ async def get_current_user(token: str = Depends(oauth2_scheme)): |
|
|
|
) |
|
|
|
try: |
|
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) |
|
|
|
username: str = payload.get("sub") |
|
|
|
username = payload.get("sub") |
|
|
|
if username is None: |
|
|
|
raise credentials_exception |
|
|
|
token_data = TokenData(username=username) |
|
|
|
|
|
@ -96,7 +96,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]): |
|
|
|
) |
|
|
|
try: |
|
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) |
|
|
|
username: str = payload.get("sub") |
|
|
|
username = payload.get("sub") |
|
|
|
if username is None: |
|
|
|
raise credentials_exception |
|
|
|
token_data = TokenData(username=username) |
|
|
|
|
|
@ -95,7 +95,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]): |
|
|
|
) |
|
|
|
try: |
|
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) |
|
|
|
username: str = payload.get("sub") |
|
|
|
username = payload.get("sub") |
|
|
|
if username is None: |
|
|
|
raise credentials_exception |
|
|
|
token_data = TokenData(username=username) |
|
|
|
|
|
@ -95,7 +95,7 @@ async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]): |
|
|
|
) |
|
|
|
try: |
|
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) |
|
|
|
username: str = payload.get("sub") |
|
|
|
username = payload.get("sub") |
|
|
|
if username is None: |
|
|
|
raise credentials_exception |
|
|
|
token_data = TokenData(username=username) |
|
|
|
|
|
@ -94,7 +94,7 @@ async def get_current_user(token: str = Depends(oauth2_scheme)): |
|
|
|
) |
|
|
|
try: |
|
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) |
|
|
|
username: str = payload.get("sub") |
|
|
|
username = payload.get("sub") |
|
|
|
if username is None: |
|
|
|
raise credentials_exception |
|
|
|
token_data = TokenData(username=username) |
|
|
|
|
|
@ -117,7 +117,7 @@ async def get_current_user( |
|
|
|
) |
|
|
|
try: |
|
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) |
|
|
|
username: str = payload.get("sub") |
|
|
|
username = payload.get("sub") |
|
|
|
if username is None: |
|
|
|
raise credentials_exception |
|
|
|
token_scopes = payload.get("scopes", []) |
|
|
|
|
|
@ -116,7 +116,7 @@ async def get_current_user( |
|
|
|
) |
|
|
|
try: |
|
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) |
|
|
|
username: str = payload.get("sub") |
|
|
|
username = payload.get("sub") |
|
|
|
if username is None: |
|
|
|
raise credentials_exception |
|
|
|
token_scopes = payload.get("scopes", []) |
|
|
|
|
|
@ -1,5 +1,5 @@ |
|
|
|
from datetime import datetime, timedelta, timezone |
|
|
|
from typing import Annotated, List, Union |
|
|
|
from typing import Annotated, Union |
|
|
|
|
|
|
|
import jwt |
|
|
|
from fastapi import Depends, FastAPI, HTTPException, Security, status |
|
|
@ -44,7 +44,7 @@ class Token(BaseModel): |
|
|
|
|
|
|
|
class TokenData(BaseModel): |
|
|
|
username: Union[str, None] = None |
|
|
|
scopes: List[str] = [] |
|
|
|
scopes: list[str] = [] |
|
|
|
|
|
|
|
|
|
|
|
class User(BaseModel): |
|
|
@ -116,7 +116,7 @@ async def get_current_user( |
|
|
|
) |
|
|
|
try: |
|
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) |
|
|
|
username: str = payload.get("sub") |
|
|
|
username = payload.get("sub") |
|
|
|
if username is None: |
|
|
|
raise credentials_exception |
|
|
|
token_scopes = payload.get("scopes", []) |
|
|
|