feat(credentials): Per-profile keychain source pin + auto-refresh#242
Open
Taeknology wants to merge 1 commit into
Open
feat(credentials): Per-profile keychain source pin + auto-refresh#242Taeknology wants to merge 1 commit into
Taeknology wants to merge 1 commit into
Conversation
…elfayome#239) For multi-account CLAUDE_CONFIG_DIR setups, non-active profiles silently stop tracking usage once their cached refresh_token falls behind Claude Code's rotation (HTTP 400 invalid_grant). This adds: - Profile.customKeychainServiceName: pin a profile to a specific `Claude Code-credentials-<HASH>` keychain entry as its source of truth. - ClaudeCodeSyncService.ensureFreshCredentials: source-aware; reads fresh tokens directly from the pinned entry, falls back to the cached cliJSON when the pinned payload is unparseable, auto-refreshes via the OAuth refresh_token grant against platform.claude.com when expired, and writes rotated tokens back to BOTH the keychain entry and the profile cache. - listClaudeCodeKeychainServices via SecItemCopyMatching (no shell subprocess, no dump-keychain hang on macOS 26.x) + describeKeychainEntry that resolves account labels by walking ~/.claude* config dirs and matching their oauthAccount via the path hash (sha256(absolute path).hex()[:8]). - CLIAccountView 'Advanced — Credentials source' picker. - MenuBarManager: filter widened from hasUsageCredentials to hasAnyCredentials so expired-but-refreshable profiles still get polled; fetchUsageForProfile Priority 2 now routes through ensureFreshCredentials. - Unit tests covering Codable back-compat, the keychain-hash algorithm, mergeRefreshedCredentials correctness, and KeychainEntryDescription.displayLabel. No breaking changes; new optional Profile field decodes via decodeIfPresent.
This was referenced May 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #239.
Summary
Multi-account
CLAUDE_CONFIG_DIRsetups currently lose usage tracking for non-active profiles after Claude Code rotates their OAuth tokens. This PR:Claude Code-credentials-<HASH>keychain entry (the same entry Claude Code itself rotates).refresh_tokengrant againsthttps://platform.claude.com/v1/oauth/tokenand writes the new tokens back to both the keychain entry and the profile cache. Falls back to the profile's cachedcliCredentialsJSONif the pinned keychain payload is unparseable.SecItemCopyMatching(no shelldump-keychainsubprocess, which has been observed to hang on macOS 26.x), with human-readable labels assembled from.claude.jsonoauthAccountmetadata across~/.claude*config dirs.No breaking changes; the new optional
Profile.customKeychainServiceNamefield decodes viadecodeIfPresent, so existingprofiles_v3plists load unchanged.Files changed
Claude Usage/Shared/Models/Profile.swiftcustomKeychainServiceName: String?+ init param;hasAnyCredentialsupdatedClaude Usage/Shared/Services/ClaudeCodeSyncService.swiftSecItemCopyMatching) +discoverAccountLabels(resolves emails via.claude.json)Claude Usage/MenuBar/MenuBarManager.swifthasUsageCredentialstohasAnyCredentials;fetchUsageForProfilePriority 2 callsensureFreshCredentialsClaude Usage/Views/Settings/Credentials/CLIAccountView.swiftClaude UsageTests/ClaudeUsageTests.swiftmergeRefreshedCredentialscorrectness,KeychainEntryDescription.displayLabel(5 branches)Implementation notes
Claude Code-credentials-<HASH>isClaude Code-credentials-+sha256(absolute CLAUDE_CONFIG_DIR path).hex()[:8]. Verified against live entries across multiple config dirs.discoverAccountLabels()walks~/.claudeand~/.claude-*dirs, reads each.claude.jsonoauthAccount, computes the path hash, and maps it to the corresponding keychain entry so the picker shows<email> — <org> · <hash>instead of bare hashes.writeKeychainCredentialsuses a single atomicadd -U(not delete-then-add) andmergeRefreshedCredentialsemits single-line JSON — both observed-and-fixed during testing to avoidsecurity -wpayload corruption.client_iddefaults to the public Claude Code value and is overridable viaCLAUDE_CODE_OAUTH_CLIENT_IDenv var, matching the convention used in Claude Code's own HUD.Test plan
Tested locally with two
CLAUDE_CONFIG_DIRaccounts:refresh_tokenfor the non-active profile becomes stale (Claude Code rotates within hours of active use).HTTP 400 invalid_granton the affected profile andlastWeeklyRecordTime_<id>stops advancing.Claude Code-credentials-<hash>entry for that profile.lastWeeklyRecordTime_<id>advances to today, no furtherinvalid_grant.Unit tests run via Xcode's
Testaction; all 11 new cases pass alongside the existing suite (** TEST SUCCEEDED **).Caveats
CryptoKit(already the project's minimum target).discoverAccountLabelswalks the home directory, so picker labels assume.claude*config dirs live there — fine for the common pattern (~/.claude-work,~/.claude-personal, etc.). Exotic locations will still show as raw hashes; the pin still works.Related
The 5-hour / weekly reset notifications follow-up is split into a separate PR for #241 — it depends on this PR for non-active-profile reliability but is otherwise independent.