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

38 lines
649 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 := ""
authcode := c.GetHeader("Authorization")
parts := strings.SplitN(authcode, " ", 2)
if len(parts) == 2 && parts[0] == "Bearer" {
token = parts[1]
}
if token != args.Httpd.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 SwagGuard(c *gin.Context) {
if !args.Httpd.Swag && strings.HasPrefix(c.Request.URL.Path, "/swag") {
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()
}
}