71f0a1abdb
- 所有Go模块路径从 github.com/yourname/cyrene-ai 迁移到 git.yeij.top/AskaEth/Cyrene - 5个Go Dockerfile添加 GOPROXY=https://goproxy.cn,direct 解决国内构建问题 - ai-core go.mod 添加 pkg/plugins replace 指令 - Caddyfile 简化为 http:// 通配 + handle 保留 /api 前缀 - ethend Dockerfile 适配 (npm install + 仅 COPY package.json) - ethend 新增 RUNNING_IN_DOCKER 环境变量,健康检查改用Docker服务名 - ethend 数据库状态检查支持Docker hostname (postgres/redis/qdrant/minio) - process-manager 新增 CONTAINER_SVC_MAP + Docker模式自动检测 - 统一 docker-compose.dev.db.yml 卷名 (pg_data/redis_data/qdrant_data/minio_data) - docker-compose.yml ethend服务挂载docker.sock + 端口变量化 - 清理 .env 统一后的残留文件与提示信息 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene/gateway/internal/config"
|
|
)
|
|
|
|
// Auth 用户键值在context中的key
|
|
const UserIDKey = "user_id"
|
|
const IsAdminKey = "is_admin"
|
|
|
|
// JWTAuth JWT认证中间件
|
|
func JWTAuth(cfg *config.Config) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
authHeader := c.GetHeader("Authorization")
|
|
if authHeader == "" {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "未提供认证令牌"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
// Bearer token
|
|
parts := strings.SplitN(authHeader, " ", 2)
|
|
if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "认证格式错误"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
tokenString := parts[1]
|
|
userID, err := cfg.ValidateToken(tokenString)
|
|
if err != nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "认证令牌无效或已过期"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
// 将userID注入上下文
|
|
c.Set(UserIDKey, userID)
|
|
// 设置管理员标记 (admin 用户 ID 为固定值 "admin")
|
|
c.Set(IsAdminKey, userID == "admin")
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// GetUserID 从上下文获取用户ID
|
|
func GetUserID(c *gin.Context) string {
|
|
userID, _ := c.Get(UserIDKey)
|
|
if userID == nil {
|
|
return ""
|
|
}
|
|
return userID.(string)
|
|
}
|
|
|
|
// GetIsAdmin 从上下文获取是否为管理员
|
|
func GetIsAdmin(c *gin.Context) bool {
|
|
isAdmin, _ := c.Get(IsAdminKey)
|
|
if isAdmin == nil {
|
|
return false
|
|
}
|
|
return isAdmin.(bool)
|
|
}
|