@hienlh/ppm 0.7.29 → 0.7.30
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/CHANGELOG.md +5 -0
- package/package.json +1 -1
- package/src/cli/commands/restart.ts +14 -5
- package/test-claude-oauth-v2.mjs +0 -165
- package/test-claude-oauth.mjs +0 -175
- package/test-verify-oat.mjs +0 -106
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.7.30] - 2026-03-23
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
- **Restart from PPM terminal**: worker and restart command now ignore SIGHUP — killing the old server destroys the terminal PTY which sends SIGHUP to the process group, previously killing the worker before it could spawn the new server
|
|
7
|
+
|
|
3
8
|
## [0.7.29] - 2026-03-22
|
|
4
9
|
|
|
5
10
|
### Added
|
package/package.json
CHANGED
|
@@ -10,6 +10,9 @@ const RESTART_RESULT = resolve(PPM_DIR, ".restart-result");
|
|
|
10
10
|
|
|
11
11
|
/** Restart only the server process, keeping the tunnel alive */
|
|
12
12
|
export async function restartServer(options: { config?: string }) {
|
|
13
|
+
// Ignore SIGHUP so this process survives when PPM terminal dies
|
|
14
|
+
process.on("SIGHUP", () => {});
|
|
15
|
+
|
|
13
16
|
if (!existsSync(STATUS_FILE)) {
|
|
14
17
|
console.log("No PPM daemon running. Use 'ppm start' instead.");
|
|
15
18
|
process.exit(1);
|
|
@@ -51,7 +54,8 @@ export async function restartServer(options: { config?: string }) {
|
|
|
51
54
|
console.log(" If you're using PPM terminal, wait a few seconds then reload the page.\n");
|
|
52
55
|
|
|
53
56
|
// Generate a self-contained restart worker script.
|
|
54
|
-
//
|
|
57
|
+
// Worker ignores SIGHUP so it survives when killing the server causes the
|
|
58
|
+
// terminal (and its process group) to receive SIGHUP.
|
|
55
59
|
const params = JSON.stringify({
|
|
56
60
|
serverPid, port, host, serverScript,
|
|
57
61
|
config: options.config ?? "",
|
|
@@ -67,6 +71,11 @@ export async function restartServer(options: { config?: string }) {
|
|
|
67
71
|
import { readFileSync, writeFileSync, openSync, unlinkSync, appendFileSync } from "node:fs";
|
|
68
72
|
import { createServer } from "node:net";
|
|
69
73
|
|
|
74
|
+
// Ignore SIGHUP — when we kill the old server, the terminal PTY dies and
|
|
75
|
+
// SIGHUP is sent to the entire process group. Without this, the worker
|
|
76
|
+
// would be killed before it can spawn the new server.
|
|
77
|
+
process.on("SIGHUP", () => {});
|
|
78
|
+
|
|
70
79
|
const P = ${params};
|
|
71
80
|
|
|
72
81
|
async function main() {
|
|
@@ -163,7 +172,8 @@ main();
|
|
|
163
172
|
`);
|
|
164
173
|
|
|
165
174
|
// Spawn worker as a fully detached process
|
|
166
|
-
const
|
|
175
|
+
const logFile = resolve(PPM_DIR, "ppm.log");
|
|
176
|
+
const logFd = openSync(logFile, "a");
|
|
167
177
|
const worker = Bun.spawn({
|
|
168
178
|
cmd: [process.execPath, "run", workerPath],
|
|
169
179
|
stdio: ["ignore", logFd, logFd],
|
|
@@ -171,9 +181,8 @@ main();
|
|
|
171
181
|
});
|
|
172
182
|
worker.unref();
|
|
173
183
|
|
|
174
|
-
// Poll for result —
|
|
175
|
-
//
|
|
176
|
-
// the user already saw the pre-restart message above.
|
|
184
|
+
// Poll for result — works from both PPM terminal (SIGHUP-immune) and external terminal.
|
|
185
|
+
// Output may not be visible if PPM terminal PTY is dead, but process stays alive.
|
|
177
186
|
const pollStart = Date.now();
|
|
178
187
|
while (Date.now() - pollStart < 20000) {
|
|
179
188
|
await Bun.sleep(500);
|
package/test-claude-oauth-v2.mjs
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
/**
|
|
3
|
-
* Claude OAuth PKCE test — fixed version
|
|
4
|
-
*
|
|
5
|
-
* Fixes vs v1:
|
|
6
|
-
* 1. Token endpoint: console.anthropic.com (not api.anthropic.com)
|
|
7
|
-
* 2. Content-Type: application/x-www-form-urlencoded (not JSON)
|
|
8
|
-
* 3. Removed `state` from token exchange body (client-side CSRF only)
|
|
9
|
-
* 4. Only platform.claude.com redirect (confirmed to work with this client_id)
|
|
10
|
-
*
|
|
11
|
-
* Usage: bun test-claude-oauth-v2.mjs
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import crypto from 'crypto'
|
|
15
|
-
import readline from 'readline'
|
|
16
|
-
import { execSync } from 'child_process'
|
|
17
|
-
|
|
18
|
-
const CLIENT_ID = '9d1c250a-e61b-44d9-88ed-5944d1962f5e'
|
|
19
|
-
const REDIRECT = 'https://platform.claude.com/oauth/code/callback'
|
|
20
|
-
const SCOPE = 'org:create_api_key user:profile user:inference user:sessions:claude_code user:mcp_servers user:file_upload'
|
|
21
|
-
const TOKEN_URL = 'https://console.anthropic.com/v1/oauth/token'
|
|
22
|
-
|
|
23
|
-
// ── PKCE ────────────────────────────────────────────────────────────────────
|
|
24
|
-
|
|
25
|
-
function generatePKCE() {
|
|
26
|
-
// 32 random bytes → 43 base64url chars (RFC 7636 minimum entropy)
|
|
27
|
-
const verifier = crypto.randomBytes(32).toString('base64url')
|
|
28
|
-
const challenge = crypto.createHash('sha256').update(verifier).digest('base64url')
|
|
29
|
-
const state = crypto.randomBytes(16).toString('base64url')
|
|
30
|
-
return { verifier, challenge, state }
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
34
|
-
|
|
35
|
-
function copyToClipboard(text) {
|
|
36
|
-
try { execSync(`echo ${JSON.stringify(text)} | pbcopy`); return true }
|
|
37
|
-
catch { return false }
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function ask(rl, question) {
|
|
41
|
-
return new Promise(resolve => rl.question(question, resolve))
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function parseCode(input) {
|
|
45
|
-
input = input.trim()
|
|
46
|
-
// Full URL
|
|
47
|
-
try {
|
|
48
|
-
const url = new URL(input)
|
|
49
|
-
const code = url.searchParams.get('code')
|
|
50
|
-
if (code) return code.split('#')[0]
|
|
51
|
-
} catch {}
|
|
52
|
-
// platform.claude.com format: CODE#STATE
|
|
53
|
-
if (input.includes('#')) return input.split('#')[0]
|
|
54
|
-
// raw code
|
|
55
|
-
return input.split('?')[0].split('&')[0]
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// ── Token exchange ───────────────────────────────────────────────────────────
|
|
59
|
-
|
|
60
|
-
async function exchangeCode({ code, verifier }) {
|
|
61
|
-
console.log(`\nPOST ${TOKEN_URL}`)
|
|
62
|
-
|
|
63
|
-
const body = new URLSearchParams({
|
|
64
|
-
grant_type: 'authorization_code',
|
|
65
|
-
client_id: CLIENT_ID,
|
|
66
|
-
code,
|
|
67
|
-
redirect_uri: REDIRECT,
|
|
68
|
-
code_verifier: verifier,
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
const res = await fetch(TOKEN_URL, {
|
|
72
|
-
method: 'POST',
|
|
73
|
-
headers: {
|
|
74
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
75
|
-
'Accept': 'application/json',
|
|
76
|
-
},
|
|
77
|
-
body,
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
const data = await res.json()
|
|
81
|
-
return { ok: res.ok, status: res.status, data }
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// ── Main ─────────────────────────────────────────────────────────────────────
|
|
85
|
-
|
|
86
|
-
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
|
87
|
-
|
|
88
|
-
console.log('\n╔══════════════════════════════════════════╗')
|
|
89
|
-
console.log('║ Claude OAuth PKCE Test — v2 (fixed) ║')
|
|
90
|
-
console.log('╚══════════════════════════════════════════╝\n')
|
|
91
|
-
|
|
92
|
-
const { verifier, challenge, state } = generatePKCE()
|
|
93
|
-
|
|
94
|
-
console.log(`verifier (${verifier.length} chars): ${verifier.slice(0, 20)}...`)
|
|
95
|
-
console.log(`challenge (${challenge.length} chars): ${challenge}`)
|
|
96
|
-
console.log(`state (${state.length} chars): ${state}\n`)
|
|
97
|
-
|
|
98
|
-
// Build auth URL — code=true tells claude.ai to show a code page instead of redirecting
|
|
99
|
-
const params = new URLSearchParams({
|
|
100
|
-
code: 'true',
|
|
101
|
-
client_id: CLIENT_ID,
|
|
102
|
-
response_type: 'code',
|
|
103
|
-
redirect_uri: REDIRECT,
|
|
104
|
-
scope: SCOPE,
|
|
105
|
-
code_challenge: challenge,
|
|
106
|
-
code_challenge_method: 'S256',
|
|
107
|
-
state,
|
|
108
|
-
})
|
|
109
|
-
const authUrl = `https://claude.ai/oauth/authorize?${params}`
|
|
110
|
-
|
|
111
|
-
console.log('┌─ Authorization URL ──────────────────────────────────────────────')
|
|
112
|
-
console.log(`│ ${authUrl}`)
|
|
113
|
-
console.log('└─────────────────────────────────────────────────────────────────\n')
|
|
114
|
-
|
|
115
|
-
const copied = copyToClipboard(authUrl)
|
|
116
|
-
if (copied) console.log('✓ Copied to clipboard\n')
|
|
117
|
-
|
|
118
|
-
console.log('1. Mở URL trong browser')
|
|
119
|
-
console.log('2. Login / approve')
|
|
120
|
-
console.log('3. platform.claude.com sẽ hiển thị code dạng: XXXX#STATE\n')
|
|
121
|
-
|
|
122
|
-
const input = await ask(rl, 'Paste code/URL: ')
|
|
123
|
-
const code = parseCode(input)
|
|
124
|
-
|
|
125
|
-
if (!code) {
|
|
126
|
-
console.error('❌ Không parse được code')
|
|
127
|
-
rl.close()
|
|
128
|
-
process.exit(1)
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
console.log(`\n✓ code = ${code.slice(0, 30)}...`)
|
|
132
|
-
|
|
133
|
-
// Validate state (CSRF check — client-side only, not sent to server)
|
|
134
|
-
const returnedState = input.includes('#') ? input.split('#')[1]?.split('&')[0] : null
|
|
135
|
-
if (returnedState && returnedState !== state) {
|
|
136
|
-
console.warn(`⚠️ State mismatch! Expected: ${state}, Got: ${returnedState}`)
|
|
137
|
-
} else if (returnedState) {
|
|
138
|
-
console.log(`✓ state OK`)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
console.log('\nExchanging code for token...')
|
|
142
|
-
const { ok, status, data } = await exchangeCode({ code, verifier })
|
|
143
|
-
|
|
144
|
-
console.log(`\nHTTP ${status}`)
|
|
145
|
-
|
|
146
|
-
if (ok) {
|
|
147
|
-
console.log('\n✅ SUCCESS!')
|
|
148
|
-
console.log('─'.repeat(50))
|
|
149
|
-
console.log(`access_token: ${(data.access_token ?? data.accessToken)?.slice(0, 35)}...`)
|
|
150
|
-
console.log(`refresh_token: ${(data.refresh_token ?? data.refreshToken)?.slice(0, 35)}...`)
|
|
151
|
-
console.log(`token_type: ${data.token_type}`)
|
|
152
|
-
console.log(`expires_in: ${data.expires_in}s`)
|
|
153
|
-
if (data.account) {
|
|
154
|
-
console.log(`email: ${data.account.email_address}`)
|
|
155
|
-
console.log(`account_uuid: ${data.account.uuid}`)
|
|
156
|
-
}
|
|
157
|
-
console.log('─'.repeat(50))
|
|
158
|
-
console.log('\nFull response:')
|
|
159
|
-
console.log(JSON.stringify(data, null, 2))
|
|
160
|
-
} else {
|
|
161
|
-
console.log('\n❌ FAILED')
|
|
162
|
-
console.log(JSON.stringify(data, null, 2))
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
rl.close()
|
package/test-claude-oauth.mjs
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
/**
|
|
3
|
-
* Test Claude OAuth flow — manual copy/paste approach
|
|
4
|
-
* Usage: bun test-claude-oauth.mjs
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import crypto from 'crypto'
|
|
8
|
-
import readline from 'readline'
|
|
9
|
-
import { execSync } from 'child_process'
|
|
10
|
-
|
|
11
|
-
const CLIENT_ID = '9d1c250a-e61b-44d9-88ed-5944d1962f5e'
|
|
12
|
-
const SCOPE = 'org:create_api_key user:profile user:inference user:sessions:claude_code user:mcp_servers user:file_upload'
|
|
13
|
-
|
|
14
|
-
// Redirect URIs to test (in order)
|
|
15
|
-
const REDIRECT_URI_OPTIONS = [
|
|
16
|
-
{ label: 'platform.claude.com (manual code page)', uri: 'https://platform.claude.com/oauth/code/callback' },
|
|
17
|
-
{ label: 'localhost:54545 (CLIProxyAPI style)', uri: 'http://localhost:54545/callback' },
|
|
18
|
-
{ label: 'localhost (no port)', uri: 'http://localhost/callback' },
|
|
19
|
-
]
|
|
20
|
-
|
|
21
|
-
// Token endpoints to try
|
|
22
|
-
const TOKEN_ENDPOINTS = [
|
|
23
|
-
'https://api.anthropic.com/v1/oauth/token',
|
|
24
|
-
'https://claude.ai/api/oauth/token',
|
|
25
|
-
]
|
|
26
|
-
|
|
27
|
-
// ── PKCE ────────────────────────────────────────────────────────────────────
|
|
28
|
-
|
|
29
|
-
function generatePKCE() {
|
|
30
|
-
// 96 bytes → 128 base64url chars (matches CLIProxyAPI exactly)
|
|
31
|
-
const verifierBytes = crypto.randomBytes(96)
|
|
32
|
-
const verifier = verifierBytes.toString('base64url')
|
|
33
|
-
const challenge = crypto.createHash('sha256').update(verifier).digest('base64url')
|
|
34
|
-
const state = crypto.randomBytes(16).toString('base64url')
|
|
35
|
-
return { verifier, challenge, state }
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// ── CLI helpers ──────────────────────────────────────────────────────────────
|
|
39
|
-
|
|
40
|
-
function copyToClipboard(text) {
|
|
41
|
-
try {
|
|
42
|
-
execSync(`echo ${JSON.stringify(text)} | pbcopy`)
|
|
43
|
-
return true
|
|
44
|
-
} catch { return false }
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function ask(rl, question) {
|
|
48
|
-
return new Promise(resolve => rl.question(question, resolve))
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function parseCode(input) {
|
|
52
|
-
input = input.trim()
|
|
53
|
-
// Full URL: http://localhost/callback?code=XXX&state=YYY
|
|
54
|
-
try {
|
|
55
|
-
const url = new URL(input)
|
|
56
|
-
const code = url.searchParams.get('code')
|
|
57
|
-
if (code) return code.split('#')[0]
|
|
58
|
-
} catch {}
|
|
59
|
-
// code#state format (platform.claude.com)
|
|
60
|
-
if (input.includes('#')) return input.split('#')[0]
|
|
61
|
-
// Raw code
|
|
62
|
-
return input.split('?')[0].split('&')[0]
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// ── Token exchange ───────────────────────────────────────────────────────────
|
|
66
|
-
|
|
67
|
-
async function exchangeCode({ code, verifier, state, redirectUri }) {
|
|
68
|
-
const body = {
|
|
69
|
-
grant_type: 'authorization_code',
|
|
70
|
-
client_id: CLIENT_ID,
|
|
71
|
-
code,
|
|
72
|
-
redirect_uri: redirectUri,
|
|
73
|
-
code_verifier: verifier,
|
|
74
|
-
state,
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
for (const endpoint of TOKEN_ENDPOINTS) {
|
|
78
|
-
process.stdout.write(` → POST ${endpoint} ... `)
|
|
79
|
-
const res = await fetch(endpoint, {
|
|
80
|
-
method: 'POST',
|
|
81
|
-
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
|
|
82
|
-
body: JSON.stringify(body),
|
|
83
|
-
})
|
|
84
|
-
const data = await res.json()
|
|
85
|
-
console.log(`HTTP ${res.status}`)
|
|
86
|
-
|
|
87
|
-
if (res.ok) return { endpoint, data }
|
|
88
|
-
console.log(` error: ${JSON.stringify(data?.error || data)}`)
|
|
89
|
-
}
|
|
90
|
-
return null
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// ── Main ─────────────────────────────────────────────────────────────────────
|
|
94
|
-
|
|
95
|
-
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
|
96
|
-
|
|
97
|
-
console.log('\n╔══════════════════════════════════════════╗')
|
|
98
|
-
console.log('║ Claude OAuth Test — PPM ║')
|
|
99
|
-
console.log('╚══════════════════════════════════════════╝\n')
|
|
100
|
-
|
|
101
|
-
// Pick redirect URI
|
|
102
|
-
console.log('Redirect URI options:')
|
|
103
|
-
REDIRECT_URI_OPTIONS.forEach((o, i) => console.log(` ${i + 1}. ${o.label}`))
|
|
104
|
-
const choice = parseInt(await ask(rl, '\nChọn (1-3) [default: 1]: ') || '1') - 1
|
|
105
|
-
const { uri: redirectUri, label } = REDIRECT_URI_OPTIONS[choice] || REDIRECT_URI_OPTIONS[0]
|
|
106
|
-
console.log(`✓ Using: ${redirectUri}\n`)
|
|
107
|
-
|
|
108
|
-
// Generate PKCE + URL
|
|
109
|
-
const { verifier, challenge, state } = generatePKCE()
|
|
110
|
-
console.log(`code_verifier length: ${verifier.length} chars`)
|
|
111
|
-
|
|
112
|
-
const params = new URLSearchParams({
|
|
113
|
-
client_id: CLIENT_ID,
|
|
114
|
-
response_type: 'code',
|
|
115
|
-
redirect_uri: redirectUri,
|
|
116
|
-
scope: SCOPE,
|
|
117
|
-
code_challenge: challenge,
|
|
118
|
-
code_challenge_method: 'S256',
|
|
119
|
-
state,
|
|
120
|
-
prompt: 'login',
|
|
121
|
-
})
|
|
122
|
-
// platform.claude.com redirect uses special `code=true` trigger
|
|
123
|
-
if (redirectUri.includes('platform.claude.com')) {
|
|
124
|
-
params.set('code', 'true')
|
|
125
|
-
}
|
|
126
|
-
const authUrl = `https://claude.ai/oauth/authorize?${params}`
|
|
127
|
-
|
|
128
|
-
console.log('\n┌─ Authorization URL ──────────────────────────────────────────────')
|
|
129
|
-
console.log(`│ ${authUrl}`)
|
|
130
|
-
console.log('└─────────────────────────────────────────────────────────────────\n')
|
|
131
|
-
|
|
132
|
-
const copied = copyToClipboard(authUrl)
|
|
133
|
-
if (copied) console.log('✓ Copied to clipboard (pbcopy)\n')
|
|
134
|
-
|
|
135
|
-
console.log('Mở URL trong browser → authorize → copy code/URL trả về\n')
|
|
136
|
-
if (label.includes('platform')) {
|
|
137
|
-
console.log(' platform.claude.com sẽ hiện code dạng: XXXX#STATE')
|
|
138
|
-
} else {
|
|
139
|
-
console.log(' Browser sẽ redirect về localhost (lỗi kết nối là bình thường)')
|
|
140
|
-
console.log(' Copy toàn bộ URL từ address bar: http://localhost.../callback?code=XXX')
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const input = await ask(rl, '\nPaste code/URL: ')
|
|
144
|
-
const code = parseCode(input)
|
|
145
|
-
if (!code) { console.error('❌ Không parse được code'); process.exit(1) }
|
|
146
|
-
console.log(`✓ code = ${code.slice(0, 20)}...\n`)
|
|
147
|
-
|
|
148
|
-
// Exchange
|
|
149
|
-
console.log('Thử exchange token:')
|
|
150
|
-
const result = await exchangeCode({ code, verifier, state, redirectUri })
|
|
151
|
-
|
|
152
|
-
if (result) {
|
|
153
|
-
const { endpoint, data } = result
|
|
154
|
-
console.log(`\n✅ Success! Endpoint: ${endpoint}`)
|
|
155
|
-
console.log('\n── Token info ──────────────────────────────────────')
|
|
156
|
-
console.log(` access_token: ${data.access_token?.slice(0, 30)}...`)
|
|
157
|
-
console.log(` refresh_token: ${data.refresh_token?.slice(0, 30)}...`)
|
|
158
|
-
console.log(` token_type: ${data.token_type}`)
|
|
159
|
-
console.log(` expires_in: ${data.expires_in}s`)
|
|
160
|
-
if (data.account) {
|
|
161
|
-
console.log(` email: ${data.account.email_address}`)
|
|
162
|
-
console.log(` account_uuid: ${data.account.uuid}`)
|
|
163
|
-
}
|
|
164
|
-
if (data.organization) {
|
|
165
|
-
console.log(` org_name: ${data.organization.name}`)
|
|
166
|
-
console.log(` org_uuid: ${data.organization.uuid}`)
|
|
167
|
-
}
|
|
168
|
-
console.log('────────────────────────────────────────────────────')
|
|
169
|
-
console.log('\nFull response:')
|
|
170
|
-
console.log(JSON.stringify(data, null, 2))
|
|
171
|
-
} else {
|
|
172
|
-
console.log('\n❌ Tất cả endpoints đều fail')
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
rl.close()
|
package/test-verify-oat.mjs
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
/**
|
|
3
|
-
* Test verify sk-ant-oat token — see what APIs return
|
|
4
|
-
* Usage: bun test-verify-oat.mjs [TOKEN]
|
|
5
|
-
* If no TOKEN arg, reads TEST_OAUTH_TOKEN_1 from .env.test
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { readFileSync } from "node:fs";
|
|
9
|
-
import { resolve, dirname } from "node:path";
|
|
10
|
-
import { fileURLToPath } from "node:url";
|
|
11
|
-
|
|
12
|
-
const __dir = dirname(fileURLToPath(import.meta.url));
|
|
13
|
-
|
|
14
|
-
// Token from CLI arg or .env.test
|
|
15
|
-
let token = process.argv[2];
|
|
16
|
-
if (!token) {
|
|
17
|
-
const envPath = resolve(__dir, ".env.test");
|
|
18
|
-
const env = Object.fromEntries(
|
|
19
|
-
readFileSync(envPath, "utf-8")
|
|
20
|
-
.split("\n")
|
|
21
|
-
.filter((l) => l && !l.startsWith("#"))
|
|
22
|
-
.map((l) => l.split("=").map((s) => s.trim()))
|
|
23
|
-
.filter(([k, v]) => k && v),
|
|
24
|
-
);
|
|
25
|
-
token = env.TEST_OAUTH_TOKEN_1;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (!token || token.startsWith("REPLACE")) {
|
|
29
|
-
console.error("Usage: bun test-verify-oat.mjs [TOKEN]");
|
|
30
|
-
process.exit(1);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
console.log(`Token: ${token.slice(0, 30)}...`);
|
|
34
|
-
|
|
35
|
-
// ── Helper ──────────────────────────────────────────────
|
|
36
|
-
async function tryEndpoint({ label, url, method = "GET", headers = {}, body }) {
|
|
37
|
-
console.log(`\n── ${label} ──`);
|
|
38
|
-
try {
|
|
39
|
-
const opts = {
|
|
40
|
-
method,
|
|
41
|
-
headers: {
|
|
42
|
-
Accept: "application/json",
|
|
43
|
-
Authorization: `Bearer ${token}`,
|
|
44
|
-
...headers,
|
|
45
|
-
},
|
|
46
|
-
signal: AbortSignal.timeout(10_000),
|
|
47
|
-
};
|
|
48
|
-
if (body) opts.body = typeof body === "string" ? body : JSON.stringify(body);
|
|
49
|
-
const res = await fetch(url, opts);
|
|
50
|
-
const text = await res.text();
|
|
51
|
-
console.log(`HTTP ${res.status}`);
|
|
52
|
-
try {
|
|
53
|
-
console.log(JSON.stringify(JSON.parse(text), null, 2));
|
|
54
|
-
} catch {
|
|
55
|
-
console.log(text.slice(0, 500));
|
|
56
|
-
}
|
|
57
|
-
} catch (e) {
|
|
58
|
-
console.log(`ERROR: ${e.message}`);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// ── 1. claude auth status (CLI) ─────────────────────────
|
|
63
|
-
console.log("\n── claude auth status (CLI) ──");
|
|
64
|
-
try {
|
|
65
|
-
const proc = Bun.spawn(["claude", "auth", "status"], {
|
|
66
|
-
env: { ...process.env, CLAUDE_CODE_OAUTH_TOKEN: token, ANTHROPIC_API_KEY: "" },
|
|
67
|
-
stdout: "pipe",
|
|
68
|
-
stderr: "pipe",
|
|
69
|
-
});
|
|
70
|
-
const stdout = await new Response(proc.stdout).text();
|
|
71
|
-
const stderr = await new Response(proc.stderr).text();
|
|
72
|
-
await proc.exited;
|
|
73
|
-
console.log("stdout:", stdout);
|
|
74
|
-
if (stderr) console.log("stderr:", stderr);
|
|
75
|
-
} catch (e) {
|
|
76
|
-
console.log(`ERROR: ${e.message}`);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// ── 2. /api/oauth/usage ─────────────────────────────────
|
|
80
|
-
await tryEndpoint({
|
|
81
|
-
label: "GET /api/oauth/usage",
|
|
82
|
-
url: "https://api.anthropic.com/api/oauth/usage",
|
|
83
|
-
headers: { "anthropic-beta": "oauth-2025-04-20", "User-Agent": "ppm/1.0" },
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
// ── 3. /v1/organizations (org info) ─────────────────────
|
|
87
|
-
await tryEndpoint({
|
|
88
|
-
label: "GET /v1/organizations",
|
|
89
|
-
url: "https://api.anthropic.com/v1/organizations",
|
|
90
|
-
headers: { "anthropic-version": "2023-06-01" },
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// ── 4. /api/auth/session (claude.ai session) ────────────
|
|
94
|
-
await tryEndpoint({
|
|
95
|
-
label: "GET /api/auth/session (claude.ai)",
|
|
96
|
-
url: "https://claude.ai/api/auth/session",
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
// ── 5. /v1/messages (minimal — test if token works) ─────
|
|
100
|
-
await tryEndpoint({
|
|
101
|
-
label: "POST /v1/messages (1 token test)",
|
|
102
|
-
url: "https://api.anthropic.com/v1/messages",
|
|
103
|
-
method: "POST",
|
|
104
|
-
headers: { "Content-Type": "application/json", "anthropic-version": "2023-06-01" },
|
|
105
|
-
body: { model: "claude-sonnet-4-20250514", max_tokens: 1, messages: [{ role: "user", content: "hi" }] },
|
|
106
|
-
});
|