我目前正在使用 basic 进行身份验证,遵循 this tutorial:
import secrets
from fastapi import FastAPI, Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials,
http_basic = HTTPBasic()
def authorize_basic(credentials: HTTPBasicCredentials = Depends(http_basic)):
correct_username = secrets.compare_digest(credentials.username, "test")
correct_password = secrets.compare_digest(credentials.password, "test")
if not (correct_username and correct_password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Basic"},
)
@app.get("/auth/", dependencies=[Depends(authorize_basic)])
def auth():
return {"success": "true"}
如何改用 HTTPDigest?