@seavideo/cli 0.0.0-stage → 0.3.0
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/LICENSE +12 -0
- package/README.md +528 -2
- package/dist/bin/seavideo.js +11 -0
- package/dist/src/atomic-write.js +14 -0
- package/dist/src/canvas/api.js +172 -0
- package/dist/src/canvas/browser.js +40 -0
- package/dist/src/canvas/chrome-binary.js +109 -0
- package/dist/src/canvas/daemon.js +264 -0
- package/dist/src/canvas/engine-install.js +46 -0
- package/dist/src/canvas/engine-log.js +9 -0
- package/dist/src/canvas/engine.js +99 -0
- package/dist/src/canvas/entry.js +16 -0
- package/dist/src/canvas/env-lock.js +164 -0
- package/dist/src/canvas/launch.js +97 -0
- package/dist/src/canvas/lifecycle.js +65 -0
- package/dist/src/canvas/names.js +61 -0
- package/dist/src/canvas/params.js +38 -0
- package/dist/src/canvas/puppeteer.js +153 -0
- package/dist/src/canvas/screenshot.js +23 -0
- package/dist/src/canvas/session-record.js +77 -0
- package/dist/src/canvas/team.js +97 -0
- package/dist/src/canvas/tool-args.js +47 -0
- package/dist/src/canvas/tool-session.js +165 -0
- package/dist/src/canvas/tool-specs.js +54 -0
- package/dist/src/canvas/url.js +38 -0
- package/dist/src/cli/args.js +16 -0
- package/dist/src/cli/commands/auth.js +51 -0
- package/dist/src/cli/commands/canvas/common.js +31 -0
- package/dist/src/cli/commands/canvas/engine-choice.js +41 -0
- package/dist/src/cli/commands/canvas/index.js +5 -0
- package/dist/src/cli/commands/canvas/item.js +233 -0
- package/dist/src/cli/commands/canvas/list.js +168 -0
- package/dist/src/cli/commands/canvas/register.js +106 -0
- package/dist/src/cli/commands/canvas/render.js +229 -0
- package/dist/src/cli/commands/canvas/screenshot.js +52 -0
- package/dist/src/cli/commands/canvas/session.js +249 -0
- package/dist/src/cli/commands/canvas/teams.js +142 -0
- package/dist/src/cli/commands/config.js +74 -0
- package/dist/src/cli/commands/doctor.js +261 -0
- package/dist/src/cli/commands/engine.js +96 -0
- package/dist/src/cli/commands/env/create.js +160 -0
- package/dist/src/cli/commands/env/fields.js +26 -0
- package/dist/src/cli/commands/env/index.js +4 -0
- package/dist/src/cli/commands/env/list.js +44 -0
- package/dist/src/cli/commands/env/mutate.js +118 -0
- package/dist/src/cli/commands/login/flow-token.js +41 -0
- package/dist/src/cli/commands/login/index.js +12 -0
- package/dist/src/cli/commands/login/oauth.js +69 -0
- package/dist/src/cli/commands/login/save.js +26 -0
- package/dist/src/cli/commands/logout.js +92 -0
- package/dist/src/cli/commands/skill.js +185 -0
- package/dist/src/cli/commands/tool.js +279 -0
- package/dist/src/cli/commands/update.js +117 -0
- package/dist/src/cli/commands/wallet.js +69 -0
- package/dist/src/cli/commands/whoami.js +147 -0
- package/dist/src/cli/context.js +41 -0
- package/dist/src/cli/credential.js +93 -0
- package/dist/src/cli/elapsed.js +25 -0
- package/dist/src/cli/failure-screenshot.js +24 -0
- package/dist/src/cli/flags.js +1 -0
- package/dist/src/cli/hints.js +7 -0
- package/dist/src/cli/main.js +44 -0
- package/dist/src/cli/output-error.js +23 -0
- package/dist/src/cli/output.js +47 -0
- package/dist/src/cli/program.js +261 -0
- package/dist/src/cli/run.js +11 -0
- package/dist/src/cli/session.js +63 -0
- package/dist/src/config/base-url.js +26 -0
- package/dist/src/config/credential-vault.js +115 -0
- package/dist/src/config/dotenv.js +59 -0
- package/dist/src/config/envname.js +41 -0
- package/dist/src/config/paths.js +22 -0
- package/dist/src/config/preset-schema.js +67 -0
- package/dist/src/config/presets.js +132 -0
- package/dist/src/config/resolve.js +41 -0
- package/dist/src/config/secret-key.js +63 -0
- package/dist/src/config/store-schema.js +143 -0
- package/dist/src/config/store.js +273 -0
- package/dist/src/config/vault-crypto.js +44 -0
- package/dist/src/deps.js +191 -0
- package/dist/src/deps.types.js +1 -0
- package/dist/src/errors.js +24 -0
- package/dist/src/fs-retry.js +34 -0
- package/dist/src/i18n.js +1214 -0
- package/dist/src/loopback.js +134 -0
- package/dist/src/sdk/auth.js +20 -0
- package/dist/src/sdk/credential-expiry.js +67 -0
- package/dist/src/sdk/discovery.js +90 -0
- package/dist/src/sdk/http.js +98 -0
- package/dist/src/sdk/identity.js +17 -0
- package/dist/src/sdk/jwks.js +119 -0
- package/dist/src/sdk/oauth-protocol.js +226 -0
- package/dist/src/sdk/oauth.js +179 -0
- package/dist/src/sdk/provider/cn.js +34 -0
- package/dist/src/sdk/provider/index.js +16 -0
- package/dist/src/sdk/provider/oversea.js +41 -0
- package/dist/src/sdk/provider/types.js +1 -0
- package/dist/src/sdk/refresh.js +66 -0
- package/dist/src/semver.js +89 -0
- package/dist/src/skill/api.js +176 -0
- package/dist/src/text.js +3 -0
- package/dist/src/types.js +1 -0
- package/dist/src/update/check-cache.js +56 -0
- package/dist/src/update/install-layout.js +79 -0
- package/dist/src/update/notify.js +15 -0
- package/dist/src/update/npm-install.js +64 -0
- package/dist/src/update/passive.js +53 -0
- package/dist/src/update/registry.js +59 -0
- package/dist/src/wallet/api.js +107 -0
- package/package.json +67 -4
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { createServer } from 'node:http';
|
|
2
|
+
export const LOOPBACK_TIMEOUT = 'ELOOPBACKTIMEOUT';
|
|
3
|
+
export const LOOPBACK_CLOSED = 'ELOOPBACKCLOSED';
|
|
4
|
+
function codedError(code) {
|
|
5
|
+
return Object.assign(new Error(code), { code });
|
|
6
|
+
}
|
|
7
|
+
export function decideCallback(input) {
|
|
8
|
+
let result;
|
|
9
|
+
if (input.method !== 'GET') {
|
|
10
|
+
result = { type: 'reject', status: 405 };
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
let url = null;
|
|
14
|
+
try {
|
|
15
|
+
url = new URL(input.url ?? '/', 'http://127.0.0.1');
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
url = null;
|
|
19
|
+
}
|
|
20
|
+
if (url === null) {
|
|
21
|
+
result = { type: 'reject', status: 400 };
|
|
22
|
+
}
|
|
23
|
+
else if (url.pathname !== input.path) {
|
|
24
|
+
result = { type: 'reject', status: 404 };
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const query = {};
|
|
28
|
+
let duplicated = false;
|
|
29
|
+
for (const [key, value] of url.searchParams) {
|
|
30
|
+
if (Object.hasOwn(query, key)) {
|
|
31
|
+
duplicated = true;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
query[key] = value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (duplicated) {
|
|
39
|
+
result = { type: 'reject', status: 400 };
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
result = { type: 'deliver', query };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
function respondTo(res, answer) {
|
|
49
|
+
if (answer.location !== undefined) {
|
|
50
|
+
res
|
|
51
|
+
.writeHead(303, {
|
|
52
|
+
location: answer.location,
|
|
53
|
+
'cache-control': 'no-store',
|
|
54
|
+
'referrer-policy': 'no-referrer',
|
|
55
|
+
'content-length': '0',
|
|
56
|
+
})
|
|
57
|
+
.end();
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
res
|
|
61
|
+
.writeHead(answer.status, {
|
|
62
|
+
'content-type': 'text/html; charset=utf-8',
|
|
63
|
+
'cache-control': 'no-store',
|
|
64
|
+
'referrer-policy': 'no-referrer',
|
|
65
|
+
})
|
|
66
|
+
.end(answer.body ?? '');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export async function startLoopbackServer(options) {
|
|
70
|
+
let pending = null;
|
|
71
|
+
let delivered = false;
|
|
72
|
+
let closed = false;
|
|
73
|
+
const server = createServer((req, res) => {
|
|
74
|
+
const decision = decideCallback({ method: req.method, url: req.url, path: options.path });
|
|
75
|
+
if (decision.type === 'reject') {
|
|
76
|
+
res.writeHead(decision.status, { 'content-length': '0' }).end();
|
|
77
|
+
}
|
|
78
|
+
else if (delivered || pending === null) {
|
|
79
|
+
res.writeHead(409, { 'content-length': '0' }).end();
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
delivered = true;
|
|
83
|
+
const waiter = pending;
|
|
84
|
+
pending = null;
|
|
85
|
+
waiter.resolve({ query: decision.query, respond: (answer) => respondTo(res, answer) });
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
await new Promise((resolve, reject) => {
|
|
89
|
+
const onError = (err) => reject(err);
|
|
90
|
+
server.once('error', onError);
|
|
91
|
+
server.listen(0, '127.0.0.1', () => {
|
|
92
|
+
server.removeListener('error', onError);
|
|
93
|
+
resolve();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
const { port } = server.address();
|
|
97
|
+
function settlePending(err) {
|
|
98
|
+
const waiter = pending;
|
|
99
|
+
pending = null;
|
|
100
|
+
if (waiter !== null)
|
|
101
|
+
waiter.reject(err);
|
|
102
|
+
else {
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async function close() {
|
|
106
|
+
if (closed)
|
|
107
|
+
return;
|
|
108
|
+
closed = true;
|
|
109
|
+
clearTimeout(timer);
|
|
110
|
+
settlePending(codedError(LOOPBACK_CLOSED));
|
|
111
|
+
await new Promise((resolve) => {
|
|
112
|
+
server.close(() => resolve());
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
const timer = setTimeout(() => {
|
|
116
|
+
closed = true;
|
|
117
|
+
settlePending(codedError(LOOPBACK_TIMEOUT));
|
|
118
|
+
server.close();
|
|
119
|
+
}, options.timeoutMs);
|
|
120
|
+
timer.unref();
|
|
121
|
+
return {
|
|
122
|
+
redirectUri: `http://127.0.0.1:${port}${options.path}`,
|
|
123
|
+
next: () => new Promise((resolve, reject) => {
|
|
124
|
+
if (closed || delivered) {
|
|
125
|
+
reject(codedError(LOOPBACK_CLOSED));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
}
|
|
130
|
+
pending = { resolve, reject };
|
|
131
|
+
}),
|
|
132
|
+
close,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { CliError } from "../errors.js";
|
|
2
|
+
import { requestJson } from "./http.js";
|
|
3
|
+
import { t } from "../i18n.js";
|
|
4
|
+
export const PROFILE_PATH = '/api/craft/v1/auth/me';
|
|
5
|
+
export function assertCredentialCharset(credential, envMap = process.env) {
|
|
6
|
+
if (!/^[\x21-\x7e]+$/.test(credential.value)) {
|
|
7
|
+
throw new CliError('usage', t('error.badCredentialCharset', {}, envMap), {
|
|
8
|
+
detail: t('error.badCredentialCharsetDetail', {}, envMap),
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export async function fetchProfile(provider, credential, deps) {
|
|
13
|
+
assertCredentialCharset(credential, deps.env);
|
|
14
|
+
const raw = await requestJson(deps, {
|
|
15
|
+
method: 'GET',
|
|
16
|
+
path: PROFILE_PATH,
|
|
17
|
+
headers: provider.authHeaders(credential),
|
|
18
|
+
});
|
|
19
|
+
return provider.normalizeProfile(raw);
|
|
20
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
function payloadOf(token) {
|
|
2
|
+
const parts = token.split('.');
|
|
3
|
+
let result;
|
|
4
|
+
if (parts.length !== 3) {
|
|
5
|
+
result = null;
|
|
6
|
+
}
|
|
7
|
+
else {
|
|
8
|
+
result = decodeSegment(parts[1]);
|
|
9
|
+
}
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
function decodeSegment(segment) {
|
|
13
|
+
let parsed;
|
|
14
|
+
try {
|
|
15
|
+
parsed = JSON.parse(Buffer.from(segment, 'base64url').toString('utf8'));
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
parsed = null;
|
|
19
|
+
}
|
|
20
|
+
let result;
|
|
21
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
22
|
+
result = null;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
result = parsed;
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
export function credentialExpiry(token, nowMs, marginMs = 0) {
|
|
30
|
+
const payload = payloadOf(token);
|
|
31
|
+
let result;
|
|
32
|
+
if (payload === null) {
|
|
33
|
+
result = null;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
result = expiryOfClaim(payload['exp'], nowMs, marginMs);
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
function expiryOfClaim(claim, nowMs, marginMs) {
|
|
41
|
+
let result;
|
|
42
|
+
if (typeof claim !== 'number' || !Number.isFinite(claim)) {
|
|
43
|
+
result = null;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
const expiredAt = new Date(claim * 1000);
|
|
47
|
+
result = { expired: nowMs + marginMs >= claim * 1000, expiredAt };
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
export function credentialUnrecoverable(token, refreshToken, nowMs) {
|
|
52
|
+
const access = credentialExpiry(token, nowMs);
|
|
53
|
+
const accessExpired = access !== null && access.expired;
|
|
54
|
+
const recoverable = !accessExpired || hasUsableRefresh(refreshToken, nowMs);
|
|
55
|
+
return accessExpired && !recoverable;
|
|
56
|
+
}
|
|
57
|
+
function hasUsableRefresh(refreshToken, nowMs) {
|
|
58
|
+
let result;
|
|
59
|
+
if (refreshToken === null || refreshToken === undefined || refreshToken === '') {
|
|
60
|
+
result = false;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
const refresh = credentialExpiry(refreshToken, nowMs);
|
|
64
|
+
result = refresh === null || !refresh.expired;
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { CliError } from "../errors.js";
|
|
2
|
+
import { t } from "../i18n.js";
|
|
3
|
+
import { safeErrorText } from "./http.js";
|
|
4
|
+
function trimSlash(value) {
|
|
5
|
+
return value.replace(/\/+$/, '');
|
|
6
|
+
}
|
|
7
|
+
function requireEndpoint(value, field, envMap) {
|
|
8
|
+
if (typeof value !== 'string' || value === '') {
|
|
9
|
+
throw new CliError('server', t('discovery.missingEndpoint', { field }, envMap));
|
|
10
|
+
}
|
|
11
|
+
let url;
|
|
12
|
+
try {
|
|
13
|
+
url = new URL(value);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
throw new CliError('server', t('discovery.missingEndpoint', { field }, envMap));
|
|
17
|
+
}
|
|
18
|
+
if (url.protocol !== 'https:') {
|
|
19
|
+
throw new CliError('server', t('discovery.missingEndpoint', { field }, envMap));
|
|
20
|
+
}
|
|
21
|
+
return url.href;
|
|
22
|
+
}
|
|
23
|
+
function optionalEndpoint(value) {
|
|
24
|
+
let result;
|
|
25
|
+
if (typeof value !== 'string' || value === '') {
|
|
26
|
+
result = null;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
try {
|
|
30
|
+
const url = new URL(value);
|
|
31
|
+
result = url.protocol === 'https:' ? url.href : null;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
result = null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
export async function fetchDiscovery(deps, issuer, timeoutMs = 20_000) {
|
|
40
|
+
const url = `${trimSlash(issuer)}/.well-known/openid-configuration`;
|
|
41
|
+
let response;
|
|
42
|
+
try {
|
|
43
|
+
response = await deps.fetch(url, {
|
|
44
|
+
headers: { accept: 'application/json' },
|
|
45
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
throw new CliError('network', t('discovery.network', { url }, deps.env), {
|
|
50
|
+
detail: safeErrorText(err),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
let text;
|
|
54
|
+
try {
|
|
55
|
+
text = await response.text();
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
throw new CliError('network', t('discovery.network', { url }, deps.env), {
|
|
59
|
+
detail: safeErrorText(err),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
throw new CliError('server', t('discovery.status', { status: response.status, url }, deps.env));
|
|
64
|
+
}
|
|
65
|
+
let payload;
|
|
66
|
+
try {
|
|
67
|
+
payload = text === '' ? undefined : JSON.parse(text);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
throw new CliError('server', t('discovery.notJson', { url }, deps.env));
|
|
71
|
+
}
|
|
72
|
+
if (payload === null || typeof payload !== 'object' || Array.isArray(payload)) {
|
|
73
|
+
throw new CliError('server', t('discovery.notObject', { url }, deps.env));
|
|
74
|
+
}
|
|
75
|
+
const body = payload;
|
|
76
|
+
const declared = body['issuer'];
|
|
77
|
+
if (typeof declared !== 'string' || trimSlash(declared) !== trimSlash(issuer)) {
|
|
78
|
+
throw new CliError('server', t('discovery.issuerMismatch', { url }, deps.env), {
|
|
79
|
+
detail: t('discovery.issuerMismatchDetail', { expected: trimSlash(issuer) }, deps.env),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
issuer: trimSlash(issuer),
|
|
84
|
+
authorizationEndpoint: requireEndpoint(body['authorization_endpoint'], 'authorization_endpoint', deps.env),
|
|
85
|
+
tokenEndpoint: requireEndpoint(body['token_endpoint'], 'token_endpoint', deps.env),
|
|
86
|
+
jwksUri: requireEndpoint(body['jwks_uri'], 'jwks_uri', deps.env),
|
|
87
|
+
userinfoEndpoint: optionalEndpoint(body['userinfo_endpoint']),
|
|
88
|
+
revocationEndpoint: optionalEndpoint(body['revocation_endpoint']),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { CliError } from "../errors.js";
|
|
2
|
+
import { t } from "../i18n.js";
|
|
3
|
+
import { sanitizeText } from "../text.js";
|
|
4
|
+
export const SUCCESS_CODE = 10000;
|
|
5
|
+
export const CANVAS_SUCCESS_CODE = 200;
|
|
6
|
+
export const SKILL_SUCCESS_CODE = 'ok';
|
|
7
|
+
const DEFAULT_TIMEOUT_MS = 20_000;
|
|
8
|
+
function isTimeoutLike(err) {
|
|
9
|
+
return err instanceof Error && (err.name === 'TimeoutError' || err.name === 'AbortError');
|
|
10
|
+
}
|
|
11
|
+
export function safeErrorText(err) {
|
|
12
|
+
let result;
|
|
13
|
+
if (!(err instanceof Error)) {
|
|
14
|
+
result = 'unknown error';
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
const code = err.code;
|
|
18
|
+
result = code === undefined ? err.name : `${err.name} (${code})`;
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
function serverText(envelope) {
|
|
23
|
+
const text = envelope.message ?? envelope.msg;
|
|
24
|
+
return text === undefined ? undefined : sanitizeText(text);
|
|
25
|
+
}
|
|
26
|
+
export async function requestJson(deps, options) {
|
|
27
|
+
const url = `${deps.baseUrl.replace(/\/+$/, '')}${options.path}`;
|
|
28
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
29
|
+
let response;
|
|
30
|
+
try {
|
|
31
|
+
response = await deps.fetch(url, {
|
|
32
|
+
method: options.method,
|
|
33
|
+
headers: {
|
|
34
|
+
accept: 'application/json',
|
|
35
|
+
...(options.body === undefined ? {} : { 'content-type': 'application/json' }),
|
|
36
|
+
...options.headers,
|
|
37
|
+
},
|
|
38
|
+
...(options.body === undefined ? {} : { body: JSON.stringify(options.body) }),
|
|
39
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
if (isTimeoutLike(err)) {
|
|
44
|
+
throw new CliError('timeout', t('http.timeout', { ms: timeoutMs, url }, deps.env));
|
|
45
|
+
}
|
|
46
|
+
throw new CliError('network', t('http.network', { url }, deps.env), {
|
|
47
|
+
detail: safeErrorText(err),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const requestId = response.headers.get('x-request-id') ?? undefined;
|
|
51
|
+
if (response.status === 401) {
|
|
52
|
+
throw new CliError('auth', t('http.auth', {}, deps.env), { detail: requestId });
|
|
53
|
+
}
|
|
54
|
+
let text;
|
|
55
|
+
try {
|
|
56
|
+
text = await response.text();
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
if (isTimeoutLike(err)) {
|
|
60
|
+
throw new CliError('timeout', t('http.timeout', { ms: timeoutMs, url }, deps.env));
|
|
61
|
+
}
|
|
62
|
+
throw new CliError('network', t('http.readFailed', { url }, deps.env), {
|
|
63
|
+
detail: safeErrorText(err),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
let payload;
|
|
67
|
+
try {
|
|
68
|
+
payload = text === '' ? undefined : JSON.parse(text);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
throw new CliError('server', t('http.notJson', { status: response.status, url }, deps.env), {
|
|
72
|
+
detail: requestId,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (payload === undefined) {
|
|
76
|
+
throw new CliError('server', t('http.emptyBody', { url }, deps.env), { detail: requestId });
|
|
77
|
+
}
|
|
78
|
+
if (payload === null || typeof payload !== 'object' || Array.isArray(payload)) {
|
|
79
|
+
throw new CliError('server', t('http.notObject', { url }, deps.env), { detail: requestId });
|
|
80
|
+
}
|
|
81
|
+
const envelope = payload;
|
|
82
|
+
const code = envelope.code;
|
|
83
|
+
const rid = envelope.request_id ?? requestId;
|
|
84
|
+
if (code === 20000) {
|
|
85
|
+
throw new CliError('auth', t('http.auth', {}, deps.env), { detail: rid });
|
|
86
|
+
}
|
|
87
|
+
if (!response.ok) {
|
|
88
|
+
throw new CliError('server', t('http.serverStatus', { status: response.status }, deps.env), {
|
|
89
|
+
detail: [rid, serverText(envelope)].filter(Boolean).join(' · ') || undefined,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (code !== undefined && code !== (options.successCode ?? SUCCESS_CODE)) {
|
|
93
|
+
throw new CliError('server', t('http.serverCode', { code, message: serverText(envelope) ?? '' }, deps.env), {
|
|
94
|
+
detail: rid,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return envelope.data;
|
|
98
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CliError } from "../errors.js";
|
|
2
|
+
import { CANVAS_SUCCESS_CODE, requestJson } from "./http.js";
|
|
3
|
+
import { t } from "../i18n.js";
|
|
4
|
+
export const FLOW_IDENTITY_PATH = '/api/user/profile';
|
|
5
|
+
export async function fetchIdentity(deps, token) {
|
|
6
|
+
const data = await requestJson(deps, {
|
|
7
|
+
method: 'GET',
|
|
8
|
+
path: FLOW_IDENTITY_PATH,
|
|
9
|
+
headers: { authorization: `Bearer ${token}` },
|
|
10
|
+
successCode: CANVAS_SUCCESS_CODE,
|
|
11
|
+
});
|
|
12
|
+
const userId = data?.['user_id'];
|
|
13
|
+
if (typeof userId !== 'string' || userId === '') {
|
|
14
|
+
throw new CliError('server', t('identity.missingUserId', {}, deps.env));
|
|
15
|
+
}
|
|
16
|
+
return { userId, displayName: null, raw: data };
|
|
17
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { CliError } from "../errors.js";
|
|
2
|
+
import { t } from "../i18n.js";
|
|
3
|
+
import { safeErrorText } from "./http.js";
|
|
4
|
+
function decodeSection(section) {
|
|
5
|
+
let result;
|
|
6
|
+
try {
|
|
7
|
+
result = JSON.parse(Buffer.from(section, 'base64url').toString('utf8'));
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
result = null;
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
export function decodeJwtHeader(token) {
|
|
15
|
+
const parts = token.split('.');
|
|
16
|
+
let result;
|
|
17
|
+
if (parts.length !== 3) {
|
|
18
|
+
result = null;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
const header = decodeSection(parts[0]);
|
|
22
|
+
if (header === null || typeof header !== 'object' || Array.isArray(header)) {
|
|
23
|
+
result = null;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
result = header;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
export async function fetchJwks(deps, jwksUri, timeoutMs = 20_000) {
|
|
32
|
+
let response;
|
|
33
|
+
try {
|
|
34
|
+
response = await deps.fetch(jwksUri, {
|
|
35
|
+
headers: { accept: 'application/json' },
|
|
36
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
throw new CliError('network', t('jwks.network', { url: jwksUri }, deps.env), {
|
|
41
|
+
detail: safeErrorText(err),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
let text;
|
|
45
|
+
try {
|
|
46
|
+
text = await response.text();
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
throw new CliError('network', t('jwks.network', { url: jwksUri }, deps.env), {
|
|
50
|
+
detail: safeErrorText(err),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
throw new CliError('server', t('jwks.status', { status: response.status, url: jwksUri }, deps.env));
|
|
55
|
+
}
|
|
56
|
+
let payload;
|
|
57
|
+
try {
|
|
58
|
+
payload = text === '' ? undefined : JSON.parse(text);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
throw new CliError('server', t('jwks.notJson', { url: jwksUri }, deps.env));
|
|
62
|
+
}
|
|
63
|
+
if (payload === null || typeof payload !== 'object' || Array.isArray(payload)) {
|
|
64
|
+
throw new CliError('server', t('jwks.notObject', { url: jwksUri }, deps.env));
|
|
65
|
+
}
|
|
66
|
+
const keys = payload['keys'];
|
|
67
|
+
if (!Array.isArray(keys)) {
|
|
68
|
+
throw new CliError('server', t('jwks.noKeys', { url: jwksUri }, deps.env));
|
|
69
|
+
}
|
|
70
|
+
return keys.filter((key) => typeof key === 'object' && key !== null);
|
|
71
|
+
}
|
|
72
|
+
function pickKey(keys, kid) {
|
|
73
|
+
const usable = keys.filter((key) => key.kty === 'RSA' &&
|
|
74
|
+
typeof key.n === 'string' &&
|
|
75
|
+
key.n !== '' &&
|
|
76
|
+
typeof key.e === 'string' &&
|
|
77
|
+
key.e !== '');
|
|
78
|
+
let result;
|
|
79
|
+
if (typeof kid === 'string' && kid !== '') {
|
|
80
|
+
result = usable.find((key) => key.kid === kid) ?? null;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
result = usable.length === 1 ? usable[0] : null;
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
export async function verifyIdTokenSignature(token, keys) {
|
|
88
|
+
const parts = token.split('.');
|
|
89
|
+
let result;
|
|
90
|
+
if (parts.length !== 3) {
|
|
91
|
+
result = { ok: false, reason: 'header' };
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
const header = decodeJwtHeader(token);
|
|
95
|
+
if (header === null) {
|
|
96
|
+
result = { ok: false, reason: 'header' };
|
|
97
|
+
}
|
|
98
|
+
else if (header.alg !== 'RS256') {
|
|
99
|
+
result = { ok: false, reason: 'alg' };
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const jwk = pickKey(keys, header.kid);
|
|
103
|
+
if (jwk === null) {
|
|
104
|
+
result = { ok: false, reason: 'key' };
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
const key = await globalThis.crypto.subtle.importKey('jwk', { kty: jwk.kty, n: jwk.n, e: jwk.e, alg: 'RS256', ext: true }, { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }, false, ['verify']);
|
|
108
|
+
const signingInput = new TextEncoder().encode(`${parts[0]}.${parts[1]}`);
|
|
109
|
+
const signature = Buffer.from(parts[2], 'base64url');
|
|
110
|
+
const valid = await globalThis.crypto.subtle.verify('RSASSA-PKCS1-v1_5', key, signature, signingInput);
|
|
111
|
+
result = valid ? { ok: true } : { ok: false, reason: 'bad' };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
export function signatureErrorReason(reason, envMap) {
|
|
118
|
+
return t(`jwks.reason.${reason}`, {}, envMap);
|
|
119
|
+
}
|