@payez/next-mvp 4.7.9 → 4.8.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.
@@ -6,6 +6,7 @@
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.GET = GET;
9
+ const revocation_fields_1 = require("../../auth/revocation-fields");
9
10
  const server_1 = require("next/server");
10
11
  const session_store_1 = require("../../lib/session-store");
11
12
  const auth_1 = require("../../server/auth");
@@ -45,7 +46,7 @@ async function GET(req) {
45
46
  const sessionState = await (0, auth_1.resolveSessionState)(req);
46
47
  if (sessionState.state === 'unknown') {
47
48
  console.warn('[VIABILITY] Session store unavailable - session state unknown');
48
- return server_1.NextResponse.json({ authenticated: false, revocationStatus: 'unknown', error: 'Session store unavailable', code: 'STORE_UNAVAILABLE' }, { status: 500 });
49
+ return server_1.NextResponse.json({ authenticated: false, revocationStatus: 'unknown', unavailable: true, error: 'Session store unavailable', code: 'STORE_UNAVAILABLE' }, { status: 500 });
49
50
  }
50
51
  if (sessionState.state === 'missing') {
51
52
  console.warn('[VIABILITY] Stale cookie detected - verified cookie, session not in Redis');
@@ -174,8 +175,7 @@ async function GET(req) {
174
175
  // Session revocation (PAY-1882 step 4): the four-state status, plus the
175
176
  // boolean the middleware's existing force-invalid branch already reads.
176
177
  revocationStatus,
177
- forceInvalid: revocationStatus === 'revoked' || revocationStatus === 'unknown',
178
- revoked: revocationStatus === 'revoked',
178
+ ...(0, revocation_fields_1.revocationFields)(revocationStatus), // PAY-2339: forceInvalid only for 'revoked'; unavailable:true for 'unknown'
179
179
  // Cards 141688/141741 — the IDP's routing decision, relayed verbatim.
180
180
  //
181
181
  // THIS IS THE SECOND IMPLEMENTATION OF VIABILITY. The other is
@@ -17,6 +17,8 @@ export interface AuthContext {
17
17
  sessionStatus: {
18
18
  exists: boolean | null;
19
19
  forceInvalid: boolean | null;
20
+ /** PAY-2339: the producer said the revocation backend could not answer. makeAuthDecision HOLDS (no sign-out). */
21
+ unavailable?: boolean;
20
22
  requires2FA: boolean;
21
23
  twoFactorComplete: boolean;
22
24
  };
@@ -34,6 +36,7 @@ export type AuthAction = {
34
36
  } | {
35
37
  type: 'service_error';
36
38
  reason: string;
39
+ check?: 'revocation' | 'viability';
37
40
  };
38
41
  /**
39
42
  * Main decision function - pure, testable, deterministic.
@@ -94,6 +94,15 @@ function decide(context) {
94
94
  reason: 'redis_unavailable'
95
95
  };
96
96
  }
97
+ // PAY-2339 (BAPert 74633): the revocation backend could not answer. Hold: not a verdict on the session, so no
98
+ // sign-out and no cookie is touched. A caller maps this to its own "temporarily unavailable" answer (the
99
+ // createMvpMiddleware twin sends /service-unavailable or a 503 with unavailable:true, check:'revocation').
100
+ if (sessionStatus.unavailable) {
101
+ if (isLoginPage) {
102
+ return { type: 'allow' };
103
+ }
104
+ return { type: 'service_error', reason: 'revocation_unknown', check: 'revocation' };
105
+ }
97
106
  // Session force invalidated - need fresh login
98
107
  if (sessionStatus.forceInvalid) {
99
108
  if (isLoginPage) {
@@ -0,0 +1,16 @@
1
+ /**
2
+ * PAY-2339 (BAPert 74633 gap 1): the two revocation fields every viability producer emits, in ONE place so the
3
+ * producers (api-handlers/session/viability.ts and routes/auth/viability.ts, four return sites) cannot drift.
4
+ *
5
+ * forceInvalid - true ONLY for 'revoked'. An 'unknown' answer is no longer a sign-out verdict.
6
+ * unavailable - true when the revocation backend could not answer: 'unknown' and ANY string that is not one of the
7
+ * three recognised states. A consumer that reads this JSON (useSessionCheck, makeAuthDecision) holds
8
+ * instead of signing the member out. `revocationStatus` itself is emitted unchanged.
9
+ *
10
+ * 'not_configured' is a configuration fault, not an outage: neither field is set for it (unchanged).
11
+ */
12
+ export declare function revocationFields(status: unknown): {
13
+ forceInvalid: boolean;
14
+ unavailable: boolean;
15
+ revoked: boolean;
16
+ };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.revocationFields = revocationFields;
4
+ /**
5
+ * PAY-2339 (BAPert 74633 gap 1): the two revocation fields every viability producer emits, in ONE place so the
6
+ * producers (api-handlers/session/viability.ts and routes/auth/viability.ts, four return sites) cannot drift.
7
+ *
8
+ * forceInvalid - true ONLY for 'revoked'. An 'unknown' answer is no longer a sign-out verdict.
9
+ * unavailable - true when the revocation backend could not answer: 'unknown' and ANY string that is not one of the
10
+ * three recognised states. A consumer that reads this JSON (useSessionCheck, makeAuthDecision) holds
11
+ * instead of signing the member out. `revocationStatus` itself is emitted unchanged.
12
+ *
13
+ * 'not_configured' is a configuration fault, not an outage: neither field is set for it (unchanged).
14
+ */
15
+ function revocationFields(status) {
16
+ const revoked = status === 'revoked';
17
+ const unavailable = !(status === 'revoked' || status === 'active' || status === 'not_configured');
18
+ return { forceInvalid: revoked, unavailable, revoked };
19
+ }
@@ -9,6 +9,8 @@ interface AuthProviderProps {
9
9
  */
10
10
  useDynamicProviders?: boolean;
11
11
  }
12
+ /** PAY-2334: how long /api/auth/settings may take before the sign-in card stops waiting and says it could not load. */
13
+ export declare const SETTINGS_FETCH_TIMEOUT_MS = 10000;
12
14
  export declare function AuthProvider({ children, config, useDynamicProviders }: AuthProviderProps): import("react/jsx-runtime").JSX.Element;
13
15
  export declare function useAuthConfig(): AuthConfig;
14
16
  export declare function useAuthMode(): AuthMode;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  'use client';
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.SETTINGS_FETCH_TIMEOUT_MS = void 0;
4
5
  exports.AuthProvider = AuthProvider;
5
6
  exports.useAuthConfig = useAuthConfig;
6
7
  exports.useAuthMode = useAuthMode;
@@ -29,9 +30,14 @@ const PROVIDER_MAP = {
29
30
  };
30
31
  // OAuth providers we support in UI (excludes credentials)
31
32
  const OAUTH_PROVIDER_IDS = ['google', 'apple', 'facebook', 'github', 'azure-ad', 'microsoft-entra-id'];
33
+ /** PAY-2334: how long /api/auth/settings may take before the sign-in card stops waiting and says it could not load. */
34
+ exports.SETTINGS_FETCH_TIMEOUT_MS = 10000;
32
35
  function AuthProvider({ children, config, useDynamicProviders = true }) {
33
36
  const [dynamicProviders, setDynamicProviders] = (0, react_1.useState)([]);
34
37
  const [providersLoaded, setProvidersLoaded] = (0, react_1.useState)(!useDynamicProviders);
38
+ // PAY-2334: providersLoaded is true on failure too (it only means "stop waiting"), so it cannot tell an empty answer from
39
+ // no answer. This can: loading -> ready (answered) | failed (non-2xx, thrown, timed out).
40
+ const [settingsStatus, setSettingsStatus] = (0, react_1.useState)(useDynamicProviders ? 'loading' : 'ready');
35
41
  // What the IDP says this client offers. null until /api/auth/settings answers.
36
42
  const [serverSettings, setServerSettings] = (0, react_1.useState)(null);
37
43
  // Create QueryClient instance for React Query - used internally by MVP hooks
@@ -50,6 +56,9 @@ function AuthProvider({ children, config, useDynamicProviders = true }) {
50
56
  return;
51
57
  let mounted = true;
52
58
  async function fetchDynamicProviders() {
59
+ const controller = new AbortController();
60
+ const timer = setTimeout(() => controller.abort(), exports.SETTINGS_FETCH_TIMEOUT_MS);
61
+ let status = 'failed';
53
62
  try {
54
63
  // READ THE IDP. This used to be a hardcoded literal:
55
64
  // const result = { google: { id: 'google', name: 'Google' } };
@@ -59,7 +68,7 @@ function AuthProvider({ children, config, useDynamicProviders = true }) {
59
68
  //
60
69
  // /api/auth/settings already serves the real answer (enabledProviders,
61
70
  // allowFederatedLogin, enablePasswordReset) and nothing consumed it.
62
- const response = await fetch('/api/auth/settings');
71
+ const response = await fetch('/api/auth/settings', { signal: controller.signal });
63
72
  if (!response.ok) {
64
73
  // No silent substitution. Absent settings means we do not know what this
65
74
  // client offers, and inventing a provider list is how a tenant ends up with a
@@ -79,6 +88,7 @@ function AuthProvider({ children, config, useDynamicProviders = true }) {
79
88
  .filter(id => OAUTH_PROVIDER_IDS.includes(id))
80
89
  .map(id => PROVIDER_MAP[id])
81
90
  .filter((p) => p !== undefined);
91
+ status = 'ready';
82
92
  setDynamicProviders(oauthProviders);
83
93
  setServerSettings({
84
94
  allowFederatedLogin: settings?.allowFederatedLogin !== false,
@@ -92,7 +102,9 @@ function AuthProvider({ children, config, useDynamicProviders = true }) {
92
102
  console.error('[AuthProvider] could not read /api/auth/settings:', err);
93
103
  }
94
104
  finally {
105
+ clearTimeout(timer);
95
106
  if (mounted) {
107
+ setSettingsStatus(status);
96
108
  setProvidersLoaded(true);
97
109
  }
98
110
  }
@@ -110,6 +122,7 @@ function AuthProvider({ children, config, useDynamicProviders = true }) {
110
122
  ...defaultConfig,
111
123
  ...config,
112
124
  providers, // from the IDP when useDynamicProviders is on
125
+ settingsStatus: useDynamicProviders ? settingsStatus : 'ready',
113
126
  // THE SERVER DECIDES, THE SITE MAY OVERRIDE. A site that passes an explicit value
114
127
  // still wins; absent that, what the tenant's IDP reports is what renders. A client
115
128
  // hardcoding its own auth mode is how a sign-in screen and its IDP disagree.
@@ -37,7 +37,10 @@ function ModeAwareLoginPage({ onFederatedSignIn, onTraditionalSignIn, onForgotPa
37
37
  return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [error && ((0, jsx_runtime_1.jsx)("div", { className: "mb-6 px-4 py-3 rounded-lg", style: {
38
38
  background: 'var(--color-error, #ef4444)',
39
39
  color: 'white',
40
- }, children: error })), isFederated && ((0, jsx_runtime_1.jsx)(FederatedAuthSection_1.FederatedAuthSection, { providers: config.providers, onProviderClick: handleFederatedClick, isLoading: totalLoading })), isFederated && isTraditional && ((0, jsx_runtime_1.jsxs)("div", { className: "flex items-center gap-3 my-6", "aria-hidden": "true", children: [(0, jsx_runtime_1.jsx)("span", { className: "h-px flex-1", style: { background: 'var(--border-default, #e5e7eb)' } }), (0, jsx_runtime_1.jsx)("span", { className: "text-xs uppercase tracking-wide", style: { color: 'var(--text-muted, #6b7280)' }, children: "or" }), (0, jsx_runtime_1.jsx)("span", { className: "h-px flex-1", style: { background: 'var(--border-default, #e5e7eb)' } })] })), isTraditional && ((0, jsx_runtime_1.jsx)(TraditionalAuthSection_1.TraditionalAuthSection, { email: email, password: password, onEmailChange: setEmail, onPasswordChange: setPassword, onSubmit: handleTraditionalSubmit, isLoading: totalLoading, buttonText: "Sign In", showForgotPassword: config.enableRecovery, onForgotPassword: onForgotPassword })), !isFederated && !isTraditional && ((0, jsx_runtime_1.jsxs)("div", { role: "alert", className: "px-4 py-3 rounded-lg text-sm text-left", style: {
40
+ }, children: error })), isFederated && ((0, jsx_runtime_1.jsx)(FederatedAuthSection_1.FederatedAuthSection, { providers: config.providers, onProviderClick: handleFederatedClick, isLoading: totalLoading })), isFederated && isTraditional && ((0, jsx_runtime_1.jsxs)("div", { className: "flex items-center gap-3 my-6", "aria-hidden": "true", children: [(0, jsx_runtime_1.jsx)("span", { className: "h-px flex-1", style: { background: 'var(--border-default, #e5e7eb)' } }), (0, jsx_runtime_1.jsx)("span", { className: "text-xs uppercase tracking-wide", style: { color: 'var(--text-muted, #6b7280)' }, children: "or" }), (0, jsx_runtime_1.jsx)("span", { className: "h-px flex-1", style: { background: 'var(--border-default, #e5e7eb)' } })] })), isTraditional && ((0, jsx_runtime_1.jsx)(TraditionalAuthSection_1.TraditionalAuthSection, { email: email, password: password, onEmailChange: setEmail, onPasswordChange: setPassword, onSubmit: handleTraditionalSubmit, isLoading: totalLoading, buttonText: "Sign In", showForgotPassword: config.enableRecovery, onForgotPassword: onForgotPassword })), !isFederated && !isTraditional && config.settingsStatus === 'loading' && ((0, jsx_runtime_1.jsx)("div", { role: "status", className: "px-4 py-3 rounded-lg text-sm text-left", style: { color: 'var(--text-secondary, #6b7280)' }, children: (0, jsx_runtime_1.jsx)("p", { children: "Loading sign-in options\u2026" }) })), !isFederated && !isTraditional && config.settingsStatus === 'failed' && ((0, jsx_runtime_1.jsxs)("div", { role: "alert", className: "px-4 py-3 rounded-lg text-sm text-left", style: {
41
+ border: '1px solid var(--border-default, #e5e7eb)',
42
+ color: 'var(--text-primary, #111827)',
43
+ }, children: [(0, jsx_runtime_1.jsx)("p", { className: "font-semibold", children: "Could not load sign-in options." }), (0, jsx_runtime_1.jsx)("p", { className: "mt-1", children: "Please reload the page. If it keeps happening, contact an administrator." })] })), !isFederated && !isTraditional && (config.settingsStatus ?? 'ready') === 'ready' && ((0, jsx_runtime_1.jsxs)("div", { role: "alert", className: "px-4 py-3 rounded-lg text-sm text-left", style: {
41
44
  border: '1px solid var(--border-default, #e5e7eb)',
42
45
  color: 'var(--text-primary, #111827)',
43
46
  }, children: [(0, jsx_runtime_1.jsx)("p", { className: "font-semibold", children: "Sign-in is not available for this application." }), (0, jsx_runtime_1.jsx)("p", { className: "mt-1", children: "No sign-in method is enabled for this client. An administrator needs to enable a federated provider or username and password sign-in." })] }))] }));
@@ -56,26 +56,11 @@ function ModeAwareSignupPage({ onFederatedSignUp, onTraditionalSignUp, isLoading
56
56
  border: '1px solid var(--border-default)',
57
57
  color: 'white',
58
58
  background: 'var(--color-primary, #10b981)',
59
- }, children: totalLoading ? 'Creating Account...' : 'Create Account' })] })), isFederated === false && isTraditional === false && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(FederatedAuthSection_1.FederatedAuthSection, { providers: config.providers, onProviderClick: handleFederatedClick, isLoading: totalLoading }), config.providers.length > 0 && config.enableEmailSignup && ((0, jsx_runtime_1.jsxs)("div", { className: "relative my-6", children: [(0, jsx_runtime_1.jsx)("div", { className: "absolute inset-0 flex items-center", children: (0, jsx_runtime_1.jsx)("div", { className: "w-full", style: {
60
- borderTop: '1px solid var(--border-default)',
61
- } }) }), (0, jsx_runtime_1.jsx)("div", { className: "relative flex justify-center text-sm", children: (0, jsx_runtime_1.jsx)("span", { className: "px-2", style: {
62
- background: 'var(--bg-card)',
63
- color: 'var(--text-secondary)',
64
- }, children: "Or sign up with email" }) })] })), config.enableEmailSignup && ((0, jsx_runtime_1.jsxs)("form", { onSubmit: handleTraditionalSubmit, className: "space-y-4", children: [(0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { className: "block text-sm font-medium mb-2", style: { color: 'var(--text-primary)' }, children: "Email Address" }), (0, jsx_runtime_1.jsx)("input", { type: "email", value: email, onChange: (e) => setEmail(e.target.value), placeholder: "you@example.com", disabled: totalLoading, required: true, className: "w-full px-4 py-3 rounded-lg border transition-colors focus:outline-none focus:ring-2 disabled:opacity-50", style: {
65
- border: '1px solid var(--border-default)',
66
- color: 'var(--text-primary)',
67
- background: 'var(--bg-default)',
68
- } })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { className: "block text-sm font-medium mb-2", style: { color: 'var(--text-primary)' }, children: "Password" }), (0, jsx_runtime_1.jsx)("input", { type: "password", value: password, onChange: (e) => setPassword(e.target.value), placeholder: "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022", disabled: totalLoading, required: true, className: "w-full px-4 py-3 rounded-lg border transition-colors focus:outline-none focus:ring-2 disabled:opacity-50", style: {
69
- border: '1px solid var(--border-default)',
70
- color: 'var(--text-primary)',
71
- background: 'var(--bg-default)',
72
- } })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { className: "block text-sm font-medium mb-2", style: { color: 'var(--text-primary)' }, children: "Confirm Password" }), (0, jsx_runtime_1.jsx)("input", { type: "password", value: confirmPassword, onChange: (e) => setConfirmPassword(e.target.value), placeholder: "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022", disabled: totalLoading, required: true, className: "w-full px-4 py-3 rounded-lg border transition-colors focus:outline-none focus:ring-2 disabled:opacity-50", style: {
73
- border: '1px solid var(--border-default)',
74
- color: 'var(--text-primary)',
75
- background: 'var(--bg-default)',
76
- } })] }), (0, jsx_runtime_1.jsx)("button", { type: "submit", disabled: totalLoading, className: "w-full py-3 px-4 rounded-lg font-semibold focus:outline-none focus:ring-2 disabled:opacity-50 disabled:cursor-not-allowed transition-colors mt-6", style: {
77
- border: '1px solid var(--border-default)',
78
- color: 'white',
79
- background: 'var(--color-primary, #10b981)',
80
- }, children: totalLoading ? 'Creating Account...' : 'Create Account' })] }))] }))] }));
59
+ }, children: totalLoading ? 'Creating Account...' : 'Create Account' })] })), !isFederated && !isTraditional && config.settingsStatus === 'loading' && ((0, jsx_runtime_1.jsx)("div", { role: "status", className: "px-4 py-3 rounded-lg text-sm text-left", style: { color: 'var(--text-secondary, #6b7280)' }, children: (0, jsx_runtime_1.jsx)("p", { children: "Loading sign-up options\u2026" }) })), !isFederated && !isTraditional && config.settingsStatus === 'failed' && ((0, jsx_runtime_1.jsxs)("div", { role: "alert", className: "px-4 py-3 rounded-lg text-sm text-left", style: {
60
+ border: '1px solid var(--border-default, #e5e7eb)',
61
+ color: 'var(--text-primary, #111827)',
62
+ }, children: [(0, jsx_runtime_1.jsx)("p", { className: "font-semibold", children: "Could not load sign-up options." }), (0, jsx_runtime_1.jsx)("p", { className: "mt-1", children: "Please reload the page. If it keeps happening, contact an administrator." })] })), !isFederated && !isTraditional && (config.settingsStatus ?? 'ready') === 'ready' && ((0, jsx_runtime_1.jsxs)("div", { role: "alert", className: "px-4 py-3 rounded-lg text-sm text-left", style: {
63
+ border: '1px solid var(--border-default, #e5e7eb)',
64
+ color: 'var(--text-primary, #111827)',
65
+ }, children: [(0, jsx_runtime_1.jsx)("p", { className: "font-semibold", children: "Sign-up is not available for this application." }), (0, jsx_runtime_1.jsx)("p", { className: "mt-1", children: "No sign-up method is enabled for this client. An administrator needs to enable a federated provider or username and password sign-in." })] }))] }));
81
66
  }
