清理项目配置文件,移除不再使用的.idea文件和更新文档中的Python版本要求
This commit is contained in:
@@ -2,16 +2,14 @@ from typing import Annotated
|
||||
|
||||
import jwt
|
||||
from fastapi import Depends
|
||||
from fastapi import HTTPException
|
||||
from jwt import InvalidTokenError
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
import JWT
|
||||
from model import User
|
||||
from model.database import Database
|
||||
from pkg import utils
|
||||
|
||||
|
||||
# 验证是否为管理员
|
||||
async def get_current_user(
|
||||
token: Annotated[str, Depends(JWT.oauth2_scheme)],
|
||||
session: Annotated[AsyncSession, Depends(Database.get_session)],
|
||||
@@ -19,18 +17,13 @@ async def get_current_user(
|
||||
"""
|
||||
验证用户身份并返回当前用户信息。
|
||||
"""
|
||||
not_login_exception = HTTPException(
|
||||
status_code=401,
|
||||
detail="Login required",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
try:
|
||||
payload = jwt.decode(token, await JWT.get_secret_key(), algorithms=[JWT.ALGORITHM])
|
||||
username = payload.get("sub")
|
||||
stored_account = await User.get(session, User.email == username)
|
||||
if username is None or stored_account.email != username:
|
||||
raise not_login_exception
|
||||
utils.raise_unauthorized("Login required")
|
||||
return stored_account
|
||||
except InvalidTokenError:
|
||||
raise not_login_exception
|
||||
utils.raise_unauthorized("Login required")
|
||||
Reference in New Issue
Block a user