@deque/axe-auth 1.1.0-next.6ad261c8 → 1.1.0-next.759bd5c5

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 (61) hide show
  1. package/README.md +64 -11
  2. package/dist/cli/commonArgs.d.ts +66 -0
  3. package/dist/cli/commonArgs.help.d.ts +2 -0
  4. package/dist/cli/commonArgs.help.js +19 -0
  5. package/dist/cli/commonArgs.js +119 -0
  6. package/dist/cli/confirm.d.ts +17 -0
  7. package/dist/cli/confirm.js +56 -0
  8. package/dist/cli/errors.d.ts +30 -0
  9. package/dist/cli/errors.js +52 -0
  10. package/dist/cli/testUtils.d.ts +52 -0
  11. package/dist/cli/testUtils.js +100 -0
  12. package/dist/cli/types.d.ts +82 -0
  13. package/dist/cli/types.js +2 -0
  14. package/dist/commands/login.d.ts +41 -0
  15. package/dist/commands/login.help.d.ts +2 -0
  16. package/dist/commands/login.help.js +35 -0
  17. package/dist/commands/login.js +93 -0
  18. package/dist/commands/logout.d.ts +24 -0
  19. package/dist/commands/logout.help.d.ts +2 -0
  20. package/dist/commands/logout.help.js +37 -0
  21. package/dist/commands/logout.js +84 -0
  22. package/dist/commands/token.d.ts +26 -0
  23. package/dist/commands/token.help.d.ts +2 -0
  24. package/dist/commands/token.help.js +41 -0
  25. package/dist/commands/token.js +56 -0
  26. package/dist/index.js +142 -22
  27. package/dist/oauth/authorizationURL.d.ts +29 -0
  28. package/dist/oauth/authorizationURL.js +52 -0
  29. package/dist/oauth/authorize.d.ts +84 -0
  30. package/dist/oauth/authorize.js +118 -0
  31. package/dist/oauth/discoverOIDC.d.ts +50 -0
  32. package/dist/oauth/discoverOIDC.js +143 -0
  33. package/dist/oauth/errors.d.ts +55 -2
  34. package/dist/oauth/errors.js +35 -1
  35. package/dist/oauth/getValidAccessToken.d.ts +89 -0
  36. package/dist/oauth/getValidAccessToken.js +139 -0
  37. package/dist/oauth/index.d.ts +14 -2
  38. package/dist/oauth/index.js +13 -1
  39. package/dist/oauth/issuerURL.d.ts +22 -0
  40. package/dist/oauth/issuerURL.js +38 -0
  41. package/dist/oauth/keyringBinding.d.ts +22 -0
  42. package/dist/oauth/keyringBinding.js +41 -0
  43. package/dist/oauth/openBrowser.d.ts +19 -0
  44. package/dist/oauth/openBrowser.js +78 -0
  45. package/dist/oauth/pkce.d.ts +17 -0
  46. package/dist/oauth/pkce.js +43 -0
  47. package/dist/oauth/predicates.d.ts +7 -0
  48. package/dist/oauth/predicates.js +15 -0
  49. package/dist/oauth/refreshTokens.d.ts +30 -0
  50. package/dist/oauth/refreshTokens.js +61 -0
  51. package/dist/oauth/revokeToken.d.ts +28 -0
  52. package/dist/oauth/revokeToken.js +59 -0
  53. package/dist/oauth/testUtils.d.ts +35 -0
  54. package/dist/oauth/testUtils.js +61 -0
  55. package/dist/oauth/tokenExchange.d.ts +26 -0
  56. package/dist/oauth/tokenExchange.js +42 -0
  57. package/dist/oauth/tokenResponse.d.ts +54 -0
  58. package/dist/oauth/tokenResponse.js +121 -0
  59. package/dist/oauth/tokenStore.d.ts +111 -0
  60. package/dist/oauth/tokenStore.js +198 -0
  61. package/package.json +11 -2
@@ -0,0 +1,111 @@
1
+ import { type KeyringEntryFactory } from "./keyringBinding";
2
+ import type { TokenSet } from "./tokenResponse";
3
+ /**
4
+ * Current on-disk blob schema version. Exported so consumers can
5
+ * display "stored v:N, expected v:M" diagnostics when `load()` returns
6
+ * a `version-mismatch` result.
7
+ */
8
+ export declare const STORED_BLOB_VERSION = 1;
9
+ /**
10
+ * What `KeyringTokenStore` persists: the OAuth tokens plus the
11
+ * issuer/client coordinates they were minted against. Carrying the
12
+ * coordinates inside the entry means a verb can recover its full
13
+ * config from the keychain alone, with no separate "default issuer"
14
+ * pointer.
15
+ */
16
+ export interface StoredEntry {
17
+ tokens: TokenSet;
18
+ /** OIDC issuer URL the tokens were minted against. */
19
+ issuerURL: string;
20
+ /** OAuth client ID used at login. */
21
+ clientId: string;
22
+ /** Whether the original login allowed a non-loopback http issuer. */
23
+ allowInsecureIssuer: boolean;
24
+ }
25
+ /**
26
+ * Outcome of a `TokenStore.load()` call.
27
+ *
28
+ * Note on downgrades: the migrator chain only walks *forward*. A user
29
+ * who downgrades `axe-auth` to a release that predates a schema bump
30
+ * will see `version-mismatch` on any blob written by the newer
31
+ * release, even if the change was strictly additive. That is the safe
32
+ * default for a credentials blob — the older version cannot vouch for
33
+ * the meaning of fields it has never seen. Callers hitting this case
34
+ * should treat it as "re-authenticate" rather than attempting to
35
+ * parse an unknown future shape.
36
+ */
37
+ export type LoadResult = {
38
+ ok: true;
39
+ entry: StoredEntry;
40
+ } | {
41
+ ok: false;
42
+ reason: "empty";
43
+ } | {
44
+ ok: false;
45
+ reason: "corrupt";
46
+ } | {
47
+ ok: false;
48
+ reason: "version-mismatch";
49
+ storedVersion: number;
50
+ };
51
+ /** Persistence layer for an OAuth `StoredEntry`. */
52
+ export interface TokenStore {
53
+ /** Write-through save. Replaces any previously stored entry. */
54
+ save(entry: StoredEntry): Promise<void>;
55
+ /**
56
+ * Reads the stored entry and returns a structured result.
57
+ *
58
+ * Callers should branch on `result.ok` first. When `ok` is `false`,
59
+ * `reason` tells them *why* there is no usable entry: `empty`
60
+ * (nothing stored), `corrupt` (unparseable or shape-invalid), or
61
+ * `version-mismatch` (stored under a schema we cannot migrate from).
62
+ * The library does not emit output on these cases — surfacing them
63
+ * to the user is the caller's responsibility.
64
+ */
65
+ load(): Promise<LoadResult>;
66
+ /** Removes any stored entry. No-op if none is present. */
67
+ clear(): Promise<void>;
68
+ }
69
+ /**
70
+ * Outcome of `parseAndMigrateBlob`: same set of failure reasons as
71
+ * `LoadResult`, but on success carries the post-migration blob as an
72
+ * unknown payload. The caller is responsible for shape-validating
73
+ * that payload against the latest schema.
74
+ */
75
+ export type BlobChainResult = {
76
+ ok: true;
77
+ blob: unknown;
78
+ } | {
79
+ ok: false;
80
+ reason: "empty";
81
+ } | {
82
+ ok: false;
83
+ reason: "corrupt";
84
+ } | {
85
+ ok: false;
86
+ reason: "version-mismatch";
87
+ storedVersion: number;
88
+ };
89
+ /**
90
+ * JSON-parses the raw keychain password and walks the migrator chain
91
+ * until it reaches `expectedVersion`. Exported with `expectedVersion`
92
+ * and `migrators` parameters only for testing the chain mechanics
93
+ * against synthetic versions / migrators; production callers use
94
+ * `KeyringTokenStore.load()`, which feeds in `STORED_BLOB_VERSION`
95
+ * and `MIGRATORS` and applies the latest-shape check on top.
96
+ */
97
+ export declare function parseAndMigrateBlob(raw: string | null, expectedVersion?: number, migrators?: ReadonlyMap<number, (old: unknown) => unknown | null>): BlobChainResult;
98
+ /**
99
+ * `TokenStore` backed by the operating system's native keychain via
100
+ * `@napi-rs/keyring` (macOS Keychain, Windows Credential Manager, Linux
101
+ * Secret Service). One entry per machine, keyed by a fixed account
102
+ * name; the blob carries its own issuer/client coordinates so verbs
103
+ * can recover full config without per-issuer keying.
104
+ */
105
+ export declare class KeyringTokenStore implements TokenStore {
106
+ #private;
107
+ constructor(entryFactory?: KeyringEntryFactory);
108
+ save(entry: StoredEntry): Promise<void>;
109
+ load(): Promise<LoadResult>;
110
+ clear(): Promise<void>;
111
+ }
@@ -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.6ad261c8",
3
+ "version": "1.1.0-next.759bd5c5",
4
4
  "description": "CLI authentication utility for Deque services",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "commonjs",
@@ -20,6 +20,11 @@
20
20
  "engines": {
21
21
  "node": ">=22.13.0"
22
22
  },
23
+ "dependencies": {
24
+ "@napi-rs/keyring": "^1.2.0",
25
+ "remove-trailing-slash": "^0.1.1",
26
+ "ts-dedent": "^2.2.0"
27
+ },
23
28
  "devDependencies": {
24
29
  "@types/node": "^22.13.10",
25
30
  "c8": "^10.1.3",
@@ -29,6 +34,10 @@
29
34
  "scripts": {
30
35
  "build": "tsc",
31
36
  "test": "tsx --test 'src/**/*.test.ts'",
32
- "coverage": "c8 pnpm test"
37
+ "coverage": "c8 pnpm test",
38
+ "register-dev-client": "tsx scripts/registerDevClient.ts",
39
+ "smoke-authorize": "tsx scripts/smokeAuthorize.ts",
40
+ "smoke-cli": "tsx scripts/smokeCLI.ts",
41
+ "manual-authorize": "tsx scripts/manualAuthorize.ts"
33
42
  }
34
43
  }