diff --git a/app/core/security.py b/app/core/security.py index b1ff9183..3bb25d79 100644 --- a/app/core/security.py +++ b/app/core/security.py @@ -52,6 +52,32 @@ def verify_token(token: str = Depends(reusable_oauth2)) -> schemas.TokenPayload: ) +def get_token(token: str = None) -> str: + """ + 从请求URL中获取token + """ + if token is None: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="token请求参数缺失", + headers={"WWW-Authenticate": "Bearer"}, + ) + return token + + +def verify_uri_token(token: str = Depends(get_token)) -> str: + """ + 通过依赖项使用token进行身份认证 + """ + if token != settings.API_TOKEN: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="token校验不通过", + headers={"WWW-Authenticate": "Bearer"}, + ) + return token + + def verify_password(plain_password: str, hashed_password: str) -> bool: return pwd_context.verify(plain_password, hashed_password)