feat(gateway): 真实鉴权片1 —— JWT 注册/登录 + 校验中间件(后端核心)
替掉"裸 X-User-ID 头当身份"的临时方案,落地无状态 JWT 鉴权后端: - internal/auth:JWT 签发/校验(HS256,密钥 env JWT_SECRET,仅接受 HMAC 防 alg 混淆) + bcrypt 密码哈希/校验。纯包,含单测。 - User 模型加 Name + PasswordHash(json:"-" 不外泄);store 加 CreateUser/GetUserByEmail/ GetUserByID(邮箱唯一冲突 → ErrUserExists)。 - handler/auth:POST /auth/register(建用户+签发)· POST /auth/login(校验+签发, 用户不存在与密码错同一文案防枚举)· GET /auth/me。 - middleware/auth:解析 Bearer JWT,校验通过把已验证 userID 注入上下文(非阻断)。 - userID(c) 改为优先取 JWT 注入的 uid,兜底 X-User-ID 头(前端尚未接登录,保持可用)。 验证: - 单测:JWT 签发/解析往返、过期拒绝、篡改/非法拒绝、bcrypt 哈希校验。 - 实跑(nats+pg+gateway):注册→token+user(无密码)、重复注册 409、错密码 401、 /auth/me 带 token 200 / 无 token 401;owner 隔离改用已验证 uid —— 带 token 建的库 匿名/伪造 header 都看不到(JWT 用户数据归于雪花 id,header 无法臆测)。 片 2 待做:前端登录页 + 存令牌带 Bearer + 处理 401 + 去掉 header 兜底 + 保护路由。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -184,8 +184,14 @@ func (h *Handler) SetMemory(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok", "message": res.Content})
|
||||
}
|
||||
|
||||
// userID 从请求取已登录用户标识(真实场景应由鉴权中间件注入)。
|
||||
// userID 取当前用户标识:优先 JWT 鉴权中间件注入的已验证 uid;
|
||||
// 兜底 X-User-ID 头(开发期 / 前端尚未接登录),都没有则匿名。
|
||||
func userID(c *gin.Context) string {
|
||||
if v, ok := c.Get("uid"); ok {
|
||||
if s, _ := v.(string); s != "" {
|
||||
return s
|
||||
}
|
||||
}
|
||||
if u := c.GetHeader("X-User-ID"); u != "" {
|
||||
return u
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user