Repository navigation
Expand file tree
/
Copy pathauth-manager-email-sms-delivery.ts
More file actions
544 lines (527 loc) · 27 KB
/
Copy pathauth-manager-email-sms-delivery.ts
File metadata and controls
544 lines (527 loc) · 27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* [#25224 cut 5] The email and SMS delivery of `AuthManager` — the notice to
* the address a change-email request moves an account away from, the
* per-number phone OTP send guard and its admission check, the phone OTP and
* SMS invitation sends with their quota wall, the read of a recipient's own
* stored `sys_user.locale`, and the auth SMS body render — moved out of
* `auth-manager.ts` whole, with the four module-level declarations that only
* these bodies read: the SMS service's quota refusal code and its matcher, the
* 429 the quota wall answers with, and the one ADR-0112 code both phone OTP
* send walls carry.
*
* A MOVE, not a redesign. Each function below is the body of the
* `AuthManager` method of the same name as it stood, re-indented one level
* and otherwise unedited, and each module-level declaration is its text as it
* stood in `auth-manager.ts`: the same sends, the same locale ladder, the same
* refusals and codes, failing open and closed exactly where they did.
*
* ## How the moved bodies reach the class
*
* The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts`
* header carries the full reasoning), which `auth-manager-actor-guards.ts`,
* `auth-manager-sign-in-lockout.ts`, `auth-manager-password-session-policy.ts`
* and `auth-manager-database-hooks.ts` follow, applied per method. The class
* keeps all seven methods — the published `dist/*.d.ts` lists each one's
* name, docblock and position — and each body is one literal of exactly the
* {@link EmailSmsDeliveryHost} members that method reads, plus a `.call`. The
* six `async` methods are no longer `async`: each returns the moved function's
* own promise, so a caller awaits the same promise the body settles, with no
* extra wrapper.
*
* A method goes as a closure that calls it on the manager at call time, so a
* test double or spy set on the manager is still the one a body reaches —
* `storedRecipientLocale`, `renderPhoneSmsBody` and `getOtpSendGuard`
* included, which the bodies here ask through the manager rather than calling
* the functions here directly. `config` and `smsLocale` go as GETTERS: the
* class reassigns both after construction (`setRuntimeBaseUrl` and
* `applyConfigPatch`; `setDefaultSmsLocale`), so each is read off the manager
* when the body reads it, as before. `_otpSendGuard` goes as a get/set pair,
* because `getOtpSendGuard` creates the guard once and stores it on the
* manager: the guard, and the per-number budget it counts, stays the one the
* manager holds.
*
* The four module-level declarations move with the bodies rather than being
* imported back, because only these bodies read them. (`index.ts` lists the
* names it publishes from `auth-manager.ts` one by one, so a helper exported
* there in place for a module split out of it stays off the published
* surface.) Nothing re-exports this module, so the published surface does not
* move.
*
* Added here since, in the same shape: the reminder a password change sends
* of what it left active (`sendPasswordChangeReminder`), and the notice a
* completed self-service reset sends of what it ended and what it kept
* (`sendPasswordResetNotice`), both `password-reset-ends-added-access.ts`.
*/
import type { StandardErrorCode } from '@objectstack/spec/api';
import type { AuthManager } from './auth-manager.js';
import { OtpSendGuard } from './otp-send-guard.js';
import {
PHONE_SMS_TOPICS,
interpolatePhoneSms,
resolvePhoneSmsTemplateBody,
} from './phone-sms-texts.js';
// commit 35e94c96b — the stored rung of the ruled locale ladder reuses the messaging
// seam's normalizer rather than growing a second one. `normalizeRecipientLocale`
// is the platform's ONE reader of a value at rest in `sys_user.locale`, and its
// refusal of the stringified-nothing literals (`"undefined"`, `"null"`) is part
// of its contract — hotcrm measured that literal dead-lettering every delivery
// for every user without a preference row. A private copy here would be a
// second place for that refusal to rot.
import {
RECIPIENT_LOCALE_FIELD,
normalizeRecipientLocale,
} from '@objectstack/service-messaging';
/**
* #6039 / #2814 — the error code an SMS service reports a QUOTA refusal with.
*
* `SmsService.send()` cannot throw an HTTP-shaped error: it is a kernel service
* with no idea who is calling. It reports the deployment's daily cost ceiling
* being hit as a normal failed result whose `error` carries the service's
* `CODE: message` envelope — here `TOO_MANY_REQUESTS: daily SMS quota
* exhausted`. Turning that back into an HTTP answer is the auth endpoint's job,
* which is why the mapping lives here (see {@link isSmsQuotaRefusal}).
*
* **Source of truth**: `SMS_QUOTA_EXCEEDED_CODE` in
* `packages/services/service-sms/src/sms-daily-quota.ts` (exported alongside
* `SMS_QUOTA_EXCEEDED_ERROR`).
*
* **Why it is restated instead of imported**: `@objectstack/service-sms` already
* depends on THIS package — its day counter imports `InProcessCounterStore` /
* `incrementFixedWindow` from `@objectstack/plugin-auth` — so importing the
* constant back would close a dependency cycle. This is the same call, in the
* other direction, that `normalizeSmsRecipient` makes in
* `packages/services/service-sms/src/sms-service.ts` ("Same shape rule as
* plugin-auth's `normalizePhoneNumber` … kept local: the two packages must not
* depend on each other").
*
* Only the envelope's CODE token crosses the boundary, never its prose: the
* message half is service-owned and may be reworded without touching this
* file. That token is the service's own spelling, which is better-call's
* status NAME for 429; it is not an ADR-0112 error code (`ErrorCode` refuses
* it as a reason-phrase alias of `RATE_LIMIT_EXCEEDED`). So it is only ever
* MATCHED here, never put in a body: the code the 429 carries to the caller
* is {@link PHONE_OTP_SEND_REFUSAL_CODE}.
*/
const SMS_QUOTA_EXCEEDED_CODE = 'TOO_MANY_REQUESTS';
/**
* #6039 — is this `SendSmsResult.error` the quota wall's refusal?
*
* Matched as a PREFIX of the service's `CODE: message` envelope, never as a
* substring: a transport failure puts the provider's raw text in `error`
* (`SmsService` truncates it and reports it verbatim), and one that happens to
* mention the code mid-sentence is still a transport failure — 500, not 429.
*/
function isSmsQuotaRefusal(error: string | undefined): boolean {
return typeof error === 'string' && error.startsWith(`${SMS_QUOTA_EXCEEDED_CODE}:`);
}
/**
* #6039 — the 429 an SMS quota refusal must reach the caller as.
*
* better-call (better-auth's router) maps ONLY `APIError` to a real HTTP status:
* `isAPIError(err) = err instanceof APIError || err?.name === 'APIError'`
* (better-call@1.4.0 `dist/utils.mjs:55-56`), consumed at `dist/router.mjs:92`,
* where everything else takes the `console.error` + `500 / null body` branch
* (`:93-97`; re-measured 2026-08-23 against the installed copy).
* A plain `Error` therefore buried `TOO_MANY_REQUESTS` in a server log while the
* per-number wall on the same endpoint ({@link AuthManager.assertPhoneOtpSendAllowed})
* answered 429 — one endpoint, two voices.
*
* The message follows the per-number wall's shape and carries NO budget detail:
* no ceiling, no remaining count, no reset clock. #2814's requirement is that
* the two walls be indistinguishable from outside — an attacker must not learn
* which budget they hit, and a legitimate caller needs no more than "not now".
* (The per-number wall names its own retry window because it can compute one;
* this wall states no time it cannot honestly promise.)
*
* The status is spelled out as better-call's name for 429 rather than read off
* {@link SMS_QUOTA_EXCEEDED_CODE}: that constant is the service envelope's
* token, matched on the way in, and the HTTP status is this endpoint's own.
*/
async function smsQuotaExceededApiError(message: string): Promise<Error> {
const { APIError } = await import('better-auth/api');
return new APIError('TOO_MANY_REQUESTS', { message, code: PHONE_OTP_SEND_REFUSAL_CODE });
}
/**
* [#24243, ADR-0112] The ONE code both phone-OTP send walls answer with: the
* deployment's daily SMS quota ({@link smsQuotaExceededApiError}) and the
* per-number cooldown and hourly cap
* ({@link AuthManager.assertPhoneOtpSendAllowed}). One code, not one per wall:
* #2814 requires the two walls to be indistinguishable from outside, so a
* caller learns "not now" and never which budget it hit. The standard
* catalog's member for request pacing; `QUOTA_EXCEEDED` would name the first
* wall alone, and so would tell them apart.
*/
const PHONE_OTP_SEND_REFUSAL_CODE: StandardErrorCode = 'RATE_LIMIT_EXCEEDED';
/**
* The `AuthManager` members the functions below read — every one of them, and
* no other; each function's `this` is the `Pick` of the members IT reads. Each
* is typed AS the class member (an indexed access on the class), so a change
* to a member's signature on the class is a change here too, and naming a
* member the class does not have is a compile error. See the module header for
* why `config` and `smsLocale` are getters and `_otpSendGuard` an accessor.
*/
export interface EmailSmsDeliveryHost {
readonly config: AuthManager['config'];
readonly smsLocale: AuthManager['smsLocale'];
_otpSendGuard: AuthManager['_otpSendGuard'];
emailLocaleArg: AuthManager['emailLocaleArg'];
getAppName: AuthManager['getAppName'];
getCanonicalOrigin: AuthManager['getCanonicalOrigin'];
getConsolePageUrl: AuthManager['getConsolePageUrl'];
getDataEngine: AuthManager['getDataEngine'];
getEmailService: AuthManager['getEmailService'];
getOtpSendGuard: AuthManager['getOtpSendGuard'];
getSmsService: AuthManager['getSmsService'];
isPhoneOtpDeliverable: AuthManager['isPhoneOtpDeliverable'];
renderPhoneSmsBody: AuthManager['renderPhoneSmsBody'];
storedRecipientLocale: AuthManager['storedRecipientLocale'];
}
/** The body of `AuthManager#sendChangeEmailNotice`; the method's docblock there is its contract. */
export async function sendChangeEmailNotice(
this: Pick<EmailSmsDeliveryHost, 'config' | 'emailLocaleArg' | 'getAppName' | 'getEmailService' | 'storedRecipientLocale'>,
from: { email: string; name?: string; id?: string },
newEmail: string,
requestSource?: unknown,
): Promise<void> {
try {
const email = this.getEmailService();
if (!email) return;
// The endpoint lower-cases `newEmail` before it does anything with it
// (`update-user.mjs`: `ctx.body.newEmail.toLowerCase()`), so the raw body
// spelling is not what would become the account identity. Report the
// address that would actually land.
const target = newEmail.trim().toLowerCase();
if (!target) return;
// commit 35e94c96b — the stored rung on top of the #14319 ladder. The recipient is
// the account holder, so their own column outranks the header the
// request happened to carry.
const storedLocale = from.id ? await this.storedRecipientLocale({ id: from.id }) : undefined;
await email.sendTemplate({
template: 'auth.email_change_notice',
to: { address: from.email, ...(from.name ? { name: from.name } : {}) },
...this.emailLocaleArg(requestSource, storedLocale),
data: {
user: { name: from.name || from.email, email: from.email, ...(from.id ? { id: from.id } : {}) },
newEmail: target,
appName: this.getAppName(),
},
relatedObject: 'sys_user',
...(from.id ? { relatedId: from.id } : {}),
// [ADR-0131 D7, Q2 A] A notice to the account holder: identity mail.
identityMail: true,
});
} catch (err) {
// Best-effort by design — see the ⛔ above. Logged so an operator can see
// a notice was lost; never rethrown, so the change-email flow cannot be
// taken down by its own security notification.
this.config.logger?.warn?.('AuthManager: change-email notice to the previous address failed', {
error: (err as Error)?.message,
});
}
}
/** The body of `AuthManager#sendPasswordChangeReminder`; the method's docblock there is its contract. */
export async function sendPasswordChangeReminder(
this: Pick<EmailSmsDeliveryHost, 'config' | 'emailLocaleArg' | 'getAppName' | 'getEmailService' | 'storedRecipientLocale'>,
to: { email: string; name?: string; id?: string },
kept: {
apiKeys: readonly string[];
signInAccounts: readonly string[];
authorizedApps: readonly string[];
ownedApps: readonly string[];
scimCredentials: readonly string[];
},
requestSource?: unknown,
): Promise<void> {
try {
const email = this.getEmailService();
if (!email || !to.email) return;
const storedLocale = to.id ? await this.storedRecipientLocale({ id: to.id }) : undefined;
await email.sendTemplate({
template: 'auth.password_changed_notice',
to: { address: to.email, ...(to.name ? { name: to.name } : {}) },
...this.emailLocaleArg(requestSource, storedLocale),
data: {
user: { name: to.name || to.email, email: to.email, ...(to.id ? { id: to.id } : {}) },
apiKeyCount: kept.apiKeys.length,
apiKeys: list(kept.apiKeys),
signInAccountCount: kept.signInAccounts.length,
signInAccounts: list(kept.signInAccounts),
authorizedAppCount: kept.authorizedApps.length,
authorizedApps: list(kept.authorizedApps),
ownedAppCount: kept.ownedApps.length,
ownedApps: list(kept.ownedApps),
scimCredentialCount: kept.scimCredentials.length,
scimCredentials: list(kept.scimCredentials),
appName: this.getAppName(),
},
relatedObject: 'sys_user',
...(to.id ? { relatedId: to.id } : {}),
// A notice to the account holder: identity mail (ADR-0131 D7, Q2 A).
identityMail: true,
});
} catch (err) {
// Best-effort, as the change-email notice: a reminder that cannot be sent
// never fails the password change it follows.
this.config.logger?.warn?.('AuthManager: the password-change reminder could not be sent', {
error: (err as Error)?.message,
});
}
}
/** One list of a notice, prepared: the names joined with commas, or a dash when there are none. */
function list(items: readonly string[]): string {
return items.length > 0 ? items.join(', ') : '—';
}
/** The body of `AuthManager#sendPasswordResetNotice`; the method's docblock there is its contract. */
export async function sendPasswordResetNotice(
this: Pick<EmailSmsDeliveryHost, 'config' | 'emailLocaleArg' | 'getAppName' | 'getEmailService' | 'storedRecipientLocale'>,
to: { email: string; name?: string; id?: string },
ended: {
apiKeys: number;
signInAccounts: number;
authorizedApps: number;
ownedApps: readonly string[];
scimCredentials: readonly string[];
},
kept: { ssoProviders: readonly string[]; twoFactor: boolean },
requestSource?: unknown,
): Promise<void> {
try {
const email = this.getEmailService();
if (!email || !to.email) return;
const storedLocale = to.id ? await this.storedRecipientLocale({ id: to.id }) : undefined;
await email.sendTemplate({
template: 'auth.password_reset_notice',
to: { address: to.email, ...(to.name ? { name: to.name } : {}) },
...this.emailLocaleArg(requestSource, storedLocale),
data: {
user: { name: to.name || to.email, email: to.email, ...(to.id ? { id: to.id } : {}) },
apiKeyCount: ended.apiKeys,
signInAccountCount: ended.signInAccounts,
authorizedAppCount: ended.authorizedApps,
ownedAppCount: ended.ownedApps.length,
ownedApps: list(ended.ownedApps),
scimCredentialCount: ended.scimCredentials.length,
scimCredentials: list(ended.scimCredentials),
ssoProviderCount: kept.ssoProviders.length,
ssoProviders: list(kept.ssoProviders),
twoFactorCount: kept.twoFactor ? 1 : 0,
appName: this.getAppName(),
},
relatedObject: 'sys_user',
...(to.id ? { relatedId: to.id } : {}),
// A notice to the account holder: identity mail (ADR-0131 D7, Q2 A).
identityMail: true,
});
} catch (err) {
// Best-effort, as the change reminder: a notice that cannot be sent never
// fails the reset it follows, which has already ended what it ends.
this.config.logger?.warn?.('AuthManager: the password-reset notice could not be sent', {
error: (err as Error)?.message,
});
}
}
/** The body of `AuthManager#getOtpSendGuard`; the method's docblock there is its contract. */
export function getOtpSendGuard(this: Pick<EmailSmsDeliveryHost, '_otpSendGuard' | 'config'>): OtpSendGuard {
if (!this._otpSendGuard) {
const otpCfg = this.config.phoneOtp ?? {};
// WHERE the budget is counted decides whether it is a budget at all
// (#4790): counted per process, a declared "5 per hour" is 5×N across N
// nodes. Precedence, most deliberate first:
// 1. a host-supplied `secondaryStorage` — an explicit cross-node KV;
// 2. `sharedCounterStore` — AuthPlugin's lazily-resolved kernel `cache`,
// which also announces the degraded (no cache) case loudly;
// 3. neither → the guard's own bounded per-process store.
const storeOption = this.config.secondaryStorage
? { storage: this.config.secondaryStorage }
: this.config.sharedCounterStore
? { resolveStore: this.config.sharedCounterStore }
: {};
this._otpSendGuard = new OtpSendGuard({
...(otpCfg.cooldownSeconds != null ? { cooldownSeconds: otpCfg.cooldownSeconds } : {}),
...(otpCfg.maxPerHour != null ? { maxPerHour: otpCfg.maxPerHour } : {}),
...storeOption,
});
}
return this._otpSendGuard;
}
/** The body of `AuthManager#assertPhoneOtpSendAllowed`; the method's docblock there is its contract. */
export async function assertPhoneOtpSendAllowed(this: Pick<EmailSmsDeliveryHost, 'getOtpSendGuard' | 'isPhoneOtpDeliverable'>, phone: string): Promise<void> {
if (!phone || !this.isPhoneOtpDeliverable()) return;
const decision = await this.getOtpSendGuard().checkAndRecord(phone);
if (!decision.ok) {
const { APIError } = await import('better-auth/api');
throw new APIError('TOO_MANY_REQUESTS', {
message: `Too many verification codes requested for this phone number. Retry in ${decision.retryAfterSeconds ?? 60}s.`,
// The quota wall's code too (#2814): see PHONE_OTP_SEND_REFUSAL_CODE.
code: PHONE_OTP_SEND_REFUSAL_CODE,
});
}
}
/** The body of `AuthManager#deliverPhoneOtp`; the method's docblock there is its contract. */
export async function deliverPhoneOtp(this: Pick<EmailSmsDeliveryHost, 'config' | 'getAppName' | 'getSmsService' | 'isPhoneOtpDeliverable' | 'renderPhoneSmsBody' | 'storedRecipientLocale'>, phone: string, code: string): Promise<void> {
const sms = this.getSmsService();
if (!sms || !this.isPhoneOtpDeliverable()) {
// Absent service, or a log-only transport in production (the code
// would vanish into a log no user can read) — refuse loudly, as a real
// `APIError` like the quota branch below: better-call maps ONLY an
// `APIError` to a status, and a plain `Error` reached the caller as a
// 500 with a null body, leaving the login page nothing to branch on.
// 400 + `SMS_SERVICE_REQUIRED` mirrors the email sibling
// (`EMAIL_SERVICE_REQUIRED`, also 400): a delivery service this request
// needs is not configured on this deployment. The code is registered
// for this package in the ADR-0112 error-code ledger. The message names
// the capability and the fix, and never the OTP code.
const { APIError } = await import('better-auth/api');
throw new APIError('BAD_REQUEST', {
message:
'Phone verification codes are unavailable: this deployment has no configured SMS ' +
'delivery service. An administrator can configure one under ' +
'Setup → Notifications → SMS Channel; until then, sign in with your phone number and password.',
code: 'SMS_SERVICE_REQUIRED',
});
}
const otpCfg = this.config.phoneOtp ?? {};
const minutes = Math.max(1, Math.round((otpCfg.expiresIn ?? 300) / 60));
// #2815 — localised, tenant-customisable body: a sys_notification_template
// row for (auth.phone_otp, sms, resolved locale) wins; the built-in
// bilingual text is the fallback. Purpose-neutral wording on purpose —
// one provider template covers sign-in and reset, and the SMS reveals
// nothing about what the code unlocks.
//
// commit 35e94c96b — the recipient of an OTP IS the user, so the locale is theirs to
// name: `sys_user.locale` first, the deployment default underneath.
//
// ⚠️ The row is looked up here rather than taken from the callback: the
// card's suggested shape assumed `sendPhoneOtp` already held one, and it
// does not — better-auth's phone-number plugin calls both `sendOTP` and
// `sendPasswordResetOTP` with `{ phoneNumber, code }` and no user
// (measured in the installed 1.7.x `routes.mjs`; `request-password-reset`
// resolves the user for its own gate and does not pass it on). One
// projected read on the unique `phone_number` index is the cost, paid only
// once the send is going to happen, and swallowed on any failure.
//
// ⚠️ Matched EXACTLY, as better-auth matches it for its own user lookup on
// the same route — a caller that posts a differently formatted spelling of
// a stored number resolves no row and lands on the deployment default,
// which is the documented floor rather than a failure.
const storedLocale = await this.storedRecipientLocale({ phone_number: phone });
const body = await this.renderPhoneSmsBody(
PHONE_SMS_TOPICS.otp,
{
code,
appName: this.getAppName(),
minutes,
},
storedLocale,
);
const result = await sms.send({
to: phone,
body,
// Template-only providers (Aliyun) substitute into a registered OTP
// template; `code` is the conventional variable name.
templateParams: { code },
});
if (result.status === 'failed') {
// #6039 — the deployment's daily SMS quota refused this send. Answer it
// the way the per-number wall on this same endpoint answers: a real
// `APIError`, hence 429 TOO_MANY_REQUESTS instead of a 500 with a null
// body. Everything else stays a plain Error — a transport outage IS a
// server-side failure and 500 is the honest answer for it.
if (isSmsQuotaRefusal(result.error)) {
throw await smsQuotaExceededApiError(
'Too many verification codes requested. Please try again later.',
);
}
// `result.error` is transport detail (never the code) — safe to surface.
throw new Error(`Phone OTP could not be sent: ${result.error ?? 'SMS delivery failed'}`);
}
}
/** The body of `AuthManager#sendPhoneInviteSms`; the method's docblock there is its contract. */
export async function sendPhoneInviteSms(this: Pick<EmailSmsDeliveryHost, 'getAppName' | 'getCanonicalOrigin' | 'getConsolePageUrl' | 'getSmsService' | 'renderPhoneSmsBody' | 'storedRecipientLocale'>, phone: string): Promise<void> {
const sms = this.getSmsService();
if (!sms) {
throw new Error('SMS_SERVICE_REQUIRED: no SMS service is configured for this deployment.');
}
// #2815 — localised, tenant-customisable body (see deliverPhoneOtp).
// `loginUrl` points at the actual Console sign-in page; `baseUrl` (bare
// origin) is kept for backward-compatibility with tenant-overridden
// templates that still interpolate `{{baseUrl}}`.
//
// #14641 — the invitee's OWN stored language, on the same two branches the
// invitation EMAIL takes. A number that resolves no row, or a row naming no
// language, keeps the deployment default. Matched exactly on
// `phone_number`, the same predicate {@link deliverPhoneOtp} matches on.
//
// ⚠️ What this yields TODAY, so the next reader does not over-read it: the
// one in-repo caller (the identity import endpoint's `invite` policy)
// CREATES the account before sending, so a ROW is always there — but
// `admin-import-users.ts` never writes `locale` and the column declares no
// default, so the value is empty at send time and this path still resolves
// to the deployment default, exactly as it did before #14641. The rung is
// wired, not yet exercised in-repo: it answers for an out-of-repo caller,
// or for a future import that populates the column. ⛔ Do not read this
// site as a behaviour change users see today — that one is on the
// invitation EMAIL, whose invitee may already hold a locale-bearing row.
const storedLocale = await this.storedRecipientLocale({ phone_number: phone });
const body = await this.renderPhoneSmsBody(
PHONE_SMS_TOPICS.invite,
{
appName: this.getAppName(),
baseUrl: this.getCanonicalOrigin(),
loginUrl: this.getConsolePageUrl('/login'),
},
storedLocale,
);
const result = await sms.send({ to: phone, body, templateParams: { content: body } });
if (result.status === 'failed') {
// #6039 — same quota wall, same outward shape as the OTP path above.
// (The one in-repo caller, admin import-users, catches this per row and
// records INVITE_SMS_FAILED rather than failing the request — so what
// changes there is that the row's message no longer carries the raw
// service envelope. The 429 matters for any caller that surfaces this
// rejection directly, which is what a public AuthManager method must be
// correct for.)
if (isSmsQuotaRefusal(result.error)) {
throw await smsQuotaExceededApiError(
'Too many SMS messages requested. Please try again later.',
);
}
throw new Error(`Invitation SMS failed: ${result.error ?? 'SMS delivery failed'}`);
}
}
/** The body of `AuthManager#storedRecipientLocale`; the method's docblock there is its contract. */
export async function storedRecipientLocale(this: Pick<EmailSmsDeliveryHost, 'getDataEngine'>, where: Record<string, unknown>): Promise<string | undefined> {
const engine = this.getDataEngine();
if (!engine || typeof engine.findOne !== 'function') return undefined;
try {
const row = await engine.findOne('sys_user', {
where,
fields: [RECIPIENT_LOCALE_FIELD],
context: { isSystem: true, positions: [], permissions: [] },
} as never);
return normalizeRecipientLocale((row as Record<string, unknown> | null | undefined)?.[RECIPIENT_LOCALE_FIELD]);
} catch {
// A `sys_user` shape without the column, a datasource outage, an engine
// that rejects the projection — all mean "no stored language", never
// "no mail".
return undefined;
}
}
/** The body of `AuthManager#renderPhoneSmsBody`; the method's docblock there is its contract. */
export async function renderPhoneSmsBody(
this: Pick<EmailSmsDeliveryHost, 'config' | 'getDataEngine' | 'smsLocale'>,
topic: string,
data: Record<string, unknown>,
storedLocale?: string,
): Promise<string> {
const locale = storedLocale ?? this.smsLocale;
let organizationId: string | null = null;
try {
organizationId = (await this.config.getTenancy?.()?.defaultOrgId()) ?? null;
} catch {
organizationId = null;
}
const template = await resolvePhoneSmsTemplateBody(this.getDataEngine(), topic, locale, organizationId);
return interpolatePhoneSms(template, data);
}