2023-04-22 12:39:27 +00:00
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"net/http"
|
2023-04-22 13:14:09 +00:00
|
|
|
|
"one-api/common"
|
|
|
|
|
"one-api/model"
|
2023-04-23 10:24:11 +00:00
|
|
|
|
"strings"
|
2023-04-22 12:39:27 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func authHelper(c *gin.Context, minRole int) {
|
|
|
|
|
session := sessions.Default(c)
|
|
|
|
|
username := session.Get("username")
|
|
|
|
|
role := session.Get("role")
|
|
|
|
|
id := session.Get("id")
|
|
|
|
|
status := session.Get("status")
|
|
|
|
|
if username == nil {
|
2023-04-26 12:54:39 +00:00
|
|
|
|
// Check access token
|
|
|
|
|
accessToken := c.Request.Header.Get("Authorization")
|
|
|
|
|
if accessToken == "" {
|
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": "无权进行此操作,未登录且未提供 access token",
|
|
|
|
|
})
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
user := model.ValidateAccessToken(accessToken)
|
|
|
|
|
if user != nil && user.Username != "" {
|
|
|
|
|
// Token is valid
|
|
|
|
|
username = user.Username
|
|
|
|
|
role = user.Role
|
|
|
|
|
id = user.Id
|
|
|
|
|
status = user.Status
|
|
|
|
|
} else {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": "无权进行此操作,access token 无效",
|
|
|
|
|
})
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-22 12:39:27 +00:00
|
|
|
|
}
|
|
|
|
|
if status.(int) == common.UserStatusDisabled {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": "用户已被封禁",
|
|
|
|
|
})
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if role.(int) < minRole {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"success": false,
|
|
|
|
|
"message": "无权进行此操作,权限不足",
|
|
|
|
|
})
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.Set("username", username)
|
|
|
|
|
c.Set("role", role)
|
|
|
|
|
c.Set("id", id)
|
|
|
|
|
c.Next()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func UserAuth() func(c *gin.Context) {
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
authHelper(c, common.RoleCommonUser)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AdminAuth() func(c *gin.Context) {
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
authHelper(c, common.RoleAdminUser)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RootAuth() func(c *gin.Context) {
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
authHelper(c, common.RoleRootUser)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 10:24:11 +00:00
|
|
|
|
func TokenAuth() func(c *gin.Context) {
|
2023-04-22 12:39:27 +00:00
|
|
|
|
return func(c *gin.Context) {
|
2023-04-23 10:24:11 +00:00
|
|
|
|
key := c.Request.Header.Get("Authorization")
|
|
|
|
|
parts := strings.Split(key, "-")
|
|
|
|
|
key = parts[0]
|
|
|
|
|
token, err := model.ValidateUserToken(key)
|
|
|
|
|
if err != nil {
|
2023-04-22 12:39:27 +00:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2023-04-23 10:24:11 +00:00
|
|
|
|
"error": gin.H{
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
"type": "one_api_error",
|
|
|
|
|
},
|
2023-04-22 12:39:27 +00:00
|
|
|
|
})
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-27 07:05:33 +00:00
|
|
|
|
if !model.IsUserEnabled(token.UserId) {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"error": gin.H{
|
|
|
|
|
"message": "用户已被封禁",
|
|
|
|
|
"type": "one_api_error",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-23 10:24:11 +00:00
|
|
|
|
c.Set("id", token.UserId)
|
2023-04-26 02:45:34 +00:00
|
|
|
|
c.Set("token_id", token.Id)
|
2023-04-28 08:58:55 +00:00
|
|
|
|
requestURL := c.Request.URL.String()
|
2023-05-14 12:36:28 +00:00
|
|
|
|
consumeQuota := !token.UnlimitedQuota
|
|
|
|
|
if strings.HasPrefix(requestURL, "/models") {
|
|
|
|
|
consumeQuota = false
|
2023-04-28 08:58:55 +00:00
|
|
|
|
}
|
|
|
|
|
c.Set("consume_quota", consumeQuota)
|
2023-04-23 10:24:11 +00:00
|
|
|
|
if len(parts) > 1 {
|
2023-04-26 06:49:27 +00:00
|
|
|
|
if model.IsAdmin(token.UserId) {
|
|
|
|
|
c.Set("channelId", parts[1])
|
|
|
|
|
} else {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"error": gin.H{
|
|
|
|
|
"message": "普通用户不支持指定渠道",
|
|
|
|
|
"type": "one_api_error",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-22 12:39:27 +00:00
|
|
|
|
}
|
|
|
|
|
c.Next()
|
|
|
|
|
}
|
|
|
|
|
}
|