| 12345678910111213141516171819202122232425262728293031323334353637 |
- """
- 动态HMAC密钥模型
- 用于存储用户特定的动态HMAC密钥
- """
- from datetime import datetime
- from sqlalchemy import Column, Integer, String, DateTime, Boolean, Text
- from app.database import Base
- from app.utils.timezone import beijing_now_naive
- class DynamicHmacKey(Base):
- """动态HMAC密钥表
-
- 用于存储用户登录后获取的动态HMAC密钥,
- 该密钥用于后续请求的签名验证。
- """
- __tablename__ = "dynamic_hmac_keys"
-
- id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
- user_id = Column(Integer, nullable=False, comment="用户ID")
- hmac_key = Column(Text, nullable=False, comment="HMAC密钥(Base64编码)")
- machine_code = Column(String(128), nullable=False, comment="机器码")
- key_version = Column(Integer, default=1, comment="密钥版本")
- is_active = Column(Boolean, default=True, comment="是否激活")
- is_deleted = Column(Boolean, default=False, comment="是否删除")
- created_at = Column(DateTime, default=beijing_now_naive, comment="创建时间")
- updated_at = Column(DateTime, nullable=True, comment="更新时间")
- expired_at = Column(DateTime, nullable=True, comment="过期时间")
-
- def __repr__(self):
- return f"<DynamicHmacKey(id={self.id}, user_id={self.user_id}, key_version={self.key_version})>"
- def is_expired(self) -> bool:
- """检查密钥是否过期"""
- if self.expired_at:
- return beijing_now_naive() > self.expired_at
- return False
|