package middleware import ( "encoding/json" "eta/eta_mini_ht_api/common/component/config" logger "eta/eta_mini_ht_api/common/component/log" "eta/eta_mini_ht_api/common/contants" "eta/eta_mini_ht_api/common/utils/auth" "fmt" "github.com/beego/beego/v2/server/web" "github.com/beego/beego/v2/server/web/context" "strings" ) var ( htConfig = config.GetConfig(contants.HT).(*config.HTBizConfig) ) func WebHookAuthMiddleware() web.FilterFunc { return func(ctx *context.Context) { if strings.Contains(ctx.Input.URL(), "payment") || strings.Contains(ctx.Input.URL(), "es/delete") { return } 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.ParsePrivateKey(htConfig.GetWebhookPrivateKey()) if err != nil { rep := webhookSysErr("系统异常") logger.Error("解析私钥失败: %v", err) _ = ctx.JSONResp(rep) return } logger.Info("解码请求: %v", webhookRequest.Data) decodeData, err := auth.DecryptWithRSA(privateKey, webhookRequest.Data) if err != nil { rep := webhookSysErr("解密请求体失败") logger.Error("解密请求体失败: %v", err) _ = ctx.JSONResp(rep) return } fmt.Printf("解密后的请求: %v", string(decodeData)) ctx.Input.RequestBody = decodeData return } } type WebhookRequest struct { Data string `json:"data"` }