WeChatFerry/clients/gohttp/httpd/midware/guard.go

40 lines
688 B
Go
Raw Normal View History

2023-12-17 13:16:15 +08:00
package midware
import (
"strings"
"github.com/gin-gonic/gin"
2023-12-18 08:48:14 +08:00
"wechat-rest/args"
2023-12-17 13:16:15 +08:00
)
2023-12-29 21:28:18 +08:00
func ApiGuard(c *gin.Context) {
2023-12-17 13:16:15 +08:00
token := ""
// 取回 Token
2023-12-17 13:16:15 +08:00
authcode := c.GetHeader("Authorization")
parts := strings.SplitN(authcode, " ", 2)
if len(parts) == 2 && parts[0] == "Bearer" {
token = parts[1]
}
// 校验 Token
if token != args.Web.Token {
2023-12-29 21:28:18 +08:00
c.Set("Error", gin.H{"Code": 401, "Message": "操作未授权"})
2023-12-17 13:16:15 +08:00
c.Set("ExitCode", 401)
c.Abort()
}
}
2023-12-29 21:28:18 +08:00
func SwaggerGuard(c *gin.Context) {
2023-12-29 21:28:18 +08:00
if !args.Web.Swagger && strings.HasPrefix(c.Request.URL.Path, "/swagger") {
2024-01-10 09:59:47 +08:00
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(200, "功能已禁用")
2023-12-29 21:28:18 +08:00
c.Abort()
}
}