Files
ai-admin-server/app/utils/CrpyUtils.py
T

49 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from datetime import timedelta, datetime, timezone
from typing import TypedDict, Literal, TypeAlias
import jwt
from passlib.context import CryptContext
from app.config.env import env
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# token的类型,access用于接口认证,refresh用于刷新access token,verify用于激活用户账号
AccessTokenType: TypeAlias = Literal["access", "refresh", "verify", "api"]
class TokenInfo(TypedDict):
# 用户名信息
username: str
# token过期时间
exp: datetime
type: AccessTokenType
class CryptUtils:
@staticmethod
def get_password_hash(password: str):
return pwd_context.hash(password)
@staticmethod
def verify_password(plain_password: str, hashed_password: str):
return pwd_context.verify(plain_password, hashed_password)
@staticmethod
def create_token(
username: str,
type: AccessTokenType,
expires_delta: timedelta
):
data: TokenInfo = {
"username": username,
"type": type,
"exp": datetime.now(timezone.utc) + expires_delta
}
return jwt.encode(data, env.jwt_secret_key, env.jwt_algorithm)
@staticmethod
def get_token_info(token: str) -> TokenInfo:
data = jwt.decode(token, env.jwt_secret_key, algorithms=[env.jwt_algorithm])
return data