feat(auth): an extension can switch the SMS-to-card fallback on or off over config (#4747)

Adds the puter.card-fallback.policy event, asked before
phone_verification_card_fallback.enabled is read. A listener that sets
enabled decides in place of config; with none, config decides as before.
This commit is contained in:
Daniel Salazar authored and GitHub committed 2026-11-21 14:41:15 -07:00
1 parent 9d89fc7a29
commit 78a7400c98
5 files changed
+82

No files matched your search

+6
View File
@@ -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;
@@ -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<void>,
): Promise<void> => {
const ctrl = controller as { clients: { event: unknown } };
const real = ctrl.clients.event;
ctrl.clients.event = {
emitAndWait: async (
key: string,
event: Record<string, unknown>,
) => {
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,
@@ -1184,6 +1184,7 @@ describe('requireAnyVerifiedGate', () => {
): AnyVerifiedDeps => ({
smsConfigured: () => sms,
probeCardVerification: async () => card,
probeEnabledOverride: async () => null,
hasPaidPlan: async () => paid,
});
+2
View File
@@ -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;
/**
+19
View File
@@ -103,6 +103,8 @@ export interface CardFallbackDeps {
* installed.
*/
probeCardVerification: () => Promise<boolean | null>;
/** An extension's answer for `enabled` in place of config; null when none. */
probeEnabledOverride: () => Promise<boolean | null>;
}
/** 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<IConfig, 'phone_verification_card_fallback'>,
deps: CardFallbackDeps,
): Promise<boolean> {
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;