mirror of
https://github.com/yangjian102621/geekai.git
synced 2026-09-16 18:27:13 +00:00
d4fd38ab7e
- 同步 Plus v4.3.1 功能源并移除商业 License 闭环 - 更新开源镜像命名、Docker 部署版本和 geekai 数据库配置 - 补充前端 ESLint 检查配置并修复存量解析与模板问题 - 保留 JWT、管理员权限、API Key 和 OAuth 等正常鉴权机制
90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
package handler
|
|
|
|
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
// * Copyright 2023 The Geek-AI Authors. All rights reserved.
|
|
// * Use of this source code is governed by a Apache-2.0 license
|
|
// * that can be found in the LICENSE file.
|
|
// * @Author yangjian102621@163.com
|
|
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/hex"
|
|
"geekai/core"
|
|
"geekai/utils/resp"
|
|
"log"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type WxGzhHandler struct {
|
|
BaseHandler
|
|
}
|
|
|
|
func NewWxGzhHandler(server *core.AppServer) *WxGzhHandler {
|
|
return &WxGzhHandler{
|
|
BaseHandler: BaseHandler{
|
|
App: server,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (h *WxGzhHandler) RegisterRoutes() {
|
|
group := h.App.Engine.Group("/api/wx/")
|
|
group.GET("verify", h.WechatVerify)
|
|
}
|
|
|
|
// 处理微信服务器验证请求
|
|
func (h *WxGzhHandler) WechatVerify(c *gin.Context) {
|
|
logger.Info("WechatVerify")
|
|
// 只处理 GET 请求
|
|
if c.Request.Method != "GET" {
|
|
resp.ERROR(c, "Method Not Allowed")
|
|
return
|
|
}
|
|
|
|
// 解析 URL 参数
|
|
signature := c.Query("signature")
|
|
timestamp := c.Query("timestamp")
|
|
nonce := c.Query("nonce")
|
|
echostr := c.Query("echostr")
|
|
|
|
// 验证参数完整性
|
|
if signature == "" || timestamp == "" || nonce == "" || echostr == "" {
|
|
log.Println("Missing parameters")
|
|
resp.ERROR(c, "Missing parameters")
|
|
return
|
|
}
|
|
|
|
// 验证签名
|
|
if validateSignature(signature, h.App.SysConfig.WxGzh.Token, timestamp, nonce) {
|
|
// 验证成功,返回 echostr(必须是纯文本)
|
|
c.String(http.StatusOK, echostr)
|
|
log.Println("Token verification success")
|
|
} else {
|
|
// 验证失败
|
|
resp.ERROR(c, "Forbidden: Invalid signature")
|
|
log.Println("Token verification failed")
|
|
}
|
|
}
|
|
|
|
func validateSignature(signature, token, timestamp, nonce string) bool {
|
|
// 1. 将 token、timestamp、nonce 按字典序排序
|
|
strs := []string{token, timestamp, nonce}
|
|
sort.Strings(strs)
|
|
|
|
// 2. 拼接字符串
|
|
joined := strings.Join(strs, "")
|
|
|
|
// 3. 计算 SHA1 哈希
|
|
hash := sha1.New()
|
|
hash.Write([]byte(joined))
|
|
hashed := hex.EncodeToString(hash.Sum(nil))
|
|
|
|
// 4. 与 signature 比对
|
|
return hashed == signature
|
|
}
|