forked from MartialBE/one-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
229 lines (205 loc) · 4.98 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package middleware
import (
"net/http"
"one-api/common/config"
"one-api/common/utils"
"one-api/model"
"strings"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
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 {
// 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
}
}
if status.(int) == config.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 TrySetUserBySession() func(c *gin.Context) {
return func(c *gin.Context) {
session := sessions.Default(c)
id := session.Get("id")
if id == nil {
c.Next()
return
}
idInt, ok := id.(int)
if !ok {
c.Next()
return
}
c.Set("id", idInt)
userGroup, err := model.CacheGetUserGroup(idInt)
if err == nil {
c.Set("group", userGroup)
}
c.Next()
}
}
func UserAuth() func(c *gin.Context) {
return func(c *gin.Context) {
authHelper(c, config.RoleCommonUser)
}
}
func AdminAuth() func(c *gin.Context) {
return func(c *gin.Context) {
authHelper(c, config.RoleAdminUser)
}
}
func RootAuth() func(c *gin.Context) {
return func(c *gin.Context) {
authHelper(c, config.RoleRootUser)
}
}
func tokenAuth(c *gin.Context, key string) {
key = strings.TrimPrefix(key, "Bearer ")
key = strings.TrimPrefix(key, "sk-")
if len(key) < 48 {
abortWithMessage(c, http.StatusUnauthorized, "无效的令牌")
return
}
parts := strings.Split(key, "#")
key = parts[0]
token, err := model.ValidateUserToken(key)
if err != nil {
abortWithMessage(c, http.StatusUnauthorized, err.Error())
return
}
c.Set("id", token.UserId)
c.Set("token_id", token.Id)
c.Set("token_name", token.Name)
c.Set("token_group", token.Group)
if len(parts) > 1 {
if model.IsAdmin(token.UserId) {
if strings.HasPrefix(parts[1], "!") {
channelId := utils.String2Int(parts[1][1:])
c.Set("skip_channel_ids", []int{channelId})
} else {
channelId := utils.String2Int(parts[1])
if channelId == 0 {
abortWithMessage(c, http.StatusForbidden, "无效的渠道 Id")
return
}
c.Set("specific_channel_id", channelId)
if len(parts) == 3 && parts[2] == "ignore" {
c.Set("specific_channel_id_ignore", true)
}
}
} else {
abortWithMessage(c, http.StatusForbidden, "普通用户不支持指定渠道")
return
}
}
c.Next()
}
func OpenaiAuth() func(c *gin.Context) {
return func(c *gin.Context) {
isWebSocket := c.GetHeader("Upgrade") == "websocket"
key := c.Request.Header.Get("Authorization")
if isWebSocket && key == "" {
protocols := c.Request.Header["Sec-Websocket-Protocol"]
if len(protocols) > 0 {
protocolList := strings.Split(protocols[0], ",")
for _, protocol := range protocolList {
protocol = strings.TrimSpace(protocol)
if strings.HasPrefix(protocol, "openai-insecure-api-key.") {
key = strings.TrimPrefix(protocol, "openai-insecure-api-key.")
break
}
}
}
}
tokenAuth(c, key)
}
}
func ClaudeAuth() func(c *gin.Context) {
return func(c *gin.Context) {
key := c.Request.Header.Get("x-api-key")
tokenAuth(c, key)
}
}
func GeminiAuth() func(c *gin.Context) {
return func(c *gin.Context) {
key := c.Request.Header.Get("x-goog-api-key")
if key == "" {
// 查询GET参数
key = c.Query("key")
}
tokenAuth(c, key)
}
}
func MjAuth() func(c *gin.Context) {
return func(c *gin.Context) {
// 判断path :mode
model := c.Param("mode")
if model != "" && model != "mj-fast" && model != "mj-turbo" && model != "mj-relax" {
midjourneyAbortWithMessage(c, 4, "无效的加速模式")
return
}
if model == "" {
model = "mj-fast"
}
model = strings.TrimPrefix(model, "mj-")
c.Set("mj_model", model)
key := c.Request.Header.Get("mj-api-secret")
tokenAuth(c, key)
}
}
func SpecifiedChannel() func(c *gin.Context) {
return func(c *gin.Context) {
channelId := c.GetInt("specific_channel_id")
c.Set("specific_channel_id_ignore", false)
if channelId <= 0 {
abortWithMessage(c, http.StatusForbidden, "必须指定渠道")
return
}
c.Next()
}
}