router-for-me/CLIProxyAPI · error

invalid auth file: %w

Error message

invalid auth file: %w

What it means

Thrown by buildAuthFromFileData when the auth file's bytes are not valid JSON (json.Unmarshal into map[string]any fails). The handler first parses the file generically to extract provider/type, email, and refresh metadata before provider-specific synthesis, so any syntax error aborts record building with the JSON decoder's cause wrapped in %w.

Source

Thrown at internal/api/handlers/management/auth_files_crud.go:477

		return err
	}
	return h.upsertAuthRecord(ctx, auth)
}

func (h *Handler) buildAuthFromFileData(path string, data []byte) (*coreauth.Auth, error) {
	if path == "" {
		return nil, fmt.Errorf("auth path is empty")
	}
	if data == nil {
		var err error
		data, err = os.ReadFile(path)
		if err != nil {
			return nil, fmt.Errorf("failed to read auth file: %w", err)
		}
	}
	metadata := make(map[string]any)
	if err := json.Unmarshal(data, &metadata); err != nil {
		return nil, fmt.Errorf("invalid auth file: %w", err)
	}
	provider, _ := metadata["type"].(string)
	if provider == "" {
		provider = "unknown"
	}
	label := provider
	if email, ok := metadata["email"].(string); ok && email != "" {
		label = email
	}
	lastRefresh, hasLastRefresh := extractLastRefreshTimestamp(metadata)

	authID := h.authIDForPath(path)
	if authID == "" {
		authID = path
	}
	auth := (*coreauth.Auth)(nil)
	if h != nil && h.cfg != nil {
		sctx := &synthesizer.SynthesisContext{

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Validate the file with an external parser: jq . auths/<file> — the exact offset/line of the syntax error appears in the wrapped message
  2. Fix the reported syntax error (remove comments, trailing commas, fences) or regenerate the file via the provider's OAuth login flow
  3. Ensure the file is complete: re-export or re-run the login command if it was truncated
  4. Retry the management import/list call after the file parses cleanly

Example fix

// before (auths/qwen.json)
{ "type": "qwen", "token": "...", } // trailing comma
// after
{ "type": "qwen", "token": "..." }
Defensive patterns

Strategy: validation

Validate before calling

if data, err := os.ReadFile(path); err == nil {
    if !json.Valid(data) {
        return fmt.Errorf("%s is not valid JSON; fix before import", path)
    }
}

Type guard

func isValidAuthJSON(data []byte) bool { return json.Valid(bytes.TrimSpace(data)) }

Try / catch

if _, err := buildAuthFromFileData(path, data); err != nil {
    if strings.HasPrefix(err.Error(), "invalid auth file") {
        // quarantine the file; report the JSON offset from the wrapped cause
    }
}

Prevention

When it happens

Trigger: Importing or scanning an auth file that is truncated, contains a BOM or trailing garbage, was hand-edited with a trailing comma or comment, or is actually YAML/TOML saved with a .json extension; endpoints that call buildAuthFromFileData with data==nil on such a file.

Common situations: Manual editing of auths/*.json to tweak a token or label; a partially written file after a crash during save; copy-pasting OAuth JSON from docs including markdown fences; mixing config.yaml syntax into an auth file.

Related errors


AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15). Data as JSON: /api/errors/a517ac5d22b57db8. Report an issue: GitHub.