📝 Update tutorial/security/oauth2-jwt/ to use pwdlib with Argon2 instead of passlib (#13917)

Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
Neizvestnyj
2025-09-29 05:57:38 +03:00
committed by GitHub
parent 450a334253
commit efdafa4361
15 changed files with 75 additions and 77 deletions

View File

@@ -4,7 +4,7 @@ import jwt
from fastapi import Depends, FastAPI, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from jwt.exceptions import InvalidTokenError
from passlib.context import CryptContext
from pwdlib import PasswordHash
from pydantic import BaseModel
# to get a string like this run:
@@ -19,7 +19,7 @@ fake_users_db = {
"username": "johndoe",
"full_name": "John Doe",
"email": "johndoe@example.com",
"hashed_password": "$2b$12$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW",
"hashed_password": "$argon2id$v=19$m=65536,t=3,p=4$wagCPXjifgvUFBzq4hqe3w$CYaIb8sB+wtD+Vu/P4uod1+Qof8h+1g7bbDlBID48Rc",
"disabled": False,
}
}
@@ -45,7 +45,7 @@ class UserInDB(User):
hashed_password: str
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
password_hash = PasswordHash.recommended()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
@@ -53,11 +53,11 @@ app = FastAPI()
def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password, hashed_password)
return password_hash.verify(plain_password, hashed_password)
def get_password_hash(password):
return pwd_context.hash(password)
return password_hash.hash(password)
def get_user(db, username: str):