@payez/next-mvp 4.10.0 → 4.11.0
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 +31 -0
- package/dist/api-handlers/auth/refresh.d.ts +17 -58
- package/dist/api-handlers/auth/refresh.js +71 -772
- package/dist/auth/utils/token-utils.d.ts +1 -1
- package/dist/auth/utils/token-utils.js +3 -2
- package/dist/lib/api-handler.js +4 -3
- package/dist/lib/ensure-fresh-access-token.d.ts +24 -1
- package/dist/lib/ensure-fresh-access-token.js +194 -28
- package/dist/lib/idp-fetch.js +3 -2
- package/dist/lib/refresh-in-doubt.d.ts +31 -0
- package/dist/lib/refresh-in-doubt.js +81 -0
- package/dist/lib/refresh-token-validator.js +2 -1
- package/dist/lib/refresh-window.d.ts +16 -0
- package/dist/lib/refresh-window.js +19 -0
- package/dist/lib/safe-trace-id.d.ts +11 -0
- package/dist/lib/safe-trace-id.js +23 -0
- package/dist/lib/session-store.js +14 -4
- package/dist/lib/token-lifecycle.js +3 -2
- package/dist/models/SessionModel.d.ts +7 -0
- package/dist/routes/auth/refresh.d.ts +1 -15
- package/dist/server/auth.d.ts +1 -1
- package/dist/stores/authStore.js +3 -2
- package/package.json +1 -1
- package/scripts/verify-release-provenance.mjs +1 -0
- package/src/api-handlers/auth/refresh.ts +72 -812
- package/src/auth/utils/token-utils.ts +3 -2
- package/src/lib/api-handler.ts +4 -3
- package/src/lib/ensure-fresh-access-token.ts +213 -29
- package/src/lib/idp-fetch.ts +3 -2
- package/src/lib/refresh-in-doubt.ts +79 -0
- package/src/lib/refresh-token-validator.ts +2 -1
- package/src/lib/refresh-window.ts +16 -0
- package/src/lib/safe-trace-id.ts +21 -0
- package/src/lib/session-store.ts +15 -3
- package/src/lib/token-lifecycle.ts +3 -2
- package/src/models/SessionModel.ts +7 -0
- package/src/server/auth.ts +1 -1
- package/src/stores/authStore.ts +3 -2
|
@@ -46,7 +46,7 @@ export declare function extractAmrFromToken(decoded: DecodedIdpAccessToken): str
|
|
|
46
46
|
* Check if a token expiry timestamp indicates the token needs refresh.
|
|
47
47
|
*
|
|
48
48
|
* @param expiresAt - Token expiry timestamp (Unix milliseconds)
|
|
49
|
-
* @param bufferMs - How early to trigger refresh (default 5 minutes)
|
|
49
|
+
* @param bufferMs - How early to trigger refresh (default: the one refresh window, `REFRESH_WINDOW_MS`, 5 minutes)
|
|
50
50
|
* @returns true if token is expired or will expire within buffer period
|
|
51
51
|
*/
|
|
52
52
|
export declare function tokenNeedsRefresh(expiresAt: number | undefined, bufferMs?: number): boolean;
|
|
@@ -20,6 +20,7 @@ exports.msUntilExpiry = msUntilExpiry;
|
|
|
20
20
|
exports.expClaimToMs = expClaimToMs;
|
|
21
21
|
exports.validateTokenExpiry = validateTokenExpiry;
|
|
22
22
|
const jwt_decode_1 = require("../../lib/jwt-decode");
|
|
23
|
+
const refresh_window_1 = require("../../lib/refresh-window");
|
|
23
24
|
// Re-export header utilities for consumers
|
|
24
25
|
var jwt_decode_2 = require("../../lib/jwt-decode");
|
|
25
26
|
Object.defineProperty(exports, "decodeJwtHeader", { enumerable: true, get: function () { return jwt_decode_2.decodeJwtHeader; } });
|
|
@@ -129,10 +130,10 @@ function extractAmrFromToken(decoded) {
|
|
|
129
130
|
* Check if a token expiry timestamp indicates the token needs refresh.
|
|
130
131
|
*
|
|
131
132
|
* @param expiresAt - Token expiry timestamp (Unix milliseconds)
|
|
132
|
-
* @param bufferMs - How early to trigger refresh (default 5 minutes)
|
|
133
|
+
* @param bufferMs - How early to trigger refresh (default: the one refresh window, `REFRESH_WINDOW_MS`, 5 minutes)
|
|
133
134
|
* @returns true if token is expired or will expire within buffer period
|
|
134
135
|
*/
|
|
135
|
-
function tokenNeedsRefresh(expiresAt, bufferMs =
|
|
136
|
+
function tokenNeedsRefresh(expiresAt, bufferMs = refresh_window_1.REFRESH_WINDOW_MS) {
|
|
136
137
|
if (!expiresAt) {
|
|
137
138
|
return true; // No expiry info = assume needs refresh
|
|
138
139
|
}
|
package/dist/lib/api-handler.js
CHANGED
|
@@ -21,6 +21,7 @@ const nanoid_1 = require("nanoid");
|
|
|
21
21
|
const session_store_1 = require("./session-store");
|
|
22
22
|
const test_aware_get_token_1 = require("./test-aware-get-token");
|
|
23
23
|
const internal_api_1 = require("./internal-api");
|
|
24
|
+
const refresh_window_1 = require("./refresh-window");
|
|
24
25
|
// ============================================================================
|
|
25
26
|
// API Response Builder (for backward compatibility)
|
|
26
27
|
// ============================================================================
|
|
@@ -189,7 +190,7 @@ class ApiHandler {
|
|
|
189
190
|
// Skip the optimization (hasRefreshToken check) when access token is missing —
|
|
190
191
|
// some session shapes store the refresh token under different field names,
|
|
191
192
|
// and we still want a refresh attempt to populate the access token.
|
|
192
|
-
const thresholdMs =
|
|
193
|
+
const thresholdMs = refresh_window_1.REFRESH_WINDOW_MS;
|
|
193
194
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
194
195
|
const expires = sessionData.idpAccessTokenExpires || sessionData.accessTokenExpires || 0;
|
|
195
196
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -305,7 +306,7 @@ class ApiHandler {
|
|
|
305
306
|
try {
|
|
306
307
|
// Double-check if still needs refresh
|
|
307
308
|
const latest = await (0, session_store_1.getSession)(sessionToken);
|
|
308
|
-
const thresholdMs =
|
|
309
|
+
const thresholdMs = refresh_window_1.REFRESH_WINDOW_MS;
|
|
309
310
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
310
311
|
const latestAny = latest;
|
|
311
312
|
// Sessions may store the access token under either `accessToken` or `idpAccessToken`,
|
|
@@ -348,7 +349,7 @@ class ApiHandler {
|
|
|
348
349
|
const session = await (0, session_store_1.getSession)(sessionToken);
|
|
349
350
|
if (session?.accessToken) {
|
|
350
351
|
const expires = session.accessTokenExpires || 0;
|
|
351
|
-
if ((expires - Date.now()) >
|
|
352
|
+
if ((expires - Date.now()) > refresh_window_1.REFRESH_WINDOW_MS) {
|
|
352
353
|
return { success: true };
|
|
353
354
|
}
|
|
354
355
|
}
|
|
@@ -4,10 +4,15 @@ export interface EnsureFreshConfig {
|
|
|
4
4
|
refreshEndpoint?: string;
|
|
5
5
|
}
|
|
6
6
|
export interface EnsureFreshOptions {
|
|
7
|
-
/** Refresh if the access token is within this many ms of expiry. Default
|
|
7
|
+
/** Refresh if the access token is within this many ms of expiry. Default REFRESH_WINDOW_MS (5 min, lib/refresh-window). */
|
|
8
8
|
safetyWindowMs?: number;
|
|
9
9
|
/** Max wait while another caller holds the refresh lock. Default 5000. */
|
|
10
10
|
lockWaitMs?: number;
|
|
11
|
+
/**
|
|
12
|
+
* The refresh ROUTE sets this (BAPert 76449, R-over-E PR 2): a session with no refresh token is refused with the terminal NO_REFRESH_TOKEN
|
|
13
|
+
* even when its access token is still fresh, as the route always did. Other callers leave it unset and get `already_fresh` for a fresh token.
|
|
14
|
+
*/
|
|
15
|
+
requireRefreshToken?: boolean;
|
|
11
16
|
/**
|
|
12
17
|
* Optional caller request id, for LOG CORRELATION ONLY. It is never the lock identity: the lock id is minted here with
|
|
13
18
|
* crypto.randomUUID(). (BAPert 76298 (b): the lock id used to be this value, taken from the request's x-request-id header.)
|
|
@@ -20,6 +25,14 @@ export type EnsureFreshResult = {
|
|
|
20
25
|
accessTokenExpires: number;
|
|
21
26
|
/** True if we refreshed (or a concurrent refresh completed); false if the stored token was already fresh. */
|
|
22
27
|
refreshed: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* WHY this result is ok (BAPert 76449, additive; `api-handlers/auth/refresh` reproduces its bodies from it and `lib/token-lifecycle` reads
|
|
30
|
+
* `already_fresh`): 'already_fresh' = nothing was needed; 'completed_by_concurrent_request' = another request refreshed and we returned
|
|
31
|
+
* its result; 'refreshed' = THIS call refreshed.
|
|
32
|
+
*/
|
|
33
|
+
reason: 'refreshed' | 'already_fresh' | 'completed_by_concurrent_request';
|
|
34
|
+
/** Whether the stored session holds a refresh token (the refresh route reports it in its body). */
|
|
35
|
+
hasRefreshToken: boolean;
|
|
23
36
|
/**
|
|
24
37
|
* CARD 320072. Is the token in THIS result actually in the session store?
|
|
25
38
|
*
|
|
@@ -56,4 +69,14 @@ export type EnsureFreshResult = {
|
|
|
56
69
|
retryable?: boolean;
|
|
57
70
|
resolution?: string;
|
|
58
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* The refresh envelope rule (BAPert 76455 (B)): `success` is a boolean; `success:true` needs a `data` object holding a string `access_token`;
|
|
74
|
+
* `success:false` needs `error.code` as a string; `meta`, `request_id` and `time_stamp` are optional; a non-2xx can not say success:true.
|
|
75
|
+
*/
|
|
76
|
+
export declare function checkRefreshEnvelope(body: any, httpOk: boolean): {
|
|
77
|
+
ok: true;
|
|
78
|
+
} | {
|
|
79
|
+
ok: false;
|
|
80
|
+
why: string;
|
|
81
|
+
};
|
|
59
82
|
export declare function ensureFreshAccessToken(sessionToken: string, config: EnsureFreshConfig, options?: EnsureFreshOptions): Promise<EnsureFreshResult>;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkRefreshEnvelope = checkRefreshEnvelope;
|
|
3
4
|
exports.ensureFreshAccessToken = ensureFreshAccessToken;
|
|
4
5
|
/**
|
|
5
6
|
* ensureFreshAccessToken — server-side preflight refresh.
|
|
@@ -21,7 +22,31 @@ const session_store_1 = require("./session-store");
|
|
|
21
22
|
const token_expiry_1 = require("./token-expiry");
|
|
22
23
|
const token_utils_1 = require("../auth/utils/token-utils");
|
|
23
24
|
const refresh_rotation_1 = require("./refresh-rotation");
|
|
24
|
-
const
|
|
25
|
+
const refresh_window_1 = require("./refresh-window");
|
|
26
|
+
const safe_trace_id_1 = require("./safe-trace-id");
|
|
27
|
+
const refresh_in_doubt_1 = require("./refresh-in-doubt");
|
|
28
|
+
/**
|
|
29
|
+
* The refresh envelope rule (BAPert 76455 (B)): `success` is a boolean; `success:true` needs a `data` object holding a string `access_token`;
|
|
30
|
+
* `success:false` needs `error.code` as a string; `meta`, `request_id` and `time_stamp` are optional; a non-2xx can not say success:true.
|
|
31
|
+
*/
|
|
32
|
+
function checkRefreshEnvelope(body, httpOk) {
|
|
33
|
+
if (!body || typeof body !== 'object' || Array.isArray(body))
|
|
34
|
+
return { ok: false, why: 'body is not an object' };
|
|
35
|
+
if (typeof body.success !== 'boolean')
|
|
36
|
+
return { ok: false, why: 'success is not a boolean' };
|
|
37
|
+
if (body.success === true) {
|
|
38
|
+
if (!httpOk)
|
|
39
|
+
return { ok: false, why: 'success:true on a non-2xx' };
|
|
40
|
+
if (!body.data || typeof body.data !== 'object' || typeof body.data.access_token !== 'string' || !body.data.access_token) {
|
|
41
|
+
return { ok: false, why: 'success:true without data.access_token' };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else if (!body.error || typeof body.error !== 'object' || typeof body.error.code !== 'string' || !body.error.code) {
|
|
45
|
+
return { ok: false, why: 'success:false without error.code' };
|
|
46
|
+
}
|
|
47
|
+
return { ok: true };
|
|
48
|
+
}
|
|
49
|
+
const DEFAULT_SAFETY_WINDOW_MS = refresh_window_1.REFRESH_WINDOW_MS; // was 60_000; one window for R, E and idp-fetch (BAPert 76397 decision 1)
|
|
25
50
|
const DEFAULT_LOCK_WAIT_MS = 5000;
|
|
26
51
|
function decodeJwtExp(token) {
|
|
27
52
|
const parts = token.split('.');
|
|
@@ -45,37 +70,58 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
45
70
|
// reuse as compromise and revokes descendants across devices) must not be bypassable by an id the caller picks. Date.now() also
|
|
46
71
|
// collided for two calls in the same millisecond. The caller's id is kept as `traceId`, for logs only.
|
|
47
72
|
const requestId = `ensure_fresh_${(0, crypto_1.randomUUID)()}`;
|
|
48
|
-
|
|
49
|
-
|
|
73
|
+
// Attacker-chosen text going to the log sink: control characters stripped and length capped (PAY-2376).
|
|
74
|
+
const traceId = (0, safe_trace_id_1.sanitizeTraceId)(options.requestId);
|
|
75
|
+
const canonicalFirst = await (0, session_store_1.getSession)(sessionToken);
|
|
76
|
+
let session = canonicalFirst;
|
|
50
77
|
if (!session || !session.idpRefreshToken) {
|
|
51
78
|
const baSession = await (0, session_store_1.getBetterAuthSession)(sessionToken);
|
|
52
79
|
if (baSession?.idpRefreshToken) {
|
|
53
80
|
session = baSession;
|
|
54
81
|
}
|
|
55
82
|
}
|
|
83
|
+
// `persisted` means the CANONICAL row (BAPert 76449): a token served from the Better Auth fallback record is not stored where the
|
|
84
|
+
// persist writes.
|
|
85
|
+
const sessionIsCanonical = !!canonicalFirst && session === canonicalFirst;
|
|
56
86
|
if (!session) {
|
|
57
87
|
return { ok: false, code: 'NO_SESSION', message: 'No session for token', status: 401, terminal: true };
|
|
58
88
|
}
|
|
89
|
+
if (options.requireRefreshToken && !session.idpRefreshToken) {
|
|
90
|
+
return {
|
|
91
|
+
ok: false,
|
|
92
|
+
code: 'NO_REFRESH_TOKEN',
|
|
93
|
+
message: 'No refresh token available',
|
|
94
|
+
status: 401,
|
|
95
|
+
terminal: true,
|
|
96
|
+
resolution: 'User must re-authenticate',
|
|
97
|
+
};
|
|
98
|
+
}
|
|
59
99
|
const storedToken = session.idpAccessToken;
|
|
60
100
|
if (!storedToken) {
|
|
61
101
|
// Card 187871: same conflation as getIdpToken (server/auth.ts) - a live
|
|
62
102
|
// session with no access token and mfaVerified !== true is a user
|
|
63
103
|
// mid-2FA, not dead. Only terminal once the session itself claims 2FA
|
|
64
104
|
// is already done.
|
|
105
|
+
//
|
|
106
|
+
// BAPert 76449 (A): a pending-2FA row keeps NO_TOKEN_PENDING_2FA EVEN WITH a refresh token: refresh must never be a way around 2FA.
|
|
107
|
+
// A row that is NOT pending 2FA and has a refresh token is refreshed below (a missing access token is the reason to refresh, as the
|
|
108
|
+
// refresh route always did); with no refresh token it is NO_TOKEN as before.
|
|
65
109
|
if (session.mfaVerified !== true) {
|
|
66
110
|
return { ok: false, code: 'NO_TOKEN_PENDING_2FA', message: 'Two-factor authentication required', status: 401, terminal: false };
|
|
67
111
|
}
|
|
68
|
-
|
|
112
|
+
if (!session.idpRefreshToken) {
|
|
113
|
+
return { ok: false, code: 'NO_TOKEN', message: 'No IDP access token in session', status: 401, terminal: true };
|
|
114
|
+
}
|
|
69
115
|
}
|
|
70
116
|
const now = Date.now();
|
|
71
117
|
const redisExpires = session.idpAccessTokenExpires ?? 0;
|
|
72
|
-
const jwtExpires = decodeJwtExp(storedToken);
|
|
118
|
+
const jwtExpires = storedToken ? decodeJwtExp(storedToken) : 0;
|
|
73
119
|
// Use the smaller of the two to be conservative — Redis can be stale, JWT exp is authoritative.
|
|
74
120
|
const effectiveExpires = jwtExpires > 0 ? Math.min(redisExpires || jwtExpires, jwtExpires) : redisExpires;
|
|
75
121
|
const msUntilExpiry = effectiveExpires - now;
|
|
76
|
-
if (msUntilExpiry > safetyWindowMs) {
|
|
77
|
-
//
|
|
78
|
-
return { ok: true, accessToken: storedToken, accessTokenExpires: effectiveExpires, refreshed: false, persisted:
|
|
122
|
+
if (storedToken && msUntilExpiry > safetyWindowMs) {
|
|
123
|
+
// This token came OUT of the store and we wrote nothing; `persisted` is true only when that store row is the canonical one.
|
|
124
|
+
return { ok: true, accessToken: storedToken, accessTokenExpires: effectiveExpires, refreshed: false, reason: 'already_fresh', hasRefreshToken: !!session.idpRefreshToken, persisted: sessionIsCanonical };
|
|
79
125
|
}
|
|
80
126
|
if (!session.idpRefreshToken) {
|
|
81
127
|
return {
|
|
@@ -97,7 +143,8 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
97
143
|
await new Promise(r => setTimeout(r, 200));
|
|
98
144
|
const stillLocked = await (0, session_store_1.checkRefreshLock)(sessionToken);
|
|
99
145
|
if (!stillLocked) {
|
|
100
|
-
|
|
146
|
+
const canonicalAfter = await (0, session_store_1.getSession)(sessionToken);
|
|
147
|
+
let after = canonicalAfter;
|
|
101
148
|
if (!after || !after.idpRefreshToken) {
|
|
102
149
|
const baAfter = await (0, session_store_1.getBetterAuthSession)(sessionToken);
|
|
103
150
|
if (baAfter?.idpRefreshToken)
|
|
@@ -111,8 +158,10 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
111
158
|
accessToken: after.idpAccessToken,
|
|
112
159
|
accessTokenExpires: after.idpAccessTokenExpires,
|
|
113
160
|
refreshed: true,
|
|
114
|
-
|
|
115
|
-
|
|
161
|
+
reason: 'completed_by_concurrent_request',
|
|
162
|
+
hasRefreshToken: !!after.idpRefreshToken,
|
|
163
|
+
// Read back from the store after the winner's refresh; `persisted` only when that row is the canonical one.
|
|
164
|
+
persisted: !!canonicalAfter && after === canonicalAfter,
|
|
116
165
|
};
|
|
117
166
|
}
|
|
118
167
|
}
|
|
@@ -134,7 +183,8 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
134
183
|
}
|
|
135
184
|
try {
|
|
136
185
|
// Re-check after lock — another caller may have already refreshed.
|
|
137
|
-
|
|
186
|
+
const canonicalLatest = await (0, session_store_1.getSession)(sessionToken);
|
|
187
|
+
let latest = canonicalLatest;
|
|
138
188
|
if (!latest || !latest.idpRefreshToken) {
|
|
139
189
|
const baLatest = await (0, session_store_1.getBetterAuthSession)(sessionToken);
|
|
140
190
|
if (baLatest?.idpRefreshToken)
|
|
@@ -150,11 +200,51 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
150
200
|
accessToken: latest.idpAccessToken,
|
|
151
201
|
accessTokenExpires: latest.idpAccessTokenExpires,
|
|
152
202
|
refreshed: true,
|
|
153
|
-
|
|
154
|
-
|
|
203
|
+
reason: 'completed_by_concurrent_request',
|
|
204
|
+
hasRefreshToken: !!latest.idpRefreshToken,
|
|
205
|
+
// Post-lock re-read: another caller already refreshed and stored this; `persisted` only when the row read is the canonical one.
|
|
206
|
+
persisted: !!canonicalLatest && latest === canonicalLatest,
|
|
155
207
|
};
|
|
156
208
|
}
|
|
157
209
|
}
|
|
210
|
+
// R-over-E (BAPert 76318 item 4 / 1432 (b2), now E's own): the token SENT is the one in the POST-lock read, so that read must still hold
|
|
211
|
+
// one. A row that VANISHED while we waited for the lock is 'no session'; a row whose refresh token was DISCARDED meanwhile (token cleared,
|
|
212
|
+
// marker written by a concurrent request) is the terminal NO_REFRESH_TOKEN. Neither reaches the IdP, and an empty token is never sent.
|
|
213
|
+
if (!latest) {
|
|
214
|
+
return { ok: false, code: 'NO_SESSION', message: 'No session for token', status: 401, terminal: true };
|
|
215
|
+
}
|
|
216
|
+
if (!latest.idpRefreshToken) {
|
|
217
|
+
return {
|
|
218
|
+
ok: false,
|
|
219
|
+
code: 'NO_REFRESH_TOKEN',
|
|
220
|
+
message: 'No refresh token available',
|
|
221
|
+
status: 401,
|
|
222
|
+
terminal: true,
|
|
223
|
+
resolution: 'User must re-authenticate',
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
// BAPert 76397 Finding 1: the persist below writes ONLY to the canonical row (updateSession merges into it and returns null when it is
|
|
227
|
+
// absent). A session that lives only under the Better Auth key can be READ above through the fallback, but the rotated tokens would have
|
|
228
|
+
// nowhere to go: the IdP would spend the refresh token and we would lose its replacement (SESSION_PERSIST_FAILED, after the fact). So
|
|
229
|
+
// refuse BEFORE the IdP call. Nothing is spent, nothing is discarded, the Better Auth record is left alone; the session has to be
|
|
230
|
+
// re-established.
|
|
231
|
+
if (!canonicalLatest) {
|
|
232
|
+
console.warn('[ENSURE-FRESH] refresh refused before the IDP call - no canonical session row to persist into', {
|
|
233
|
+
requestId,
|
|
234
|
+
traceId,
|
|
235
|
+
betterAuthOnly: !!latest,
|
|
236
|
+
});
|
|
237
|
+
return {
|
|
238
|
+
ok: false,
|
|
239
|
+
code: 'NO_CANONICAL_SESSION',
|
|
240
|
+
message: 'The session has no canonical record to store a refreshed token in; no refresh was attempted.',
|
|
241
|
+
status: 401,
|
|
242
|
+
terminal: true,
|
|
243
|
+
retryable: false,
|
|
244
|
+
discardToken: false,
|
|
245
|
+
resolution: 'REAUTHENTICATE',
|
|
246
|
+
};
|
|
247
|
+
}
|
|
158
248
|
// Build refresh request body — wire shape must match what the IDP expects.
|
|
159
249
|
let authMethods = [];
|
|
160
250
|
if (Array.isArray(session.authenticationMethods)) {
|
|
@@ -185,6 +275,25 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
185
275
|
// Auth-context (amr/acr/2FA) intentionally still derives from `session` — it is
|
|
186
276
|
// the identity context of THIS request, unchanged by a token rotation.
|
|
187
277
|
const sentRefreshToken = latest?.idpRefreshToken ?? session.idpRefreshToken;
|
|
278
|
+
// BAPert 76403: an earlier attempt for THIS token timed out, so Core may already have consumed it; re-sending it is a replay, and a
|
|
279
|
+
// replay revokes every newer token the user holds. Refuse WITHOUT calling the IdP; one fresh login instead.
|
|
280
|
+
if ((0, refresh_in_doubt_1.isRefreshInDoubt)(canonicalLatest, sentRefreshToken)) {
|
|
281
|
+
console.error('[ENSURE-FRESH] refusing to re-send a refresh token whose earlier attempt timed out (outcome unknown)', {
|
|
282
|
+
requestId,
|
|
283
|
+
traceId,
|
|
284
|
+
inDoubtSince: canonicalLatest.refreshInDoubtAt,
|
|
285
|
+
});
|
|
286
|
+
return {
|
|
287
|
+
ok: false,
|
|
288
|
+
code: refresh_in_doubt_1.REFRESH_OUTCOME_UNKNOWN_CODE,
|
|
289
|
+
message: 'An earlier refresh for this session did not complete and its outcome is unknown; the session must be re-established.',
|
|
290
|
+
status: 401,
|
|
291
|
+
terminal: true,
|
|
292
|
+
retryable: false,
|
|
293
|
+
discardToken: false,
|
|
294
|
+
resolution: 'REAUTHENTICATE',
|
|
295
|
+
};
|
|
296
|
+
}
|
|
188
297
|
const body = {
|
|
189
298
|
refresh_token: sentRefreshToken,
|
|
190
299
|
amr: authMethods,
|
|
@@ -196,19 +305,47 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
196
305
|
body.two_factor_method = twoFactorMethod;
|
|
197
306
|
if (session.mfaCompletedAt)
|
|
198
307
|
body.two_factor_completed_at = new Date(session.mfaCompletedAt).toISOString();
|
|
308
|
+
// BAPert 76403: a timeout is OUTCOME-UNKNOWN. Record the identity of the token we sent (a hash) so the next attempt refuses instead of
|
|
309
|
+
// replaying it, and answer 504. `retryable` is false on purpose: the retry would be refused with REFRESH_OUTCOME_UNKNOWN.
|
|
310
|
+
const answerTimedOut = async (cause) => {
|
|
311
|
+
console.error('[ENSURE-FRESH] IDP did not answer (timeout or a transport failure that does not prove the request never left) - outcome unknown, marking the refresh in doubt', {
|
|
312
|
+
requestId,
|
|
313
|
+
traceId,
|
|
314
|
+
timeoutMs: refresh_in_doubt_1.REFRESH_FETCH_TIMEOUT_MS,
|
|
315
|
+
cause: cause instanceof Error ? `${cause.name}: ${cause.message}` : undefined,
|
|
316
|
+
});
|
|
317
|
+
try {
|
|
318
|
+
await (0, session_store_1.updateSession)(sessionToken, (0, refresh_in_doubt_1.inDoubtPatch)(sentRefreshToken));
|
|
319
|
+
}
|
|
320
|
+
catch (markError) {
|
|
321
|
+
console.error('[ENSURE-FRESH] could not record the in-doubt marker', { error: markError instanceof Error ? markError.message : String(markError) });
|
|
322
|
+
}
|
|
323
|
+
return {
|
|
324
|
+
ok: false,
|
|
325
|
+
code: refresh_in_doubt_1.UPSTREAM_TIMEOUT_CODE,
|
|
326
|
+
message: 'The identity service did not answer in time; the outcome of this refresh is unknown.',
|
|
327
|
+
status: 504,
|
|
328
|
+
terminal: false,
|
|
329
|
+
retryable: false,
|
|
330
|
+
discardToken: false,
|
|
331
|
+
};
|
|
332
|
+
};
|
|
199
333
|
let idpResponse;
|
|
200
334
|
try {
|
|
201
335
|
idpResponse = await fetch(`${idpBaseUrl}${refreshEndpoint}`, {
|
|
202
336
|
method: 'POST',
|
|
203
337
|
headers: { 'Content-Type': 'application/json', 'X-Client-Id': clientId },
|
|
204
338
|
body: JSON.stringify(body),
|
|
339
|
+
signal: AbortSignal.timeout(refresh_in_doubt_1.REFRESH_FETCH_TIMEOUT_MS),
|
|
205
340
|
});
|
|
206
341
|
}
|
|
207
342
|
catch (err) {
|
|
343
|
+
if (!(0, refresh_in_doubt_1.requestNeverLeft)(err))
|
|
344
|
+
return await answerTimedOut(err); // timeout or ANY transport failure that does not prove the request never left (BAPert 76433)
|
|
208
345
|
return {
|
|
209
346
|
ok: false,
|
|
210
347
|
code: 'UPSTREAM_SERVICE_UNAVAILABLE',
|
|
211
|
-
message:
|
|
348
|
+
message: 'IDP service unavailable', // BAPert 76449: R's wording; the raw error text (host names, socket details) is not returned
|
|
212
349
|
status: 503,
|
|
213
350
|
retryable: true,
|
|
214
351
|
};
|
|
@@ -217,12 +354,32 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
217
354
|
try {
|
|
218
355
|
const text = await idpResponse.text();
|
|
219
356
|
if (!text.trim()) {
|
|
357
|
+
if (idpResponse.ok)
|
|
358
|
+
return await answerTimedOut(new Error('empty 2xx body')); // a 2xx with no body: the replacement is lost (BAPert 76441)
|
|
220
359
|
return { ok: false, code: 'UPSTREAM_SERVICE_ERROR', message: 'Empty response from IDP', status: 502, retryable: true };
|
|
221
360
|
}
|
|
222
361
|
responseData = JSON.parse(text);
|
|
223
362
|
}
|
|
224
363
|
catch (err) {
|
|
225
|
-
|
|
364
|
+
if (!(err instanceof SyntaxError) || idpResponse.ok)
|
|
365
|
+
return await answerTimedOut(err); // BAPert 76441: a body-READ failure is in doubt; so is a 2xx whose body does not parse (Core processed the grant, i.e. consumed the token, and the replacement is lost). A non-2xx unparseable body (a gateway HTML 502/503) did not mint: a completed failure, no marker.
|
|
366
|
+
return { ok: false, code: 'UPSTREAM_SERVICE_ERROR', message: 'Invalid response from IDP', status: 502, retryable: true };
|
|
367
|
+
}
|
|
368
|
+
// BAPert 76455 (B): ONE envelope rule for both refresh implementations. `meta` is NOT required: Core's four hint-carrying error returns on
|
|
369
|
+
// POST /api/ExternalAuth/refresh (ExternalAuthController :3708, :3778, :3805, :3822) are hand-built { success:false, error:{ code,
|
|
370
|
+
// message, discard_token, retryable, resolution } } objects with no meta, no data, no request_id, no time_stamp, and they are exactly the
|
|
371
|
+
// answers whose code and flags matter. Requiring meta would turn them into a 502 and lose both.
|
|
372
|
+
const envelope = checkRefreshEnvelope(responseData, idpResponse.ok);
|
|
373
|
+
if (!envelope.ok) {
|
|
374
|
+
console.warn('[ENSURE-FRESH] non-compliant IDP envelope', {
|
|
375
|
+
requestId,
|
|
376
|
+
traceId,
|
|
377
|
+
// the response header if Core sent one, otherwise our own generated id - never a made-up Core request_id
|
|
378
|
+
idpRequestId: idpResponse.headers?.get?.('x-request-id') ?? requestId,
|
|
379
|
+
status: idpResponse.status,
|
|
380
|
+
why: envelope.why,
|
|
381
|
+
});
|
|
382
|
+
return { ok: false, code: 'UPSTREAM_SERVICE_ERROR', message: 'Non-compliant IDP envelope', status: 502, retryable: true };
|
|
226
383
|
}
|
|
227
384
|
if (!idpResponse.ok) {
|
|
228
385
|
const idpError = responseData?.error || {};
|
|
@@ -285,7 +442,9 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
285
442
|
accessToken: postRefresh.idpAccessToken,
|
|
286
443
|
accessTokenExpires: postRefresh.idpAccessTokenExpires,
|
|
287
444
|
refreshed: true,
|
|
288
|
-
|
|
445
|
+
reason: 'completed_by_concurrent_request',
|
|
446
|
+
hasRefreshToken: !!postRefresh.idpRefreshToken,
|
|
447
|
+
// `postRefresh` is a canonical store read (getSession), so this token is stored where the persist writes.
|
|
289
448
|
persisted: true,
|
|
290
449
|
};
|
|
291
450
|
}
|
|
@@ -348,6 +507,8 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
348
507
|
accessToken: postRefresh.idpAccessToken,
|
|
349
508
|
accessTokenExpires: postRefresh.idpAccessTokenExpires,
|
|
350
509
|
refreshed: true,
|
|
510
|
+
reason: 'completed_by_concurrent_request',
|
|
511
|
+
hasRefreshToken: !!postRefresh.idpRefreshToken,
|
|
351
512
|
persisted: true,
|
|
352
513
|
};
|
|
353
514
|
}
|
|
@@ -391,13 +552,13 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
391
552
|
amrClaims = typeof decoded.amr === 'string' ? JSON.parse(decoded.amr) : decoded.amr;
|
|
392
553
|
}
|
|
393
554
|
catch {
|
|
394
|
-
amrClaims =
|
|
555
|
+
amrClaims = canonicalLatest.authenticationMethods || [];
|
|
395
556
|
}
|
|
396
557
|
}
|
|
397
558
|
else {
|
|
398
|
-
amrClaims =
|
|
559
|
+
amrClaims = canonicalLatest.authenticationMethods || [];
|
|
399
560
|
}
|
|
400
|
-
const acrLevel = String(decoded?.acr ||
|
|
561
|
+
const acrLevel = String(decoded?.acr || canonicalLatest.authenticationLevel || '1');
|
|
401
562
|
const hasNewRefresh = typeof newRefresh === 'string' && newRefresh.length > 0;
|
|
402
563
|
const newKid = (0, token_utils_1.extractKidFromToken)(newAccess);
|
|
403
564
|
// *** CARD 320072. CAPTURE THIS RETURN. ***
|
|
@@ -407,14 +568,19 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
407
568
|
// report `ok:true, refreshed:true` for a refresh it had just thrown away.
|
|
408
569
|
// The full rationale for what we DO about it is at the `if (!persisted)`
|
|
409
570
|
// block below; this line only makes the answer available.
|
|
571
|
+
//
|
|
572
|
+
// BAPert 76449 item 11 (the twin of 1432 (b2)): every FALLBACK below reads the POST-lock canonical row (`canonicalLatest`), never the
|
|
573
|
+
// pre-lock `session`. With no new refresh token from the IdP the store must keep the token that was SENT (`sentRefreshToken`); the
|
|
574
|
+
// pre-lock value is spent whenever a concurrent request rotated the token while this one waited for the lock. The old `...session`
|
|
575
|
+
// spread (the whole PRE-lock row) is gone too: updateSession merges into the current row, so only the fields below are written and a
|
|
576
|
+
// concurrent writer's change to anything else survives.
|
|
410
577
|
const persisted = await (0, session_store_1.updateSession)(sessionToken, {
|
|
411
|
-
...session,
|
|
412
578
|
idpAccessToken: newAccess,
|
|
413
579
|
idpAccessTokenExpires: accessTokenExpires,
|
|
414
|
-
idpRefreshToken: hasNewRefresh ? newRefresh :
|
|
415
|
-
idpRefreshTokenExpires: hasNewRefresh ? refreshTokenExpires :
|
|
580
|
+
idpRefreshToken: hasNewRefresh ? newRefresh : sentRefreshToken,
|
|
581
|
+
idpRefreshTokenExpires: hasNewRefresh ? refreshTokenExpires : canonicalLatest.idpRefreshTokenExpires,
|
|
416
582
|
decodedAccessToken: decoded,
|
|
417
|
-
bearerKeyId: newKid ||
|
|
583
|
+
bearerKeyId: newKid || canonicalLatest.bearerKeyId,
|
|
418
584
|
authenticationMethods: amrClaims,
|
|
419
585
|
authenticationLevel: acrLevel,
|
|
420
586
|
// 170007 rule 2 (BAPert ruling on 121350): re-derive from the token
|
|
@@ -422,9 +588,9 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
422
588
|
// rationale in api-handlers/auth/refresh.ts's sessionUpdate — this is
|
|
423
589
|
// the same defect duplicated at a second call site.
|
|
424
590
|
mfaVerified: amrClaims.includes('mfa'),
|
|
425
|
-
mfaCompletedAt: decoded?.mfa_time ? parseInt(decoded.mfa_time) * 1000 :
|
|
426
|
-
mfaExpiresAt: decoded?.mfa_expires ? parseInt(decoded.mfa_expires) * 1000 :
|
|
427
|
-
mfaValidityHours: decoded?.mfa_validity_hours ? parseInt(decoded.mfa_validity_hours) :
|
|
591
|
+
mfaCompletedAt: decoded?.mfa_time ? parseInt(decoded.mfa_time) * 1000 : canonicalLatest.mfaCompletedAt,
|
|
592
|
+
mfaExpiresAt: decoded?.mfa_expires ? parseInt(decoded.mfa_expires) * 1000 : canonicalLatest.mfaExpiresAt,
|
|
593
|
+
mfaValidityHours: decoded?.mfa_validity_hours ? parseInt(decoded.mfa_validity_hours) : canonicalLatest.mfaValidityHours,
|
|
428
594
|
});
|
|
429
595
|
/**
|
|
430
596
|
* 320072: DO NOT REPORT SUCCESS FOR A WRITE THAT WAS DISCARDED.
|
|
@@ -489,7 +655,7 @@ async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
|
489
655
|
// the only way out for a dropped write, so by construction anything reaching
|
|
490
656
|
// this line WAS written. Keeping the field keeps the ok:true branch of
|
|
491
657
|
// EnsureFreshResult total (Cases 2 and 7 assert on it) — BAPert R1145 §2d.
|
|
492
|
-
return { ok: true, accessToken: newAccess, accessTokenExpires, refreshed: true, persisted: true };
|
|
658
|
+
return { ok: true, accessToken: newAccess, accessTokenExpires, refreshed: true, reason: 'refreshed', hasRefreshToken: true, persisted: true };
|
|
493
659
|
}
|
|
494
660
|
finally {
|
|
495
661
|
if (weHoldLock) {
|
package/dist/lib/idp-fetch.js
CHANGED
|
@@ -42,6 +42,7 @@ const idp_url_guard_1 = require("./idp-url-guard");
|
|
|
42
42
|
Object.defineProperty(exports, "assertAbsoluteIdpUrl", { enumerable: true, get: function () { return idp_url_guard_1.assertAbsoluteIdpUrl; } });
|
|
43
43
|
const retry_after_1 = require("./retry-after");
|
|
44
44
|
Object.defineProperty(exports, "parseRetryAfterSeconds", { enumerable: true, get: function () { return retry_after_1.parseRetryAfterSeconds; } });
|
|
45
|
+
const refresh_window_1 = require("./refresh-window");
|
|
45
46
|
function buildHeaders(req, bearer, extra) {
|
|
46
47
|
const headers = {
|
|
47
48
|
Accept: 'application/json',
|
|
@@ -118,8 +119,8 @@ async function ensureFreshAccessToken(req) {
|
|
|
118
119
|
const hasRefreshToken = !!session.idpRefreshToken;
|
|
119
120
|
const now = Date.now();
|
|
120
121
|
const timeLeft = (session.idpAccessTokenExpires ?? now) - now;
|
|
121
|
-
// Treat tokens as effectively expired if within
|
|
122
|
-
if (timeLeft >
|
|
122
|
+
// Treat tokens as effectively expired if within REFRESH_WINDOW_MS of expiry (lib/refresh-window: the one window)
|
|
123
|
+
if (timeLeft > refresh_window_1.REFRESH_WINDOW_MS) {
|
|
123
124
|
return { sessionToken, accessToken: session.idpAccessToken, hasRefreshToken };
|
|
124
125
|
}
|
|
125
126
|
// Only attempt refresh when we actually have a refresh token to use.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** How long a refresh fetch (connect, response and body) may take. 5 s lock wait + this + the persist stays well inside the 60 s lock TTL. */
|
|
2
|
+
export declare const REFRESH_FETCH_TIMEOUT_MS = 10000;
|
|
3
|
+
/** The typed terminal answer for an attempt that would re-send an in-doubt token. */
|
|
4
|
+
export declare const REFRESH_OUTCOME_UNKNOWN_CODE = "REFRESH_OUTCOME_UNKNOWN";
|
|
5
|
+
/** The typed answer for the timed-out call itself. */
|
|
6
|
+
export declare const UPSTREAM_TIMEOUT_CODE = "UPSTREAM_TIMEOUT";
|
|
7
|
+
/** A one-way identity for a refresh token. Not reversible and not a prefix. */
|
|
8
|
+
export declare function refreshTokenIdentity(token: string): string;
|
|
9
|
+
/** The row fields recorded at a timeout. */
|
|
10
|
+
export declare function inDoubtPatch(sentToken: string): {
|
|
11
|
+
refreshInDoubtTokenHash: string;
|
|
12
|
+
refreshInDoubtAt: number;
|
|
13
|
+
};
|
|
14
|
+
/** True when the token we are about to send is the one a previous attempt left in doubt. */
|
|
15
|
+
export declare function isRefreshInDoubt(session: {
|
|
16
|
+
refreshInDoubtTokenHash?: string | null;
|
|
17
|
+
} | null | undefined, tokenToSend: string | null | undefined): boolean;
|
|
18
|
+
/** True for the error `AbortSignal.timeout` produces (a DOMException named TimeoutError); AbortError is treated the same. */
|
|
19
|
+
export declare function isTimeoutError(err: unknown): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* The ONLY transport error codes that PROVE the request never left this process (BAPert 76433 item 4): the connection was refused, or the
|
|
22
|
+
* name did not resolve. Anything else (ECONNRESET, UND_ERR_SOCKET, a timeout, an error with no code) may have happened AFTER the request was
|
|
23
|
+
* written, so the IdP may have consumed the token: outcome unknown. The cost is lopsided on purpose: a false in-doubt costs one re-login, a
|
|
24
|
+
* missed one costs a user-wide revocation. Do NOT add a code here without proof it can only occur before the request is sent.
|
|
25
|
+
*/
|
|
26
|
+
export declare const REQUEST_NEVER_LEFT_CODES: ReadonlySet<string>;
|
|
27
|
+
/**
|
|
28
|
+
* True only when the error carries at least one cause code and EVERY code it carries is in the never-left allowlist. An error with no code
|
|
29
|
+
* is NOT proof, so it is in-doubt.
|
|
30
|
+
*/
|
|
31
|
+
export declare function requestNeverLeft(err: unknown): boolean;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.REQUEST_NEVER_LEFT_CODES = exports.UPSTREAM_TIMEOUT_CODE = exports.REFRESH_OUTCOME_UNKNOWN_CODE = exports.REFRESH_FETCH_TIMEOUT_MS = void 0;
|
|
4
|
+
exports.refreshTokenIdentity = refreshTokenIdentity;
|
|
5
|
+
exports.inDoubtPatch = inDoubtPatch;
|
|
6
|
+
exports.isRefreshInDoubt = isRefreshInDoubt;
|
|
7
|
+
exports.isTimeoutError = isTimeoutError;
|
|
8
|
+
exports.requestNeverLeft = requestNeverLeft;
|
|
9
|
+
/**
|
|
10
|
+
* The IdP fetch timeout and the "refresh in doubt" marker (BAPert 76397 decision 3, corrected by 76403; Core facts DotNetPert 76400).
|
|
11
|
+
*
|
|
12
|
+
* THE PROBLEM. Core consumes a refresh token with one atomic UPDATE and has NO reuse grace: re-presenting a used token revokes every newer
|
|
13
|
+
* refresh token the USER holds, on every device (RevokeDescendantTokens). So when OUR fetch to the IdP times out, we do not know whether Core
|
|
14
|
+
* consumed the token and minted a replacement whose answer we never saw. The store still holds the token we SENT (nothing was persisted).
|
|
15
|
+
* Holding the refresh lock only delays the next request by its 60 s TTL; after that the next request re-reads the same token and replays it.
|
|
16
|
+
*
|
|
17
|
+
* THE RULE. A timed-out refresh answers 504 UPSTREAM_TIMEOUT and records, on the session row, an IDENTITY (a SHA-256, never the token and
|
|
18
|
+
* never a prefix of it) of the token that was sent. Any later attempt whose token-to-send matches that identity refuses WITHOUT calling the
|
|
19
|
+
* IdP: terminal REFRESH_OUTCOME_UNKNOWN / REAUTHENTICATE. One login on one session is better than a user-wide revocation. The marker is
|
|
20
|
+
* dropped by the same write that stores a DIFFERENT refresh token (session-store `dropStaleDiscardMarker`); nothing tries to resolve the doubt
|
|
21
|
+
* without a login.
|
|
22
|
+
*
|
|
23
|
+
* Leaf module: no imports from the rest of the package, so session-store can use it without a cycle.
|
|
24
|
+
*/
|
|
25
|
+
const crypto_1 = require("crypto");
|
|
26
|
+
/** How long a refresh fetch (connect, response and body) may take. 5 s lock wait + this + the persist stays well inside the 60 s lock TTL. */
|
|
27
|
+
exports.REFRESH_FETCH_TIMEOUT_MS = 10_000;
|
|
28
|
+
/** The typed terminal answer for an attempt that would re-send an in-doubt token. */
|
|
29
|
+
exports.REFRESH_OUTCOME_UNKNOWN_CODE = 'REFRESH_OUTCOME_UNKNOWN';
|
|
30
|
+
/** The typed answer for the timed-out call itself. */
|
|
31
|
+
exports.UPSTREAM_TIMEOUT_CODE = 'UPSTREAM_TIMEOUT';
|
|
32
|
+
/** A one-way identity for a refresh token. Not reversible and not a prefix. */
|
|
33
|
+
function refreshTokenIdentity(token) {
|
|
34
|
+
return (0, crypto_1.createHash)('sha256').update(token, 'utf8').digest('hex');
|
|
35
|
+
}
|
|
36
|
+
/** The row fields recorded at a timeout. */
|
|
37
|
+
function inDoubtPatch(sentToken) {
|
|
38
|
+
return { refreshInDoubtTokenHash: refreshTokenIdentity(sentToken), refreshInDoubtAt: Date.now() };
|
|
39
|
+
}
|
|
40
|
+
/** True when the token we are about to send is the one a previous attempt left in doubt. */
|
|
41
|
+
function isRefreshInDoubt(session, tokenToSend) {
|
|
42
|
+
if (!session || typeof session.refreshInDoubtTokenHash !== 'string' || !session.refreshInDoubtTokenHash)
|
|
43
|
+
return false;
|
|
44
|
+
if (typeof tokenToSend !== 'string' || tokenToSend.length === 0)
|
|
45
|
+
return false;
|
|
46
|
+
return refreshTokenIdentity(tokenToSend) === session.refreshInDoubtTokenHash;
|
|
47
|
+
}
|
|
48
|
+
/** True for the error `AbortSignal.timeout` produces (a DOMException named TimeoutError); AbortError is treated the same. */
|
|
49
|
+
function isTimeoutError(err) {
|
|
50
|
+
const name = err?.name;
|
|
51
|
+
return name === 'TimeoutError' || name === 'AbortError';
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The ONLY transport error codes that PROVE the request never left this process (BAPert 76433 item 4): the connection was refused, or the
|
|
55
|
+
* name did not resolve. Anything else (ECONNRESET, UND_ERR_SOCKET, a timeout, an error with no code) may have happened AFTER the request was
|
|
56
|
+
* written, so the IdP may have consumed the token: outcome unknown. The cost is lopsided on purpose: a false in-doubt costs one re-login, a
|
|
57
|
+
* missed one costs a user-wide revocation. Do NOT add a code here without proof it can only occur before the request is sent.
|
|
58
|
+
*/
|
|
59
|
+
exports.REQUEST_NEVER_LEFT_CODES = new Set(['ECONNREFUSED', 'ENOTFOUND', 'EAI_AGAIN']);
|
|
60
|
+
/** Every `code` on the error, its `cause`, and the members of an AggregateError (undici wraps the socket error in `cause`). */
|
|
61
|
+
function transportCodes(err, depth = 0, out = []) {
|
|
62
|
+
if (!err || typeof err !== 'object' || depth > 4)
|
|
63
|
+
return out;
|
|
64
|
+
const e = err;
|
|
65
|
+
if (typeof e.code === 'string' && e.code)
|
|
66
|
+
out.push(e.code);
|
|
67
|
+
if (e.cause)
|
|
68
|
+
transportCodes(e.cause, depth + 1, out);
|
|
69
|
+
if (Array.isArray(e.errors))
|
|
70
|
+
for (const inner of e.errors)
|
|
71
|
+
transportCodes(inner, depth + 1, out);
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* True only when the error carries at least one cause code and EVERY code it carries is in the never-left allowlist. An error with no code
|
|
76
|
+
* is NOT proof, so it is in-doubt.
|
|
77
|
+
*/
|
|
78
|
+
function requestNeverLeft(err) {
|
|
79
|
+
const codes = transportCodes(err);
|
|
80
|
+
return codes.length > 0 && codes.every((c) => exports.REQUEST_NEVER_LEFT_CODES.has(c));
|
|
81
|
+
}
|