mirror of
https://github.com/coaidev/coai.git
synced 2025-05-19 21:10:18 +09:00
66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"chat/auth"
|
|
"chat/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"strings"
|
|
)
|
|
|
|
func ProcessToken(c *gin.Context, token string) bool {
|
|
if user := auth.ParseToken(c, token); user != nil {
|
|
c.Set("auth", true)
|
|
c.Set("user", user.Username)
|
|
c.Set("agent", "token")
|
|
c.Next()
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func ProcessKey(c *gin.Context, key string) bool {
|
|
addr := c.ClientIP()
|
|
cache := utils.GetCacheFromContext(c)
|
|
|
|
if utils.IsInBlackList(cache, addr) {
|
|
c.AbortWithStatusJSON(200, gin.H{
|
|
"code": 403,
|
|
"message": "ip in black list",
|
|
})
|
|
return false
|
|
}
|
|
|
|
if user := auth.ParseApiKey(c, key); user != nil {
|
|
c.Set("auth", true)
|
|
c.Set("user", user.Username)
|
|
c.Set("agent", "api")
|
|
c.Next()
|
|
return true
|
|
}
|
|
|
|
utils.IncrIP(cache, addr)
|
|
return false
|
|
}
|
|
|
|
func AuthMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
k := strings.TrimSpace(c.GetHeader("Authorization"))
|
|
if k != "" {
|
|
if strings.HasPrefix(k, "sk-") { // api agent
|
|
if ProcessKey(c, k) {
|
|
return
|
|
}
|
|
} else { // token agent
|
|
if ProcessToken(c, k) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
c.Set("auth", false)
|
|
c.Set("user", "")
|
|
c.Set("agent", "")
|
|
c.Next()
|
|
}
|
|
}
|