@skrr-ai/cli 0.1.79 → 0.1.80

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 (47) hide show
  1. package/dist/base-command.js +9 -2
  2. package/dist/commands/inbox/index.js +4 -2
  3. package/dist/commands/login.d.ts +8 -3
  4. package/dist/commands/login.js +14 -4
  5. package/dist/commands/spaces/workflows/events.js +9 -2
  6. package/dist/lib/agentic-stream.js +13 -4
  7. package/dist/lib/api-fetch.js +6 -2
  8. package/dist/lib/brokered-dpop.d.ts +34 -0
  9. package/dist/lib/brokered-dpop.js +89 -0
  10. package/dist/lib/daemonBroker.d.ts +71 -2
  11. package/dist/lib/daemonBroker.js +203 -36
  12. package/dist/lib/daemonBrokerRefusal.d.ts +26 -0
  13. package/dist/lib/daemonBrokerRefusal.js +83 -4
  14. package/dist/lib/daemonRestartWait.d.ts +39 -0
  15. package/dist/lib/daemonRestartWait.js +181 -0
  16. package/dist/lib/dedicated-service.js +10 -5
  17. package/dist/lib/dedicated-ssh.js +6 -3
  18. package/dist/lib/dedicated-terminal.js +6 -3
  19. package/dist/lib/dpop-auth.d.ts +35 -0
  20. package/dist/lib/dpop-auth.js +57 -0
  21. package/dist/lib/login.d.ts +6 -0
  22. package/dist/lib/login.js +13 -4
  23. package/dist/lib/node-adapter.js +4 -0
  24. package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/cliHandoffWire.d.ts +42 -1
  25. package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/cliHandoffWire.js +35 -3
  26. package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/credentialSession.d.ts +49 -1
  27. package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/credentialSession.js +47 -9
  28. package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/deviceKey.d.ts +56 -0
  29. package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/deviceKey.js +42 -20
  30. package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/dpop.d.ts +101 -0
  31. package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/dpop.js +179 -0
  32. package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/index.d.ts +1 -0
  33. package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/index.js +19 -3
  34. package/dist/node_modules/@skrr-ai/auth-core/dist/esm/cliHandoffWire.d.ts +42 -1
  35. package/dist/node_modules/@skrr-ai/auth-core/dist/esm/cliHandoffWire.js +32 -2
  36. package/dist/node_modules/@skrr-ai/auth-core/dist/esm/credentialSession.d.ts +49 -1
  37. package/dist/node_modules/@skrr-ai/auth-core/dist/esm/credentialSession.js +47 -9
  38. package/dist/node_modules/@skrr-ai/auth-core/dist/esm/deviceKey.d.ts +56 -0
  39. package/dist/node_modules/@skrr-ai/auth-core/dist/esm/deviceKey.js +39 -20
  40. package/dist/node_modules/@skrr-ai/auth-core/dist/esm/dpop.d.ts +101 -0
  41. package/dist/node_modules/@skrr-ai/auth-core/dist/esm/dpop.js +138 -0
  42. package/dist/node_modules/@skrr-ai/auth-core/dist/esm/index.d.ts +1 -0
  43. package/dist/node_modules/@skrr-ai/auth-core/dist/esm/index.js +4 -0
  44. package/dist/node_modules/@skrr-ai/auth-core/package.json +1 -1
  45. package/dist/node_modules/@skrr-ai/data-provider/index.js +3422 -3407
  46. package/oclif.manifest.json +31930 -31930
  47. package/package.json +2 -2
@@ -22,7 +22,7 @@
22
22
  * - `access_token`: broker mode — a short-lived access token from a
23
23
  * daemon-owned family; nothing durable leaves the daemon.
24
24
  */
25
- export declare const CLI_HANDOFF_MODES: readonly ["refresh_family", "access_token"];
25
+ export declare const CLI_HANDOFF_MODES: readonly ["refresh_family", "access_token", "access_token_dpop"];
26
26
  export type CliHandoffMode = (typeof CLI_HANDOFF_MODES)[number];
27
27
  /** Broker-mode redeem route on the daemon's loopback server. */
28
28
  export declare const CLI_HANDOFF_TOKEN_PATH = "/v1/auth/cli-handoff-token";
@@ -74,10 +74,26 @@ export declare function normalizeHandoffModes(value: unknown): CliHandoffMode[];
74
74
  */
75
75
  export declare function advertisedHandoffModes(d: CliHandoffDescriptor): CliHandoffMode[];
76
76
  export declare function descriptorOffersAccessToken(d: CliHandoffDescriptor): boolean;
