-
Notifications
You must be signed in to change notification settings - Fork 85
feat: prioritize CLAUDE_CONFIG_DIR over default credentials path #45
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: master
Are you sure you want to change the base?
feat: prioritize CLAUDE_CONFIG_DIR over default credentials path #45
Conversation
Add support for reading OAuth credentials from $CLAUDE_CONFIG_DIR/.credentials.json with proper priority ordering. When CLAUDE_CONFIG_DIR is set, it takes precedence over the default ~/.claude/.credentials.json location. Priority order: 1. Try $CLAUDE_CONFIG_DIR/.credentials.json (if env var is set) 2. Fall back to ~/.claude/.credentials.json (default) 3. Return None if both fail This respects explicit user configuration via environment variable while maintaining backward compatibility with existing installations that use the default path. Use cases: - Custom Claude Code installations in non-standard locations - Containerized environments with custom config directories - Multi-user setups with isolated config paths - CI/CD pipelines with temporary config locations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Reviewer's GuideRefactors OAuth token retrieval to first attempt reading credentials from a custom directory specified by CLAUDE_CONFIG_DIR, falling back to the default ~/.claude/.credentials.json and returning None if both attempts fail. Sequence diagram for OAuth token retrieval with CLAUDE_CONFIG_DIR prioritysequenceDiagram
participant "get_oauth_token_file()"
participant "Environment"
participant "get_oauth_token_from_config_dir()"
participant "get_credentials_path()"
participant "Filesystem"
"get_oauth_token_file()"->>"Environment": Check CLAUDE_CONFIG_DIR
alt CLAUDE_CONFIG_DIR is set
"get_oauth_token_file()"->>"get_oauth_token_from_config_dir()": Try custom config dir
"get_oauth_token_from_config_dir()"->>"Filesystem": Read $CLAUDE_CONFIG_DIR/.credentials.json
alt Credentials found
"get_oauth_token_from_config_dir()"-->>"get_oauth_token_file()": Return token
else Credentials not found
"get_oauth_token_from_config_dir()"-->>"get_oauth_token_file()": Return None
end
end
alt Token not found in custom dir
"get_oauth_token_file()"->>"get_credentials_path()": Get default path
"get_oauth_token_file()"->>"Filesystem": Read ~/.claude/.credentials.json
alt Credentials found
"get_oauth_token_file()"-->>"Caller": Return token
else Credentials not found
"get_oauth_token_file()"-->>"Caller": Return None
end
end
Class diagram for updated credential retrieval functionsclassDiagram
class CredentialsFile {
claude_ai_oauth: Option<ClaudeAiOAuth>
}
class ClaudeAiOAuth {
access_token: String
}
class "get_oauth_token_file()" {
+get_oauth_token_file() Option<String>
}
class "get_oauth_token_from_config_dir()" {
+get_oauth_token_from_config_dir() Option<String>
}
class "get_credentials_path()" {
+get_credentials_path() Option<PathBuf>
}
CredentialsFile --> "ClaudeAiOAuth"
"get_oauth_token_file()" --> "get_oauth_token_from_config_dir()"
"get_oauth_token_file()" --> "get_credentials_path()"
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey there - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `src/utils/credentials.rs:73-83` </location>
<code_context>
+
+ // Fall back to default ~/.claude/.credentials.json
+ if let Some(credentials_path) = get_credentials_path() {
+ if credentials_path.exists() {
+ if let Ok(content) = std::fs::read_to_string(&credentials_path) {
+ if let Ok(creds_file) = serde_json::from_str::<CredentialsFile>(&content) {
+ if let Some(token) = creds_file.claude_ai_oauth.map(|oauth| oauth.access_token) {
</code_context>
<issue_to_address>
**suggestion:** Consider logging or surfacing errors when reading or parsing the credentials file.
If silent failure is not intended, add error logging to help diagnose issues with file access or JSON parsing.
```suggestion
if let Some(credentials_path) = get_credentials_path() {
if credentials_path.exists() {
match std::fs::read_to_string(&credentials_path) {
Ok(content) => {
match serde_json::from_str::<CredentialsFile>(&content) {
Ok(creds_file) => {
if let Some(token) = creds_file.claude_ai_oauth.map(|oauth| oauth.access_token) {
return Some(token);
}
}
Err(e) => {
eprintln!(
"Failed to parse credentials file as JSON ({}): {}",
credentials_path.display(),
e
);
}
}
}
Err(e) => {
eprintln!(
"Failed to read credentials file ({}): {}",
credentials_path.display(),
e
);
}
}
}
}
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Summary
Adds support for reading OAuth credentials from custom locations via
$CLAUDE_CONFIG_DIRenvironment variable, with proper priority ordering that respects explicit user configuration.Changes
File:
src/utils/credentials.rsRefactored
get_oauth_token_file()to check environment variable first:Priority Order
$CLAUDE_CONFIG_DIR/.credentials.json(ifCLAUDE_CONFIG_DIRis set)~/.claude/.credentials.json(default fallback)None(if both fail)Implementation
CLAUDE_CONFIG_DIRenv var is set$CLAUDE_CONFIG_DIR/.credentials.json~/.claude/.credentials.jsonNoneonly if both paths failMotivation
This change provides flexibility for users with non-standard Claude Code installations while maintaining backward compatibility:
Backward Compatibility
✅ No breaking changes
CLAUDE_CONFIG_DIRset~/.claude/.credentials.jsonTesting
cargo build --releasecargo testCLAUDE_CONFIG_DIRwhen setTechnical Details
Lines changed: 1 file, 31 insertions, 6 deletions
The refactoring extracts
get_oauth_token_from_config_dir()as a separate function for cleaner code organization and reusability.🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com
Summary by Sourcery
Prioritize loading OAuth credentials from $CLAUDE_CONFIG_DIR/.credentials.json over the default ~/.claude/.credentials.json path and refactor credential retrieval logic into helper functions
New Features:
Enhancements: