@deque/axe-auth 1.1.0-next.f7b98204 → 1.1.0-next.fda76051

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.
@@ -1,15 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.KeyringTokenStore = exports.STORED_BLOB_VERSION = void 0;
4
- const node_module_1 = require("node:module");
4
+ exports.parseAndMigrateBlob = parseAndMigrateBlob;
5
5
  const errors_1 = require("./errors");
6
- const issuerURL_1 = require("./issuerURL");
7
- const requireFromHere = (0, node_module_1.createRequire)(__filename);
6
+ const keyringBinding_1 = require("./keyringBinding");
8
7
  // On macOS: Keychain generic password item with the service name below.
9
8
  // On Windows: Credential Manager entry. On Linux: Secret Service / libsecret.
10
9
  // Exposed as a human-readable string because these all surface the service
11
10
  // name in OS UIs (Keychain Access, credmgr.exe, seahorse).
12
11
  const SERVICE_NAME = "axe-auth";
12
+ // Single keychain entry per machine. The blob it holds is fully
13
+ // self-describing (issuerURL, clientId, allowInsecureIssuer, plus the
14
+ // tokens), so verbs that don't pass `--server` / `--realm` /
15
+ // `--client-id` can resolve their config from the entry.
16
+ //
17
+ // Account name is human-readable so users investigating the entry in
18
+ // macOS Keychain Access (or `secret-tool` on Linux, credmgr on
19
+ // Windows) can tell what it is. Not versioned: the schema version
20
+ // lives inside the blob and migrators handle the upgrade path.
21
+ const ACCOUNT_NAME = "credentials";
13
22
  /**
14
23
  * Current on-disk blob schema version. Exported so consumers can
15
24
  * display "stored v:N, expected v:M" diagnostics when `load()` returns
@@ -32,35 +41,17 @@ exports.STORED_BLOB_VERSION = 1;
32
41
  const MIGRATORS = new Map([
33
42
  // [1, (v1) => migrateV1ToV2(v1 as StoredBlobV1)],
34
43
  ]);
35
- // Lazy-resolved Entry constructor. Importing @napi-rs/keyring at the
36
- // top of this module would run its native binding loader at
37
- // module-load time, throwing before any of our OAuthFlowError code
38
- // catches it and preventing the module from being imported at all on
39
- // platforms without a prebuilt. We defer the require into
40
- // `defaultEntryFactory`, which turns that import-time failure into a
41
- // `KEYRING_UNAVAILABLE` surfaced on the first `new
42
- // KeyringTokenStore(...)` call not on first save/load/clear, since
43
- // `authorize()` (and similar callers) construct the store eagerly as
44
- // a default-arg expression. Runtime keychain errors (missing D-Bus
45
- // Secret Service, macOS Keychain denial, etc.) are a separate
46
- // concern and surface later, inside save/load/clear.
47
- let cachedEntryCtor = null;
48
- function resolveEntryCtor() {
49
- if (cachedEntryCtor)
50
- return cachedEntryCtor;
51
- try {
52
- const mod = requireFromHere("@napi-rs/keyring");
53
- cachedEntryCtor = mod.Entry;
54
- return cachedEntryCtor;
55
- }
56
- catch (cause) {
57
- throw new errors_1.OAuthFlowError("KEYRING_UNAVAILABLE", `Could not load @napi-rs/keyring. A prebuilt native binding for this platform may be missing.`, { cause });
44
+ // Sanity-check the migrator map at module load. Every key must be
45
+ // strictly less than `STORED_BLOB_VERSION` the chain only walks
46
+ // forward, so a leftover migrator at the current (or future) version
47
+ // would either be unreachable or confuse the loop. Fail-fast so a
48
+ // dev forgetting to remove a stale entry during a version bump
49
+ // notices before shipping.
50
+ for (const fromVersion of MIGRATORS.keys()) {
51
+ if (fromVersion >= exports.STORED_BLOB_VERSION) {
52
+ throw new Error(`MIGRATORS contains a key (v${fromVersion}) that is not strictly less than STORED_BLOB_VERSION (${exports.STORED_BLOB_VERSION}). The chain only walks forward; remove stale migrators when bumping the schema version.`);
58
53
  }
59
54
  }
60
- const defaultEntryFactory = (service, account) => {
61
- const Ctor = resolveEntryCtor();
62
- return new Ctor(service, account);
63
- };
64
55
  function getStoredVersion(blob) {
65
56
  if (blob === null || typeof blob !== "object")
66
57
  return null;
@@ -72,45 +63,109 @@ function isLatestBlob(blob) {
72
63
  return false;
73
64
  const b = blob;
74
65
  return (b.v === exports.STORED_BLOB_VERSION &&
66
+ // Empty access token is treated as corrupt rather than a usable
67
+ // credential. `axe-auth token` printing an empty line and exiting
68
+ // 0 would look like success and silently break downstream.
75
69
  typeof b.accessToken === "string" &&
70
+ b.accessToken.length > 0 &&
76
71
  typeof b.expiresAt === "number" &&
77
- (b.refreshToken === undefined || typeof b.refreshToken === "string"));
72
+ (b.refreshToken === undefined || typeof b.refreshToken === "string") &&
73
+ typeof b.issuerURL === "string" &&
74
+ typeof b.clientId === "string" &&
75
+ typeof b.allowInsecureIssuer === "boolean");
78
76
  }
79
- function blobToTokens(blob) {
77
+ function blobToEntry(blob) {
80
78
  const tokens = {
81
79
  accessToken: blob.accessToken,
82
80
  expiresAt: blob.expiresAt,
83
81
  };
84
- if (blob.refreshToken !== undefined)
82
+ if (blob.refreshToken)
85
83
  tokens.refreshToken = blob.refreshToken;
86
- return tokens;
84
+ return {
85
+ tokens,
86
+ issuerURL: blob.issuerURL,
87
+ clientId: blob.clientId,
88
+ allowInsecureIssuer: blob.allowInsecureIssuer,
89
+ };
87
90
  }
88
- function tokensToBlob(tokens) {
91
+ function entryToBlob(entry) {
89
92
  const blob = {
90
93
  v: exports.STORED_BLOB_VERSION,
91
- accessToken: tokens.accessToken,
92
- expiresAt: tokens.expiresAt,
94
+ accessToken: entry.tokens.accessToken,
95
+ expiresAt: entry.tokens.expiresAt,
96
+ issuerURL: entry.issuerURL,
97
+ clientId: entry.clientId,
98
+ allowInsecureIssuer: entry.allowInsecureIssuer,
93
99
  };
94
- if (tokens.refreshToken !== undefined)
95
- blob.refreshToken = tokens.refreshToken;
100
+ if (entry.tokens.refreshToken)
101
+ blob.refreshToken = entry.tokens.refreshToken;
96
102
  return blob;
97
103
  }
104
+ /**
105
+ * JSON-parses the raw keychain password and walks the migrator chain
106
+ * until it reaches `expectedVersion`. Exported with `expectedVersion`
107
+ * and `migrators` parameters only for testing the chain mechanics
108
+ * against synthetic versions / migrators; production callers use
109
+ * `KeyringTokenStore.load()`, which feeds in `STORED_BLOB_VERSION`
110
+ * and `MIGRATORS` and applies the latest-shape check on top.
111
+ */
112
+ function parseAndMigrateBlob(raw, expectedVersion = exports.STORED_BLOB_VERSION, migrators = MIGRATORS) {
113
+ if (raw === null)
114
+ return { ok: false, reason: "empty" };
115
+ let parsed;
116
+ try {
117
+ parsed = JSON.parse(raw);
118
+ }
119
+ catch {
120
+ return { ok: false, reason: "corrupt" };
121
+ }
122
+ const storedVersion = getStoredVersion(parsed);
123
+ if (storedVersion === null)
124
+ return { ok: false, reason: "corrupt" };
125
+ // Walk the migrator chain until we reach the expected version. A
126
+ // missing or null-returning migrator means the old blob cannot be
127
+ // upgraded; surface that so callers can prompt re-auth with a
128
+ // clear signal instead of silently returning `empty`.
129
+ let current = parsed;
130
+ let currentVersion = storedVersion;
131
+ while (currentVersion !== expectedVersion) {
132
+ const migrator = migrators.get(currentVersion);
133
+ if (!migrator) {
134
+ return { ok: false, reason: "version-mismatch", storedVersion };
135
+ }
136
+ const next = migrator(current);
137
+ if (next === null) {
138
+ return { ok: false, reason: "version-mismatch", storedVersion };
139
+ }
140
+ const nextVersion = getStoredVersion(next);
141
+ if (nextVersion === null || nextVersion <= currentVersion) {
142
+ // Migrator output is malformed or didn't advance. Treat the
143
+ // stored blob as un-migratable rather than loop forever.
144
+ return { ok: false, reason: "version-mismatch", storedVersion };
145
+ }
146
+ current = next;
147
+ currentVersion = nextVersion;
148
+ }
149
+ return { ok: true, blob: current };
150
+ }
98
151
  function wrapKeyringError(op, cause) {
99
152
  throw new errors_1.OAuthFlowError("KEYRING_UNAVAILABLE", `System keychain ${op} failed. On Linux this usually means no D-Bus Secret Service is running.`, { cause });
100
153
  }
