You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
656 B

package middleware
import (
"api2gpt-mid/service"
"github.com/gin-gonic/gin"
"log"
)
func TokenAuth() func(c *gin.Context) {
return func(c *gin.Context) {
auth := c.Request.Header.Get("Authorization")
log.Printf("auth: %v", auth)
if auth == "" {
c.AbortWithStatusJSON(401, gin.H{"code": 40001})
} else {
//key := strings.Trim(auth, "Bearer ")
key := auth[7:]
log.Printf("key: %v", key)
msg, err := service.CheckKeyAndTimeCount(key)
if err != nil {
log.Printf("checkKeyMid err: %v", err)
c.AbortWithStatusJSON(msg, gin.H{"code": err.Error()})
}
}
log.Printf("auth check end")
// 执行函数
c.Next()
}
}