@ornncompute/cli 1.0.159 → 1.0.160

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/README.md CHANGED
@@ -46,6 +46,15 @@ authentication origin. Use `--no-browser` when the CLI must print the URL instea
46
46
  in `~/.config/ornn`; the gateway re-derives organization and role scope from the
47
47
  bearer on every request.
48
48
 
49
+ For automation, set `ORNN_API_KEY` to an organization API key from a secrets
50
+ manager and run the same commands. It overrides saved login, never falls back
51
+ on failure, and retains the key's organization and read/read-write access; it
52
+ cannot grant staff access. Unset it before `ornn login` or `ornn logout`.
53
+ Keys are never stored by the CLI. Local identity and workspace recovery state
54
+ use `api-keys/<sha256-of-key>.json` under the config directory, isolated from
55
+ `auth.json` and other keys. Key management (including `ornn registry login`)
56
+ still requires a user session. See the [customer guide](../../docs-site/api-keys.mdx).
57
+
49
58
  ## Command map
50
59
 
51
60
  The three primary customer journeys are:
@@ -69,7 +78,7 @@ ornn completion <bash|fish|zsh> --format human
69
78
  ornn resources
70
79
  ```
71
80
 
72
- The CLI uses Authorization's newest membership on every request, regardless of
81
+ With a login session, the CLI uses Authorization's newest membership on every request, regardless of
73
82
  approval. `ornn context` displays that current organization; login and launch
74
83
  use the same context. Reserve browsing and purchasing still require approval.
75
84
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ornncompute/cli",
3
- "version": "1.0.159",
3
+ "version": "1.0.160",
4
4
  "description": "Command-line interface for Ornn compute access workflows.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -36,6 +36,7 @@
36
36
  },
37
37
  "devDependencies": {
38
38
  "@ornn/api-client": "0.1.0",
39
+ "@ornn/authorization": "0.1.0",
39
40
  "@ornn/capabilities": "0.1.0",
40
41
  "esbuild": "^0.28.2"
41
42
  },
@@ -1,4 +1,5 @@
1
1
  import { createRequire } from "node:module";
2
+ import { userGatewayHeaders } from "../vendor/authorization.js";
2
3
 
3
4
  import {
4
5
  clearAuthCredentials,
@@ -80,12 +81,11 @@ export async function loadRequiredSession({ env = process.env } = {}) {
80
81
  }
81
82
  export function userCredentialHeaders(session) {
82
83
  if (!session?.accessToken) return {};
83
- return {
84
- Authorization: `Bearer ${session.accessToken}`,
85
- // The gateway resolves the bearer to trusted user and organization scope.
86
- "X-Ornn-Credential-Type": "user",
87
- "X-Ornn-CLI-Version": CLI_VERSION,
88
- };
84
+ return userGatewayHeaders({
85
+ token: session.accessToken,
86
+ actor: "cli",
87
+ clientVersion: CLI_VERSION,
88
+ });
89
89
  }
90
90
 
91
91
  export async function cliRequest({
@@ -112,6 +112,7 @@ export async function cliRequest({
112
112
  if (
113
113
  [
114
114
  "authorization",
115
+ "x-actor",
115
116
  "x-auth-tenant-id",
116
117
  "x-auth-user-id",
117
118
  "x-organization-id",
@@ -342,7 +343,7 @@ function parseJson(text, url, response) {
342
343
  }
343
344
 
344
345
  async function handleAuthExpiry({ accessToken, env, response }) {
345
- if (response.status !== 401 || !accessToken) {
346
+ if (response.status !== 401 || !accessToken || accessToken.startsWith("ornn_sk_")) {
346
347
  return;
347
348
  }
348
349
  await clearAuthCredentials({ env, expectedAccessToken: accessToken });
@@ -1,7 +1,8 @@
1
+ import { createHash } from "node:crypto";
1
2
  import { readFileSync } from "node:fs";
2
3
  import { mkdir, readFile, rm } from "node:fs/promises";
3
4
  import { homedir } from "node:os";
4
- import { join, resolve } from "node:path";
5
+ import { dirname, join, resolve } from "node:path";
5
6
 
6
7
  import { writeAtomicOutput } from "./atomic-output.mjs";
7
8
 
@@ -22,9 +23,26 @@ export function getAuthConfigDir(env = process.env) {
22
23
  }
23
24
 
24
25
  export function getAuthConfigPath(env = process.env) {
26
+ const key = apiKey(env);
27
+ if (key) {
28
+ // Keep each key's recovery state separate from browser login; never store the key.
29
+ const fingerprint = createHash("sha256").update(key).digest("hex");
30
+ return join(getAuthConfigDir(env), "api-keys", `${fingerprint}.json`);
31
+ }
25
32
  return join(getAuthConfigDir(env), AUTH_FILE);
26
33
  }
27
34
 
35
+ function apiKey(env) {
36
+ if (env.ORNN_API_KEY === undefined) return null;
37
+ const key = env.ORNN_API_KEY.trim();
38
+ if (!key.startsWith("ornn_sk_")) {
39
+ throw new Error(
40
+ "ORNN_API_KEY must contain an organization API key (ornn_sk_...). Unset it to use your saved login."
41
+ );
42
+ }
43
+ return key;
44
+ }
45
+
28
46
  export function getCliConfigPath(env = process.env) {
29
47
  return join(getAuthConfigDir(env), CONFIG_FILE);
30
48
  }
@@ -58,23 +76,24 @@ export function configuredInstallerBaseUrl(env = process.env) {
58
76
  }
59
77
 
60
78
  export async function loadAuthSession({ env = process.env } = {}) {
79
+ const key = apiKey(env);
61
80
  const path = getAuthConfigPath(env);
62
-
81
+ let stored = null;
63
82
  try {
64
83
  const raw = await readFile(path, "utf8");
65
- return JSON.parse(raw);
84
+ stored = JSON.parse(raw);
66
85
  } catch (error) {
67
- if (error?.code === "ENOENT") {
68
- return null;
69
- }
70
- throw error;
86
+ if (error?.code !== "ENOENT") throw error;
71
87
  }
88
+ return key ? { ...stored, accessToken: key } : stored;
72
89
  }
73
90
 
74
91
  async function writeAuthSession(session, env) {
75
92
  const path = getAuthConfigPath(env);
76
- await mkdir(getAuthConfigDir(env), { recursive: true, mode: 0o700 });
77
- await writeAtomicOutput(path, `${JSON.stringify(session, null, 2)}\n`, {
93
+ const stored = { ...session };
94
+ if (apiKey(env)) delete stored.accessToken;
95
+ await mkdir(dirname(path), { recursive: true, mode: 0o700 });
96
+ await writeAtomicOutput(path, `${JSON.stringify(stored, null, 2)}\n`, {
78
97
  mode: 0o600,
79
98
  overwrite: true,
80
99
  });
@@ -443,15 +443,7 @@ function requireMutationPreflight(input, runtime) {
443
443
  function safeExternalError(error, fallback, { action, forcePartial = false } = {}) {
444
444
  if (error instanceof StorageCommandError) return error;
445
445
  if (error instanceof CliApiError && (error.status === 401 || error.status === 403)) {
446
- return new StorageCommandError(
447
- error.status === 401
448
- ? "Sign in to continue."
449
- : "This organization cannot perform that operation.",
450
- {
451
- code: error.status === 401 ? "authentication_required" : "authorization_denied",
452
- exitCode: 5,
453
- }
454
- );
446
+ return error;
455
447
  }
456
448
  const code = codeOf(error);
457
449
  const unavailable = error?.status === undefined || error?.status >= 500;
@@ -335,6 +335,9 @@ async function logout(runtime) {
335
335
  }
336
336
 
337
337
  export async function executeSessionCommand(command, input, runtime) {
338
+ if ((command === "login" || command === "logout") && runtime.env.ORNN_API_KEY !== undefined) {
339
+ usage(`Unset ORNN_API_KEY before running \`ornn ${command}\`. Manage API keys in the console.`);
340
+ }
338
341
  if (command === "login") return login(input, runtime);
