chore: 统一本地服务器与远程仓库源码
- 合并 main 与 master 历史并以服务器结构为基线 - 保留迁移、数据保留、AWS 账单与部署安全能力 - 建立 SDD 规格和 CI 验证闭环
This commit is contained in:
commit
fd60361d93
460 files changed
+37492
-23871
No files matched your search
@@ -0,0 +1,54 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"ezops/model"
|
||||
"ezops/pkg/agentauth"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const agentCredentialContextKey = "agentCredential"
|
||||
|
||||
// AgentAuth 在兼容阶段识别新 Token,强制阶段拒绝旧的匿名和用户 JWT 调用。 // Logan.Li
|
||||
func AgentAuth(db *gorm.DB, enforce bool) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
if enforce {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, model.Response{Code: 401, Message: "未提供 Agent 凭证"})
|
||||
}
|
||||
return
|
||||
}
|
||||
parts := strings.SplitN(authHeader, " ", 2)
|
||||
if len(parts) == 2 && parts[0] == "Bearer" && strings.HasPrefix(parts[1], "agt_") {
|
||||
credential, err := agentauth.Authenticate(db, parts[1])
|
||||
if err == nil {
|
||||
c.Set(agentCredentialContextKey, credential)
|
||||
return
|
||||
}
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, model.Response{Code: 401, Message: "无效 Agent 凭证"})
|
||||
return
|
||||
}
|
||||
if enforce {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, model.Response{Code: 401, Message: "无效 Agent 凭证"})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RequireAgentHostname 校验请求中主机名与凭证绑定主机一致。 // Logan.Li
|
||||
func RequireAgentHostname(c *gin.Context, hostname string) bool {
|
||||
credential, exists := c.Get(agentCredentialContextKey)
|
||||
if !exists {
|
||||
return true
|
||||
}
|
||||
agentCredential, ok := credential.(*model.AgentCredential)
|
||||
if !ok || agentCredential.Hostname != hostname {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, model.Response{Code: 403, Message: "Agent 凭证与主机不匹配"})
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in new issue
Block a user