MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / TokenAuth

Function TokenAuth

middleware/auth.go:176–274  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

174}
175
176func TokenAuth() func(c *gin.Context) {
177 return func(c *gin.Context) {
178 log.Println("********************", c)
179
180 // 先检测是否为ws
181 if c.Request.Header.Get("Sec-WebSocket-Protocol") != "" {
182 // Sec-WebSocket-Protocol: realtime, openai-insecure-api-key.sk-xxx, openai-beta.realtime-v1
183 // read sk from Sec-WebSocket-Protocol
184 key := c.Request.Header.Get("Sec-WebSocket-Protocol")
185 parts := strings.Split(key, ",")
186 for _, part := range parts {
187 part = strings.TrimSpace(part)
188 if strings.HasPrefix(part, "openai-insecure-api-key") {
189 key = strings.TrimPrefix(part, "openai-insecure-api-key.")
190 break
191 }
192 }
193 c.Request.Header.Set("Authorization", "Bearer "+key)
194 }
195 // 检查path包含/v1/messages
196 if strings.Contains(c.Request.URL.Path, "/v1/messages") {
197 // 从x-api-key中获取key
198 key := c.Request.Header.Get("x-api-key")
199 if key != "" {
200 c.Request.Header.Set("Authorization", "Bearer "+key)
201 }
202 }
203 // gemini api 从query中获取key
204 if strings.HasPrefix(c.Request.URL.Path, "/v1beta/models/") || strings.HasPrefix(c.Request.URL.Path, "/v1/models/") {
205 skKey := c.Query("key")
206 if skKey != "" {
207 c.Request.Header.Set("Authorization", "Bearer "+skKey)
208 }
209 // 从x-goog-api-key header中获取key
210 xGoogKey := c.Request.Header.Get("x-goog-api-key")
211 if xGoogKey != "" {
212 c.Request.Header.Set("Authorization", "Bearer "+xGoogKey)
213 }
214 }
215 key := c.Request.Header.Get("Authorization")
216 parts := make([]string, 0)
217 key = strings.TrimPrefix(key, "Bearer ")
218 if key == "" || key == "midjourney-proxy" {
219 key = c.Request.Header.Get("mj-api-secret")
220 key = strings.TrimPrefix(key, "Bearer ")
221 key = strings.TrimPrefix(key, "sk-")
222 parts = strings.Split(key, "-")
223 key = parts[0]
224 } else {
225 key = strings.TrimPrefix(key, "sk-")
226 parts = strings.Split(key, "-")
227 key = parts[0]
228 }
229 token, err := model.ValidateUserToken(key)
230 if token != nil {
231 id := c.GetInt("id")
232 if id == 0 {
233 c.Set("id", token.UserId)

Callers 4

SetDashboardRouterFunction · 0.92
SetVideoRouterFunction · 0.92
SetRelayRouterFunction · 0.92
registerMjRouterGroupFunction · 0.92

Calls 12

ValidateUserTokenFunction · 0.92
GetUserCacheFunction · 0.92
IncreaseTokenUsageCountFunction · 0.92
SysErrorFunction · 0.92
RecordTokenUsageFunction · 0.92
abortWithOpenAiMessageFunction · 0.85
SetupContextForTokenFunction · 0.85
GetMethod · 0.80
ContainsMethod · 0.80
ErrorMethod · 0.80
WriteContextMethod · 0.80
NextMethod · 0.80

Tested by

no test coverage detected