Files
xiaoxia-saas/packages/domain/verification_code.py
T
CI Bot 100e054896
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 34s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m3s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 2m23s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 2m25s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 3m3s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m12s
CI/CD Pipeline / Integration Tests (pull_request) Failing after 23s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 4m8s
AI Code Review / AI Code Review (pull_request) Successful in 5m38s
feat(#558): 微信登录 + 绑定手机号邮箱完整后端实现
- 数据库:users表加phone/phone_verified/binding_completed_at字段 + 新建verification_codes表(migration 049)
- 微信OAuth:GET /auth/wechat/url + POST /auth/wechat/callback,支持配置缺失时的mock模式
- 验证码服务:统一管理手机+邮箱验证码,60s冷却+每日10次+5次尝试错误频控
- 绑定接口:POST /auth/send-verification-code + POST /auth/bind-contact
- /me 接口扩展:返回 phone / phone_verified / binding_complete
- 短信服务:Noop + 阿里云适配器骨架
- 单元测试20个全绿
2026-07-21 08:38:12 +08:00

67 lines
1.8 KiB
Python
Executable File

"""
验证码领域实体
"""
from dataclasses import dataclass, field
from datetime import datetime, timedelta, timezone
from uuid import uuid4
@dataclass(slots=True)
class VerificationCode:
"""验证码(邮箱/手机统一)"""
id: str
recipient: str # 邮箱或手机号
code: str
code_type: str # email_bind / phone_bind / email_login / phone_login / reset_password
expires_at: datetime
used_at: datetime | None = None
attempts: int = 0
created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
@classmethod
def create(
cls,
recipient: str,
code_type: str,
ttl_seconds: int = 300,
custom_code: str | None = None,
) -> "VerificationCode":
"""创建验证码"""
import random
code = custom_code or "".join(random.choices("0123456789", k=6))
now = datetime.now(timezone.utc)
return cls(
id=uuid4().hex,
recipient=recipient.strip(),
code=code,
code_type=code_type,
expires_at=now + timedelta(seconds=ttl_seconds),
created_at=now,
)
@property
def is_expired(self) -> bool:
"""是否已过期"""
return datetime.now(timezone.utc) > self.expires_at
@property
def is_used(self) -> bool:
"""是否已使用"""
return self.used_at is not None
@property
def is_valid(self) -> bool:
"""是否有效(未过期且未使用)"""
return not self.is_expired and not self.is_used
def mark_used(self) -> None:
"""标记为已使用"""
self.used_at = datetime.now(timezone.utc)
def increment_attempts(self) -> None:
"""增加尝试次数"""
self.attempts += 1