@deque/axe-auth 1.1.0-next.fb07beab → 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.
Files changed (36) hide show
  1. package/README.md +14 -8
  2. package/dist/oauth/authorizationURL.d.ts +29 -0
  3. package/dist/oauth/authorizationURL.js +52 -0
  4. package/dist/oauth/authorize.d.ts +84 -0
  5. package/dist/oauth/authorize.js +118 -0
  6. package/dist/oauth/discoverOIDC.d.ts +50 -0
  7. package/dist/oauth/discoverOIDC.js +143 -0
  8. package/dist/oauth/errors.d.ts +55 -2
  9. package/dist/oauth/errors.js +35 -1
  10. package/dist/oauth/getValidAccessToken.d.ts +89 -0
  11. package/dist/oauth/getValidAccessToken.js +139 -0
  12. package/dist/oauth/index.d.ts +14 -2
  13. package/dist/oauth/index.js +13 -1
  14. package/dist/oauth/issuerURL.d.ts +22 -0
  15. package/dist/oauth/issuerURL.js +38 -0
  16. package/dist/oauth/keyringBinding.d.ts +22 -0
  17. package/dist/oauth/keyringBinding.js +41 -0
  18. package/dist/oauth/openBrowser.d.ts +19 -0
  19. package/dist/oauth/openBrowser.js +78 -0
  20. package/dist/oauth/pkce.d.ts +17 -0
  21. package/dist/oauth/pkce.js +43 -0
  22. package/dist/oauth/predicates.d.ts +7 -0
  23. package/dist/oauth/predicates.js +15 -0
  24. package/dist/oauth/refreshTokens.d.ts +30 -0
  25. package/dist/oauth/refreshTokens.js +61 -0
  26. package/dist/oauth/revokeToken.d.ts +28 -0
  27. package/dist/oauth/revokeToken.js +59 -0
  28. package/dist/oauth/testUtils.d.ts +35 -0
  29. package/dist/oauth/testUtils.js +61 -0
  30. package/dist/oauth/tokenExchange.d.ts +26 -0
  31. package/dist/oauth/tokenExchange.js +42 -0
  32. package/dist/oauth/tokenResponse.d.ts +54 -0
  33. package/dist/oauth/tokenResponse.js +121 -0
  34. package/dist/oauth/tokenStore.d.ts +111 -0
  35. package/dist/oauth/tokenStore.js +198 -0
  36. package/package.json +8 -2
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KeyringTokenStore = exports.STORED_BLOB_VERSION = void 0;
4
+ exports.parseAndMigrateBlob = parseAndMigrateBlob;
5
+ const errors_1 = require("./errors");
6
+ const keyringBinding_1 = require("./keyringBinding");
7
+ // On macOS: Keychain generic password item with the service name below.
8
+ // On Windows: Credential Manager entry. On Linux: Secret Service / libsecret.
9
+ // Exposed as a human-readable string because these all surface the service
10
+ // name in OS UIs (Keychain Access, credmgr.exe, seahorse).
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";
22
+ /**
23
+ * Current on-disk blob schema version. Exported so consumers can
24
+ * display "stored v:N, expected v:M" diagnostics when `load()` returns
25
+ * a `version-mismatch` result.
26
+ */
27
+ exports.STORED_BLOB_VERSION = 1;
28
+ /**
29
+ * Migrators upgrade an older blob to the next version up. Walked by
30
+ * `load()` until the stored blob reaches `STORED_BLOB_VERSION`.
31
+ *
32
+ * A migrator returns `null` when the bump cannot be inferred from the
33
+ * old shape (e.g. a new required field with no derivable default); the
34
+ * caller then sees `{ ok: false, reason: "version-mismatch" }` and
35
+ * decides whether to re-auth, prompt, or preserve the old blob.
36
+ *
37
+ * Each migrator is responsible for taking `vN` → `vN+1`. To skip a
38
+ * version deliberately, register a migrator that returns `null` for
39
+ * that `fromVersion`.
40
+ */
41
+ const MIGRATORS = new Map([
42
+ // [1, (v1) => migrateV1ToV2(v1 as StoredBlobV1)],
43
+ ]);
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.`);
53
+ }
54
+ }
55
+ function getStoredVersion(blob) {
56
+ if (blob === null || typeof blob !== "object")
57
+ return null;
58
+ const v = blob.v;
59
+ return typeof v === "number" && Number.isInteger(v) && v > 0 ? v : null;
60
+ }
61
+ function isLatestBlob(blob) {
62
+ if (blob === null || typeof blob !== "object")
63
+ return false;
64
+ const b = blob;
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.
69
+ typeof b.accessToken === "string" &&
70
+ b.accessToken.length > 0 &&
71
+ typeof b.expiresAt === "number" &&
72
+ (b.refreshToken === undefined || typeof b.refreshToken === "string") &&
73
+ typeof b.issuerURL === "string" &&
74
+ typeof b.clientId === "string" &&
75
+ typeof b.allowInsecureIssuer === "boolean");
76
+ }
77
+ function blobToEntry(blob) {
78
+ const tokens = {
79
+ accessToken: blob.accessToken,
80
+ expiresAt: blob.expiresAt,
81
+ };
82
+ if (blob.refreshToken)
83
+ tokens.refreshToken = blob.refreshToken;
84
+ return {
85
+ tokens,
86
+ issuerURL: blob.issuerURL,
87
+ clientId: blob.clientId,
88
+ allowInsecureIssuer: blob.allowInsecureIssuer,
89
+ };
90
+ }
91
+ function entryToBlob(entry) {
92
+ const blob = {
93
+ v: exports.STORED_BLOB_VERSION,
94
+ accessToken: entry.tokens.accessToken,
95
+ expiresAt: entry.tokens.expiresAt,
96
+ issuerURL: entry.issuerURL,
97
+ clientId: entry.clientId,
98
+ allowInsecureIssuer: entry.allowInsecureIssuer,
99
+ };
100
+ if (entry.tokens.refreshToken)
101
+ blob.refreshToken = entry.tokens.refreshToken;
102
+ return blob;
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
+ }
151
+ function wrapKeyringError(op, cause) {
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 });
153
+ }
154
+ /**
155
+ * `TokenStore` backed by the operating system's native keychain via
156
+ * `@napi-rs/keyring` (macOS Keychain, Windows Credential Manager, Linux
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.
160
+ */
161
+ class KeyringTokenStore {
162
+ #entry;
163
+ constructor(entryFactory = keyringBinding_1.defaultEntryFactory) {
164
+ this.#entry = entryFactory(SERVICE_NAME, ACCOUNT_NAME);
165
+ }
166
+ async save(entry) {
167
+ try {
168
+ this.#entry.setPassword(JSON.stringify(entryToBlob(entry)));
169
+ }
170
+ catch (cause) {
171
+ wrapKeyringError("write", cause);
172
+ }
173
+ }
174
+ async load() {
175
+ let raw;
176
+ try {
177
+ raw = this.#entry.getPassword();
178
+ }
179
+ catch (cause) {
180
+ wrapKeyringError("read", cause);
181
+ }
182
+ const chain = parseAndMigrateBlob(raw);
183
+ if (!chain.ok)
184
+ return chain;
185
+ if (!isLatestBlob(chain.blob))
186
+ return { ok: false, reason: "corrupt" };
187
+ return { ok: true, entry: blobToEntry(chain.blob) };
188
+ }
189
+ async clear() {
190
+ try {
191
+ this.#entry.deletePassword();
192
+ }
193
+ catch (cause) {
194
+ wrapKeyringError("delete", cause);
195
+ }
196
+ }
197
+ }
198
+ 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.fb07beab",
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",
@@ -20,6 +20,9 @@
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",
25
28
  "c8": "^10.1.3",
@@ -29,6 +32,9 @@
29
32
  "scripts": {
30
33
  "build": "tsc",
31
34
  "test": "tsx --test 'src/**/*.test.ts'",
32
- "coverage": "c8 pnpm test"
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"
33
39
  }
34
40
  }