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
1515 "github.com/gin-gonic/gin"
1616 "github.com/router-for-me/CLIProxyAPI/v7/internal/auth/codex"
1717 "github.com/router-for-me/CLIProxyAPI/v7/internal/config"
18+ "github.com/router-for-me/CLIProxyAPI/v7/internal/credentialweight"
1819 "github.com/router-for-me/CLIProxyAPI/v7/internal/registry"
1920 coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
2021 log "github.com/sirupsen/logrus"
@@ -260,6 +261,20 @@ func (h *Handler) listAuthFilesFromDisk(c *gin.Context) { · 239 unmodified lines
260261 }
261262 }
262263 }
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+ }
263278 if nv := gjson.GetBytes(data, "note"); nv.Exists() && nv.Type == gjson.String {
264279 if trimmed := strings.TrimSpace(nv.String()); trimmed != "" {
265280 fileData["note"] = trimmed
@@ -403,12 +418,34 @@ func (h *Handler) buildAuthFileEntryLocked(auth *coreauth.Auth) gin.H { · 137 unmodified lines
403418 }
404419 }
405420 }
421+ if weight, ok := authWeightValue(auth); ok {
422+ entry[coreauth.AttributeWeight] = weight
423+ }
406424 if websockets, ok := authWebsocketsValue(auth); ok {
407425 entry["websockets"] = websockets
408426 }
409427 return entry
410428 }
411429
430+func 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+
412449 func authWebsocketsValue(auth *coreauth.Auth) (bool, bool) {
413450 if auth == nil {
414451 return false, false