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:
2026-08-29 23:40:20 +08:00
parent ae45aba607
commit 630d240dc0
32 changed files with 2923 additions and 188 deletions
+28 -14
View File
@@ -9,13 +9,14 @@ import (
"github.com/gin-gonic/gin"
"ws_usernode/internal/api"
"ws_usernode/internal/auth"
"ws_usernode/internal/config"
"ws_usernode/internal/webui"
)
// New 构建根 routerAPI v1 + 前端静态资源(go:embed)。
// production 模式启用 gin.ReleaseMode;否则启用调试模式与开发日志。
func New(cfg *config.Config, h *api.Handler, log *slog.Logger) *gin.Engine {
func New(cfg *config.Config, h *api.Handler, sessions auth.SessionStore, log *slog.Logger) *gin.Engine {
if cfg.App.Env == "production" {
gin.SetMode(gin.ReleaseMode)
}
@@ -28,21 +29,34 @@ func New(cfg *config.Config, h *api.Handler, log *slog.Logger) *gin.Engine {
// RESTful API v1
v1 := r.Group("/api/v1")
{
auth := v1.Group("/auth")
authGrp := v1.Group("/auth")
{
// M1captcha / otp/send / otp/login / admin/login / logout / me
auth.GET("/captcha", notImplemented("图形验证码(M1"))
auth.POST("/otp/send", notImplemented("OTP 发送(M1"))
auth.POST("/otp/login", notImplemented("OTP 登录(M1"))
auth.POST("/admin/login", notImplemented("管理员登录(M1"))
auth.POST("/logout", notImplemented("登出(M1"))
auth.GET("/me", notImplemented("当前会话(M1"))
authGrp.GET("/captcha", h.Auth.Captcha)
authGrp.POST("/otp/send", h.Auth.OTPSend)
authGrp.POST("/otp/login", h.Auth.OTPLogin)
authGrp.POST("/admin/login", h.Auth.AdminLogin)
authGrp.POST("/admin/forgot", h.Auth.AdminForgot)
authGrp.POST("/admin/reset", h.Auth.AdminReset)
// 需要会话(管理员或外部用户)
authed := authGrp.Group("", sessionMiddleware(sessions))
authed.POST("/logout", h.Auth.Logout)
authed.GET("/me", h.Auth.Me)
}
v1.GET("/users", h.User.List)
v1.POST("/users", h.User.Create)
v1.POST("/users/:id/disable", notImplemented("禁用用户(M1"))
v1.POST("/users/:id/enable", notImplemented("启用用户(M1"))
v1.POST("/users/:id/extend", notImplemented("延期(M1"))
// 用户管理(admin
users := v1.Group("/users", sessionMiddleware(sessions), requireUserType(auth.SessionUserAdmin))
{
users.GET("", h.User.List)
users.POST("", h.User.Create)
users.GET("/:id", h.User.Get)
users.PATCH("/:id", h.User.Update)
users.POST("/:id/disable", h.User.Disable)
users.POST("/:id/enable", h.User.Enable)
users.POST("/:id/extend", h.User.Extend)
users.DELETE("/:id", h.User.Delete)
}
// 后续里程碑
v1.POST("/approvals", notImplemented("提交申请(M3"))
v1.GET("/approvals", notImplemented("申请列表(M3"))
v1.POST("/approvals/:id/review", notImplemented("审批(M3"))