opencode-claude-auth 0.5.2 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +65 -97
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAqEjD,QAAA,MAAM,MAAM,EAAE,MAmCb,CAAA;AAED,eAAe,MAAM,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,39 @@
|
|
|
1
1
|
import { execSync } from "node:child_process";
|
|
2
|
-
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
3
|
import { dirname, join } from "node:path";
|
|
5
4
|
import { homedir } from "node:os";
|
|
6
5
|
import { readClaudeCredentials } from "./keychain.js";
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
join(homedir(), ".local", "share", "opencode", "auth.json")
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
6
|
+
function getAuthJsonPath() {
|
|
7
|
+
if (process.platform === "win32") {
|
|
8
|
+
return join(process.env.USERPROFILE ?? homedir(), ".local", "share", "opencode", "auth.json");
|
|
9
|
+
}
|
|
10
|
+
return join(homedir(), ".local", "share", "opencode", "auth.json");
|
|
11
|
+
}
|
|
12
|
+
function syncAuthJson(creds) {
|
|
13
|
+
const authPath = getAuthJsonPath();
|
|
14
|
+
let auth = {};
|
|
15
|
+
if (existsSync(authPath)) {
|
|
16
|
+
const raw = readFileSync(authPath, "utf-8").trim();
|
|
17
|
+
if (raw) {
|
|
18
|
+
try {
|
|
19
|
+
auth = JSON.parse(raw);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// Malformed file, start fresh
|
|
20
23
|
}
|
|
21
|
-
}
|
|
22
|
-
catch {
|
|
23
|
-
// Non-fatal: may not have write permissions
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
+
auth.anthropic = {
|
|
27
|
+
type: "oauth",
|
|
28
|
+
access: creds.accessToken,
|
|
29
|
+
refresh: creds.refreshToken,
|
|
30
|
+
expires: creds.expiresAt,
|
|
31
|
+
};
|
|
32
|
+
const dir = dirname(authPath);
|
|
33
|
+
if (!existsSync(dir)) {
|
|
34
|
+
mkdirSync(dir, { recursive: true });
|
|
35
|
+
}
|
|
36
|
+
writeFileSync(authPath, JSON.stringify(auth, null, 2), "utf-8");
|
|
26
37
|
}
|
|
27
38
|
function refreshViaCli() {
|
|
28
39
|
try {
|
|
@@ -37,92 +48,49 @@ function refreshViaCli() {
|
|
|
37
48
|
// Non-fatal: Claude CLI may not be available
|
|
38
49
|
}
|
|
39
50
|
}
|
|
40
|
-
function
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return readFileSync(promptPath, "utf-8");
|
|
51
|
+
function refreshIfNeeded() {
|
|
52
|
+
let creds = readClaudeCredentials();
|
|
53
|
+
if (creds && creds.expiresAt > Date.now() + 60_000) {
|
|
54
|
+
return creds;
|
|
45
55
|
}
|
|
46
|
-
|
|
47
|
-
|
|
56
|
+
// Token is expired or near expiry, try CLI refresh
|
|
57
|
+
refreshViaCli();
|
|
58
|
+
creds = readClaudeCredentials();
|
|
59
|
+
if (creds && creds.expiresAt > Date.now() + 60_000) {
|
|
60
|
+
return creds;
|
|
48
61
|
}
|
|
62
|
+
return null;
|
|
49
63
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
refreshViaCli();
|
|
61
|
-
const afterRefresh = readClaudeCredentials();
|
|
62
|
-
if (afterRefresh && afterRefresh.expiresAt > Date.now() + 60_000) {
|
|
63
|
-
current = afterRefresh;
|
|
64
|
-
onRefresh(current);
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
throw new Error("opencode-claude-auth: Token expired and refresh failed. " +
|
|
68
|
-
"Re-authenticate with Claude Code by running `claude` in your terminal.");
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
const headers = new Headers(init?.headers);
|
|
73
|
-
headers.set("Authorization", `Bearer ${current.accessToken}`);
|
|
74
|
-
return fetch(fetchInput, { ...init, headers });
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
const plugin = async (input) => {
|
|
78
|
-
const creds = readClaudeCredentials();
|
|
64
|
+
const SYNC_INTERVAL = 5 * 60 * 1000; // 5 minutes
|
|
65
|
+
const plugin = async () => {
|
|
66
|
+
let creds = null;
|
|
67
|
+
try {
|
|
68
|
+
creds = readClaudeCredentials();
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
console.warn("opencode-claude-auth: Failed to read Claude Code credentials:", err instanceof Error ? err.message : err);
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
79
74
|
if (!creds) {
|
|
75
|
+
console.warn("opencode-claude-auth: No Claude Code credentials found. " +
|
|
76
|
+
"Plugin disabled. Run `claude` to authenticate.");
|
|
80
77
|
return {};
|
|
81
78
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
79
|
+
// Sync credentials to auth.json on startup
|
|
80
|
+
syncAuthJson(creds);
|
|
81
|
+
// Keep auth.json synced, refreshing via CLI if token is near expiry
|
|
82
|
+
setInterval(() => {
|
|
83
|
+
try {
|
|
84
|
+
const fresh = refreshIfNeeded();
|
|
85
|
+
if (fresh) {
|
|
86
|
+
syncAuthJson(fresh);
|
|
90
87
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
expires: initialCreds.expiresAt,
|
|
98
|
-
},
|
|
99
|
-
});
|
|
100
|
-
return {
|
|
101
|
-
apiKey: "oauth",
|
|
102
|
-
fetch: createAuthFetch(initialCreds, (updated) => {
|
|
103
|
-
void input.client.auth.set({
|
|
104
|
-
path: { id: "anthropic" },
|
|
105
|
-
body: {
|
|
106
|
-
type: "oauth",
|
|
107
|
-
access: updated.accessToken,
|
|
108
|
-
refresh: updated.refreshToken,
|
|
109
|
-
expires: updated.expiresAt,
|
|
110
|
-
},
|
|
111
|
-
});
|
|
112
|
-
}),
|
|
113
|
-
};
|
|
114
|
-
},
|
|
115
|
-
methods: [],
|
|
116
|
-
};
|
|
117
|
-
return {
|
|
118
|
-
auth,
|
|
119
|
-
async "experimental.chat.system.transform"(hookInput, output) {
|
|
120
|
-
if (hookInput.model.providerID !== "anthropic")
|
|
121
|
-
return;
|
|
122
|
-
const prompt = loadSessionPrompt();
|
|
123
|
-
output.system.unshift(prompt);
|
|
124
|
-
},
|
|
125
|
-
};
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
// Non-fatal
|
|
91
|
+
}
|
|
92
|
+
}, SYNC_INTERVAL);
|
|
93
|
+
return {};
|
|
126
94
|
};
|
|
127
95
|
export default plugin;
|
|
128
96
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBA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