@tempoxyz/id 0.0.0-pr-834.sha-1d74ebf → 0.0.0-pr-831.sha-2eced5f

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.
Files changed (2) hide show
  1. package/dist/bin.js +79 -134
  2. package/package.json +1 -1
package/dist/bin.js CHANGED
@@ -1,6 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  import "./chunk-MLKGABMK.js";
3
3
 
4
+ // src/bin.ts
5
+ import * as ChildProcess from "node:child_process";
6
+
4
7
  // src/AdminApi.ts
5
8
  var timeout = 1e4;
6
9
  async function load(options) {
@@ -13,25 +16,22 @@ async function load(options) {
13
16
  });
14
17
  if (!response.ok)
15
18
  throw new Error(
16
- `The admin host at ${url2} responded to /openapi.json with ${response.status}. Set TEMPO_ID_ADMIN_TOKEN to a fresh credential, and TEMPO_ID_ADMIN_TOKEN_HEADER to the header that the admin host reads.`
19
+ `The admin host at ${url2} responded to /openapi.json with ${response.status}. Run \`tempo-id admin login\`, or set TEMPO_ID_ADMIN_TOKEN to a fresh Access token.`
17
20
  );
18
21
  const document = await response.json();
19
- const { components } = document;
20
- const schemas = components?.schemas ?? {};
21
22
  const entries = Object.entries(document.paths ?? {}).filter(
22
23
  ([path]) => !reference.includes(path)
23
24
  );
25
+ const objects = objectSchemas(document);
24
26
  const paths = Object.fromEntries(
25
27
  entries.map(([path, item]) => [
26
28
  path,
27
- withFlags(withJsonOptions(withoutSecrets(item), { schemas }))
29
+ withJsonOptions(withoutSecrets(item), { objects })
28
30
  ])
29
31
  );
30
- const fills = entries.flatMap(
31
- ([path, item]) => secretFields(item, { path, schemas })
32
- );
32
+ const fills = entries.flatMap(([path, item]) => secretFields(path, item));
33
33
  const parses = entries.flatMap(
34
- ([path, item]) => jsonFields(item, { path, schemas })
34
+ ([path, item]) => jsonFields(item, { objects, path })
35
35
  );
