-
Notifications
You must be signed in to change notification settings - Fork 683
fix: support Claude Code 2.x authentication detection #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: support Claude Code 2.x authentication detection #276
Conversation
Claude Code 2.x stores OAuth account info in ~/.claude/.claude.json under the oauthAccount key, rather than the legacy location at ~/.claude/.credentials.json. This change checks the new location first, then falls back to the legacy location for backwards compatibility.
WalkthroughcheckClaudeCredentials now checks for Claude Code 2.x credentials at Changes
Sequence DiagramsequenceDiagram
participant Client
participant Auth as Auth Handler
participant FS as File System
Client->>Auth: Request credentials check
Auth->>FS: Read ~/.claude/.claude.json
alt Code 2.x file exists
FS-->>Auth: File contents
Auth->>Auth: Inspect oauthAccount.emailAddress
alt emailAddress present
Auth-->>Client: authenticated (method: 'claude_code_2x')
else
Auth->>FS: Read ~/.claude/.credentials.json
end
else no Code 2.x file
Auth->>FS: Read ~/.claude/.credentials.json
end
alt legacy file exists
FS-->>Auth: File contents
Auth->>Auth: Check oauth token and expiry
alt token valid
Auth-->>Client: authenticated (method: 'legacy_oauth')
else
Auth-->>Client: unauthenticated / error
end
else no legacy file
Auth-->>Client: unauthenticated / error
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
📜 Recent review detailsConfiguration used: Repository UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
server/routes/cli-auth.js (1)
9-18: Critical: Method field is incorrectly overwritten.Line 17 hardcodes
method: 'credentials_file', which overwrites themethodfield returned bycheckClaudeCredentials(). This defeats the purpose of distinguishing between'claude_code_2x'and'legacy_oauth'authentication methods.🔎 Proposed fix
if (credentialsResult.authenticated) { return res.json({ authenticated: true, email: credentialsResult.email || 'Authenticated', - method: 'credentials_file' + method: credentialsResult.method || 'credentials_file' }); }
🧹 Nitpick comments (1)
server/routes/cli-auth.js (1)
81-94: Consider adding debug logging for the fallback path.The broad try-catch correctly handles both file-not-found and JSON parsing errors by falling back to the legacy credential check. However, for troubleshooting, consider logging the error when the Claude Code 2.x check fails.
🔎 Suggested enhancement
} catch (e) { // .claude.json not found or invalid, try legacy location + console.debug('Claude Code 2.x credentials not found or invalid, falling back to legacy:', e.message); }
📜 Review details
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
server/routes/cli-auth.js
🧰 Additional context used
🧬 Code graph analysis (1)
server/routes/cli-auth.js (1)
server/index.js (3)
path(1327-1327)os(1329-1329)fs(1328-1328)
🔇 Additional comments (2)
server/routes/cli-auth.js (2)
96-112: LGTM: Legacy fallback correctly enhanced with method field.The legacy credential check properly adds the
method: 'legacy_oauth'field while preserving existing token expiry validation and email fallback logic. This correctly complements the new Claude Code 2.x detection.
79-94: Consider adding expiry validation to the Claude Code 2.x authentication path.The legacy credential check (lines 103–111) validates token expiry using
expiresAt, but the new Claude Code 2.x path (lines 79–94) does not perform any expiry validation. Since OAuth credentials typically include expiry information (as shown in the legacy path and standard OAuth structures), verify whetheroauthAccountcontains anexpiresAtfield and add an expiry check if present, consistent with the legacy authentication method.Additionally, consider validating that
emailAddressis not an empty string before returning it.
- Don't overwrite the method field returned by checkClaudeCredentials - Allows distinguishing between 'claude_code_2x' and 'legacy_oauth'
|
@goodhanded I'm not able to reproduce it. In all my tests with the latest claude code release the .credentials is always created regarldess if it's oauth or API keys. The .claude.json file contains also the auth method however it doesn't look like .credentials is a legacy method. Do you have some sources that its a legacy method? Moreover what is your system configuration and how have you installed claude code? |
Summary
~/.claude/.claude.jsonunder theoauthAccountkey~/.claude/.credentials.jsonlocation for backwards compatibilityProblem
Claude Code UI shows Claude as "disconnected" in Settings > Agents for users running Claude Code 2.x, even though Claude is properly authenticated and working.
Solution
Updated
checkClaudeCredentials()inserver/routes/cli-auth.jsto:~/.claude/.claude.jsonforoauthAccount.emailAddress(Claude Code 2.x)~/.claude/.credentials.jsonwithclaudeAiOauth(legacy)Test plan
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.