@@ -35,9 +35,13 @@ export interface SessionPointer {
35
35
  roles: string[];
36
36
  clientId: string;
37
37
  }
38
+ /** PAY-2339: which check could not answer. A fixed, machine-readable value: consumers never parse message text. */
39
+ export type UnavailableCheck = 'revocation' | 'viability';
38
40
  export interface SessionStatus {
39
41
  exists: boolean | null;
40
42
  forceInvalid: boolean | null;
43
+ /** PAY-2339: the revocation backend could not answer and this app HOLDS (revocationFailMode 'hold'): no sign-out. */
44
+ unavailable?: boolean;
41
45
  requires2FA: boolean;
42
46
  twoFactorComplete: boolean;
43
47
  }
@@ -51,6 +55,7 @@ export type AuthAction = {
51
55
  } | {
52
56
  type: 'service_error';
53
57
  reason: string;
58
+ check?: UnavailableCheck;
54
59
  } | {
55
60
  type: 'refresh_needed';
56
61
  };
@@ -113,29 +118,41 @@ export interface MvpMiddlewareOptions {
113
118
  */
114
119
  sessionResetRoute?: boolean | string;
115
120
  /**
116
- * PAY-1882 step 4 - how this app treats a producer that reports the session's
121
+ * PAY-1882 step 4 / PAY-2339 - how this app treats a producer that reports the session's
117
122
  * revocation status as `'unknown'` (a Vibe read that FAILED: non-OK, timeout,
118
123
  * throw - the status could not be determined).
119
124
  *
120
- * 'closed' (DEFAULT) - an unknown revocation status is NOT viable; the
121
- * session is force-invalidated and the user re-auths.
122
- * This is Jon's PAY-1880 ruling (option A, fail closed),
123
- * 2026-09-27, and BAPert 66316/66320 restates it for
124
- * step 4. Consequence, accepted explicitly: a Vibe
125
- * outage signs this app's users out. 'not_configured'
126
- * also fails closed here - both live consumers ARE
127
- * configured (BAPert 66356), so for them it means a
128
- * misconfiguration, not "no revocation source".
129
- * 'open' - an unknown OR not-configured status stays viable
130
- * (availability over fail-closed). Per-app opt-out for a
131
- * deployment with no revocation source, or the two live
132
- * consumers while Jon decides.
125
+ * 'hold' (DEFAULT since PAY-2339, Jon 2026-10-13, option C) - the member is NOT signed out. The request is
126
+ * answered "temporarily unavailable": a page goes to /service-unavailable?callbackUrl=
127
+ * (the session cookie is NOT touched, so "Try again" returns the member still signed in);
128
+ * an /api request gets a 503 carrying `unavailable: true` and `check: 'revocation'`, a
129
+ * fixed machine-readable value. Nothing protected is served while the answer is unknown.
130
+ * Each held request logs one countable `revocation_unavailable` line.
131
+ * THIS REPLACES THE OLD DEFAULT: the PAY-1880 ruling (Jon, 2026-09-27, option A) made
132
+ * 'closed' the default and accepted that a Vibe outage signs an app's users out. That
133
+ * sentence no longer applies to 'unknown' by default: a Vibe outage no longer signs members
134
+ * out unless the app asks for 'closed'. 'revoked' is unchanged.
135
+ * 'closed' - an unknown revocation status is NOT viable; the session is force-invalidated and the
136
+ * user re-auths (TODAY'S sign-out, now opt-in by name). Consequence, accepted explicitly:
137
+ * a Vibe outage signs this app's users out.
138
+ * 'open' - an unknown status stays viable (availability over fail-closed). Per-app opt-out for a
139
+ * deployment with no revocation source.
133
140
  *
134
- * Neither mode changes `'revoked'` (always force-invalid) or an ABSENT
135
- * `revocationStatus` (an older producer - unchanged behaviour, so no consumer
136
- * is signed out for not implementing the check).
141
+ * `'not_configured'` is UNCHANGED under the default: it is a configuration fault, not an outage, so it still
142
+ * force-invalidates exactly as before (a "try again" page would be false); `noRevocationSource` is the only way
143
+ * to make it viable. Neither mode changes `'revoked'` (always force-invalid, unchanged) or an ABSENT
144
+ * `revocationStatus` (an older producer - unchanged behaviour, so no consumer is signed out for not
145
+ * implementing the check). A consumer that wrote 'closed' or 'open' by name keeps exactly what it asked for.
137
146
  */
138
- revocationFailMode?: 'closed' | 'open';
147
+ revocationFailMode?: 'hold' | 'closed' | 'open';
148
+ /**
149
+ * PAY-2339 (BAPert 74640): where a page request goes while a revocation answer is held. DEFAULT (absent): the
150
+ * middleware answers inline with a minimal 503 HTML page ("Temporarily unavailable", a "Try again" link to the safe
151
+ * callback, no Set-Cookie), so a consumer that never added a page cannot get a 404. Name a same-site path (e.g.
152
+ * '/service-unavailable') to redirect there instead with `?callbackUrl=`; the consumer then mounts
153
+ * `export { default } from '@payez/next-mvp/pages/service-unavailable'` (or its own page) at that route.
154
+ */
155
+ unavailablePage?: string;
139
156
  /**
140
157
  * PAY-2302 cut 1c (BAPert 73585): this app has NO revocation source - for example a development environment
141
158
  * provisioned without a vibe client id / HMAC key - and says so EXPLICITLY. Under it a viability answer of
@@ -173,8 +190,21 @@ export declare function createMvpMiddleware(options?: MvpMiddlewareOptions): (re
173
190
  * 'unknown'/'not_configured'. The booleans are read ONLY when revocationStatus is
174
191
  * absent (idealresume.online's older boolean-only route).
175
192
  */
193
+ export type RevocationOutcome = 'viable' | 'invalid' | 'hold';
194
+ /**
195
+ * PAY-2339: the three-way form of the revocation decision. `decideForceInvalid` below is kept as its boolean view.
196
+ *
197
+ * 'invalid' - sign the member out (cookies cleared, login page)
198
+ * 'hold' - the backend could not answer and this app holds: no sign-out, answer "temporarily unavailable"
199
+ * 'viable' - carry on
200
+ */
201
+ export declare function decideRevocationOutcome(data: {
202
+ revocationStatus?: unknown;
203
+ forceInvalid?: unknown;
204
+ revoked?: unknown;
205
+ }, revocationFailMode: 'hold' | 'closed' | 'open', noRevocationSource?: boolean): RevocationOutcome;
176
206
  export declare function decideForceInvalid(data: {
177
207
  revocationStatus?: unknown;
178
208
  forceInvalid?: unknown;
179
209
  revoked?: unknown;
180
- }, revocationFailMode: 'closed' | 'open', noRevocationSource?: boolean): boolean;
210
+ }, revocationFailMode: 'hold' | 'closed' | 'open', noRevocationSource?: boolean): boolean;
@@ -21,6 +21,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
21
21
  exports.TWO_FACTOR_TIMEOUT_ERROR = void 0;
