mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 22:06:38 +08:00
2f60329886
代码质量全量清理,零行为变化:golangci 扩展集 13 类 linter(gosec/modernize/perfsprint/canonicalheader/usestdlibvars/wastedassign/intrange/errorlint/forcetypeassert/recvcheck/exhaustive/unparam)全量修复,测试代码质量(testifylint/thelper/usetesting)25→0,frpc 进程生命周期真 bug(进程组击杀)、全仓 go test -race 6 类数据竞争(含 1 个生产竞争)、SPDX license 头补齐 131 文件、前端测试套件 next-intl 迁移后 44 失败→全绿、过期 swagger 文档重新生成、pnpm-workspace 构建审批。 Experiments: #2-#17, #18, #20, #21, #23 Metric: total_issues 108 → 8 (-92.6%)
61 lines
1.7 KiB
Go
61 lines
1.7 KiB
Go
// Copyright 2025 linux.do
|
|
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Package model 定义数据模型与 GORM 实体
|
|
package model
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
tokenByteLength = 24 // Token 随机字节长度
|
|
maskThreshold = 8 // 脱敏显示阈值
|
|
)
|
|
|
|
// AccessToken 个人访问令牌实体
|
|
type AccessToken struct {
|
|
ID uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
|
|
UserID uint64 `json:"user_id" gorm:"index;not null"`
|
|
Name string `json:"name" gorm:"size:128;not null"`
|
|
TokenHash string `json:"-" gorm:"size:64;uniqueIndex;not null"`
|
|
MaskedToken string `json:"masked_token" gorm:"size:64;not null"`
|
|
IsAdmin bool `json:"is_admin" gorm:"default:false"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (AccessToken) TableName() string {
|
|
return "w_access_tokens"
|
|
}
|
|
|
|
// GenerateTokenString 生成加密安全的随机 Token 值
|
|
func GenerateTokenString() (string, error) {
|
|
bytes := make([]byte, tokenByteLength)
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
return "", err
|
|
}
|
|
return "at_" + hex.EncodeToString(bytes), nil
|
|
}
|
|
|
|
// HashToken 计算 Token 的 SHA-256 哈希值用于数据库存储与查询
|
|
func HashToken(token string) string {
|
|
h := sha256.New()
|
|
h.Write([]byte(token))
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
// MaskTokenString 生成脱敏显示的 Token,仅保留前缀和最后四位
|
|
func MaskTokenString(token string) string {
|
|
if len(token) <= maskThreshold {
|
|
return "at_****"
|
|
}
|
|
return fmt.Sprintf("%s...%s", token[:7], token[len(token)-4:])
|
|
}
|