Add Linux support for Claude Code OAuth token retrieval#258
Conversation
Read OAuth token from ~/.claude/.credentials.json on Linux, in addition to the existing macOS Keychain integration. Refactored token fetching into platform-dispatched helpers with a shared JSON parser for reuse. https://claude.ai/code/session_01VQbqeC4PtoWTXdvETzsepV
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Claude Code tool by introducing Linux support for OAuth token retrieval, which in turn enables the display of rate limit quota information on Linux systems. Previously, this feature was exclusive to macOS due to its reliance on Keychain access. The changes involve refactoring credential handling, implementing platform-specific token fetching mechanisms, and centralizing JSON parsing logic, thereby improving cross-platform compatibility and user experience for Claude Code users. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively extends OAuth token retrieval for Claude Code to support Linux systems. The refactoring of credential handling into platform-specific functions for macOS and Linux is well-executed, and extracting common JSON parsing logic into a shared function improves code reuse. The addition of comprehensive test cases for the new Linux functionality is commendable. I have one minor suggestion to use constants for file path components to enhance maintainability, aligning with best practices for path handling. Overall, this is a high-quality change.
| func fetchOAuthTokenFromCredentialsFile() (string, error) { | ||
| homeDir, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to get home directory: %w", err) | ||
| } | ||
|
|
||
| data, err := os.ReadFile(filepath.Join(homeDir, ".claude", ".credentials.json")) | ||
| if err != nil { | ||
| return "", fmt.Errorf("credentials file read failed: %w", err) | ||
| } | ||
|
|
||
| return parseOAuthTokenFromJSON(data) | ||
| } |
There was a problem hiding this comment.
To improve maintainability and avoid hardcoding file path components, consider defining ".claude" and ".credentials.json" as constants. This makes the path easier to read and manage, especially if these values are used elsewhere. This aligns with best practices for platform-independent path handling.
func fetchOAuthTokenFromCredentialsFile() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get home directory: %w", err)
}
const (
claudeConfigDir = ".claude"
claudeCredentialsFile = ".credentials.json"
)
credentialsPath := filepath.Join(homeDir, claudeConfigDir, claudeCredentialsFile)
data, err := os.ReadFile(credentialsPath)
if err != nil {
return "", fmt.Errorf("credentials file read failed: %w", err)
}
return parseOAuthTokenFromJSON(data)
}References
- For platform-independent paths, use
filepath.Jointo combine segments andos.UserHomeDir()to get the home directory, rather than hardcoding path separators or environment variables like$HOME. Defining path components as constants further enhances maintainability and readability.
| // Quota utilization (macOS only - requires Keychain for OAuth token) | ||
| if runtime.GOOS == "darwin" { | ||
| // Quota utilization (macOS: Keychain, Linux: ~/.claude/.credentials.json) | ||
| if runtime.GOOS == "darwin" || runtime.GOOS == "linux" { |
There was a problem hiding this comment.
🔴 CLAUDE.md rule states quota is macOS-only but code now enables it on Linux without updating the rule
CLAUDE.md line 127 states: "CC statusline quota display is macOS-only (requires Keychain access to Claude Code OAuth token)". The new code at commands/cc_statusline.go:218 adds || runtime.GOOS == "linux" to enable quota display on Linux, directly contradicting this documented constraint. The same violation applies to the fallback output at commands/cc_statusline.go:282 and the daemon fetch guard at daemon/cc_info_timer.go:398. CLAUDE.md must be updated to reflect that quota is now supported on both macOS (Keychain) and Linux (~/.claude/.credentials.json).
Prompt for agents
Update CLAUDE.md line 127 in the Important Notes section to reflect the new Linux support. Change:
- CC statusline quota display is macOS-only (requires Keychain access to Claude Code OAuth token)
To something like:
- CC statusline quota display is supported on macOS (Keychain) and Linux (~/.claude/.credentials.json) for Claude Code OAuth token access
Also update docs/CC_STATUSLINE.md in multiple places:
- Line 15: Remove "macOS only" or change to "macOS and Linux"
- Line 81: Update parenthetical about macOS only
- Line 87: Update the Platform Note section to reflect Linux now shows quota
- Lines 200-204: Update the Requirements for Quota Utilization section
- Line 245: Update the troubleshooting entry
Also update README.md line 100: Remove "macOS only" from the quota row description.
Was this helpful? React with 👍 or 👎 to provide feedback.
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
Summary
Extends OAuth token retrieval for Claude Code to support Linux systems in addition to macOS. Previously, rate limit quota information was only available on macOS via Keychain. This change adds support for Linux by reading credentials from
~/.claude/.credentials.json.Key Changes
keychainCredentialstoclaudeCodeCredentialsandkeychainOAuthEntrytoclaudeCodeOAuthEntryto reflect support for multiple credential sourcesfetchClaudeCodeOAuthToken()to dispatch to platform-specific implementations:fetchOAuthTokenFromKeychain()(existing behavior viasecuritycommand)fetchOAuthTokenFromCredentialsFile()(new, reads from~/.claude/.credentials.json)parseOAuthTokenFromJSON()to handle JSON parsing and validation for both platformsformatStatuslineOutput()andoutputFallback()incc_statusline.goto show quota utilization on both macOS and LinuxfetchRateLimit()incc_info_timer.goto support both platformsImplementation Details
~/.claude/.credentials.jsonwith proper file permission handling (0600)claudeAiOauthobject withaccessTokenfield)https://claude.ai/code/session_01VQbqeC4PtoWTXdvETzsepV