feat(M1): 认证与用户管理 — 双通道登录、cookie 会话、用户 CRUD 与真实系统账号对接
认证: - 图形验证码 GET /auth/captcha(内置 PNG 渲染,零第三方依赖) - 外部用户 OTP 双通道:DB 存储(otp_codes)使邮件与 CLI 共用同一验证码/冷却/失败限速 - 管理员 bcrypt 登录 + 连续失败限速锁定;admin/forgot + admin/reset 邮件重置(SMTP 或日志) - cookie 会话(HttpOnly/SameSite)、me/logout、admin/user 鉴权中间件 用户管理(admin): - CRUD + disable/enable/extend/delete,对接 system 层真实 useradd/usermod/userdel/passwd - system 层三执行模式:dry-run(默认,安全)/ direct(容器/测试用户)/ sudo(生产 sudoers 白名单) - Exists 系统账号一致性检查;deploy/sudoers.example 白名单模板 - 关键操作接入 append-only 审计 其他: - CLI user otp 改 DB store,与邮件通道真正对齐 - 容器镜像补 shadow(alpine 无 useradd);Makefile VERSION 0.2.0-m1 - 测试:auth/service 单测 + api httptest 集成 + 容器内真实系统账号端到端验证
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"ws_usernode/internal/model"
|
||||
"ws_usernode/internal/pkg"
|
||||
)
|
||||
|
||||
// ErrResetTokenInvalid 表示重置令牌无效、已使用或已过期。
|
||||
var ErrResetTokenInvalid = errors.New("auth: 重置令牌无效或已过期")
|
||||
|
||||
// ResetTokenStore 为管理员密码重置令牌存储(邮件重置)。
|
||||
type ResetTokenStore interface {
|
||||
// Create 生成令牌并存储其哈希,返回令牌明文(仅经邮件/日志发出)。
|
||||
Create(ctx context.Context, adminID uint, ttl time.Duration, ip string) (string, error)
|
||||
// Consume 校验令牌并标记已使用,返回对应的管理员 ID。
|
||||
Consume(ctx context.Context, token string) (uint, error)
|
||||
}
|
||||
|
||||
// DBResetTokenStore 基于 model.PasswordResetToken 的存储实现。
|
||||
type DBResetTokenStore struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewDBResetTokenStore 创建重置令牌存储。
|
||||
func NewDBResetTokenStore(db *gorm.DB) *DBResetTokenStore {
|
||||
return &DBResetTokenStore{db: db}
|
||||
}
|
||||
|
||||
func (s *DBResetTokenStore) Create(ctx context.Context, adminID uint, ttl time.Duration, ip string) (string, error) {
|
||||
token, err := pkg.RandomHex(24)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
row := model.PasswordResetToken{
|
||||
AdminID: adminID,
|
||||
TokenHash: hashToken(token),
|
||||
ExpiresAt: time.Now().Add(ttl),
|
||||
IP: ip,
|
||||
}
|
||||
if err := s.db.WithContext(ctx).Create(&row).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (s *DBResetTokenStore) Consume(ctx context.Context, token string) (uint, error) {
|
||||
var row model.PasswordResetToken
|
||||
if err := s.db.WithContext(ctx).Where("token_hash = ?", hashToken(token)).First(&row).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return 0, ErrResetTokenInvalid
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if row.UsedAt != nil || time.Now().After(row.ExpiresAt) {
|
||||
return 0, ErrResetTokenInvalid
|
||||
}
|
||||
now := time.Now()
|
||||
if err := s.db.WithContext(ctx).Model(&row).Update("used_at", &now).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return row.AdminID, nil
|
||||
}
|
||||
|
||||
// hashToken 计算令牌的 SHA-256 摘要(令牌本身为高熵随机串,无需加盐)。
|
||||
func hashToken(token string) string {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
Reference in New Issue
Block a user