feat: 系统全面优化 - 设备管理/登录日志导出/性能监控/设置页面
后端: - 新增全局设备管理 API(DeviceHandler.GetAllDevices) - 新增登录日志导出功能(LogHandler.ExportLoginLogs, CSV/XLSX) - 新增设置服务(SettingsService)和设置页面 API - 设备管理支持多条件筛选(状态/信任状态/关键词) - 登录日志支持流式导出防 OOM - 操作日志支持按方法/时间范围搜索 - 主题配置服务(ThemeService) - 增强监控健康检查(Prometheus metrics + SLO) - 移除旧 ratelimit.go(已迁移至 robustness) - 修复 SocialAccount NULL 扫描问题 - 新增 API 契约测试、Handler 测试、Settings 测试 前端: - 新增管理员设备管理页面(DevicesPage) - 新增管理员登录日志导出功能 - 新增系统设置页面(SettingsPage) - 设备管理支持筛选和分页 - 增强 HTTP 响应类型 测试: - 业务逻辑测试 68 个(含并发 CONC_001~003) - 规模测试 16 个(P99 百分位统计) - E2E 测试、集成测试、契约测试 - 性能基准测试、鲁棒性测试 全面测试通过(38 个测试包)
This commit is contained in:
@@ -57,15 +57,18 @@ type Claims struct {
|
||||
}
|
||||
|
||||
// generateJTI 生成唯一的 JWT ID
|
||||
// 使用 crypto/rand 生成密码学安全的随机数,仅使用随机数不包含时间戳
|
||||
// 使用时间戳 + 密码学安全随机数,防止枚举攻击
|
||||
// 格式: {timestamp(8字节hex)}{random(16字节hex)},共 24 字符
|
||||
func generateJTI() (string, error) {
|
||||
// 生成 16 字节的密码学安全随机数
|
||||
// 时间戳部分(8 字节 hex,足够 584 年)
|
||||
timestamp := time.Now().Unix()
|
||||
// 随机数部分(16 字节,128 位)
|
||||
b := make([]byte, 16)
|
||||
if _, err := cryptorand.Read(b); err != nil {
|
||||
return "", fmt.Errorf("generate jwt jti failed: %w", err)
|
||||
}
|
||||
// 使用十六进制编码,仅使用随机数确保不可预测
|
||||
return fmt.Sprintf("%x", b), nil
|
||||
// 组合时间戳和随机数:timestamp(8字节) + random(16字节) = 24字节 hex
|
||||
return fmt.Sprintf("%016x%x", timestamp, b), nil
|
||||
}
|
||||
|
||||
// NewJWT creates a legacy HS256 JWT manager for compatibility in tests and callers
|
||||
|
||||
@@ -2,7 +2,6 @@ package auth
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
@@ -119,16 +118,23 @@ func HashRecoveryCode(code string) (string, error) {
|
||||
}
|
||||
|
||||
// VerifyRecoveryCode 验证恢复码(自动哈希后比较)
|
||||
// 使用恒定时间比较防止时序攻击
|
||||
func VerifyRecoveryCode(inputCode string, hashedCodes []string) (int, bool) {
|
||||
hashedInput, err := HashRecoveryCode(inputCode)
|
||||
if err != nil {
|
||||
return -1, false
|
||||
}
|
||||
for i, hashed := range hashedCodes {
|
||||
if hmac.Equal([]byte(hashedInput), []byte(hashed)) {
|
||||
return i, true
|
||||
found := -1
|
||||
// 固定次数比较,防止时序攻击泄露匹配位置
|
||||
for i := 0; i < len(hashedCodes); i++ {
|
||||
hashed := hashedCodes[i]
|
||||
if subtle.ConstantTimeCompare([]byte(hashedInput), []byte(hashed)) == 1 {
|
||||
found = i
|
||||
}
|
||||
}
|
||||
if found >= 0 {
|
||||
return found, true
|
||||
}
|
||||
return -1, false
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user