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

38 lines
651 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") {
c.Set("Error", gin.H{"Code": 403, "Message": "功能已禁用"})
c.Set("ExitCode", 403)
c.Abort()
}
}