mirror of https://github.com/tiangolo/fastapi.git
📝 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>
This commit is contained in:
parent
53a3dd7408
commit
fd97e8efe4
|
|
@ -56,7 +56,7 @@ Oauth2️⃣ 🔧 🔬 "↔" 📇 🎻 🎏 🚀.
|
||||||
|
|
||||||
🥇, ➡️ 🔜 👀 🍕 👈 🔀 ⚪️➡️ 🖼 👑 **🔰 - 👩💻 🦮** [Oauth2️⃣ ⏮️ 🔐 (& 🔁), 📨 ⏮️ 🥙 🤝](../../tutorial/security/oauth2-jwt.md){.internal-link target=_blank}. 🔜 ⚙️ Oauth2️⃣ ↔:
|
🥇, ➡️ 🔜 👀 🍕 👈 🔀 ⚪️➡️ 🖼 👑 **🔰 - 👩💻 🦮** [Oauth2️⃣ ⏮️ 🔐 (& 🔁), 📨 ⏮️ 🥙 🤝](../../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!}
|
{!../../../docs_src/security/tutorial005.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -93,7 +93,7 @@ Oauth2️⃣ 🔧 🔬 "↔" 📇 🎻 🎏 🚀.
|
||||||
|
|
||||||
✋️ 👆 🈸, 💂♂, 👆 🔜 ⚒ 💭 👆 🕴 🚮 ↔ 👈 👩💻 🤙 💪 ✔️, ⚖️ 🕐 👆 ✔️ 🔁.
|
✋️ 👆 🈸, 💂♂, 👆 🔜 ⚒ 💭 👆 🕴 🚮 ↔ 👈 👩💻 🤙 💪 ✔️, ⚖️ 🕐 👆 ✔️ 🔁.
|
||||||
|
|
||||||
```Python hl_lines="153"
|
```Python hl_lines="155"
|
||||||
{!../../../docs_src/security/tutorial005.py!}
|
{!../../../docs_src/security/tutorial005.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -118,7 +118,7 @@ Oauth2️⃣ 🔧 🔬 "↔" 📇 🎻 🎏 🚀.
|
||||||
|
|
||||||
👥 🔨 ⚫️ 📥 🎦 ❔ **FastAPI** 🍵 ↔ 📣 🎏 🎚.
|
👥 🔨 ⚫️ 📥 🎦 ❔ **FastAPI** 🍵 ↔ 📣 🎏 🎚.
|
||||||
|
|
||||||
```Python hl_lines="4 139 166"
|
```Python hl_lines="4 139 168"
|
||||||
{!../../../docs_src/security/tutorial005.py!}
|
{!../../../docs_src/security/tutorial005.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -192,13 +192,13 @@ $ openssl rand -hex 32
|
||||||
|
|
||||||
=== "🐍 3️⃣.6️⃣ & 🔛"
|
=== "🐍 3️⃣.6️⃣ & 🔛"
|
||||||
|
|
||||||
```Python hl_lines="115-128"
|
```Python hl_lines="115-130"
|
||||||
{!> ../../../docs_src/security/tutorial004.py!}
|
{!> ../../../docs_src/security/tutorial004.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
|
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
|
||||||
|
|
||||||
```Python hl_lines="114-127"
|
```Python hl_lines="114-129"
|
||||||
{!> ../../../docs_src/security/tutorial004_py310.py!}
|
{!> ../../../docs_src/security/tutorial004_py310.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ First, let's quickly see the parts that change from the examples in the main **T
|
||||||
!!! tip
|
!!! tip
|
||||||
Prefer to use the `Annotated` version if possible.
|
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!}
|
{!> ../../../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
|
!!! tip
|
||||||
Prefer to use the `Annotated` version if possible.
|
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!}
|
{!> ../../../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
|
!!! tip
|
||||||
Prefer to use the `Annotated` version if possible.
|
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!}
|
{!> ../../../docs_src/security/tutorial005.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -199,7 +199,7 @@ And we return the scopes as part of the JWT token.
|
||||||
!!! tip
|
!!! tip
|
||||||
Prefer to use the `Annotated` version if possible.
|
Prefer to use the `Annotated` version if possible.
|
||||||
|
|
||||||
```Python hl_lines="152"
|
```Python hl_lines="154"
|
||||||
{!> ../../../docs_src/security/tutorial005_py310.py!}
|
{!> ../../../docs_src/security/tutorial005_py310.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -208,7 +208,7 @@ And we return the scopes as part of the JWT token.
|
||||||
!!! tip
|
!!! tip
|
||||||
Prefer to use the `Annotated` version if possible.
|
Prefer to use the `Annotated` version if possible.
|
||||||
|
|
||||||
```Python hl_lines="153"
|
```Python hl_lines="155"
|
||||||
{!> ../../../docs_src/security/tutorial005_py39.py!}
|
{!> ../../../docs_src/security/tutorial005_py39.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -217,7 +217,7 @@ And we return the scopes as part of the JWT token.
|
||||||
!!! tip
|
!!! tip
|
||||||
Prefer to use the `Annotated` version if possible.
|
Prefer to use the `Annotated` version if possible.
|
||||||
|
|
||||||
```Python hl_lines="153"
|
```Python hl_lines="155"
|
||||||
{!> ../../../docs_src/security/tutorial005.py!}
|
{!> ../../../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
|
!!! tip
|
||||||
Prefer to use the `Annotated` version if possible.
|
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!}
|
{!> ../../../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
|
!!! tip
|
||||||
Prefer to use the `Annotated` version if possible.
|
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!}
|
{!> ../../../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
|
!!! tip
|
||||||
Prefer to use the `Annotated` version if possible.
|
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!}
|
{!> ../../../docs_src/security/tutorial005.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -285,7 +285,7 @@ Create a real JWT access token and return it
|
||||||
!!! tip
|
!!! tip
|
||||||
Prefer to use the `Annotated` version if possible.
|
Prefer to use the `Annotated` version if possible.
|
||||||
|
|
||||||
```Python hl_lines="114-127"
|
```Python hl_lines="114-129"
|
||||||
{!> ../../../docs_src/security/tutorial004_py310.py!}
|
{!> ../../../docs_src/security/tutorial004_py310.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -294,7 +294,7 @@ Create a real JWT access token and return it
|
||||||
!!! tip
|
!!! tip
|
||||||
Prefer to use the `Annotated` version if possible.
|
Prefer to use the `Annotated` version if possible.
|
||||||
|
|
||||||
```Python hl_lines="115-128"
|
```Python hl_lines="115-130"
|
||||||
{!> ../../../docs_src/security/tutorial004.py!}
|
{!> ../../../docs_src/security/tutorial004.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -167,7 +167,7 @@ JWTトークンの署名に使用するアルゴリズム`"HS256"`を指定し
|
||||||
|
|
||||||
JWTアクセストークンを作成し、それを返します。
|
JWTアクセストークンを作成し、それを返します。
|
||||||
|
|
||||||
```Python hl_lines="115-128"
|
```Python hl_lines="115-130"
|
||||||
{!../../../docs_src/security/tutorial004.py!}
|
{!../../../docs_src/security/tutorial004.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -170,7 +170,7 @@ $ openssl rand -hex 32
|
||||||
|
|
||||||
创建并返回真正的 JWT 访问令牌。
|
创建并返回真正的 JWT 访问令牌。
|
||||||
|
|
||||||
```Python hl_lines="115-128"
|
```Python hl_lines="115-130"
|
||||||
{!../../../docs_src/security/tutorial004.py!}
|
{!../../../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
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@app.post("/token", response_model=Token)
|
@app.post("/token")
|
||||||
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
async def login_for_access_token(
|
||||||
|
form_data: OAuth2PasswordRequestForm = Depends()
|
||||||
|
) -> Token:
|
||||||
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
|
@ -125,7 +127,7 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
|
||||||
access_token = create_access_token(
|
access_token = create_access_token(
|
||||||
data={"sub": user.username}, expires_delta=access_token_expires
|
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)
|
@app.get("/users/me/", response_model=User)
|
||||||
|
|
|
||||||
|
|
@ -115,10 +115,10 @@ async def get_current_active_user(
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@app.post("/token", response_model=Token)
|
@app.post("/token")
|
||||||
async def login_for_access_token(
|
async def login_for_access_token(
|
||||||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
||||||
):
|
) -> Token:
|
||||||
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
|
@ -130,7 +130,7 @@ async def login_for_access_token(
|
||||||
access_token = create_access_token(
|
access_token = create_access_token(
|
||||||
data={"sub": user.username}, expires_delta=access_token_expires
|
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)
|
@app.get("/users/me/", response_model=User)
|
||||||
|
|
|
||||||
|
|
@ -114,10 +114,10 @@ async def get_current_active_user(
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@app.post("/token", response_model=Token)
|
@app.post("/token")
|
||||||
async def login_for_access_token(
|
async def login_for_access_token(
|
||||||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
||||||
):
|
) -> Token:
|
||||||
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
|
@ -129,7 +129,7 @@ async def login_for_access_token(
|
||||||
access_token = create_access_token(
|
access_token = create_access_token(
|
||||||
data={"sub": user.username}, expires_delta=access_token_expires
|
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)
|
@app.get("/users/me/", response_model=User)
|
||||||
|
|
|
||||||
|
|
@ -114,10 +114,10 @@ async def get_current_active_user(
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@app.post("/token", response_model=Token)
|
@app.post("/token")
|
||||||
async def login_for_access_token(
|
async def login_for_access_token(
|
||||||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
||||||
):
|
) -> Token:
|
||||||
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
|
@ -129,7 +129,7 @@ async def login_for_access_token(
|
||||||
access_token = create_access_token(
|
access_token = create_access_token(
|
||||||
data={"sub": user.username}, expires_delta=access_token_expires
|
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)
|
@app.get("/users/me/", response_model=User)
|
||||||
|
|
|
||||||
|
|
@ -111,8 +111,10 @@ async def get_current_active_user(current_user: User = Depends(get_current_user)
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@app.post("/token", response_model=Token)
|
@app.post("/token")
|
||||||
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
async def login_for_access_token(
|
||||||
|
form_data: OAuth2PasswordRequestForm = Depends()
|
||||||
|
) -> Token:
|
||||||
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
|
@ -124,7 +126,7 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
|
||||||
access_token = create_access_token(
|
access_token = create_access_token(
|
||||||
data={"sub": user.username}, expires_delta=access_token_expires
|
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)
|
@app.get("/users/me/", response_model=User)
|
||||||
|
|
|
||||||
|
|
@ -143,8 +143,10 @@ async def get_current_active_user(
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@app.post("/token", response_model=Token)
|
@app.post("/token")
|
||||||
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
async def login_for_access_token(
|
||||||
|
form_data: OAuth2PasswordRequestForm = Depends()
|
||||||
|
) -> Token:
|
||||||
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=400, detail="Incorrect username or password")
|
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},
|
data={"sub": user.username, "scopes": form_data.scopes},
|
||||||
expires_delta=access_token_expires,
|
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)
|
@app.get("/users/me/", response_model=User)
|
||||||
|
|
|
||||||
|
|
@ -144,10 +144,10 @@ async def get_current_active_user(
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@app.post("/token", response_model=Token)
|
@app.post("/token")
|
||||||
async def login_for_access_token(
|
async def login_for_access_token(
|
||||||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
||||||
):
|
) -> Token:
|
||||||
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=400, detail="Incorrect username or password")
|
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},
|
data={"sub": user.username, "scopes": form_data.scopes},
|
||||||
expires_delta=access_token_expires,
|
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)
|
@app.get("/users/me/", response_model=User)
|
||||||
|
|
|
||||||
|
|
@ -143,10 +143,10 @@ async def get_current_active_user(
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@app.post("/token", response_model=Token)
|
@app.post("/token")
|
||||||
async def login_for_access_token(
|
async def login_for_access_token(
|
||||||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
||||||
):
|
) -> Token:
|
||||||
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=400, detail="Incorrect username or password")
|
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},
|
data={"sub": user.username, "scopes": form_data.scopes},
|
||||||
expires_delta=access_token_expires,
|
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)
|
@app.get("/users/me/", response_model=User)
|
||||||
|
|
|
||||||
|
|
@ -143,10 +143,10 @@ async def get_current_active_user(
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@app.post("/token", response_model=Token)
|
@app.post("/token")
|
||||||
async def login_for_access_token(
|
async def login_for_access_token(
|
||||||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
form_data: Annotated[OAuth2PasswordRequestForm, Depends()]
|
||||||
):
|
) -> Token:
|
||||||
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=400, detail="Incorrect username or password")
|
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},
|
data={"sub": user.username, "scopes": form_data.scopes},
|
||||||
expires_delta=access_token_expires,
|
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)
|
@app.get("/users/me/", response_model=User)
|
||||||
|
|
|
||||||
|
|
@ -142,8 +142,10 @@ async def get_current_active_user(
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@app.post("/token", response_model=Token)
|
@app.post("/token")
|
||||||
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
async def login_for_access_token(
|
||||||
|
form_data: OAuth2PasswordRequestForm = Depends()
|
||||||
|
) -> Token:
|
||||||
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=400, detail="Incorrect username or password")
|
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},
|
data={"sub": user.username, "scopes": form_data.scopes},
|
||||||
expires_delta=access_token_expires,
|
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)
|
@app.get("/users/me/", response_model=User)
|
||||||
|
|
|
||||||
|
|
@ -143,8 +143,10 @@ async def get_current_active_user(
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
@app.post("/token", response_model=Token)
|
@app.post("/token")
|
||||||
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
async def login_for_access_token(
|
||||||
|
form_data: OAuth2PasswordRequestForm = Depends()
|
||||||
|
) -> Token:
|
||||||
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=400, detail="Incorrect username or password")
|
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},
|
data={"sub": user.username, "scopes": form_data.scopes},
|
||||||
expires_delta=access_token_expires,
|
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)
|
@app.get("/users/me/", response_model=User)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue