@deque/axe-auth 1.1.0-next.789db6ed → 1.1.0-next.f7b98204

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.
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KeyringTokenStore = exports.STORED_BLOB_VERSION = void 0;
4
+ const node_module_1 = require("node:module");
5
+ const errors_1 = require("./errors");
6
+ const issuerURL_1 = require("./issuerURL");
7
+ const requireFromHere = (0, node_module_1.createRequire)(__filename);
8
+ // On macOS: Keychain generic password item with the service name below.
9
+ // On Windows: Credential Manager entry. On Linux: Secret Service / libsecret.
10
+ // Exposed as a human-readable string because these all surface the service
11
+ // name in OS UIs (Keychain Access, credmgr.exe, seahorse).
12
+ const SERVICE_NAME = "axe-auth";
13
+ /**
14
+ * Current on-disk blob schema version. Exported so consumers can
15
+ * display "stored v:N, expected v:M" diagnostics when `load()` returns
16
+ * a `version-mismatch` result.
17
+ */
18
+ exports.STORED_BLOB_VERSION = 1;
19
+ /**
20
+ * Migrators upgrade an older blob to the next version up. Walked by
21
+ * `load()` until the stored blob reaches `STORED_BLOB_VERSION`.
22
+ *
23
+ * A migrator returns `null` when the bump cannot be inferred from the
24
+ * old shape (e.g. a new required field with no derivable default); the
25
+ * caller then sees `{ ok: false, reason: "version-mismatch" }` and
26
+ * decides whether to re-auth, prompt, or preserve the old blob.
27
+ *
28
+ * Each migrator is responsible for taking `vN` → `vN+1`. To skip a
29
+ * version deliberately, register a migrator that returns `null` for
30
+ * that `fromVersion`.
31
+ */
32
+ const MIGRATORS = new Map([
33
+ // [1, (v1) => migrateV1ToV2(v1 as StoredBlobV1)],
34
+ ]);
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 });
58
+ }
59
+ }
60
+ const defaultEntryFactory = (service, account) => {
61
+ const Ctor = resolveEntryCtor();
62
+ return new Ctor(service, account);
63
+ };
64
+ function getStoredVersion(blob) {
65
+ if (blob === null || typeof blob !== "object")
66
+ return null;
67
+ const v = blob.v;
68
+ return typeof v === "number" && Number.isInteger(v) && v > 0 ? v : null;
69
+ }
70
+ function isLatestBlob(blob) {
71
+ if (blob === null || typeof blob !== "object")
72
+ return false;
73
+ const b = blob;
74
+ return (b.v === exports.STORED_BLOB_VERSION &&
75
+ typeof b.accessToken === "string" &&
76
+ typeof b.expiresAt === "number" &&
77
+ (b.refreshToken === undefined || typeof b.refreshToken === "string"));
78
+ }
79
+ function blobToTokens(blob) {
80
+ const tokens = {
81
+ accessToken: blob.accessToken,
82
+ expiresAt: blob.expiresAt,
83
+ };
84
+ if (blob.refreshToken !== undefined)
85
+ tokens.refreshToken = blob.refreshToken;
86
+ return tokens;
87
+ }
88
+ function tokensToBlob(tokens) {
89
+ const blob = {
90
+ v: exports.STORED_BLOB_VERSION,
91
+ accessToken: tokens.accessToken,
92
+ expiresAt: tokens.expiresAt,
93
+ };
94
+ if (tokens.refreshToken !== undefined)
95
+ blob.refreshToken = tokens.refreshToken;
96
+ return blob;
97
+ }
98
+ function wrapKeyringError(op, cause) {
99
+ 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
+ }
101
+ /**
102
+ * `TokenStore` backed by the operating system's native keychain via
103
+ * `@napi-rs/keyring` (macOS Keychain, Windows Credential Manager, Linux
104
+ * Secret Service). Account is keyed by the normalized issuer URL.
105
+ */
106
+ class KeyringTokenStore {
107
+ #entry;
108
+ constructor(issuerURL, entryFactory = defaultEntryFactory) {
109
+ this.#entry = entryFactory(SERVICE_NAME, (0, issuerURL_1.normalizeIssuerURL)(issuerURL));
110
+ }
111
+ async save(tokens) {
112
+ try {
113
+ this.#entry.setPassword(JSON.stringify(tokensToBlob(tokens)));
114
+ }
115
+ catch (cause) {
116
+ wrapKeyringError("write", cause);
117
+ }
118
+ }
119
+ async load() {
120
+ let raw;
121
+ try {
122
+ raw = this.#entry.getPassword();
123
+ }
124
+ catch (cause) {
125
+ wrapKeyringError("read", cause);
126
+ }
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))
164
+ return { ok: false, reason: "corrupt" };
165
+ return { ok: true, tokens: blobToTokens(current) };
166
+ }
167
+ async clear() {
168
+ try {
169
+ this.#entry.deletePassword();
170
+ }
171
+ catch (cause) {
172
+ wrapKeyringError("delete", cause);
173
+ }
174
+ }
175
+ }
176
+ exports.KeyringTokenStore = KeyringTokenStore;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deque/axe-auth",
3
- "version": "1.1.0-next.789db6ed",
3
+ "version": "1.1.0-next.f7b98204",
4
4
  "description": "CLI authentication utility for Deque services",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "commonjs",
@@ -20,13 +20,21 @@
20
20
  "engines": {
21
21
  "node": ">=22.13.0"
22
22
  },
23
+ "dependencies": {
24
+ "@napi-rs/keyring": "^1.2.0"
25
+ },
23
26
  "devDependencies": {
24
27
  "@types/node": "^22.13.10",
28
+ "c8": "^10.1.3",
25
29
  "tsx": "^4.20.6",
26
30
  "typescript": "^5.9.3"
27
31
  },
28
32
  "scripts": {
29
33
  "build": "tsc",
30
- "test": "tsx --test 'src/**/*.test.ts'"
34
+ "test": "tsx --test 'src/**/*.test.ts'",
35
+ "coverage": "c8 pnpm test",
36
+ "register-dev-client": "tsx scripts/registerDevClient.ts",
37
+ "smoke-authorize": "tsx scripts/smokeAuthorize.ts",
38
+ "manual-authorize": "tsx scripts/manualAuthorize.ts"
31
39
  }
32
40
  }