101
154
  /**
102
155
  * `TokenStore` backed by the operating system's native keychain via
103
156
  * `@napi-rs/keyring` (macOS Keychain, Windows Credential Manager, Linux
104
- * Secret Service). Account is keyed by the normalized issuer URL.
157
+ * Secret Service). One entry per machine, keyed by a fixed account
158
+ * name; the blob carries its own issuer/client coordinates so verbs
159
+ * can recover full config without per-issuer keying.
105
160
  */
106
161
  class KeyringTokenStore {
107
162
  #entry;
108
- constructor(issuerURL, entryFactory = defaultEntryFactory) {
109
- this.#entry = entryFactory(SERVICE_NAME, (0, issuerURL_1.normalizeIssuerURL)(issuerURL));
163
+ constructor(entryFactory = keyringBinding_1.defaultEntryFactory) {
164
+ this.#entry = entryFactory(SERVICE_NAME, ACCOUNT_NAME);
110
165
  }
111
- async save(tokens) {
166
+ async save(entry) {
112
167
  try {
113
- this.#entry.setPassword(JSON.stringify(tokensToBlob(tokens)));
168
+ this.#entry.setPassword(JSON.stringify(entryToBlob(entry)));
114
169
  }
115
170
  catch (cause) {
116
171
  wrapKeyringError("write", cause);
@@ -124,45 +179,12 @@ class KeyringTokenStore {
124
179
  catch (cause) {
125
180
  wrapKeyringError("read", cause);
126
181
  }
127
- if (raw === null)
128
- return { ok: false, reason: "empty" };
129
- let parsed;
130
- try {
131
- parsed = JSON.parse(raw);
132
- }
133
- catch {
134
- return { ok: false, reason: "corrupt" };
135
- }
136
- const storedVersion = getStoredVersion(parsed);
137
- if (storedVersion === null)
138
- return { ok: false, reason: "corrupt" };
139
- // Walk the migrator chain until we reach the current version. A
140
- // missing or null-returning migrator means the old blob cannot be
141
- // upgraded; surface that so callers can prompt re-auth with a
142
- // clear signal instead of silently returning `empty`.
143
- let current = parsed;
144
- let currentVersion = storedVersion;
145
- while (currentVersion !== exports.STORED_BLOB_VERSION) {
146
- const migrator = MIGRATORS.get(currentVersion);
147
- if (!migrator) {
148
- return { ok: false, reason: "version-mismatch", storedVersion };
149
- }
150
- const next = migrator(current);
151
- if (next === null) {
152
- return { ok: false, reason: "version-mismatch", storedVersion };
153
- }
154
- const nextVersion = getStoredVersion(next);
155
- if (nextVersion === null || nextVersion <= currentVersion) {
156
- // Migrator output is malformed or didn't advance. Treat the
157
- // stored blob as un-migratable rather than loop forever.
158
- return { ok: false, reason: "version-mismatch", storedVersion };
159
- }
160
- current = next;
161
- currentVersion = nextVersion;
162
- }
163
- if (!isLatestBlob(current))
182
+ const chain = parseAndMigrateBlob(raw);
183
+ if (!chain.ok)
184
+ return chain;
185
+ if (!isLatestBlob(chain.blob))
164
186
  return { ok: false, reason: "corrupt" };
165
- return { ok: true, tokens: blobToTokens(current) };
187
+ return { ok: true, entry: blobToEntry(chain.blob) };
166
188
  }
167
189
  async clear() {
168
190
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deque/axe-auth",
3
- "version": "1.1.0-next.f7b98204",
3
+ "version": "1.1.0-next.fda76051",
4
4
  "description": "CLI authentication utility for Deque services",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "commonjs",