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

27 lines
455 B
Go
Raw Normal View History

2023-12-17 13:16:15 +08:00
package midware
import (
"strings"
"github.com/gin-gonic/gin"
"github.com/opentdp/wechat-rest/args"
)
func AuthGuard(c *gin.Context) {
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 {
c.Set("Error", gin.H{"Code": 401, "Message": "未授权的操作"})
c.Set("ExitCode", 401)
c.Abort()
}
}