package middleware import ( "encoding/base64" "encoding/json" logger "eta/eta_mini_ht_api/common/component/log" "eta/eta_mini_ht_api/common/utils/auth" "fmt" "github.com/beego/beego/v2/server/web" "github.com/beego/beego/v2/server/web/context" ) func WebHookAuthMiddleware() web.FilterFunc { return func(ctx *context.Context) { body := ctx.Input.RequestBody webhookRequest := new(WebhookRequest) err := json.Unmarshal(body, &webhookRequest) if err != nil { rep := webhookUnauthorized("请求参数异常") logger.Error("WebhookRequest解析失败: %v", err) _ = ctx.JSONResp(rep) return } privateKey, err := auth.ParsePrivateKeyFromPEM() if err != nil { rep := webhookUnauthorized("系统异常") logger.Error("解析私钥失败: %v", err) _ = ctx.JSONResp(rep) return } aesKey, err := auth.DecryptWithRSA(privateKey, webhookRequest.EncryptKey) if err != nil { rep := webhookUnauthorized("解析AES秘钥") logger.Error("解析AES秘钥失败: %v", err) _ = ctx.JSONResp(rep) return } decodeKey, _ := base64.StdEncoding.DecodeString(string(aesKey)) logger.Info("解码请求: %v", webhookRequest.Data) data, err := base64.StdEncoding.DecodeString(webhookRequest.Data) aes, err := auth.DecryptWithAES(decodeKey, data) if err != nil { rep := webhookUnauthorized("解密请求体失败") logger.Error("解密请求体失败: %v", err) _ = ctx.JSONResp(rep) return } fmt.Printf("解密后的请求: %v", string(aes)) ctx.Input.RequestBody = aes return } } type WebhookRequest struct { Data string `json:"data"` EncryptKey string `json:"encryptKey"` }