@atol-sh/js 0.1.0 → 0.1.1
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.
- package/CHANGELOG.md +16 -0
- package/dist/index.cjs +22 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +287 -250
- package/dist/index.d.ts +287 -250
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,256 @@
|
|
|
1
1
|
import { JWTPayload } from 'jose';
|
|
2
2
|
import { UserManager, User } from 'oidc-client-ts';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* DPoP (RFC 9449) client implementation.
|
|
6
|
+
*
|
|
7
|
+
* This module owns the per-tab ES256 keypair used for sender-constrained
|
|
8
|
+
* tokens, plus the proof-JWT builder that clients send on every
|
|
9
|
+
* /auth/token call (and, in future, on every resource-server request).
|
|
10
|
+
*
|
|
11
|
+
* Design choices:
|
|
12
|
+
* - Keys are non-exportable CryptoKey objects (`extractable: false`).
|
|
13
|
+
* XSS can use them to sign but cannot exfiltrate the private bytes.
|
|
14
|
+
* - Keys are regenerated per-tab on provider mount. A page reload ==
|
|
15
|
+
* a new keypair. The cost is one extra round-trip after reload;
|
|
16
|
+
* the benefit is no persistence surface for key material at all.
|
|
17
|
+
* - ES256 / P-256 only. Universal WebCrypto support, matches what the
|
|
18
|
+
* server's DPoP validator accepts first.
|
|
19
|
+
*
|
|
20
|
+
* The module is a no-op fallback when WebCrypto is unavailable (SSR,
|
|
21
|
+
* extremely old browsers). Callers treat a null manager as "Bearer mode".
|
|
22
|
+
*/
|
|
23
|
+
/** Optional per-proof inputs (RFC 9449 resource access + server nonce). */
|
|
24
|
+
interface DPoPProofOptions {
|
|
25
|
+
/**
|
|
26
|
+
* Access token being presented. When set, the proof carries an `ath`
|
|
27
|
+
* claim (base64url(SHA-256(accessToken))) binding the proof to that
|
|
28
|
+
* token, as required for DPoP-authenticated resource-server requests
|
|
29
|
+
* (RFC 9449 §4.3 / §7). Omit at the token endpoint.
|
|
30
|
+
*/
|
|
31
|
+
accessToken?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Server-issued nonce (RFC 9449 §8). When the AS/RS rejects a proof
|
|
34
|
+
* with `use_dpop_nonce` + a `DPoP-Nonce` header, re-sign with that
|
|
35
|
+
* nonce and retry.
|
|
36
|
+
*/
|
|
37
|
+
nonce?: string;
|
|
38
|
+
}
|
|
39
|
+
interface DPoPKeyManager {
|
|
40
|
+
/** Sign a proof for a specific (method, url) pair, plus optional ath/nonce. */
|
|
41
|
+
signProof(method: string, url: string, options?: DPoPProofOptions): Promise<string>;
|
|
42
|
+
/** Public JWK -- used by the server to compute `cnf.jkt`. */
|
|
43
|
+
publicJWK(): Promise<JsonWebKey>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Create a per-tab DPoP key manager. Returns null when WebCrypto is not
|
|
47
|
+
* available -- the caller should fall back to Bearer mode silently.
|
|
48
|
+
*/
|
|
49
|
+
declare function createDPoPKeyManager(): Promise<DPoPKeyManager | null>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Token renewal error classification.
|
|
53
|
+
*
|
|
54
|
+
* The provider's `renewOnce` has three possible reactions to a failed
|
|
55
|
+
* renewal: retry (transient), force re-auth (terminal auth failure,
|
|
56
|
+
* local state is out of sync with the server), or surface (programmer
|
|
57
|
+
* error the caller should see). Spreading those decisions across every
|
|
58
|
+
* catch block drifts; centralizing them here keeps the contract auditable.
|
|
59
|
+
*
|
|
60
|
+
* The classification is deliberately conservative: anything we don't
|
|
61
|
+
* recognize surfaces, rather than silently forcing re-auth or looping.
|
|
62
|
+
*/
|
|
63
|
+
/**
|
|
64
|
+
* OAuth 2.0 / OIDC protocol error surfaced by the authorization server.
|
|
65
|
+
* Carries the machine-readable `error` code from the token / authorize
|
|
66
|
+
* endpoint response so the recovery layer can act on it precisely.
|
|
67
|
+
*/
|
|
68
|
+
declare class OAuthProtocolError extends Error {
|
|
69
|
+
code: string;
|
|
70
|
+
description?: string;
|
|
71
|
+
constructor(code: string, description?: string);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Re-auth-class protocol errors -- login_required, consent_required,
|
|
75
|
+
* interaction_required. Subclass of OAuthProtocolError so existing
|
|
76
|
+
* `instanceof LoginRequiredError` checks keep working AND `instanceof
|
|
77
|
+
* OAuthProtocolError` matches the broader classification.
|
|
78
|
+
*/
|
|
79
|
+
declare class LoginRequiredError extends OAuthProtocolError {
|
|
80
|
+
constructor(description?: string);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Transport / network layer failure -- `fetch` rejected, DNS miss, TLS
|
|
84
|
+
* error, CORS block. Surfaced as retry because the authorization server
|
|
85
|
+
* never saw the request.
|
|
86
|
+
*/
|
|
87
|
+
declare class NetworkError extends Error {
|
|
88
|
+
cause?: unknown;
|
|
89
|
+
constructor(message: string, cause?: unknown);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* What the caller should do when a renewal throws.
|
|
93
|
+
*
|
|
94
|
+
* Retry -- transient (network blip, 5xx). Backoff and try again.
|
|
95
|
+
* ForceReauth -- terminal (stale refresh token, revoked client, session
|
|
96
|
+
* gone). Wipe local state and return the user to the
|
|
97
|
+
* signed-out screen. Don't loop.
|
|
98
|
+
* Surface -- programmer error (invalid_scope, unsupported_grant_type).
|
|
99
|
+
* Throw to the caller so they can see it during dev.
|
|
100
|
+
*/
|
|
101
|
+
declare enum RecoveryAction {
|
|
102
|
+
Retry = "retry",
|
|
103
|
+
ForceReauth = "force_reauth",
|
|
104
|
+
Surface = "surface"
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Classify a renewal error into a recovery action. The full mapping,
|
|
108
|
+
* grouped by cause:
|
|
109
|
+
*
|
|
110
|
+
* Terminal auth (ForceReauth):
|
|
111
|
+
* invalid_grant refresh token expired / revoked / rotated-reused
|
|
112
|
+
* invalid_request stale or malformed token at /auth/token
|
|
113
|
+
* (also what the server returns after a lost HMAC secret)
|
|
114
|
+
* invalid_client client was deleted / secret changed server-side
|
|
115
|
+
* unauthorized_client client no longer permitted this grant
|
|
116
|
+
* login_required prompt=none could not silently re-auth
|
|
117
|
+
* consent_required user consent missing
|
|
118
|
+
* interaction_required user action required
|
|
119
|
+
*
|
|
120
|
+
* Transient (Retry):
|
|
121
|
+
* server_error 500-class error on the authz server
|
|
122
|
+
* temporarily_unavailable documented OAuth retry signal
|
|
123
|
+
* NetworkError fetch rejection, DNS, TLS, CORS
|
|
124
|
+
*
|
|
125
|
+
* Programmer error (Surface):
|
|
126
|
+
* invalid_scope misconfigured scope list
|
|
127
|
+
* unsupported_grant_type wrong grant wiring
|
|
128
|
+
* unsupported_response_type
|
|
129
|
+
* Unknown OAuth codes -- conservative default
|
|
130
|
+
* Unknown Error subclass -- ditto
|
|
131
|
+
*/
|
|
132
|
+
declare function classifyRenewalError(err: unknown): RecoveryAction;
|
|
133
|
+
/**
|
|
134
|
+
* Exponential backoff with full jitter. Cheap, deterministic test
|
|
135
|
+
* surface -- callers can swap out `Math.random` via the seed arg.
|
|
136
|
+
*
|
|
137
|
+
* attempt 0 -> [0, 500) ms
|
|
138
|
+
* attempt 1 -> [0, 1000) ms
|
|
139
|
+
* attempt 2 -> [0, 2000) ms
|
|
140
|
+
* attempt 3 -> [0, 4000) ms
|
|
141
|
+
* attempt N -> [0, 30_000) ms (capped)
|
|
142
|
+
*
|
|
143
|
+
* The cap matters: without it an always-failing transient error fires a
|
|
144
|
+
* renewal an hour later and the user thinks the app is frozen.
|
|
145
|
+
*/
|
|
146
|
+
declare function backoffDelay(attempt: number, rand?: () => number): number;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Silent token renewal via web_message response mode.
|
|
150
|
+
*
|
|
151
|
+
* Opens a hidden iframe to the authorization endpoint with response_mode=web_message
|
|
152
|
+
* and prompt=none. The server validates the session cookie and returns an HTML page
|
|
153
|
+
* that sends the authorization response via postMessage. No SPA loading in the iframe.
|
|
154
|
+
*
|
|
155
|
+
* Spec: draft-sakimura-oauth-wmrm-01
|
|
156
|
+
*/
|
|
157
|
+
|
|
158
|
+
interface SilentRenewOptions {
|
|
159
|
+
authority: string;
|
|
160
|
+
clientId: string;
|
|
161
|
+
redirectUri: string;
|
|
162
|
+
scope: string;
|
|
163
|
+
audience?: string;
|
|
164
|
+
/**
|
|
165
|
+
* When true, asks the server to include `atol:wrapped_dek` on the renewed
|
|
166
|
+
* ID token. The KEK is NOT re-delivered (the password isn't available
|
|
167
|
+
* during silent renew), but carrying the wrapped DEK claim keeps it
|
|
168
|
+
* consistently present across every token in the session, so app code
|
|
169
|
+
* that re-reads it after a renewal (tab rehydrate, multi-tab sync) finds
|
|
170
|
+
* the same DEK material it saw at login.
|
|
171
|
+
*/
|
|
172
|
+
deriveEncryptionKey?: boolean;
|
|
173
|
+
/**
|
|
174
|
+
* Milliseconds to wait for the iframe's postMessage before rejecting.
|
|
175
|
+
* Defaults to 60s. An earlier 10s value was too tight on
|
|
176
|
+
* slow networks and caused the "return stale token" fallback to fire
|
|
177
|
+
* unnecessarily, producing `token is expired (exp)` errors on the server.
|
|
178
|
+
*/
|
|
179
|
+
timeoutMs?: number;
|
|
180
|
+
}
|
|
181
|
+
/** Default iframe timeout for silent authorization. */
|
|
182
|
+
declare const DEFAULT_AUTHORIZE_TIMEOUT_MS = 60000;
|
|
183
|
+
/** Default retry count for silent authorize on transient errors. */
|
|
184
|
+
declare const DEFAULT_SILENT_RENEW_RETRY_COUNT = 3;
|
|
185
|
+
/**
|
|
186
|
+
* Perform a silent authorization request via web_message.
|
|
187
|
+
* Returns the authorization code + state, or throws on error.
|
|
188
|
+
*/
|
|
189
|
+
declare function silentAuthorize(opts: SilentRenewOptions): Promise<{
|
|
190
|
+
code: string;
|
|
191
|
+
state: string;
|
|
192
|
+
codeVerifier: string;
|
|
193
|
+
}>;
|
|
194
|
+
/**
|
|
195
|
+
* silentAuthorize with bounded retry on transient errors (default 3
|
|
196
|
+
* attempts). `login_required` and similar
|
|
197
|
+
* re-auth errors are fatal and bypass the retry loop.
|
|
198
|
+
*/
|
|
199
|
+
declare function silentAuthorizeWithRetry(opts: SilentRenewOptions, retries?: number): Promise<{
|
|
200
|
+
code: string;
|
|
201
|
+
state: string;
|
|
202
|
+
codeVerifier: string;
|
|
203
|
+
}>;
|
|
204
|
+
/** Shape of a successful /auth/token response. */
|
|
205
|
+
interface TokenResponse {
|
|
206
|
+
access_token: string;
|
|
207
|
+
id_token?: string;
|
|
208
|
+
refresh_token?: string;
|
|
209
|
+
token_type: string;
|
|
210
|
+
expires_in: number;
|
|
211
|
+
scope?: string;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Optional per-request DPoP signer. When provided, token-endpoint calls
|
|
215
|
+
* attach a signed proof header (RFC 9449). When omitted, calls run Bearer-
|
|
216
|
+
* only and the server issues un-bound tokens. The provider wires this
|
|
217
|
+
* through from its DPoPKeyManager when `useDPoP: true`.
|
|
218
|
+
*/
|
|
219
|
+
interface DPoPSigner {
|
|
220
|
+
signProof(method: string, url: string, options?: DPoPProofOptions): Promise<string>;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Exchange an authorization code for tokens via the token endpoint.
|
|
224
|
+
*/
|
|
225
|
+
declare function exchangeCode(authority: string, clientId: string, code: string, redirectUri: string, codeVerifier: string, dpop?: DPoPSigner): Promise<TokenResponse>;
|
|
226
|
+
/**
|
|
227
|
+
* Renew tokens using a refresh token grant (RFC 6749 §6). This is the
|
|
228
|
+
* `useRefreshTokens: true` path -- a direct POST to the token
|
|
229
|
+
* endpoint, no iframe, no cross-site cookies. Works under Chrome's third-
|
|
230
|
+
* party cookie phaseout, Safari ITP, and Firefox ETP without the user
|
|
231
|
+
* needing to grant Storage Access.
|
|
232
|
+
*
|
|
233
|
+
* The server may rotate the refresh token on each use (RFC 6749 §10.4);
|
|
234
|
+
* callers must read `refresh_token` from the response and replace their
|
|
235
|
+
* stored copy before the next renew.
|
|
236
|
+
*
|
|
237
|
+
* An expired or revoked refresh token surfaces as `LoginRequiredError` so
|
|
238
|
+
* the caller can force a re-auth, matching the semantics of the iframe
|
|
239
|
+
* path for login_required.
|
|
240
|
+
*/
|
|
241
|
+
declare function refreshTokenGrant(authority: string, clientId: string, refreshToken: string, scope?: string, audience?: string, dpop?: DPoPSigner): Promise<TokenResponse>;
|
|
242
|
+
/**
|
|
243
|
+
* Revoke a token via the RFC 7009 revocation endpoint. Used on logout to
|
|
244
|
+
* proactively invalidate the refresh token server-side rather than waiting
|
|
245
|
+
* for it to expire. A best-effort call: revocation failures are surfaced
|
|
246
|
+
* to the caller, which typically swallows them (the RP-initiated logout
|
|
247
|
+
* still kills the session cookie).
|
|
248
|
+
*
|
|
249
|
+
* `tokenTypeHint` defaults to `refresh_token` -- the token most worth
|
|
250
|
+
* revoking, since it can mint new access tokens.
|
|
251
|
+
*/
|
|
252
|
+
declare function revokeToken(authority: string, clientId: string, token: string, tokenTypeHint?: 'refresh_token' | 'access_token'): Promise<void>;
|
|
253
|
+
|
|
4
254
|
/**
|
|
5
255
|
* Framework-agnostic shared types for the Atol browser security core.
|
|
6
256
|
*
|
|
@@ -141,6 +391,26 @@ interface AtolBrowserAuthConfig {
|
|
|
141
391
|
* explicit at the call site.
|
|
142
392
|
*/
|
|
143
393
|
storage?: 'memory';
|
|
394
|
+
/**
|
|
395
|
+
* TEST ONLY. Enable {@link AtolBrowserAuth.seedTestSession}, which
|
|
396
|
+
* establishes an authenticated session directly from a token response --
|
|
397
|
+
* WITHOUT an OIDC redirect/callback, and without signature, state, or
|
|
398
|
+
* nonce validation. Defaults to `false`; while `false`, `seedTestSession`
|
|
399
|
+
* throws.
|
|
400
|
+
*
|
|
401
|
+
* This exists so browser end-to-end tests (Cypress, Playwright) can
|
|
402
|
+
* bootstrap a logged-in session against the in-memory token store without a
|
|
403
|
+
* live IdP round-trip -- the reason the old `sessionStorage` priming trick
|
|
404
|
+
* stopped working once tokens moved in-memory. Skipping the OIDC checks is
|
|
405
|
+
* safe ONLY because it is test-only and your API still validates the
|
|
406
|
+
* presented token server-side; it must NEVER be enabled in a production
|
|
407
|
+
* build, or an XSS payload could mint a session. The whole point of
|
|
408
|
+
* in-memory storage is that persisted tokens are unreadable after an XSS,
|
|
409
|
+
* and this flag must not reopen that surface. When enabled, the client logs
|
|
410
|
+
* a one-time `console.warn` at construction as a guard against accidental
|
|
411
|
+
* production enablement.
|
|
412
|
+
*/
|
|
413
|
+
allowTestSessions?: boolean;
|
|
144
414
|
}
|
|
145
415
|
/** The current auth session, delivered to `onSessionChange` subscribers. */
|
|
146
416
|
interface AtolSession {
|
|
@@ -302,6 +572,23 @@ interface AtolBrowserAuth {
|
|
|
302
572
|
* again on every subsequent change. Returns an unsubscribe function.
|
|
303
573
|
*/
|
|
304
574
|
onSessionChange(cb: (session: AtolSession) => void): () => void;
|
|
575
|
+
/**
|
|
576
|
+
* TEST ONLY. Establish an authenticated session directly from a raw token
|
|
577
|
+
* response, bypassing the OIDC redirect/callback and all signature, state,
|
|
578
|
+
* and nonce checks. Throws unless the client was created with
|
|
579
|
+
* `allowTestSessions: true` (see {@link AtolBrowserAuthConfig.allowTestSessions}).
|
|
580
|
+
*
|
|
581
|
+
* The tokens are adopted through the same path a real login/renewal uses:
|
|
582
|
+
* the user is stored in the (in-memory) token store, `getAccessToken`
|
|
583
|
+
* resolves the seeded token, proactive renewal is scheduled from its
|
|
584
|
+
* `exp`, and `onSessionChange` subscribers are notified -- so a framework
|
|
585
|
+
* binding flips to authenticated exactly as it would after a real login.
|
|
586
|
+
* Provide a token whose `exp` is in the future; an already-expired access
|
|
587
|
+
* token schedules an immediate renewal the hermetic test environment
|
|
588
|
+
* cannot satisfy. Never call this in production; the presented tokens are
|
|
589
|
+
* still validated server-side by your API.
|
|
590
|
+
*/
|
|
591
|
+
seedTestSession(tokens: TokenResponse): Promise<AtolSession>;
|
|
305
592
|
/** Tear down timers, the broadcast channel, and subscriptions. */
|
|
306
593
|
destroy(): void;
|
|
307
594
|
}
|
|
@@ -328,53 +615,6 @@ declare function decodeJWTPayload(token: string): Record<string, unknown>;
|
|
|
328
615
|
*/
|
|
329
616
|
declare function getTokenExpiry(token: string): number | null;
|
|
330
617
|
|
|
331
|
-
/**
|
|
332
|
-
* DPoP (RFC 9449) client implementation.
|
|
333
|
-
*
|
|
334
|
-
* This module owns the per-tab ES256 keypair used for sender-constrained
|
|
335
|
-
* tokens, plus the proof-JWT builder that clients send on every
|
|
336
|
-
* /auth/token call (and, in future, on every resource-server request).
|
|
337
|
-
*
|
|
338
|
-
* Design choices:
|
|
339
|
-
* - Keys are non-exportable CryptoKey objects (`extractable: false`).
|
|
340
|
-
* XSS can use them to sign but cannot exfiltrate the private bytes.
|
|
341
|
-
* - Keys are regenerated per-tab on provider mount. A page reload ==
|
|
342
|
-
* a new keypair. The cost is one extra round-trip after reload;
|
|
343
|
-
* the benefit is no persistence surface for key material at all.
|
|
344
|
-
* - ES256 / P-256 only. Universal WebCrypto support, matches what the
|
|
345
|
-
* server's DPoP validator accepts first.
|
|
346
|
-
*
|
|
347
|
-
* The module is a no-op fallback when WebCrypto is unavailable (SSR,
|
|
348
|
-
* extremely old browsers). Callers treat a null manager as "Bearer mode".
|
|
349
|
-
*/
|
|
350
|
-
/** Optional per-proof inputs (RFC 9449 resource access + server nonce). */
|
|
351
|
-
interface DPoPProofOptions {
|
|
352
|
-
/**
|
|
353
|
-
* Access token being presented. When set, the proof carries an `ath`
|
|
354
|
-
* claim (base64url(SHA-256(accessToken))) binding the proof to that
|
|
355
|
-
* token, as required for DPoP-authenticated resource-server requests
|
|
356
|
-
* (RFC 9449 §4.3 / §7). Omit at the token endpoint.
|
|
357
|
-
*/
|
|
358
|
-
accessToken?: string;
|
|
359
|
-
/**
|
|
360
|
-
* Server-issued nonce (RFC 9449 §8). When the AS/RS rejects a proof
|
|
361
|
-
* with `use_dpop_nonce` + a `DPoP-Nonce` header, re-sign with that
|
|
362
|
-
* nonce and retry.
|
|
363
|
-
*/
|
|
364
|
-
nonce?: string;
|
|
365
|
-
}
|
|
366
|
-
interface DPoPKeyManager {
|
|
367
|
-
/** Sign a proof for a specific (method, url) pair, plus optional ath/nonce. */
|
|
368
|
-
signProof(method: string, url: string, options?: DPoPProofOptions): Promise<string>;
|
|
369
|
-
/** Public JWK -- used by the server to compute `cnf.jkt`. */
|
|
370
|
-
publicJWK(): Promise<JsonWebKey>;
|
|
371
|
-
}
|
|
372
|
-
/**
|
|
373
|
-
* Create a per-tab DPoP key manager. Returns null when WebCrypto is not
|
|
374
|
-
* available -- the caller should fall back to Bearer mode silently.
|
|
375
|
-
*/
|
|
376
|
-
declare function createDPoPKeyManager(): Promise<DPoPKeyManager | null>;
|
|
377
|
-
|
|
378
618
|
/**
|
|
379
619
|
* JWT signature verification against the IdP's JWKS.
|
|
380
620
|
*
|
|
@@ -407,209 +647,6 @@ declare function verifyIdToken(idToken: string, opts: {
|
|
|
407
647
|
/** Test-only: drop the discovery cache so a test can re-stub the IdP. */
|
|
408
648
|
declare function _resetDiscoveryCache(): void;
|
|
409
649
|
|
|
410
|
-
/**
|
|
411
|
-
* Token renewal error classification.
|
|
412
|
-
*
|
|
413
|
-
* The provider's `renewOnce` has three possible reactions to a failed
|
|
414
|
-
* renewal: retry (transient), force re-auth (terminal auth failure,
|
|
415
|
-
* local state is out of sync with the server), or surface (programmer
|
|
416
|
-
* error the caller should see). Spreading those decisions across every
|
|
417
|
-
* catch block drifts; centralizing them here keeps the contract auditable.
|
|
418
|
-
*
|
|
419
|
-
* The classification is deliberately conservative: anything we don't
|
|
420
|
-
* recognize surfaces, rather than silently forcing re-auth or looping.
|
|
421
|
-
*/
|
|
422
|
-
/**
|
|
423
|
-
* OAuth 2.0 / OIDC protocol error surfaced by the authorization server.
|
|
424
|
-
* Carries the machine-readable `error` code from the token / authorize
|
|
425
|
-
* endpoint response so the recovery layer can act on it precisely.
|
|
426
|
-
*/
|
|
427
|
-
declare class OAuthProtocolError extends Error {
|
|
428
|
-
code: string;
|
|
429
|
-
description?: string;
|
|
430
|
-
constructor(code: string, description?: string);
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
* Re-auth-class protocol errors -- login_required, consent_required,
|
|
434
|
-
* interaction_required. Subclass of OAuthProtocolError so existing
|
|
435
|
-
* `instanceof LoginRequiredError` checks keep working AND `instanceof
|
|
436
|
-
* OAuthProtocolError` matches the broader classification.
|
|
437
|
-
*/
|
|
438
|
-
declare class LoginRequiredError extends OAuthProtocolError {
|
|
439
|
-
constructor(description?: string);
|
|
440
|
-
}
|
|
441
|
-
/**
|
|
442
|
-
* Transport / network layer failure -- `fetch` rejected, DNS miss, TLS
|
|
443
|
-
* error, CORS block. Surfaced as retry because the authorization server
|
|
444
|
-
* never saw the request.
|
|
445
|
-
*/
|
|
446
|
-
declare class NetworkError extends Error {
|
|
447
|
-
cause?: unknown;
|
|
448
|
-
constructor(message: string, cause?: unknown);
|
|
449
|
-
}
|
|
450
|
-
/**
|
|
451
|
-
* What the caller should do when a renewal throws.
|
|
452
|
-
*
|
|
453
|
-
* Retry -- transient (network blip, 5xx). Backoff and try again.
|
|
454
|
-
* ForceReauth -- terminal (stale refresh token, revoked client, session
|
|
455
|
-
* gone). Wipe local state and return the user to the
|
|
456
|
-
* signed-out screen. Don't loop.
|
|
457
|
-
* Surface -- programmer error (invalid_scope, unsupported_grant_type).
|
|
458
|
-
* Throw to the caller so they can see it during dev.
|
|
459
|
-
*/
|
|
460
|
-
declare enum RecoveryAction {
|
|
461
|
-
Retry = "retry",
|
|
462
|
-
ForceReauth = "force_reauth",
|
|
463
|
-
Surface = "surface"
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
* Classify a renewal error into a recovery action. The full mapping,
|
|
467
|
-
* grouped by cause:
|
|
468
|
-
*
|
|
469
|
-
* Terminal auth (ForceReauth):
|
|
470
|
-
* invalid_grant refresh token expired / revoked / rotated-reused
|
|
471
|
-
* invalid_request stale or malformed token at /auth/token
|
|
472
|
-
* (also what the server returns after a lost HMAC secret)
|
|
473
|
-
* invalid_client client was deleted / secret changed server-side
|
|
474
|
-
* unauthorized_client client no longer permitted this grant
|
|
475
|
-
* login_required prompt=none could not silently re-auth
|
|
476
|
-
* consent_required user consent missing
|
|
477
|
-
* interaction_required user action required
|
|
478
|
-
*
|
|
479
|
-
* Transient (Retry):
|
|
480
|
-
* server_error 500-class error on the authz server
|
|
481
|
-
* temporarily_unavailable documented OAuth retry signal
|
|
482
|
-
* NetworkError fetch rejection, DNS, TLS, CORS
|
|
483
|
-
*
|
|
484
|
-
* Programmer error (Surface):
|
|
485
|
-
* invalid_scope misconfigured scope list
|
|
486
|
-
* unsupported_grant_type wrong grant wiring
|
|
487
|
-
* unsupported_response_type
|
|
488
|
-
* Unknown OAuth codes -- conservative default
|
|
489
|
-
* Unknown Error subclass -- ditto
|
|
490
|
-
*/
|
|
491
|
-
declare function classifyRenewalError(err: unknown): RecoveryAction;
|
|
492
|
-
/**
|
|
493
|
-
* Exponential backoff with full jitter. Cheap, deterministic test
|
|
494
|
-
* surface -- callers can swap out `Math.random` via the seed arg.
|
|
495
|
-
*
|
|
496
|
-
* attempt 0 -> [0, 500) ms
|
|
497
|
-
* attempt 1 -> [0, 1000) ms
|
|
498
|
-
* attempt 2 -> [0, 2000) ms
|
|
499
|
-
* attempt 3 -> [0, 4000) ms
|
|
500
|
-
* attempt N -> [0, 30_000) ms (capped)
|
|
501
|
-
*
|
|
502
|
-
* The cap matters: without it an always-failing transient error fires a
|
|
503
|
-
* renewal an hour later and the user thinks the app is frozen.
|
|
504
|
-
*/
|
|
505
|
-
declare function backoffDelay(attempt: number, rand?: () => number): number;
|
|
506
|
-
|
|
507
|
-
/**
|
|
508
|
-
* Silent token renewal via web_message response mode.
|
|
509
|
-
*
|
|
510
|
-
* Opens a hidden iframe to the authorization endpoint with response_mode=web_message
|
|
511
|
-
* and prompt=none. The server validates the session cookie and returns an HTML page
|
|
512
|
-
* that sends the authorization response via postMessage. No SPA loading in the iframe.
|
|
513
|
-
*
|
|
514
|
-
* Spec: draft-sakimura-oauth-wmrm-01
|
|
515
|
-
*/
|
|
516
|
-
|
|
517
|
-
interface SilentRenewOptions {
|
|
518
|
-
authority: string;
|
|
519
|
-
clientId: string;
|
|
520
|
-
redirectUri: string;
|
|
521
|
-
scope: string;
|
|
522
|
-
audience?: string;
|
|
523
|
-
/**
|
|
524
|
-
* When true, asks the server to include `atol:wrapped_dek` on the renewed
|
|
525
|
-
* ID token. The KEK is NOT re-delivered (the password isn't available
|
|
526
|
-
* during silent renew), but carrying the wrapped DEK claim keeps it
|
|
527
|
-
* consistently present across every token in the session, so app code
|
|
528
|
-
* that re-reads it after a renewal (tab rehydrate, multi-tab sync) finds
|
|
529
|
-
* the same DEK material it saw at login.
|
|
530
|
-
*/
|
|
531
|
-
deriveEncryptionKey?: boolean;
|
|
532
|
-
/**
|
|
533
|
-
* Milliseconds to wait for the iframe's postMessage before rejecting.
|
|
534
|
-
* Defaults to 60s. An earlier 10s value was too tight on
|
|
535
|
-
* slow networks and caused the "return stale token" fallback to fire
|
|
536
|
-
* unnecessarily, producing `token is expired (exp)` errors on the server.
|
|
537
|
-
*/
|
|
538
|
-
timeoutMs?: number;
|
|
539
|
-
}
|
|
540
|
-
/** Default iframe timeout for silent authorization. */
|
|
541
|
-
declare const DEFAULT_AUTHORIZE_TIMEOUT_MS = 60000;
|
|
542
|
-
/** Default retry count for silent authorize on transient errors. */
|
|
543
|
-
declare const DEFAULT_SILENT_RENEW_RETRY_COUNT = 3;
|
|
544
|
-
/**
|
|
545
|
-
* Perform a silent authorization request via web_message.
|
|
546
|
-
* Returns the authorization code + state, or throws on error.
|
|
547
|
-
*/
|
|
548
|
-
declare function silentAuthorize(opts: SilentRenewOptions): Promise<{
|
|
549
|
-
code: string;
|
|
550
|
-
state: string;
|
|
551
|
-
codeVerifier: string;
|
|
552
|
-
}>;
|
|
553
|
-
/**
|
|
554
|
-
* silentAuthorize with bounded retry on transient errors (default 3
|
|
555
|
-
* attempts). `login_required` and similar
|
|
556
|
-
* re-auth errors are fatal and bypass the retry loop.
|
|
557
|
-
*/
|
|
558
|
-
declare function silentAuthorizeWithRetry(opts: SilentRenewOptions, retries?: number): Promise<{
|
|
559
|
-
code: string;
|
|
560
|
-
state: string;
|
|
561
|
-
codeVerifier: string;
|
|
562
|
-
}>;
|
|
563
|
-
/** Shape of a successful /auth/token response. */
|
|
564
|
-
interface TokenResponse {
|
|
565
|
-
access_token: string;
|
|
566
|
-
id_token?: string;
|
|
567
|
-
refresh_token?: string;
|
|
568
|
-
token_type: string;
|
|
569
|
-
expires_in: number;
|
|
570
|
-
scope?: string;
|
|
571
|
-
}
|
|
572
|
-
/**
|
|
573
|
-
* Optional per-request DPoP signer. When provided, token-endpoint calls
|
|
574
|
-
* attach a signed proof header (RFC 9449). When omitted, calls run Bearer-
|
|
575
|
-
* only and the server issues un-bound tokens. The provider wires this
|
|
576
|
-
* through from its DPoPKeyManager when `useDPoP: true`.
|
|
577
|
-
*/
|
|
578
|
-
interface DPoPSigner {
|
|
579
|
-
signProof(method: string, url: string, options?: DPoPProofOptions): Promise<string>;
|
|
580
|
-
}
|
|
581
|
-
/**
|
|
582
|
-
* Exchange an authorization code for tokens via the token endpoint.
|
|
583
|
-
*/
|
|
584
|
-
declare function exchangeCode(authority: string, clientId: string, code: string, redirectUri: string, codeVerifier: string, dpop?: DPoPSigner): Promise<TokenResponse>;
|
|
585
|
-
/**
|
|
586
|
-
* Renew tokens using a refresh token grant (RFC 6749 §6). This is the
|
|
587
|
-
* `useRefreshTokens: true` path -- a direct POST to the token
|
|
588
|
-
* endpoint, no iframe, no cross-site cookies. Works under Chrome's third-
|
|
589
|
-
* party cookie phaseout, Safari ITP, and Firefox ETP without the user
|
|
590
|
-
* needing to grant Storage Access.
|
|
591
|
-
*
|
|
592
|
-
* The server may rotate the refresh token on each use (RFC 6749 §10.4);
|
|
593
|
-
* callers must read `refresh_token` from the response and replace their
|
|
594
|
-
* stored copy before the next renew.
|
|
595
|
-
*
|
|
596
|
-
* An expired or revoked refresh token surfaces as `LoginRequiredError` so
|
|
597
|
-
* the caller can force a re-auth, matching the semantics of the iframe
|
|
598
|
-
* path for login_required.
|
|
599
|
-
*/
|
|
600
|
-
declare function refreshTokenGrant(authority: string, clientId: string, refreshToken: string, scope?: string, audience?: string, dpop?: DPoPSigner): Promise<TokenResponse>;
|
|
601
|
-
/**
|
|
602
|
-
* Revoke a token via the RFC 7009 revocation endpoint. Used on logout to
|
|
603
|
-
* proactively invalidate the refresh token server-side rather than waiting
|
|
604
|
-
* for it to expire. A best-effort call: revocation failures are surfaced
|
|
605
|
-
* to the caller, which typically swallows them (the RP-initiated logout
|
|
606
|
-
* still kills the session cookie).
|
|
607
|
-
*
|
|
608
|
-
* `tokenTypeHint` defaults to `refresh_token` -- the token most worth
|
|
609
|
-
* revoking, since it can mint new access tokens.
|
|
610
|
-
*/
|
|
611
|
-
declare function revokeToken(authority: string, clientId: string, token: string, tokenTypeHint?: 'refresh_token' | 'access_token'): Promise<void>;
|
|
612
|
-
|
|
613
650
|
/**
|
|
614
651
|
* Create an oidc-client-ts UserManager configured for Atol.
|
|
615
652
|
*
|
package/dist/index.js
CHANGED
|
@@ -871,6 +871,12 @@ function createAtolBrowserAuth(config) {
|
|
|
871
871
|
const useRefreshTokens = config.useRefreshTokens ?? false;
|
|
872
872
|
const useRefreshTokensFallback = config.useRefreshTokensFallback ?? false;
|
|
873
873
|
const deriveEncryptionKey = config.deriveEncryptionKey ?? false;
|
|
874
|
+
const allowTestSessions = config.allowTestSessions ?? false;
|
|
875
|
+
if (allowTestSessions && typeof console !== "undefined") {
|
|
876
|
+
console.warn(
|
|
877
|
+
"[Atol] allowTestSessions is ENABLED: seedTestSession can establish a session without OIDC validation. This is for tests only and must never be enabled in production."
|
|
878
|
+
);
|
|
879
|
+
}
|
|
874
880
|
const requestedScopes = config.scopes ?? DEFAULT_SCOPES;
|
|
875
881
|
const scope = useRefreshTokens && !requestedScopes.split(" ").includes("offline_access") ? `${requestedScopes} offline_access` : requestedScopes;
|
|
876
882
|
const manager = createUserManager(issuer, clientId, redirectUri, scope, audience);
|
|
@@ -1288,6 +1294,21 @@ function createAtolBrowserAuth(config) {
|
|
|
1288
1294
|
const proof = dpop ? await dpop.signProof(method, url, { accessToken: token }) : void 0;
|
|
1289
1295
|
return { token, proof };
|
|
1290
1296
|
}
|
|
1297
|
+
async function seedTestSession(tokens) {
|
|
1298
|
+
if (!allowTestSessions) {
|
|
1299
|
+
throw new Error(
|
|
1300
|
+
"seedTestSession is disabled: pass allowTestSessions:true to createAtolBrowserAuth to enable it. It bypasses OIDC signature, state, and nonce validation and must never be enabled in production."
|
|
1301
|
+
);
|
|
1302
|
+
}
|
|
1303
|
+
const exp = getTokenExpiry(tokens.access_token);
|
|
1304
|
+
if ((exp === null || exp <= Date.now()) && typeof console !== "undefined") {
|
|
1305
|
+
console.warn(
|
|
1306
|
+
"[Atol] seedTestSession: the access token has no future `exp` claim; the seeded session may immediately attempt renewal and drop. Seed a token whose exp is in the future."
|
|
1307
|
+
);
|
|
1308
|
+
}
|
|
1309
|
+
await applyTokens(tokens);
|
|
1310
|
+
return session();
|
|
1311
|
+
}
|
|
1291
1312
|
function onSessionChange(cb) {
|
|
1292
1313
|
subscribers.add(cb);
|
|
1293
1314
|
try {
|
|
@@ -1325,6 +1346,7 @@ function createAtolBrowserAuth(config) {
|
|
|
1325
1346
|
signDPoPProof,
|
|
1326
1347
|
isDPoPActive: () => dpop !== null,
|
|
1327
1348
|
onSessionChange,
|
|
1349
|
+
seedTestSession,
|
|
1328
1350
|
destroy
|
|
1329
1351
|
};
|
|
1330
1352
|
}
|