From b0fee2cb3cc76ac0b88dd162db8a5cc31f3b2734 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Fri, 10 Nov 2023 17:31:19 +0800 Subject: [PATCH] add token verify --- app/core/security.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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)