History

feat(auth): add support for credential weight parsing in auth file handling

Supra4E8C committed 3 months agov7.2.108

Commit e8e39521 parent b4d94d51 file · +37 −0Browse files

Unified · Split

internal/api/handlers/management/auth_files.go+37−0
@@ -15,6 +15,7 @@ import ( · 14 unmodified lines
15 "github.com/gin-gonic/gin"15 "github.com/gin-gonic/gin"
16 "github.com/router-for-me/CLIProxyAPI/v7/internal/auth/codex"16 "github.com/router-for-me/CLIProxyAPI/v7/internal/auth/codex"
17 "github.com/router-for-me/CLIProxyAPI/v7/internal/config"17 "github.com/router-for-me/CLIProxyAPI/v7/internal/config"
18 "github.com/router-for-me/CLIProxyAPI/v7/internal/credentialweight"
18 "github.com/router-for-me/CLIProxyAPI/v7/internal/registry"19 "github.com/router-for-me/CLIProxyAPI/v7/internal/registry"
19 coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"20 coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
20 log "github.com/sirupsen/logrus"21 log "github.com/sirupsen/logrus"
@@ -260,6 +261,20 @@ func (h *Handler) listAuthFilesFromDisk(c *gin.Context) { · 239 unmodified lines
260 }261 }
261 }262 }
262 }263 }
264 if wv := gjson.GetBytes(data, coreauth.AttributeWeight); wv.Exists() {
265 var rawWeight string
266 switch wv.Type {
267 case gjson.Number:
268 rawWeight = wv.Raw
269 case gjson.String:
270 rawWeight = wv.String()
271 }
272 if rawWeight != "" {
273 if weight, errWeight := credentialweight.ParseString(rawWeight); errWeight == nil {
274 fileData[coreauth.AttributeWeight] = weight
275 }
276 }
277 }
263 if nv := gjson.GetBytes(data, "note"); nv.Exists() && nv.Type == gjson.String {278 if nv := gjson.GetBytes(data, "note"); nv.Exists() && nv.Type == gjson.String {
264 if trimmed := strings.TrimSpace(nv.String()); trimmed != "" {279 if trimmed := strings.TrimSpace(nv.String()); trimmed != "" {
265 fileData["note"] = trimmed280 fileData["note"] = trimmed
@@ -403,12 +418,34 @@ func (h *Handler) buildAuthFileEntryLocked(auth *coreauth.Auth) gin.H { · 137 unmodified lines
403 }418 }
404 }419 }
405 }420 }
421 if weight, ok := authWeightValue(auth); ok {
422 entry[coreauth.AttributeWeight] = weight
423 }
406 if websockets, ok := authWebsocketsValue(auth); ok {424 if websockets, ok := authWebsocketsValue(auth); ok {
407 entry["websockets"] = websockets425 entry["websockets"] = websockets
408 }426 }
409 return entry427 return entry
410}428}
411 429
430func authWeightValue(auth *coreauth.Auth) (int64, bool) {
431 if auth == nil {
432 return 0, false
433 }
434 if rawWeight := strings.TrimSpace(authAttribute(auth, coreauth.AttributeWeight)); rawWeight != "" {
435 weight, errWeight := credentialweight.ParseString(rawWeight)
436 return weight, errWeight == nil
437 }
438 if auth.Metadata == nil {
439 return 0, false
440 }
441 rawWeight, ok := auth.Metadata[coreauth.AttributeWeight]
442 if !ok || rawWeight == nil {
443 return 0, false
444 }
445 weight, errWeight := credentialweight.ParseValue(rawWeight)
446 return weight, errWeight == nil
447}
448
412func authWebsocketsValue(auth *coreauth.Auth) (bool, bool) {449func authWebsocketsValue(auth *coreauth.Auth) (bool, bool) {
413 if auth == nil {450 if auth == nil {
414 return false, false451 return false, false