forked from doctly/switchboard
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclaude-auth.js
More file actions
201 lines (179 loc) · 6.72 KB
/
Copy pathclaude-auth.js
File metadata and controls
201 lines (179 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// claude-auth.js — Read Claude Code OAuth credentials and fetch usage data
// macOS: Keychain (primary) → ~/.claude/.credentials.json (fallback)
// Linux/Windows: ~/.claude/.credentials.json only
const { execFileSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');
function getConfigDir() {
return (process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude'));
}
function getKeychainServiceName() {
const suffix = '-credentials';
if (process.env.CLAUDE_CONFIG_DIR) {
const crypto = require('crypto');
const hash = crypto.createHash('sha256').update(getConfigDir()).digest('hex').substring(0, 8);
return `Claude Code${suffix}-${hash}`;
}
return `Claude Code${suffix}`;
}
function readFromKeychain() {
if (process.platform !== 'darwin') return null;
try {
const service = getKeychainServiceName();
const user = process.env.USER || os.userInfo().username;
// execFileSync (no shell) so $USER can't be interpolated into a command string
const json = execFileSync(
'security',
['find-generic-password', '-a', user, '-w', '-s', service],
{ encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }
).trim();
return JSON.parse(json);
} catch (err) {
console.error('[claude-auth] Keychain read error:', err.message);
return null;
}
}
function readFromFile() {
try {
const credPath = path.join(getConfigDir(), '.credentials.json');
return JSON.parse(fs.readFileSync(credPath, 'utf8'));
} catch (err) {
console.error('[claude-auth] Credentials file read error:', err.message);
return null;
}
}
function getOAuthToken() {
const creds = readFromKeychain() || readFromFile();
return creds?.claudeAiOauth || null;
}
function formatResetTime(value) {
if (!value) return null;
let resetDate;
if (typeof value === 'string') {
resetDate = new Date(value);
} else if (value > 1e12) {
resetDate = new Date(value);
} else {
resetDate = new Date(value * 1000);
}
if (isNaN(resetDate.getTime())) return null;
const now = new Date();
const diffMs = resetDate - now;
const hours = resetDate.getHours();
const minutes = resetDate.getMinutes();
const ampm = hours >= 12 ? 'pm' : 'am';
const h = hours % 12 || 12;
const timeStr = minutes === 0 ? `${h}${ampm}` : `${h}:${String(minutes).padStart(2, '0')}${ampm}`;
const tz = Intl.DateTimeFormat('en', { timeZoneName: 'short' }).formatToParts(resetDate)
.find(p => p.type === 'timeZoneName')?.value || '';
if (diffMs < 0) return `${timeStr} (${tz})`;
if (diffMs < 24 * 60 * 60 * 1000) return `${timeStr} (${tz})`;
const month = resetDate.toLocaleString('en', { month: 'short' });
const day = resetDate.getDate();
return `${month} ${day} at ${timeStr} (${tz})`;
}
function mapBucket(apiUsage, apiKey, usageKey, usage) {
try {
const u = apiUsage[apiKey];
if (!u || u.utilization === null || u.utilization === undefined) return;
usage[usageKey] = Math.floor(u.utilization);
if (u.resets_at) usage[usageKey + 'Reset'] = formatResetTime(u.resets_at);
} catch (err) {
console.error('[claude-auth] Error mapping bucket', apiKey, err.message);
}
}
/**
* Map the API's `limits` array into a renderable list.
*
* The per-model top-level buckets this file used to read — seven_day_sonnet,
* seven_day_opus, and a set of codenamed ones — now come back `null`. The live
* data moved into `limits`, which is self-describing: each row carries its own
* `kind`, `percent`, `resets_at`, and, for model-scoped windows, the model's
* display name. Reading that means new models show up on their own instead of
* needing a new key added here every time one ships.
*/
function mapLimits(apiUsage, usage) {
const rows = Array.isArray(apiUsage.limits) ? apiUsage.limits : [];
const out = [];
for (const l of rows) {
if (!l || l.percent === null || l.percent === undefined) continue;
const model = l.scope?.model?.display_name || null;
const label = l.kind === 'session' ? 'Current session'
: l.kind === 'weekly_all' ? 'Week (all models)'
: model ? `Week (${model})`
: 'Week';
out.push({
kind: l.kind || null,
label,
model,
percent: Math.floor(l.percent),
reset: l.resets_at ? formatResetTime(l.resets_at) : null,
severity: l.severity || 'normal',
});
}
if (out.length) usage.limits = out;
}
function transformUsageResponse(apiUsage) {
if (!apiUsage) return {};
const usage = {};
// Legacy flat keys — kept because existing callers read usage.session /
// usage.weekAll directly. five_hour and seven_day are still populated by the
// API; the two model-specific ones are not, and are left in only so an older
// response shape still maps.
mapBucket(apiUsage, 'five_hour', 'session', usage);
mapBucket(apiUsage, 'seven_day', 'weekAll', usage);
mapBucket(apiUsage, 'seven_day_sonnet', 'weekSonnet', usage);
mapBucket(apiUsage, 'seven_day_opus', 'weekOpus', usage);
mapLimits(apiUsage, usage);
// Backfill the legacy keys from `limits` if the flat buckets ever go null too,
// so the status bar and any other flat-key reader keep working.
for (const l of usage.limits || []) {
if (l.kind === 'session' && usage.session === undefined) {
usage.session = l.percent;
if (l.reset) usage.sessionReset = l.reset;
}
if (l.kind === 'weekly_all' && usage.weekAll === undefined) {
usage.weekAll = l.percent;
if (l.reset) usage.weekAllReset = l.reset;
}
}
return usage;
}
async function fetchUsage() {
const oauth = getOAuthToken();
if (!oauth?.accessToken) return null;
const res = await fetch('https://api.anthropic.com/api/oauth/usage', {
headers: {
'Authorization': `Bearer ${oauth.accessToken}`,
'Content-Type': 'application/json',
'User-Agent': 'claude-code/2.1.74',
'anthropic-beta': 'oauth-2025-04-20',
},
signal: AbortSignal.timeout(10000),
});
if (res.status === 429) {
const retryAfter = parseInt(res.headers.get('retry-after') || '0', 10);
return { _rateLimited: true, retryAfterSeconds: retryAfter };
}
if (!res.ok) {
console.error('[claude-auth] Usage API error:', res.status, res.statusText);
return null;
}
return await res.json();
}
async function fetchAndTransformUsage() {
try {
const raw = await fetchUsage();
if (raw === null) {
return { _error: true, message: 'Could not fetch usage (no token or API error)' };
}
if (raw?._rateLimited) {
return { _rateLimited: true, retryAfterSeconds: raw.retryAfterSeconds };
}
return transformUsageResponse(raw);
} catch (err) {
return { _error: true, message: err.message };
}
}
module.exports = { getOAuthToken, fetchUsage, fetchAndTransformUsage, getConfigDir };