FastAPI 学习之路(二十八)使用密码和 Bearer 的简单 OAuth2
OAuth2 规定在使用(我们打算用的)「password 流程」时,客户端/用户必须将 username 和 password 字段作为表单数据发送。我们看下在我们应该去如何实现呢。
我们写一个登录接口,默认返回token和token_type
from fastapi import FastAPI, Depends,status,HTTPException from pydantic import BaseModel from typing import Optional from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") fake_users = { "leizi": { "username": "leizi", "full_name": "leizishuoceshikaifa", "email": "leizi@leizi.com", "hashed_password": "leizi", "disabled": False } } app = FastAPI() def fake_hash_password(password: str): return password class User(BaseModel): username: str email: Optional[str] = None full_name: Optional[str] = None disabled: Optional[bool] = None class UserInDB(User): hashed_password: str def get_user(db, username: str): if username in db: user_dict = db[username] return UserInDB(**user_dict) def fake_decode_token(token): user = get_user(fake_users, token) return user def get_current_user(token: str = Depends(oauth2_scheme)): user = fake_decode_token(token) print(user) if not user: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid authentication", headers={"WWW-Authenticate": "Bearer"}, ) return user @app.post("/login") def login(form_data: OAuth2PasswordRequestForm = Depends()): #校验密码¶ # 目前我们已经从数据库中获取了用户数据,但尚未校验密码。 # 让我们首先将这些数据放入 Pydantic UserInDB 模型中。 # 永远不要保存明文密码,因此,我们将使用(伪)哈希密码系统。 # 如果密码不匹配,我们将返回同一个错误。 user_dict = fake_users.get(form_data.username) print(user_dict) if not user_dict: raise HTTPException(status_code=400, detail="用户名错误") user = UserInDB(**user_dict) hashed_password = fake_hash_password(form_data.password) if not hashed_password == user.hashed_password: raise HTTPException(status_code=400, detail="密码错误 ") return {"access_token": user.username, "token_type": "bearer"} @app.get("/users/me") async def read_users_me(current_user: User = Depends(get_current_user)): print(current_user) return current_user