22
22
  exports.isPrefetchRequest = isPrefetchRequest;
23
23
  exports.createMvpMiddleware = createMvpMiddleware;
24
+ exports.decideRevocationOutcome = decideRevocationOutcome;
24
25
  exports.decideForceInvalid = decideForceInvalid;
25
26
  const server_1 = require("next/server");
26
27
  const route_config_1 = require("../auth/route-config");
@@ -185,13 +186,31 @@ async function refreshFailureResponse(request, location, errorCode, clearCookies
185
186
  * requested path as `callbackUrl` so "Try again" returns the user to it still signed in. The path goes
186
187
  * through safeCallbackPath (URL-parser origin check): an off-origin or auth-surface value is dropped.
187
188
  */
188
- function serviceUnavailableResponse(request) {
189
+ function serviceUnavailableResponse(request, check = 'viability', unavailablePage) {
189
190
  const { pathname, search } = request.nextUrl;
190
191
  if (pathname.startsWith('/api/')) {
191
- return server_1.NextResponse.json({ error: 'ServiceUnavailable' }, { status: 503 });
192
+ // PAY-2339 P1/P12: one fixed machine-readable shape for every "could not check" answer. No protected data.
193
+ return server_1.NextResponse.json({ error: 'ServiceUnavailable', unavailable: true, check }, { status: 503 });
192
194
  }
193
- const target = new URL('/service-unavailable', request.url);
194
195
  const callback = (0, safe_callback_1.safeCallbackPath)(pathname + search);
196
+ // PAY-2339 (BAPert 74640, P13): a HELD revocation answer needs no page in the consumer. Unless the app names its own
197
+ // page (`unavailablePage`), the middleware answers inline: a minimal 503 HTML, a "try again" link to the safe
198
+ // callback, and no Set-Cookie (nothing here touches the session). No redirect, so no 404 is possible.
199
+ // `unavailablePage` is accepted only as a PLAIN path ([A-Za-z0-9_-/]; no query, fragment, %-encoding, dot segment or
200
+ // scheme), so the self-check below compares like with like and a mis-set value falls back to the inline page (Scout 74668).
201
+ // The named page applies to the revocation hold ONLY (BAPert 74648: the viability-failure arm is untouched). If the
202
+ // request is already FOR the named page (an app that did not register it as public), answer inline: redirecting to
203
+ // it again would loop with a longer callbackUrl each time (QAPert 74651).
204
+ const named = check === 'revocation' && unavailablePage && /^\/[A-Za-z0-9_\-\/]*$/.test(unavailablePage) && !unavailablePage.includes('//') ? unavailablePage : null;
205
+ if (check === 'revocation' && (!named || pathname.replace(/\/+$/, '') === named.replace(/\/+$/, ''))) {
206
+ const href = callback.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;');
207
+ return new server_1.NextResponse('<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Temporarily unavailable</title>' +
208
+ '<main role="status" style="font-family:system-ui,sans-serif;max-width:28rem;margin:20vh auto;padding:0 1rem;text-align:center">' +
209
+ '<h1 style="font-size:1.25rem">Temporarily unavailable</h1>' +
210
+ '<p>We could not check your session just now. You are still signed in.</p>' +
211
+ '<p><a href="' + href + '">Try again</a></p></main>', { status: 503, headers: { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store', 'retry-after': '30' } });
212
+ }
213
+ const target = new URL(named || '/service-unavailable', request.url);
195
214
  if (callback !== '/')
196
215
  target.searchParams.set('callbackUrl', callback);
197
216
  return server_1.NextResponse.redirect(target);
@@ -264,6 +283,14 @@ function makeAuthDecision(ctx) {
264
283
  return { type: 'allow' }; // the login page must stay reachable while the check is down
265
284
  return { type: 'service_error', reason: 'viability_check_failed' };
266
285
  }
286
+ // --- PAY-2339: the revocation backend could not answer and this app HOLDS (the default) ---
287
+ // Not a verdict on the session: no cookie is touched, nothing protected is served, and the answer says why in a
288
+ // fixed machine-readable form. The login page stays reachable.
289
+ if (sessionStatus.unavailable) {
290
+ if (onLoginPage)
291
+ return { type: 'allow' };
292
+ return { type: 'service_error', reason: 'revocation_unknown', check: 'revocation' };
293
+ }
267
294
  // --- Session force-invalidated (admin action, password change, etc.) ---
268
295
  if (sessionStatus.forceInvalid) {
269
296
  if (onLoginPage)
@@ -380,8 +407,9 @@ function createMvpMiddleware(options = {}) {
380
407
  const refreshEndpoint = options.refreshEndpoint || '/api/auth/refresh';
381
408
  const bypassPaths = options.bypassPaths || [];
382
409
  const rbacExemptPaths = options.rbacExemptPaths || [];
383
- // PAY-1882 step 4: default 'closed' (Jon's option A / BAPert 66316).
384
- const revocationFailMode = options.revocationFailMode || 'closed';
410
+ // PAY-2339: the default is 'hold' (Jon 2026-10-13, option C): an unknown revocation answer does not sign the member
411
+ // out. PAY-1882 step 4 had 'closed' (Jon's PAY-1880 option A, BAPert 66316); it is now opt-in by name.
412
+ const revocationFailMode = options.revocationFailMode || 'hold';
385
413
  // PAY-2302 cut 1c: strictly `=== true`, so a truthy string in a config file cannot switch revocation off.
386
414
  const noRevocationSource = options.noRevocationSource === true;
387
415
  if (noRevocationSource) {
@@ -499,42 +527,44 @@ function createMvpMiddleware(options = {}) {
499
527
  refreshEndpoint,
500
528
  rbacExemptPaths,
501
529
  reset: resolveReset(),
530
+ unavailablePage: options.unavailablePage,
502
531
  onRefreshSuccess: options.onRefreshSuccess,
503
532
  onRefreshFailure: options.onRefreshFailure,
504
533
  });
505
534
  };
506
535
  }
507
536
  /**
508
- * The revocation decision, as a pure function (PAY-1882 step 4, NightHawk 66392).
509
- *
510
- * Separate + exported so the B1 defect is testable with a REAL mutant: the bug was
511
- * that `data.forceInvalid === true` was read BEFORE revocationFailMode, so a
512
- * producer that folds 'unknown' into forceInvalid made the 'open' lever dead.
537
+ * PAY-2339: the three-way form of the revocation decision. `decideForceInvalid` below is kept as its boolean view.
513
538
  *
514
- * RULE: when the producer supplies `revocationStatus`, it is AUTHORITATIVE - the
515
- * decision is made from it plus failMode, and the folded booleans are ignored for
516
- * 'unknown'/'not_configured'. The booleans are read ONLY when revocationStatus is
517
- * absent (idealresume.online's older boolean-only route).
539
+ * 'invalid' - sign the member out (cookies cleared, login page)
540
+ * 'hold' - the backend could not answer and this app holds: no sign-out, answer "temporarily unavailable"
541
+ * 'viable' - carry on
518
542
  */
519
- function decideForceInvalid(data, revocationFailMode, noRevocationSource = false) {
543
+ function decideRevocationOutcome(data, revocationFailMode, noRevocationSource = false) {
520
544
  const status = data.revocationStatus;
521
545
  if (typeof status === 'string') {
522
546
  if (status === 'revoked')
523
- return true;
547
+ return 'invalid';
524
548
  if (status === 'active')
525
- return false;
549
+ return 'viable';
526
550
  // PAY-2302 cut 1c: an app that declared it has no revocation source. ONLY the exact 'not_configured' string.
527
551
  if (status === 'not_configured' && noRevocationSource === true)
528
- return false;
529
- // S1 (NightHawk 66409): ANY other string - 'unknown', 'not_configured', a
530
- // typo like 'Revoked', 'error', or '' - is treated as 'unknown' (fail mode
531
- // applies), NEVER as 'active'. Returning false here would let the first
532
- // producer that emits an unexpected state silently switch revocation OFF -
533
- // the exact PAY-1880 fail-open class. Only an explicit 'active' is viable.
534
- return revocationFailMode === 'closed';
552
+ return 'viable';
553
+ // PAY-2339: 'not_configured' is a configuration fault, not an outage. Under the default it stays signed out
554
+ // exactly as before; only 'open' relaxes it (a Vibe-less app).
555
+ if (status === 'not_configured')
556
+ return revocationFailMode === 'open' ? 'viable' : 'invalid';
557
+ // S1 (NightHawk 66409): ANY other string - 'unknown', a typo like 'Revoked', 'error', or '' - is treated as
558
+ // 'unknown' (the fail mode applies), NEVER as 'active'. Only an explicit 'active' is viable.
559
+ if (revocationFailMode === 'open')
560
+ return 'viable';
561
+ return revocationFailMode === 'closed' ? 'invalid' : 'hold';
535
562
  }
536
- // No revocationStatus: an older boolean producer. Absence of both stays false.
537
- return data.forceInvalid === true || data.revoked === true;
563
+ // No revocationStatus: an older boolean producer. Absence of both stays viable.
564
+ return data.forceInvalid === true || data.revoked === true ? 'invalid' : 'viable';
565
+ }
566
+ function decideForceInvalid(data, revocationFailMode, noRevocationSource = false) {
567
+ return decideRevocationOutcome(data, revocationFailMode, noRevocationSource) === 'invalid';
538
568
  }
539
569
  /**
540
570
  * PAY-2147: does the request carry a session cookie? Used ONLY when the viability read itself FAILED.
@@ -620,7 +650,13 @@ async function checkViability(request, endpoint, log, revocationFailMode, noRevo
620
650
  // revocations status, decide from it + failMode and ignore the folded
621
651
  // booleans for unknown/not_configured. The booleans are read ONLY when
622
652
  // revocationStatus is ABSENT (idealresume.online's older boolean route).
623
- sessionStatus.forceInvalid = decideForceInvalid(data, revocationFailMode, noRevocationSource);
653
+ const outcome = decideRevocationOutcome(data, revocationFailMode, noRevocationSource);
654
+ sessionStatus.forceInvalid = outcome === 'invalid';
655
+ if (outcome === 'hold') {
656
+ sessionStatus.unavailable = true;
657
+ // PAY-2339 P11: one countable line per held request, naming the check. No session value, no address.
658
+ log.warn('revocation_unavailable', { check: 'revocation' });
659
+ }
624
660
  // PAY-2216: a STALE session - the cookie resolved to a token, but no store holds the session - used to set
625
661
  // pointer.exists=false, so makeAuthDecision took no_session (login, cookie NOT cleared) and its stale_session
626
662
  // arm (clearCookies) was unreachable: a dead cookie lingered and re-ran viability on every request.
@@ -663,7 +699,7 @@ async function executeDecision(request, decision, pathname, sessionPointer, sess
663
699
  ? clearingRedirect(request, decision.location, opts.reset)
664
700
  : redirectTo(request, decision.location);
665
701
  case 'service_error':
666
- return serviceUnavailableResponse(request);
702
+ return serviceUnavailableResponse(request, decision.check ?? 'viability', opts.unavailablePage);
667
703
  case 'refresh_needed':
668
704
  // PAY-2136: a speculative request must not spend (rotate) a single-use refresh token or, on a 401/403,
669
705
  // clear the session. Let it through; the real navigation refreshes.
@@ -0,0 +1,15 @@
1
+ /**
2
+ * PAY-2339 (option C): the default "temporarily unavailable" page the middleware sends a member to when the revocation
3
+ * check (or the viability read) could not answer. Mount it in one line in the consuming app:
4
+ *
5
+ * // app/service-unavailable/page.tsx
6
+ * export { default } from '@payez/next-mvp/pages/service-unavailable';
7
+ *
8
+ * It signs nobody out and clears nothing: "Try again" goes back to `callbackUrl`, and the member is still signed in. A
9
+ * consumer may replace the page (and the wording) with its own at the same route.
10
+ *
11
+ * `callbackUrl` is untrusted input. Only a same-origin PATH is followed ("/dashboard"); anything else (an absolute URL,
12
+ * a protocol-relative "//host", a "javascript:" value, a backslash trick, nothing at all) falls back to "/".
13
+ */
14
+ export declare function safeReturnPath(raw: string | null | undefined): string;
15
+ export default function ServiceUnavailablePage(): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ 'use client';
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.safeReturnPath = safeReturnPath;
5
+ exports.default = ServiceUnavailablePage;
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ const react_1 = require("react");
8
+ const navigation_1 = require("next/navigation");
9
+ /**
10
+ * PAY-2339 (option C): the default "temporarily unavailable" page the middleware sends a member to when the revocation
11
+ * check (or the viability read) could not answer. Mount it in one line in the consuming app:
12
+ *
13
+ * // app/service-unavailable/page.tsx
14
+ * export { default } from '@payez/next-mvp/pages/service-unavailable';
15
+ *
16
+ * It signs nobody out and clears nothing: "Try again" goes back to `callbackUrl`, and the member is still signed in. A
17
+ * consumer may replace the page (and the wording) with its own at the same route.
18
+ *
19
+ * `callbackUrl` is untrusted input. Only a same-origin PATH is followed ("/dashboard"); anything else (an absolute URL,
20
+ * a protocol-relative "//host", a "javascript:" value, a backslash trick, nothing at all) falls back to "/".
21
+ */
22
+ function safeReturnPath(raw) {
23
+ if (!raw || typeof raw !== 'string')
24
+ return '/';
25
+ if (!raw.startsWith('/') || raw.startsWith('//') || raw.includes('\\'))
26
+ return '/';
27
+ if (/[\u0000-\u001f]/.test(raw))
28
+ return '/';
29
+ return raw;
30
+ }
31
+ function ServiceUnavailableContent() {
32
+ const params = (0, navigation_1.useSearchParams)();
33
+ const back = safeReturnPath(params?.get('callbackUrl'));
34
+ return ((0, jsx_runtime_1.jsx)("div", { className: "min-h-screen flex items-center justify-center p-4", children: (0, jsx_runtime_1.jsxs)("div", { role: "status", className: "max-w-md w-full text-center rounded-xl p-8 shadow-lg border", style: {
35
+ backgroundColor: 'var(--bg-card, #ffffff)',
36
+ borderColor: 'var(--border-default, #e5e7eb)',
37
+ }, children: [(0, jsx_runtime_1.jsx)("h1", { className: "text-xl font-semibold mb-2", style: { color: 'var(--text-primary, #111827)' }, children: "Temporarily unavailable" }), (0, jsx_runtime_1.jsx)("p", { className: "mb-6", style: { color: 'var(--text-secondary, #6b7280)' }, children: "We could not check your session just now. You are still signed in. Please try again in a moment." }), (0, jsx_runtime_1.jsx)("a", { href: back, className: "inline-block rounded-lg px-4 py-2 font-medium", style: { background: 'var(--color-primary, #10b981)', color: 'white' }, children: "Try again" })] }) }));
38
+ }
39
+ function ServiceUnavailablePage() {
40
+ return ((0, jsx_runtime_1.jsx)(react_1.Suspense, { fallback: null, children: (0, jsx_runtime_1.jsx)(ServiceUnavailableContent, {}) }));
41
+ }
@@ -28,6 +28,7 @@ export declare function GET(req: NextRequest): Promise<NextResponse<{
28
28
  needsRefresh: boolean;
29
29
  authenticated: boolean;
30
30
  revocationStatus: RevocationStatus;
31
+ unavailable: boolean;
31
32
  error: string;
32
33
  code: string;
33
34
  }> | NextResponse<{
@@ -36,15 +37,6 @@ export declare function GET(req: NextRequest): Promise<NextResponse<{
36
37
  authenticated: boolean;
37
38
  reason: string;
38
39
  }> | NextResponse<{
39
- viable: boolean;
40
- needsRefresh: boolean;
41
- expiresIn: number;
42
- hasRefreshToken: boolean;
43
- authenticated: boolean;
44
- sessionTokenId: string | null;
45
- revocationStatus: RevocationStatus;
46
- forceInvalid: boolean;
47
- revoked: boolean;
48
40
  tenantRequiresTwoFactor: boolean;
49
41
  userHasCompletedTenantTwoFactorRequirements: boolean;
50
42
  userStillNeedsTwoFactor: boolean;
@@ -56,4 +48,22 @@ export declare function GET(req: NextRequest): Promise<NextResponse<{
56
48
  expiresAt: string;
57
49
  roles: string[];
58
50
  clientId: string | null;
51
+ forceInvalid: boolean;
52
+ unavailable: boolean;
53
+ revoked: boolean;
54
+ viable: boolean;
55
+ needsRefresh: boolean;
56
+ expiresIn: number;
57
+ hasRefreshToken: boolean;
58
+ authenticated: boolean;
59
+ sessionTokenId: string | null;
60
+ revocationStatus: RevocationStatus;
61
+ }> | NextResponse<{
62
+ viable: boolean;
63
+ needsRefresh: boolean;
64
+ authenticated: boolean;
65
+ revocationStatus: RevocationStatus;
66
+ error: string;
67
+ code: string;
68
+ correlationId: string;
59
69
  }>>;