77
+ /** True when the token route can serve DPoP-bound tokens (OSK-12020). */
78
+ export declare function descriptorOffersDpop(d: CliHandoffDescriptor): boolean;
77
79
  export interface CliHandoffTokenRequest {
78
80
  /** Bypass the daemon broker's cached token (the 401-recovery path). */
79
81
  forceRefresh?: boolean;
82
+ /**
83
+ * The caller can fetch per-request proofs from `CLI_HANDOFF_DPOP_PROOF_PATH`
84
+ * and wants a DPoP-bound token. Sent only to a descriptor that advertises
85
+ * `access_token_dpop`; a daemon that does not know the field ignores it and
86
+ * answers an unbound token, which `tokenType` then says.
87
+ */
88
+ dpop?: boolean;
80
89
  }
90
+ /**
91
+ * How the token must be presented. `DPoP` means the token carries `cnf.jkt`
92
+ * and every request needs a fresh proof; absent or `Bearer` means an ordinary
93
+ * bearer token. Read from the RESPONSE, never assumed from the request: an
94
+ * older server ignores the binding request and mints an unbound token.
95
+ */
96
+ export type CliHandoffTokenType = 'Bearer' | 'DPoP';
81
97
  export interface CliHandoffTokenResponse {
82
98
  accessToken: string;
83
99
  /** Absolute expiry in ms since epoch, when the daemon knows it. */
@@ -85,6 +101,31 @@ export interface CliHandoffTokenResponse {
85
101
  /** Server the minted credential belongs to — lets the caller verify it is
86
102
  * about to use a token for the API it thinks it is talking to. */
87
103
  serverUrl?: string;
104
+ tokenType?: CliHandoffTokenType;
88
105
  }
89
106
  /** Parse the redeem response; null when the token itself is absent. */
90
107
  export declare function parseCliHandoffTokenResponse(value: unknown): CliHandoffTokenResponse | null;
108
+ /** The daemon refuses an UNBOUND token because its posture requires binding. */
109
+ export declare const CLI_HANDOFF_ERROR_DPOP_REQUIRED = "HANDOFF_DPOP_REQUIRED";
110
+ /**
111
+ * The daemon signs RFC 9449 proofs for a bound token with a key that never
112
+ * leaves it. Same descriptor secret as the token route: using a stolen bound
113
+ * token therefore needs live loopback access to THIS daemon, which is the
114
+ * boundary the capability already has.
115
+ */
116
+ export declare const CLI_HANDOFF_DPOP_PROOF_PATH = "/v1/auth/cli-handoff-dpop-proof";
117
+ /** The oracle refused to sign (bad htm/htu, or `ath` names no token it issued). */
118
+ export declare const CLI_HANDOFF_ERROR_DPOP_PROOF_REFUSED = "HANDOFF_DPOP_PROOF_REFUSED";
119
+ /** The daemon holds no DPoP key (binding is off on this daemon). */
120
+ export declare const CLI_HANDOFF_ERROR_DPOP_UNAVAILABLE = "HANDOFF_DPOP_UNAVAILABLE";
121
+ export interface CliHandoffDpopProofRequest {
122
+ htm: string;
123
+ htu: string;
124
+ /** base64url SHA-256 of the bound access token. The token itself is never
125
+ * sent: the oracle commits to a hash, and only to one it issued. */
126
+ ath: string;
127
+ }
128
+ export interface CliHandoffDpopProofResponse {
129
+ proof: string;
130
+ }
131
+ export declare function parseCliHandoffDpopProofResponse(value: unknown): CliHandoffDpopProofResponse | null;
@@ -22,7 +22,7 @@
22
22
  * - `access_token`: broker mode — a short-lived access token from a
23
23
  * daemon-owned family; nothing durable leaves the daemon.
24
24
  */
25
- export const CLI_HANDOFF_MODES = ['refresh_family', 'access_token'];
25
+ export const CLI_HANDOFF_MODES = ['refresh_family', 'access_token', 'access_token_dpop'];
26
26
  /** Broker-mode redeem route on the daemon's loopback server. */
27
27
  export const CLI_HANDOFF_TOKEN_PATH = '/v1/auth/cli-handoff-token';
28
28
  /** Error codes this contract emits. String literals so both ends compare
@@ -88,7 +88,12 @@ export function advertisedHandoffModes(d) {
88
88
  return declared.length > 0 ? declared : ['refresh_family'];
89
89
  }
90
90
  export function descriptorOffersAccessToken(d) {
91
- return advertisedHandoffModes(d).includes('access_token');
91
+ const modes = advertisedHandoffModes(d);
92
+ return modes.includes('access_token') || modes.includes('access_token_dpop');
93
+ }
94
+ /** True when the token route can serve DPoP-bound tokens (OSK-12020). */
95
+ export function descriptorOffersDpop(d) {
96
+ return advertisedHandoffModes(d).includes('access_token_dpop');
92
97
  }
93
98
  /** Parse the redeem response; null when the token itself is absent. */
94
99
  export function parseCliHandoffTokenResponse(value) {
@@ -101,5 +106,30 @@ export function parseCliHandoffTokenResponse(value) {
101
106
  accessToken: v.accessToken,
102
107
  ...(typeof v.accessExpiresAt === 'number' ? { accessExpiresAt: v.accessExpiresAt } : {}),
103
108
  ...(typeof v.serverUrl === 'string' ? { serverUrl: v.serverUrl } : {}),
109
+ ...(v.tokenType === 'DPoP' || v.tokenType === 'Bearer' ? { tokenType: v.tokenType } : {}),
104
110
  };
105
111
  }
112
+ /** The daemon refuses an UNBOUND token because its posture requires binding. */
113
+ export const CLI_HANDOFF_ERROR_DPOP_REQUIRED = 'HANDOFF_DPOP_REQUIRED';
114
+ // ---------------------------------------------------------------------
115
+ // Proof oracle — POST CLI_HANDOFF_DPOP_PROOF_PATH (OSK-12020)
116
+ // ---------------------------------------------------------------------
117
+ /**
118
+ * The daemon signs RFC 9449 proofs for a bound token with a key that never
119
+ * leaves it. Same descriptor secret as the token route: using a stolen bound
120
+ * token therefore needs live loopback access to THIS daemon, which is the
121
+ * boundary the capability already has.
122
+ */
123
+ export const CLI_HANDOFF_DPOP_PROOF_PATH = '/v1/auth/cli-handoff-dpop-proof';
124
+ /** The oracle refused to sign (bad htm/htu, or `ath` names no token it issued). */
125
+ export const CLI_HANDOFF_ERROR_DPOP_PROOF_REFUSED = 'HANDOFF_DPOP_PROOF_REFUSED';
126
+ /** The daemon holds no DPoP key (binding is off on this daemon). */
127
+ export const CLI_HANDOFF_ERROR_DPOP_UNAVAILABLE = 'HANDOFF_DPOP_UNAVAILABLE';
128
+ export function parseCliHandoffDpopProofResponse(value) {
129
+ if (value == null || typeof value !== 'object')
130
+ return null;
131
+ const v = value;
132
+ if (typeof v.proof !== 'string' || v.proof.split('.').length !== 3)
133
+ return null;
134
+ return { proof: v.proof };
135
+ }
@@ -13,6 +13,20 @@
13
13
  * Deliberately thin: no persistence, no retry policy, no logging. Callers
14
14
  * own their store and their error surfaces; this owns the cache/single-
15
15
  * flight/invalidate invariants so they cannot drift between surfaces.
16
+ *
17
+ * Two policies are options because mechanisms genuinely need different ones,
18
+ * and both default to the original behaviour:
19
+ *
20
+ * - `forcedAcquire: 'join'` — a forced acquire joins whatever is in flight,
21
+ * for a mechanism where a second concurrent renewal is itself the hazard
22
+ * (a refresh-family rotation or a family mint);
23
+ * - `classifyAcquireError` — stale-on-degrade: a TRANSIENT failure serves the
24
+ * still-unexpired cached credential instead of failing, a PERMANENT one
25
+ * drops it.
26
+ *
27
+ * Mechanism-specific limits — how often a new family may be minted, which
28
+ * failures retire it — stay in the mechanism. The daemon's task token broker
29
+ * (`daemon/src/task/token-broker.ts`) is the consumer that uses both options.
16
30
  */
17
31
  /** What an acquire returns. `expiresAtMs` absent means "no known expiry" —
18
32
  * the token is served until `invalidate()` or an acquire replaces it. */
@@ -26,13 +40,45 @@ export interface CredentialSessionOptions {
26
40
  * The mechanism. Called with `forceRefresh: true` when the caller asked to
27
41
  * bypass the cache (the 401-recovery path) — implementations should renew
28
42
  * rather than serve their own cache. A rejected promise propagates to every
29
- * waiter and is never cached.
43
+ * waiter and is never cached (`classifyAcquireError` is the one way a waiter
44
+ * can be served the previous credential instead).
30
45
  */
31
46
  acquire: (forceRefresh: boolean) => Promise<AcquiredCredential>;
32
47
  /** Serve the cached token until this many ms before expiry. Default 30s. */
33
48
  skewMs?: number;
34
49
  /** Injectable clock for tests. */
35
50
  now?: () => number;
51
+ /**
52
+ * What a forced acquire does when an acquire is already in flight.
53
+ *
54
+ * - `'supersede'` (default): a forced acquire never joins a NON-forced
55
+ * in-flight one — that could serve the very credential a 401 just
56
+ * invalidated — so it starts a second operation, and the superseded one
57
+ * cannot write its result back. Right for a mechanism whose plain
58
+ * acquire may serve its own cache.
59
+ * - `'join'`: every acquire, forced or not, joins the one in flight. Right
60
+ * — and required — for a mechanism whose acquire ALWAYS renews (it never
61
+ * serves a cache of its own), and for which a second concurrent renewal
62
+ * is itself the hazard: a rotation that burns a refresh family, a mint
63
+ * that creates a new one. Whatever is in flight was started after the
64
+ * credential being replaced was issued, so its result is not that
65
+ * credential.
66
+ */
67
+ forcedAcquire?: 'supersede' | 'join';
68
+ /**
69
+ * Classify an acquire failure. Without it every failure propagates and the
70
+ * cache is left as it was (the default).
71
+ *
72
+ * - `'transient'` — the renewal failed but the cached credential is still
73
+ * good: a waiter is served the cached token while it is inside its HARD
74
+ * expiry (the skew window only says when to start renewing), instead of
75
+ * the error. Nothing stale is served after `invalidate()`, after a forced
76
+ * acquire discarded it, or once it has actually expired.
77
+ * - `'permanent'` — the credential is dead upstream: the cache is dropped
78
+ * and the error propagates.
79
+ * - `undefined` — not classified: the default behaviour.
80
+ */
81
+ classifyAcquireError?: (err: unknown) => 'transient' | 'permanent' | undefined;
36
82
  }
37
83
  export interface CredentialSession {
38
84
  /**
@@ -41,6 +87,8 @@ export interface CredentialSession {
41
87
  * discards the cache first (use after a resource 401).
42
88
  */
43
89
  getToken(forceRefresh?: boolean): Promise<string>;
90
+ /** Same as getToken, but resolves the whole credential (with its expiry). */
91
+ getCredential(forceRefresh?: boolean): Promise<AcquiredCredential>;
44
92
  /** Drop the cached token; the next getToken() acquires. */
45
93
  invalidate(): void;
46
94
  }
@@ -13,11 +13,27 @@
13
13
  * Deliberately thin: no persistence, no retry policy, no logging. Callers
14
14
  * own their store and their error surfaces; this owns the cache/single-
15
15
  * flight/invalidate invariants so they cannot drift between surfaces.
16
+ *
17
+ * Two policies are options because mechanisms genuinely need different ones,
18
+ * and both default to the original behaviour:
19
+ *
20
+ * - `forcedAcquire: 'join'` — a forced acquire joins whatever is in flight,
21
+ * for a mechanism where a second concurrent renewal is itself the hazard
22
+ * (a refresh-family rotation or a family mint);
23
+ * - `classifyAcquireError` — stale-on-degrade: a TRANSIENT failure serves the
24
+ * still-unexpired cached credential instead of failing, a PERMANENT one
25
+ * drops it.
26
+ *
27
+ * Mechanism-specific limits — how often a new family may be minted, which
28
+ * failures retire it — stay in the mechanism. The daemon's task token broker
29
+ * (`daemon/src/task/token-broker.ts`) is the consumer that uses both options.
16
30
  */
17
31
  const DEFAULT_SKEW_MS = 30_000;
18
32
  export function createCredentialSession(opts) {
19
33
  const skewMs = opts.skewMs ?? DEFAULT_SKEW_MS;
20
34
  const now = opts.now ?? Date.now;
35
+ const forcedJoins = opts.forcedAcquire === 'join';
36
+ const classify = opts.classifyAcquireError;
21
37
  let cached = null;
22
38
  let inFlight = null;
23
39
  let inFlightForced = false;
@@ -26,17 +42,26 @@ export function createCredentialSession(opts) {
26
42
  // result back over the newer one.
27
43
  let generation = 0;
28
44
  const fresh = (t) => t.expiresAtMs == null || now() < t.expiresAtMs - skewMs;
45
+ /** Not yet actually expired — the bar for serving a stale credential. */
46
+ const unexpired = (t) => t.expiresAtMs == null || now() < t.expiresAtMs;
29
47
  const acquireShared = (force) => {
30
- // A forced acquire must not join a non-forced in-flight one — that would
31
- // serve the very credential a 401 just invalidated. A forced in-flight
32
- // DOES satisfy non-forced waiters: it is already renewing.
33
- if (inFlight && (inFlightForced || !force))
48
+ // Under 'supersede', a forced acquire must not join a non-forced in-flight
49
+ // one — that would serve the very credential a 401 just invalidated. A
50
+ // forced in-flight DOES satisfy non-forced waiters: it is already renewing.
51
+ // Under 'join', everything joins (see `forcedAcquire`).
52
+ if (inFlight && (forcedJoins || inFlightForced || !force))
34
53
  return inFlight;
35
54
  const gen = ++generation;
36
55
  const p = opts.acquire(force).then((t) => {
37
56
  if (gen === generation)
38
57
  cached = t;
39
58
  return t;
59
+ }, (err) => {
60
+ // Settled before any waiter resumes, so a waiter's stale check below
61
+ // already sees a permanent failure's dropped cache.
62
+ if (gen === generation && classify?.(err) === 'permanent')
63
+ cached = null;
64
+ throw err;
40
65
  });
41
66
  inFlight = p;
42
67
  inFlightForced = force;
@@ -51,13 +76,26 @@ export function createCredentialSession(opts) {
51
76
  p.then(cleanup, cleanup);
52
77
  return p;
53
78
  };
79
+ const getCredential = async (forceRefresh = false) => {
80
+ if (forceRefresh)
81
+ cached = null;
82
+ if (cached && fresh(cached))
83
+ return cached;
84
+ try {
85
+ return await acquireShared(forceRefresh);
86
+ }
87
+ catch (err) {
88
+ // Stale-on-degrade: read `cached` AFTER the failure, so an invalidate()
89
+ // or a forced discard that happened meanwhile is honoured.
90
+ if (classify?.(err) === 'transient' && cached && unexpired(cached))
91
+ return cached;
92
+ throw err;
93
+ }
94
+ };
54
95
  return {
96
+ getCredential,
55
97
  async getToken(forceRefresh = false) {
56
- if (forceRefresh)
57
- cached = null;
58
- if (cached && fresh(cached))
59
- return cached.accessToken;
60
- return (await acquireShared(forceRefresh)).accessToken;
98
+ return (await getCredential(forceRefresh)).accessToken;
61
99
  },
62
100
  invalidate() {
63
101
  cached = null;
@@ -1,3 +1,40 @@
1
+ /**
2
+ * deviceKey.ts — L11 Phase 1: Ed25519 device identity + DPoP-style proofs.
3
+ *
4
+ * The load-bearing recommendation in `docs/daemon-auth-cc-deep-comparison-
5
+ * 2026-04-24.md` §L11. Two-axis identity:
6
+ *
7
+ * user identity = OAuth refresh token (server-rotated, scope-pinned, reuse-detected)
8
+ * device identity = Ed25519 keypair (device-generated, server-revocable)
9
+ *
10
+ * Every daemon→server request carries a JWS signature over
11
+ * `{htm, htu, iat, jti, rth?, ath?}` using the device key. Server validates
12
+ * against the public key registered for this daemon.
13
+ *
14
+ * Stolen credentials require BOTH halves:
15
+ * - refresh token alone is useless without the device key
16
+ * - device key alone is useless without the refresh token
17
+ *
18
+ * Why DPoP-shaped (RFC 9449) rather than rolling our own JWS:
19
+ * - The htm/htu/iat/jti/ath claim set is well-studied. Server-side
20
+ * replay defenses, clock-skew handling, and audit-trail formats all
21
+ * have a body of prior art to copy from.
22
+ * - Future interop: if we ever expose OverSky as a third-party OAuth
23
+ * resource server, DPoP-bearer is the way clients will already
24
+ * understand.
25
+ *
26
+ * Why we extend with `rth` (refresh-token-hash):
27
+ * - RFC 9449 only mints `ath` (access-token-hash). The whole point of
28
+ * L11 is to bind the *refresh* token to the device, so the proof
29
+ * accompanying a refresh request must commit to the refresh token.
30
+ * `rth` is structurally identical to `ath`; the name disambiguates.
31
+ *
32
+ * What this module is NOT responsible for:
33
+ * - Persistence (lives in `daemon/src/deviceIdentity.ts`).
34
+ * - Server-side public-key registration (separate API in a later PR).
35
+ * - Wire-up to the daemon's outgoing HTTP path (Phase 2 of L11).
36
+ */
37
+ import crypto from 'node:crypto';
1
38
  /**
2
39
  * Ed25519 JSON Web Key — RFC 8037 §2 ("Key Type 'OKP'").
3
40
  *
@@ -123,6 +160,18 @@ export interface BuildProofOptions {
123
160
  * not a silent fallback.
124
161
  */
125
162
  export declare function buildProof(privateJwk: Ed25519PrivateJwk, publicJwk: Ed25519PublicJwk, opts: BuildProofOptions): string;
163
+ /** The header `typ` of an OverSky device-identity proof. */
164
+ export declare const DEVICE_PROOF_TYP = "oversky-dpop+jwt";
165
+ /**
166
+ * Sign a DPoP-shaped proof with an Ed25519 key under the given header `typ`.
167
+ *
168
+ * @internal Shared by the device-identity proof (`oversky-dpop+jwt`, above)
169
+ * and the RFC 9449 access-token proof (`dpop+jwt`, `dpop.ts`). `typ` is the
170
+ * only thing on the wire that separates the two protocols, which is why it is
171
+ * a required argument rather than a default: a proof signed for one must
172
+ * never verify as the other.
173
+ */
174
+ export declare function signEd25519Proof(privateKey: crypto.KeyObject, publicJwk: Ed25519PublicJwk, typ: string, opts: BuildProofOptions): string;
126
175
  export type VerifyFailureReason = 'malformed' | 'bad_alg' | 'bad_typ' | 'bad_jwk' | 'sig_invalid' | 'iat_old' | 'iat_future' | 'htm_mismatch' | 'htu_mismatch' | 'rth_mismatch' | 'ath_mismatch' | 'pin_mismatch';
127
176
  export interface VerifySuccess {
128
177
  valid: true;
@@ -165,3 +214,10 @@ export interface VerifyOptions {
165
214
  * `sig_invalid` to keep the failure surface narrow.
166
215
  */
167
216
  export declare function verifyProof(jws: string, opts?: VerifyOptions): VerifySuccess | VerifyFailure;
217
+ /**
218
+ * Verify a DPoP-shaped Ed25519 proof whose header `typ` must equal `typ`.
219
+ *
220
+ * @internal See `signEd25519Proof`. `verifyProof` (device identity) and
221
+ * `verifyDpopProof` (`dpop.ts`, RFC 9449) are the two public doors.
222
+ */
223
+ export declare function verifyEd25519Proof(jws: string, opts: VerifyOptions, typ: string): VerifySuccess | VerifyFailure;
@@ -152,17 +152,40 @@ export function normalizeHtu(url) {
152
152
  * not a silent fallback.
153
153
  */
154
154
  export function buildProof(privateJwk, publicJwk, opts) {
155
+ if (publicJwk.x !== privateJwk.x) {
156
+ // Defensive — public and private must come from the same keypair.
157
+ // A mismatch indicates a corrupted in-memory state.
158
+ throw new Error('buildProof: public/private JWK x mismatch (corrupted keypair)');
159
+ }
160
+ // Cast to crypto's `JsonWebKey` shape — that type requires an index
161
+ // signature for arbitrary string keys; our narrow OKP shape doesn't
162
+ // declare one because the strict shape is more useful at our API
163
+ // boundary. Cast through `unknown` to silence the structural mismatch
164
+ // without losing the rest of the type safety on this surface.
165
+ const privateKey = crypto.createPrivateKey({
166
+ format: 'jwk',
167
+ key: privateJwk,
168
+ });
169
+ return signEd25519Proof(privateKey, publicJwk, DEVICE_PROOF_TYP, opts);
170
+ }
171
+ /** The header `typ` of an OverSky device-identity proof. */
172
+ export const DEVICE_PROOF_TYP = 'oversky-dpop+jwt';
173
+ /**
174
+ * Sign a DPoP-shaped proof with an Ed25519 key under the given header `typ`.
175
+ *
176
+ * @internal Shared by the device-identity proof (`oversky-dpop+jwt`, above)
177
+ * and the RFC 9449 access-token proof (`dpop+jwt`, `dpop.ts`). `typ` is the
178
+ * only thing on the wire that separates the two protocols, which is why it is
179
+ * a required argument rather than a default: a proof signed for one must
180
+ * never verify as the other.
181
+ */
182
+ export function signEd25519Proof(privateKey, publicJwk, typ, opts) {
155
183
  if (typeof opts.method !== 'string' || opts.method.length === 0) {
156
184
  throw new Error('buildProof: method must be a non-empty string');
157
185
  }
158
186
  if (typeof opts.url !== 'string' || opts.url.length === 0) {
159
187
  throw new Error('buildProof: url must be a non-empty string');
160
188
  }
161
- if (publicJwk.x !== privateJwk.x) {
162
- // Defensive — public and private must come from the same keypair.
163
- // A mismatch indicates a corrupted in-memory state.
164
- throw new Error('buildProof: public/private JWK x mismatch (corrupted keypair)');
165
- }
166
189
  const claims = {
167
190
  htm: opts.method.toUpperCase(),
168
191
  htu: normalizeHtu(opts.url),
@@ -173,23 +196,10 @@ export function buildProof(privateJwk, publicJwk, opts) {
173
196
  claims.rth = tokenHash(opts.refreshToken);
174
197
  if (opts.accessToken !== undefined)
175
198
  claims.ath = tokenHash(opts.accessToken);
176
- const header = {
177
- alg: 'EdDSA',
178
- typ: 'oversky-dpop+jwt',
179
- jwk: publicJwk,
180
- };
199
+ const header = { alg: 'EdDSA', typ, jwk: publicJwk };
181
200
  const headerB64 = b64u(Buffer.from(JSON.stringify(header), 'utf-8'));
182
201
  const payloadB64 = b64u(Buffer.from(JSON.stringify(claims), 'utf-8'));
183
202
  const signingInput = `${headerB64}.${payloadB64}`;
184
- // Cast to crypto's `JsonWebKey` shape — that type requires an index
185
- // signature for arbitrary string keys; our narrow OKP shape doesn't
186
- // declare one because the strict shape is more useful at our API
187
- // boundary. Cast through `unknown` to silence the structural mismatch
188
- // without losing the rest of the type safety on this surface.
189
- const privateKey = crypto.createPrivateKey({
190
- format: 'jwk',
191
- key: privateJwk,
192
- });
193
203
  // EdDSA over Ed25519 — Node's crypto.sign expects `null` as the
194
204
  // digest algorithm because Ed25519 hashes the message internally.
195
205
  const signature = crypto.sign(null, Buffer.from(signingInput, 'utf-8'), privateKey);
@@ -205,6 +215,15 @@ export function buildProof(privateJwk, publicJwk, opts) {
205
215
  * `sig_invalid` to keep the failure surface narrow.
206
216
  */
207
217
  export function verifyProof(jws, opts = {}) {
218
+ return verifyEd25519Proof(jws, opts, DEVICE_PROOF_TYP);
219
+ }
220
+ /**
221
+ * Verify a DPoP-shaped Ed25519 proof whose header `typ` must equal `typ`.
222
+ *
223
+ * @internal See `signEd25519Proof`. `verifyProof` (device identity) and
224
+ * `verifyDpopProof` (`dpop.ts`, RFC 9449) are the two public doors.
225
+ */
226
+ export function verifyEd25519Proof(jws, opts, typ) {
208
227
  if (typeof jws !== 'string' || jws.length === 0) {
209
228
  return { valid: false, reason: 'malformed' };
210
229
  }
@@ -225,7 +244,7 @@ export function verifyProof(jws, opts = {}) {
225
244
  if (!header || typeof header !== 'object' || header.alg !== 'EdDSA') {
226
245
  return { valid: false, reason: 'bad_alg' };
227
246
  }
228
- if (header.typ !== 'oversky-dpop+jwt') {
247
+ if (header.typ !== typ) {
229
248
  return { valid: false, reason: 'bad_typ' };
230
249
  }
231
250
  const headerJwk = header.jwk;
@@ -0,0 +1,101 @@
1
+ /**
2
+ * dpop.ts — RFC 9449 proof-of-possession for ACCESS tokens.
3
+ *
4
+ * Sibling of `deviceKey.ts`, and deliberately a different protocol on the
5
+ * wire. A device-identity proof (`typ: oversky-dpop+jwt`, header
6
+ * `OverSky-DPoP`) proves a daemon holds its ENROLLED key and authorizes
7
+ * minting/rotating that daemon's families. An access-token proof
8
+ * (`typ: dpop+jwt`, header `DPoP`) proves the presenter of an access token
9
+ * holds the key the token is bound to (`cnf.jkt`). The `typ` keeps either
10
+ * proof from ever verifying as the other.
11
+ *
12
+ * Why a SEPARATE key, never the device key (OSK-12020): the access-token key
13
+ * signs whatever `{htm, htu, ath}` a local caller asks of the daemon's proof
14
+ * oracle (`/v1/auth/cli-handoff-dpop-proof`). Handing that oracle the device
15
+ * key would turn it into a signer for the credential that mints families —
16
+ * one confused-deputy step from the workload asking for a device proof over
17
+ * `POST /api/daemons/cli-handoff`. A dedicated key carries no authority but
18
+ * the tokens bound to it, lives only in daemon memory (`createDpopKey` never
19
+ * exposes the private half), and dies with the family it binds.
20
+ *
21
+ * Algorithm: EdDSA / Ed25519, the same primitive as the device key. RFC 9449
22
+ * lets the server advertise `dpop_signing_alg_values_supported`; ours is
23
+ * exactly `["EdDSA"]`, and the verifier refuses anything else.
24
+ */
25
+ import { type DeviceProofClaims, type Ed25519PublicJwk, type VerifyFailureReason } from './deviceKey.js';
26
+ /** RFC 9449 §4.2 — the proof's JOSE header `typ`. */
27
+ export declare const DPOP_PROOF_TYP = "dpop+jwt";
28
+ /** RFC 9449 §4.1 — the request header carrying the proof. */
29
+ export declare const DPOP_HEADER = "DPoP";
30
+ /** Default freshness window, matching the device-proof verifier. */
31
+ export declare const DPOP_MAX_AGE_SECONDS = 60;
32
+ /** Server error codes for a bound token presented without a usable proof. */
33
+ export declare const DPOP_PROOF_REQUIRED = "DPOP_PROOF_REQUIRED";
34
+ export declare const DPOP_PROOF_INVALID = "DPOP_PROOF_INVALID";
35
+ export declare const DPOP_PROOF_VERIFY_UNAVAILABLE = "DPOP_PROOF_VERIFY_UNAVAILABLE";
36
+ /** A proof's claims: RFC 9449's `{htm, htu, iat, jti, ath?}`. */
37
+ export type DpopProofClaims = Pick<DeviceProofClaims, 'htm' | 'htu' | 'iat' | 'jti' | 'ath'>;
38
+ export interface DpopProofRequest {
39
+ /** HTTP method of the request the proof will accompany. */
40
+ htm: string;
41
+ /** Target URI; query and fragment are stripped (RFC 9449 §4.2). */
42
+ htu: string;
43
+ /** The access token the proof commits to (hashed into `ath`). Omitted only
44
+ * at the token endpoint, where the token does not exist yet. */
45
+ accessToken?: string;
46
+ /** Pre-computed `ath`, for a signer that never sees the token itself. */
47
+ ath?: string;
48
+ /** Test hooks. */
49
+ iat?: number;
50
+ jti?: string;
51
+ }
52
+ /**
53
+ * An in-memory DPoP key. The private half lives in this closure and nowhere
54
+ * else: there is no accessor, no serializer, and it is never written to disk.
55
+ */
56
+ export interface DpopKey {
57
+ readonly publicJwk: Ed25519PublicJwk;
58
+ /** RFC 7638 thumbprint — the value a bound token carries as `cnf.jkt`. */
59
+ readonly thumbprint: string;
60
+ sign(req: DpopProofRequest): string;
61
+ }
62
+ export declare function createDpopKey(): DpopKey;
63
+ /** RFC 9449 §4.2 htu normalization — identical to the device proof's. */
64
+ export declare function normalizeDpopHtu(url: string): string;
65
+ /** `ath` for an access token: base64url(SHA-256(ascii(token))), RFC 9449 §4.2. */
66
+ export declare function accessTokenHash(accessToken: string): string;
67
+ /** True for a value shaped like an `ath` / `jkt` (43-char base64url SHA-256). */
68
+ export declare function isAth(value: unknown): value is string;
69
+ /** Same shape test, named for the `cnf.jkt` call sites. */
70
+ export declare const isJwkThumbprint: typeof isAth;
71
+ export type DpopVerifyFailureReason = Exclude<VerifyFailureReason, 'rth_mismatch' | 'pin_mismatch'> | 'ath_missing' | 'jkt_mismatch';
72
+ export interface DpopVerifyOptions {
73
+ expectedHtm: string;
74
+ expectedHtu: string;
75
+ /** `ath` the proof must carry. Required for a resource request. */
76
+ expectedAth?: string;
77
+ /** Thumbprint the proof's key must have — the token's `cnf.jkt`. */
78
+ expectedJkt?: string;
79
+ maxAgeSeconds?: number;
80
+ clockSkewSeconds?: number;
81
+ now?: number;
82
+ }
83
+ export type DpopVerifyResult = {
84
+ valid: true;
85
+ claims: DpopProofClaims;
86
+ thumbprint: string;
87
+ publicJwk: Ed25519PublicJwk;
88
+ } | {
89
+ valid: false;
90
+ reason: DpopVerifyFailureReason;
91
+ };
92
+ /**
93
+ * Verify an RFC 9449 proof: `typ` dpop+jwt, `alg` EdDSA, a well-formed inline
94
+ * JWK, the signature, iat window, htm, htu, `ath` (when expected) and the
95
+ * key's thumbprint against `expectedJkt` (when given).
96
+ *
97
+ * Replay (`jti`) is NOT checked here — it needs shared state, and the caller
98
+ * owns that (the API's Redis jti cache). A verifier that claimed to do it in
99
+ * process memory would be wrong on every multi-replica deployment.
100
+ */
101
+ export declare function verifyDpopProof(jws: string, opts: DpopVerifyOptions): DpopVerifyResult;