339
342
  if (command === "context") return context(runtime);
340
343
  if (command === "logout") return logout(runtime);
@@ -819,7 +819,7 @@ async function downloadResponseError(response, runtime, url, accessToken) {
819
819
  await raiseForFailedResponse({
820
820
  accessToken,
821
821
  env: runtime.env,
822
- fallbackMessage: "The CLI session expired. Run `ornn login` and try again.",
822
+ fallbackMessage: "Ornn could not authenticate this request.",
823
823
  response,
824
824
  text,
825
825
  url,
@@ -0,0 +1,35 @@
1
+ // ../../packages/authorization/src/index.ts
2
+ function userGatewayHeaders(credential) {
3
+ return {
4
+ authorization: `Bearer ${credential.token}`,
5
+ "X-Actor": credential.actor,
6
+ "X-Ornn-Credential-Type": "user",
7
+ ...credential.clientVersion ? { "X-Ornn-CLI-Version": credential.clientVersion } : {},
8
+ ...credential.organizationId ? { "X-Organization-Id": credential.organizationId } : {}
9
+ };
10
+ }
11
+ function staffGatewayHeaders(credential) {
12
+ return {
13
+ authorization: `Bearer ${credential.token}`,
14
+ "X-Mcp-Internal-Secret": credential.internalSecret,
15
+ "X-Ornn-Credential-Type": "user",
16
+ ...credential.authUserId ? { "X-Auth-User-Id": credential.authUserId } : {}
17
+ };
18
+ }
19
+ function reviewSecretHeaders(secret) {
20
+ return { "X-Internal-Review-Secret": secret };
21
+ }
22
+ function reviewGatewayHeaders(secret) {
23
+ return { Authorization: `Bearer ${secret}`, ...reviewSecretHeaders(secret) };
24
+ }
25
+ function writeIdempotencyKey(method, explicit) {
26
+ if (explicit) return explicit;
27
+ return ["GET", "HEAD", "OPTIONS"].includes(method.toUpperCase()) ? void 0 : crypto.randomUUID();
28
+ }
29
+ export {
30
+ reviewGatewayHeaders,
31
+ reviewSecretHeaders,
32
+ staffGatewayHeaders,
33
+ userGatewayHeaders,
34
+ writeIdempotencyKey
35
+ };