31ea370cea
- 修复 JWTConfig 缺少 mapstructure tag 导致 access_expire_min 解析为 0, token 签发即过期,所有 API 请求返回 401 - 全部 config struct 补齐 mapstructure tag(secret/dsn/hmac_secret 等) - 模型层从 hotel/HotelID 统一重命名为 shop/ShopID - 删除旧 migrations(001-004),新增 001_init 综合迁移文件 - 更新 schema.sql、testutil、handler/service/model 相关引用 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
87 lines
1.9 KiB
Go
87 lines
1.9 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/wangjia/jiu/backend/config"
|
|
)
|
|
|
|
type Claims struct {
|
|
UserID uint64 `json:"user_id"`
|
|
ShopID uint64 `json:"shop_id"`
|
|
Role string `json:"role"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
const (
|
|
CtxUserID = "user_id"
|
|
CtxShopID = "shop_id"
|
|
CtxRole = "role"
|
|
)
|
|
|
|
func JWT() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
authHeader := c.GetHeader("Authorization")
|
|
if authHeader == "" || !strings.HasPrefix(authHeader, "Bearer ") {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "missing token"})
|
|
return
|
|
}
|
|
|
|
tokenStr := strings.TrimPrefix(authHeader, "Bearer ")
|
|
claims := &Claims{}
|
|
token, err := jwt.ParseWithClaims(tokenStr, claims, func(t *jwt.Token) (interface{}, error) {
|
|
return []byte(config.C.JWT.Secret), nil
|
|
})
|
|
if err != nil || !token.Valid {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
|
|
return
|
|
}
|
|
|
|
c.Set(CtxUserID, claims.UserID)
|
|
c.Set(CtxShopID, claims.ShopID)
|
|
c.Set(CtxRole, claims.Role)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// AdminOnly 仅管理员可访问
|
|
func AdminOnly() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
role, _ := c.Get(CtxRole)
|
|
if role != "admin" {
|
|
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "admin only"})
|
|
return
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// ReadOnly 只读用户禁止写操作
|
|
func ReadOnly() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
role, _ := c.Get(CtxRole)
|
|
if role == "readonly" && c.Request.Method != "GET" {
|
|
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "readonly user"})
|
|
return
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// GetShopID 从 context 中安全获取 shop_id
|
|
func GetShopID(c *gin.Context) uint64 {
|
|
v, _ := c.Get(CtxShopID)
|
|
id, _ := v.(uint64)
|
|
return id
|
|
}
|
|
|
|
// GetUserID 从 context 中安全获取 user_id
|
|
func GetUserID(c *gin.Context) uint64 {
|
|
v, _ := c.Get(CtxUserID)
|
|
id, _ := v.(uint64)
|
|
return id
|
|
}
|