You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
1.1 KiB
29 lines
1.1 KiB
2 weeks ago
|
# dependencies.py
|
||
|
|
||
|
from fastapi import Request, HTTPException, status, Header
|
||
|
from utils.JwtUtil import *
|
||
|
|
||
|
async def get_current_user(token: str = Header(None, description="Authorization token")):
|
||
|
if token is None:
|
||
|
raise HTTPException(
|
||
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
|
detail="未提供令牌,请登录后使用!",
|
||
|
headers={"WWW-Authenticate": "Bearer"},
|
||
|
)
|
||
|
token = token.split(" ")[1] if token.startswith("Bearer ") else token
|
||
|
payload = verify_token(token)
|
||
|
if payload is None:
|
||
|
raise HTTPException(
|
||
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
|
detail="无法验证凭据,请登录后使用!",
|
||
|
headers={"WWW-Authenticate": "Bearer"},
|
||
|
)
|
||
|
user_id = payload.get("user_id")
|
||
|
identity_id = payload.get("identity_id")
|
||
|
if user_id is None or identity_id is None:
|
||
|
raise HTTPException(
|
||
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
|
detail="无法验证用户,请重新登录后使用!",
|
||
|
headers={"WWW-Authenticate": "Bearer"},
|
||
|
)
|
||
|
return user_id
|