refactor: standardize constant naming and improve file-based auth handling

- Renamed constants from uppercase to CamelCase for consistency.
- Replaced redundant file-based auth handling logic with the new `util.CountAuthFiles` helper.
- Fixed various error-handling inconsistencies and enhanced robustness in file operations.
- Streamlined auth client reload logic in server and watcher components.
- Applied minor code readability improvements across multiple packages.
This commit is contained in:
Luis Pater
2025-09-22 02:56:45 +08:00
parent 4999fce7f4
commit d9ad65622a
51 changed files with 341 additions and 270 deletions

View File

@@ -73,7 +73,7 @@ func (s *FileStore) Save(ctx context.Context, auth *Auth) error {
if err != nil {
return fmt.Errorf("auth filestore: marshal metadata failed: %w", err)
}
if existing, err := os.ReadFile(path); err == nil {
if existing, errReadFile := os.ReadFile(path); errReadFile == nil {
if jsonEqual(existing, raw) {
return nil
}
@@ -108,8 +108,8 @@ func deepEqualJSON(a, b any) bool {
return false
}
for key, subA := range valA {
subB, ok := valB[key]
if !ok || !deepEqualJSON(subA, subB) {
subB, ok1 := valB[key]
if !ok1 || !deepEqualJSON(subA, subB) {
return false
}
}

View File

@@ -795,7 +795,7 @@ func authLastRefreshTimestamp(a *Auth) (time.Time, bool) {
func lookupMetadataTime(meta map[string]any, keys ...string) (time.Time, bool) {
for _, key := range keys {
if val, ok := meta[key]; ok {
if ts, ok := parseTimeValue(val); ok {
if ts, ok1 := parseTimeValue(val); ok1 {
return ts, true
}
}

View File

@@ -84,6 +84,24 @@ func (a *Auth) AccountInfo() (bool, string) {
if a == nil {
return false, ""
}
if strings.ToLower(a.Provider) == "gemini-web" {
if a.Metadata != nil {
if v, ok := a.Metadata["secure_1psid"].(string); ok && v != "" {
return true, v
}
if v, ok := a.Metadata["__Secure-1PSID"].(string); ok && v != "" {
return true, v
}
}
if a.Attributes != nil {
if v := a.Attributes["secure_1psid"]; v != "" {
return true, v
}
if v := a.Attributes["api_key"]; v != "" {
return true, v
}
}
}
if a.Metadata != nil {
if v, ok := a.Metadata["email"].(string); ok {
return false, v
@@ -125,7 +143,7 @@ func expirationFromMap(meta map[string]any) (time.Time, bool) {
}
for _, key := range expireKeys {
if v, ok := meta[key]; ok {
if ts, ok := parseTimeValue(v); ok {
if ts, ok1 := parseTimeValue(v); ok1 {
return ts, true
}
}
@@ -134,7 +152,7 @@ func expirationFromMap(meta map[string]any) (time.Time, bool) {
if nested, ok := meta[nestedKey]; ok {
switch val := nested.(type) {
case map[string]any:
if ts, ok := expirationFromMap(val); ok {
if ts, ok1 := expirationFromMap(val); ok1 {
return ts, true
}
case map[string]string:
@@ -142,7 +160,7 @@ func expirationFromMap(meta map[string]any) (time.Time, bool) {
for k, v := range val {
temp[k] = v
}
if ts, ok := expirationFromMap(temp); ok {
if ts, ok1 := expirationFromMap(temp); ok1 {
return ts, true
}
}