3
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
"""Token 服务:生成、哈希、加密、解密。
|
||||
|
||||
用于知识库和文档的 Secret URL token 管理。
|
||||
"""
|
||||
|
||||
from app.core.security import decrypt_token, encrypt_token, generate_token, hash_token
|
||||
|
||||
|
||||
class TokenService:
|
||||
@staticmethod
|
||||
def create_token_pair() -> tuple[str, str, str, str]:
|
||||
"""生成 token 并返回 (token, token_hash, token_encrypted, token_hint)。
|
||||
|
||||
- token: 原文(仅此一次返回给用户)
|
||||
- token_hash: SHA-256 哈希(存 DB,用于查询)
|
||||
- token_encrypted: Fernet 加密原文(存 DB,供后台显示完整链接)
|
||||
- token_hint: 末 8 位明文(存 DB,供后台识别)
|
||||
"""
|
||||
token = generate_token()
|
||||
token_hash = hash_token(token)
|
||||
token_encrypted = encrypt_token(token)
|
||||
token_hint = token[-8:] if len(token) >= 8 else token
|
||||
return token, token_hash, token_encrypted, token_hint
|
||||
|
||||
@staticmethod
|
||||
def hash_token(token: str) -> str:
|
||||
return hash_token(token)
|
||||
|
||||
@staticmethod
|
||||
def decrypt_token(encrypted: str) -> str:
|
||||
return decrypt_token(encrypted)
|
||||
Reference in New Issue
Block a user