36
36
  const send = async (request) => {
37
37
  const answer = await fetch(
@@ -42,12 +42,12 @@ async function load(options) {
42
42
  );
43
43
  if (answer.status >= 300 && answer.status < 400)
44
44
  throw new Error(
45
- `The admin host at ${url2} redirected the request, such as to a login page. Set TEMPO_ID_ADMIN_TOKEN to a fresh credential.`
45
+ `The admin host at ${url2} redirected the request, such as to the Access login. Run \`tempo-id admin login\`, or set TEMPO_ID_ADMIN_TOKEN to a fresh Access token.`
46
46
  );
47
47
  return answer;
48
48
  };
49
49
  return {
50
- description: "Call Tempo ID's Admin API, one command per route. Set TEMPO_ID_ADMIN_URL to the admin host, and TEMPO_ID_ADMIN_TOKEN and TEMPO_ID_ADMIN_TOKEN_HEADER to its credential. TEMPO_ID_CLIENT_SECRET and TEMPO_ID_CLOUD_OWNER_APP_SECRET fill the secrets that no option takes.",
50
+ description: "Call Tempo ID's Admin API, one command per route. Run `tempo-id admin login` first, or set TEMPO_ID_ADMIN_TOKEN to a Cloudflare Access token. PRIVY_APP_SECRET and TEMPO_ID_CLIENT_SECRET fill the secrets that no option takes.",
51
51
  // Each request goes to the admin host, whatever origin incur uses. The path is set on the
52
52
  // host's URL, never resolved against it, so a path such as `//host` reaches no other host.
53
53
  fetch: async (request) => {
@@ -55,20 +55,11 @@ async function load(options) {
55
55
  const target = new URL(url2);
56
56
  target.pathname = source.pathname;
57
57
  target.search = source.search;
58
- const body2 = fill(
59
- parse(request.body ? await request.text() : void 0, {
60
- method: request.method,
61
- parses,
62
- pathname: source.pathname
63
- }),
64
- {
65
- env,
66
- fills,
67
- method: request.method,
68
- pathname: source.pathname,
69
- schemas
70
- }
71
- );
58
+ const body2 = parse(await fill(request, { env, fills }), {
59
+ method: request.method,
60
+ parses,
61
+ pathname: source.pathname
62
+ });
72
63
  const forwarded = new Request(target, {
73
64
  body: body2 ?? null,
74
65
  headers: request.headers,
@@ -86,28 +77,9 @@ async function load(options) {
86
77
  }
87
78
  var reference = ["/docs", "/openapi.json"];
88
79
  var secrets = {
89
- appSecret: "TEMPO_ID_CLOUD_OWNER_APP_SECRET",
80
+ appSecret: "PRIVY_APP_SECRET",
90
81
  clientSecret: "TEMPO_ID_CLIENT_SECRET"
91
82
  };
92
- var prefix = "#/components/schemas/";
93
- function resolve(schema, schemas) {
94
- if (typeof schema?.$ref !== "string") return schema ?? {};
95
- return schemas[schema.$ref.slice(prefix.length)] ?? {};
96
- }
97
- function variants(schema, schemas, seen = []) {
98
- if (schema?.$ref && seen.includes(schema.$ref)) return [];
99
- const path = schema?.$ref ? [...seen, schema.$ref] : seen;
100
- const entry = resolve(schema, schemas);
101
- const parts = [
102
- ...entry.allOf ?? [],
103
- ...entry.anyOf ?? [],
104
- ...entry.oneOf ?? []
105
- ];
106
- return [
107
- ...entry.type === "object" ? [entry] : [],
108
- ...parts.flatMap((part) => variants(part, schemas, path))
109
- ];
110
- }
111
83
  function withoutSecrets(item) {
112
84
  const copy = structuredClone(item);
113
85
  for (const operation of Object.values(copy ?? {})) {
@@ -118,66 +90,31 @@ function withoutSecrets(item) {
118
90
  }
119
91
  return copy;
120
92
  }
121
- function holdsSecret(schema, schemas, seen = []) {
122
- if (schema?.$ref && seen.includes(schema.$ref)) return false;
123
- const path = schema?.$ref ? [...seen, schema.$ref] : seen;
124
- return variants(schema, schemas).some(
125
- (variant) => Object.entries(variant.properties ?? {}).some(
126
- ([name, property]) => name in secrets || holdsSecret(property, schemas, path)
127
- )
128
- );
129
- }
130
- function secretFields(item, options) {
131
- const { path, schemas } = options;
93
+ function secretFields(path, item) {
132
94
  const pattern = new RegExp(`^${path.replaceAll(/\{[^}]+\}/g, "[^/]+")}$`);
133
95
  return Object.entries(item ?? {}).flatMap(
134
96
  ([method, operation]) => {
135
- const schema = body(operation);
136
- if (!schema || !holdsSecret(schema, schemas)) return [];
137
- return [{ method: method.toUpperCase(), path: pattern, schema }];
97
+ const names = Object.keys(body(operation)?.properties ?? {});
98
+ const fields = names.filter((name) => name in secrets);
99
+ if (fields.length === 0) return [];
100
+ return [{ fields, method: method.toUpperCase(), path: pattern }];
138
101
  }
139
102
  );
140
103
  }
141
- function fill(body2, options) {
142
- const { env, fills, method, pathname, schemas } = options;
104
+ async function fill(request, options) {
105
+ const { env, fills } = options;
106
+ const body2 = request.body ? await request.text() : void 0;
107
+ const { pathname } = new URL(request.url);
143
108
  const match = fills.find(
144
- (entry) => entry.method === method && entry.path.test(pathname)
109
+ (entry) => entry.method === request.method && entry.path.test(pathname)
145
110
  );
146
111
  if (!body2 || !match) return body2;
147
112
  const value = JSON.parse(body2);
148
- set(value, match.schema, { env, schemas });
149
- return JSON.stringify(value);
150
- }
151
- function set(value, schema, options) {
152
- if (typeof value !== "object" || value === null || Array.isArray(value))
153
- return;
154
- const object = value;
155
- for (const variant of variants(schema, options.schemas)) {
156
- const properties = Object.entries(variant.properties ?? {});
157
- const matches = properties.every(
158
- ([name, property]) => property.const === void 0 || object[name] === property.const
159
- );
160
- if (!matches) continue;
161
- for (const [name, property] of properties) {
162
- if (!(name in secrets)) {
163
- set(object[name], property, {
164
- ...options,
165
- option: options.option ?? name
166
- });
167
- continue;
168
- }
169
- const variable = secrets[name];
170
- if (object[name] !== void 0 && options.option)
171
- throw new Error(
172
- `The option --${kebab(options.option)} holds ${name}. A secret never goes in an option. Remove it, and set ${variable} instead.`
173
- );
174
- const secret = options.env[variable];
175
- if (secret && object[name] === void 0) object[name] = secret;
176
- }
113
+ for (const field of match.fields) {
114
+ const secret = env[secrets[field]];
115
+ if (secret && value[field] === void 0) value[field] = secret;
177
116
  }
178
- }
179
- function kebab(name) {
180
- return name.replaceAll(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
117
+ return JSON.stringify(value);
181
118
  }
182
119
  function parse(body2, options) {
183
120
  const { method, parses, pathname } = options;
@@ -196,27 +133,31 @@ function parse(body2, options) {
196
133
  }
197
134
  return JSON.stringify(value);
198
135
  }
199
- function isObject(schema, schemas) {
200
- const entry = resolve(schema, schemas);
201
- if (entry.type === "object") return true;
202
- if ((entry.allOf ?? []).some((part) => isObject(part, schemas))) return true;
203
- const cases = [...entry.anyOf ?? [], ...entry.oneOf ?? []].filter(
204
- (part) => part.type !== "null"
136
+ function objectSchemas(document) {
137
+ const { components } = document;
138
+ return new Set(
139
+ Object.entries(components?.schemas ?? {}).filter(([, schema]) => schema.type === "object").map(([name]) => `#/components/schemas/${name}`)
205
140
  );
206
- return cases.length > 0 && cases.every((part) => isObject(part, schemas));
207
141
  }
208
- function isJson(schema, schemas) {
209
- if (schema?.type === "array") return isObject(schema.items, schemas);
210
- return isObject(schema, schemas);
142
+ function isObject(schema, options) {
143
+ const entry = schema ?? {};
144
+ if (entry.type === "object") return true;
145
+ if (typeof entry.$ref === "string") return options.objects.has(entry.$ref);
146
+ return (entry.allOf ?? []).some((part) => isObject(part, options));
147
+ }
148
+ function isJson(schema, options) {
149
+ const entry = schema ?? {};
150
+ if (entry.type === "array") return isObject(entry.items, options);
151
+ return isObject(schema, options);
211
152
  }
212
153
  function jsonFields(item, options) {
213
- const { path, schemas } = options;
154
+ const { path } = options;
214
155
  const pattern = new RegExp(`^${path.replaceAll(/\{[^}]+\}/g, "[^/]+")}$`);
215
156
  return Object.entries(item ?? {}).flatMap(
216
157
  ([method, operation]) => {
217
158
  const properties = body(operation)?.properties ?? {};
218
159
  const fields = Object.keys(properties).filter(
219
- (name) => isJson(properties[name], schemas)
160
+ (name) => isJson(properties[name], options)
220
161
  );
221
162
  if (fields.length === 0) return [];
222
163
  return [{ fields, method: method.toUpperCase(), path: pattern }];
@@ -224,14 +165,13 @@ function jsonFields(item, options) {
224
165
  );
225
166
  }
226
167
  function withJsonOptions(item, options) {
227
- const { schemas } = options;
228
168
  const copy = structuredClone(item);
229
169
  for (const operation of Object.values(copy ?? {})) {
230
170
  const properties = body(operation)?.properties;
231
171
  if (!properties) continue;
232
172
  for (const [name, schema] of Object.entries(properties)) {
233
- if (!isJson(schema, schemas)) continue;
234
- const description = schema.description ?? resolve(schema, schemas).description;
173
+ if (!isJson(schema, options)) continue;
174
+ const { description } = schema;
235
175
  properties[name] = {
236
176
  description: `${description ?? name} As JSON.`,
237
177
  type: "string"
@@ -240,13 +180,6 @@ function withJsonOptions(item, options) {
240
180
  }
241
181
  return copy;
242
182
  }
243
- function withFlags(item) {
244
- const copy = structuredClone(item);
245
- for (const operation of Object.values(copy ?? {}))
246
- for (const schema of Object.values(body(operation)?.properties ?? {}))
247
- if (schema.type === "boolean") delete schema.const;
248
- return copy;
249
- }
250
183
  function body(operation) {
251
184
  return operation?.requestBody?.content?.["application/json"]?.schema;
252
185
  }
@@ -335,34 +268,46 @@ var v1 = await (async () => {
335
268
  return null;
336
269
  });
337
270
  })();
271
+ var adminUrl = process.env.TEMPO_ID_ADMIN_URL ?? "https://id-admin.tehq.net";
272
+ var login = command === "admin" && positionals[1] === "login";
338
273
  var adminApi = await (async () => {
339
- if (command !== "admin") return void 0;
340
- const adminUrl = process.env.TEMPO_ID_ADMIN_URL;
341
- const header = process.env.TEMPO_ID_ADMIN_TOKEN_HEADER;
342
- const token = process.env.TEMPO_ID_ADMIN_TOKEN;
343
- try {
344
- if (!adminUrl)
345
- throw new Error("Set TEMPO_ID_ADMIN_URL to the origin of the admin host.");
346
- if (token && !header)
347
- throw new Error(
348
- "Set TEMPO_ID_ADMIN_TOKEN_HEADER to the header that carries TEMPO_ID_ADMIN_TOKEN, as the admin host names it."
349
- );
350
- return await load({
351
- env: process.env,
352
- headers: token && header ? { [header]: token } : {},
353
- url: adminUrl
354
- });
355
- } catch (error) {
274
+ if (command !== "admin" || login) return void 0;
275
+ const token = process.env.TEMPO_ID_ADMIN_TOKEN ?? cachedToken(adminUrl);
276
+ return await load({
277
+ env: process.env,
278
+ headers: token ? { "cf-access-token": token } : {},
279
+ url: adminUrl
280
+ }).catch((error) => {
356
281
  process.stderr.write(
357
282
  `The admin commands are unavailable: ${error instanceof Error ? error.message : String(error)}
358
283
  `
359
284
  );
360
285
  return null;
361
- }
286
+ });
362
287
  })();
363
- if (v1 === null && group || adminApi === null) process.exitCode = 1;
288
+ if (login) {
289
+ const result = ChildProcess.spawnSync(
290
+ "cloudflared",
291
+ ["access", "login", adminUrl],
292
+ { stdio: "inherit" }
293
+ );
294
+ if (result.error)
295
+ process.stderr.write(
296
+ "Unable to run cloudflared. Install it from https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/, then run tempo-id admin login again.\n"
297
+ );
298
+ process.exitCode = result.error ? 1 : result.status ?? 1;
299
+ } else if (v1 === null && group || adminApi === null) process.exitCode = 1;
364
300
  else
365
301
  await create({
366
302
  adminApi: adminApi ?? void 0,
367
303
  v1: v1 ?? void 0
368
304
  }).serve();
305
+ function cachedToken(url2) {
306
+ const result = ChildProcess.spawnSync(
307
+ "cloudflared",
308
+ ["access", "token", "-app", url2],
309
+ { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }
310
+ );
311
+ if (result.error || result.status !== 0) return void 0;
312
+ return result.stdout.trim() || void 0;
313
+ }
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "node": ">=24.21"
10
10
  },
11
11
  "name": "@tempoxyz/id",
12
- "version": "0.0.0-pr-834.sha-1d74ebf",
12
+ "version": "0.0.0-pr-831.sha-2eced5f",
13
13
  "description": "The tempo-id CLI: sign in to Tempo ID, and call its API and the Admin API.",
14
14
  "type": "module",
15
15
  "bin": {