|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Credential Provider: Claude Code OAuth |
| 4 | +# Checks and refreshes Claude Code OAuth tokens in ~/.claude/.credentials.json |
| 5 | +# |
| 6 | +# Credential Provider Contract: |
| 7 | +# Each provider script must define these functions: |
| 8 | +# provider_name() - Echo the provider name (e.g., "claude-code") |
| 9 | +# provider_check() - Check credential health |
| 10 | +# Return 0: healthy (no action needed) |
| 11 | +# Return 1: needs refresh (will expire soon) |
| 12 | +# Return 2: critical (already expired or missing) |
| 13 | +# provider_refresh() - Refresh the credentials |
| 14 | +# Return 0: success |
| 15 | +# Return 1: failure |
| 16 | +# |
| 17 | + |
| 18 | +CREDENTIALS_FILE="$HOME/.claude/.credentials.json" |
| 19 | + |
| 20 | +# Threshold: refresh if token expires within 6 hours (21600 seconds) |
| 21 | +REFRESH_THRESHOLD_SECONDS=21600 |
| 22 | + |
| 23 | +provider_name() { |
| 24 | + echo "claude-code" |
| 25 | +} |
| 26 | + |
| 27 | +provider_check() { |
| 28 | + # Verify file exists |
| 29 | + if [[ ! -f "$CREDENTIALS_FILE" ]]; then |
| 30 | + echo "Credentials file not found: $CREDENTIALS_FILE" |
| 31 | + return 2 |
| 32 | + fi |
| 33 | + |
| 34 | + # Parse expiresAt (milliseconds epoch) using python3 |
| 35 | + local expires_at_ms |
| 36 | + expires_at_ms=$(python3 -c " |
| 37 | +import json, sys |
| 38 | +try: |
| 39 | + with open('$CREDENTIALS_FILE') as f: |
| 40 | + data = json.load(f) |
| 41 | + print(data['claudeAiOauth']['expiresAt']) |
| 42 | +except (KeyError, json.JSONDecodeError, FileNotFoundError) as e: |
| 43 | + print(f'ERROR: {e}', file=sys.stderr) |
| 44 | + sys.exit(1) |
| 45 | +" 2>&1) || { |
| 46 | + echo "Failed to parse credentials: $expires_at_ms" |
| 47 | + return 2 |
| 48 | + } |
| 49 | + |
| 50 | + # Convert ms to seconds and compare |
| 51 | + local now_seconds |
| 52 | + now_seconds=$(date +%s) |
| 53 | + local expires_at_seconds=$((expires_at_ms / 1000)) |
| 54 | + local remaining=$((expires_at_seconds - now_seconds)) |
| 55 | + |
| 56 | + if [[ $remaining -le 0 ]]; then |
| 57 | + echo "Token EXPIRED ($((remaining * -1)) seconds ago)" |
| 58 | + return 2 |
| 59 | + elif [[ $remaining -le $REFRESH_THRESHOLD_SECONDS ]]; then |
| 60 | + local hours_remaining=$((remaining / 3600)) |
| 61 | + echo "Token expires in ${hours_remaining}h (threshold: $((REFRESH_THRESHOLD_SECONDS / 3600))h)" |
| 62 | + return 1 |
| 63 | + else |
| 64 | + local hours_remaining=$((remaining / 3600)) |
| 65 | + echo "Token healthy (expires in ${hours_remaining}h)" |
| 66 | + return 0 |
| 67 | + fi |
| 68 | +} |
| 69 | + |
| 70 | +provider_refresh() { |
| 71 | + # Capture expiresAt before refresh attempt |
| 72 | + local before_expires |
| 73 | + before_expires=$(python3 -c " |
| 74 | +import json |
| 75 | +with open('$CREDENTIALS_FILE') as f: |
| 76 | + data = json.load(f) |
| 77 | +print(data['claudeAiOauth']['expiresAt']) |
| 78 | +" 2>/dev/null) || before_expires="0" |
| 79 | + |
| 80 | + # Launch claude interactively - the auth/token refresh triggers on startup |
| 81 | + # Send /exit via printf so it exits cleanly after the auth handshake |
| 82 | + printf '/exit\n' | timeout 30 claude >/dev/null 2>&1 || { |
| 83 | + echo "claude command failed or timed out" |
| 84 | + return 1 |
| 85 | + } |
| 86 | + |
| 87 | + # Verify token was actually refreshed |
| 88 | + local after_expires |
| 89 | + after_expires=$(python3 -c " |
| 90 | +import json |
| 91 | +with open('$CREDENTIALS_FILE') as f: |
| 92 | + data = json.load(f) |
| 93 | +print(data['claudeAiOauth']['expiresAt']) |
| 94 | +" 2>/dev/null) || { |
| 95 | + echo "Failed to read credentials after refresh" |
| 96 | + return 1 |
| 97 | + } |
| 98 | + |
| 99 | + if [[ "$after_expires" -gt "$before_expires" ]]; then |
| 100 | + echo "Token refreshed (new expiry: $(date -d @$((after_expires / 1000)) '+%Y-%m-%d %H:%M:%S %Z'))" |
| 101 | + return 0 |
| 102 | + elif [[ "$after_expires" -eq "$before_expires" ]]; then |
| 103 | + # Token unchanged - claude may have determined refresh wasn't needed yet |
| 104 | + echo "Token unchanged after refresh attempt (expiry still valid)" |
| 105 | + return 0 |
| 106 | + fi |
| 107 | +} |
0 commit comments