diff --git a/src/backend/clients/event/types.ts b/src/backend/clients/event/types.ts index dce474792..b93e7bf5d 100644 --- a/src/backend/clients/event/types.ts +++ b/src/backend/clients/event/types.ts @@ -545,6 +545,12 @@ export type EventMap = { 'puter.card-verification.status': { enabled: boolean | null; }; + // Asked before `phone_verification_card_fallback.enabled` is read. A + // listener sets `enabled` to switch the SMS-to-card fallback on or off in + // place of config; left null, config decides. + 'puter.card-fallback.policy': { + enabled: boolean | null; + }; 'puter.card-verification.confirm': { user_id: number; user_uid: string; diff --git a/src/backend/controllers/auth/AuthController.test.ts b/src/backend/controllers/auth/AuthController.test.ts index fba6dc6b8..861a405cc 100644 --- a/src/backend/controllers/auth/AuthController.test.ts +++ b/src/backend/controllers/auth/AuthController.test.ts @@ -6129,6 +6129,60 @@ describe('AuthController SMS → card fallback', () => { }); }); + // An extension answering the policy event decides in place of config. + const withFallbackPolicy = async ( + enabled: boolean, + fn: () => Promise, + ): Promise => { + const ctrl = controller as { clients: { event: unknown } }; + const real = ctrl.clients.event; + ctrl.clients.event = { + emitAndWait: async ( + key: string, + event: Record, + ) => { + if (key === 'puter.card-fallback.policy') { + event.enabled = enabled; + } + }, + emit: () => undefined, + }; + try { + await fn(); + } finally { + ctrl.clients.event = real; + } + }; + + it.each([ + { configured: true, policy: false, body: {} }, + { + configured: false, + policy: true, + body: { card_fallback_available: true }, + }, + ])( + 'follows an extension policy of $policy over enabled: $configured', + async ({ configured, policy, body }) => { + const { user, actor } = await makeUserAndActor({ + requires_phone_verification: 1, + }); + await withFallbackConfig({ enabled: configured }, async () => { + await withPrelude(stubPrelude(), async () => { + await withFallbackPolicy(policy, async () => { + await seedAttempts(user.id, 9); + const res = makeRes(); + await controller.handleSendConfirmPhone( + makeReq({ phone: '+14155550123' }, { actor }), + res, + ); + expect(res.body).toEqual(body); + }); + }); + }); + }, + ); + it('clamps after_attempts to the send allowance so it stays reachable', async () => { const { user, actor } = await makeUserAndActor({ requires_phone_verification: 1, diff --git a/src/backend/core/http/middleware/gates.test.ts b/src/backend/core/http/middleware/gates.test.ts index 99a734420..ac8b8b5c1 100644 --- a/src/backend/core/http/middleware/gates.test.ts +++ b/src/backend/core/http/middleware/gates.test.ts @@ -1184,6 +1184,7 @@ describe('requireAnyVerifiedGate', () => { ): AnyVerifiedDeps => ({ smsConfigured: () => sms, probeCardVerification: async () => card, + probeEnabledOverride: async () => null, hasPaidPlan: async () => paid, }); diff --git a/src/backend/types.ts b/src/backend/types.ts index 69e0d7548..db3e65650 100644 --- a/src/backend/types.ts +++ b/src/backend/types.ts @@ -995,6 +995,8 @@ interface IConfigOptional { * extension reports card verification enabled. On a build with no card * gate behind it the fallback stays off, since taking the offer there * could only strand the user. + * + * An extension answering `puter.card-fallback.policy` wins over this. */ enabled?: boolean; /** diff --git a/src/backend/util/cardFallback.ts b/src/backend/util/cardFallback.ts index b5388003d..bd9ea536c 100644 --- a/src/backend/util/cardFallback.ts +++ b/src/backend/util/cardFallback.ts @@ -103,6 +103,8 @@ export interface CardFallbackDeps { * installed. */ probeCardVerification: () => Promise; + /** An extension's answer for `enabled` in place of config; null when none. */ + probeEnabledOverride: () => Promise; } /** The clients that answer {@link CardFallbackDeps}. */ @@ -129,6 +131,15 @@ export function cardFallbackDepsFrom( ); return status.enabled; }, + probeEnabledOverride: async () => { + const policy: { enabled: boolean | null } = { enabled: null }; + await clients.event?.emitAndWait( + 'puter.card-fallback.policy', + policy, + {}, + ); + return policy.enabled; + }, }; } @@ -179,11 +190,19 @@ export async function isCardVerificationEnabled( * both halves actually work, off otherwise. Without that conjunction the offer * could show up on a deployment with no card gate behind it, where taking it * strands the user on a dialog that can only fail. + * + * An extension answering `puter.card-fallback.policy` wins over config. */ export async function isCardFallbackEnabled( config: Pick, deps: CardFallbackDeps, ): Promise { + try { + const override = await deps.probeEnabledOverride(); + if (typeof override === 'boolean') return override; + } catch (e) { + console.warn('[card-verification] fallback policy probe failed:', e); + } const configured = config.phone_verification_card_fallback?.enabled; if (typeof configured === 'boolean') return configured; if (!deps.smsConfigured()) return false;