@bymax-one/nest-notification 1.3.1 → 1.4.1
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 +170 -1
- package/README.md +28 -26
- package/dist/react/index.d.cts +2 -1
- package/dist/react/index.d.ts +2 -1
- package/dist/server/index.cjs +93 -15
- package/dist/server/index.d.cts +92 -18
- package/dist/server/index.d.ts +92 -18
- package/dist/server/index.mjs +93 -15
- package/dist/shared/index.cjs +8 -1
- package/dist/shared/index.d.cts +11 -3
- package/dist/shared/index.d.ts +11 -3
- package/dist/shared/index.mjs +8 -1
- package/dist/testing/index.cjs +49 -2
- package/dist/testing/index.d.cts +2 -1
- package/dist/testing/index.d.ts +2 -1
- package/dist/testing/index.mjs +49 -2
- package/package.json +9 -9
package/dist/server/index.d.cts
CHANGED
|
@@ -483,6 +483,10 @@ interface GlobalOptions {
|
|
|
483
483
|
* Resolves the tenant id from the request. When provided, the module prefers
|
|
484
484
|
* this source over a tenant id passed in the body/argument — preventing tenant
|
|
485
485
|
* spoofing.
|
|
486
|
+
*
|
|
487
|
+
* What it returns must identify a tenant: a value that is not a string, is empty or
|
|
488
|
+
* is whitespace-only is refused by the audit interceptor rather than recorded (see
|
|
489
|
+
* `audit.swallowErrors` for what the caller then sees).
|
|
486
490
|
*/
|
|
487
491
|
tenantIdResolver?: (req: NotificationRequest) => string | Promise<string>;
|
|
488
492
|
}
|
|
@@ -1074,6 +1078,16 @@ declare class OtpService {
|
|
|
1074
1078
|
private buildOtpEmail;
|
|
1075
1079
|
/** Finalizes a correct verification: consume-and-clear, or mark `validated`. */
|
|
1076
1080
|
private finalizeVerifySuccess;
|
|
1081
|
+
/**
|
|
1082
|
+
* Refuses a tenant id or recipient that identifies nobody, before any storage call.
|
|
1083
|
+
*
|
|
1084
|
+
* The reference storage already refuses these through `hashTenantRecipient`, but a
|
|
1085
|
+
* consumer-supplied {@link IOtpStorage} is not required to, and a blank scope would
|
|
1086
|
+
* then be shared by every caller that sent one.
|
|
1087
|
+
*
|
|
1088
|
+
* @throws NotificationException `INVALID_SCOPE_IDENTIFIER` when either is blank.
|
|
1089
|
+
*/
|
|
1090
|
+
private assertScope;
|
|
1077
1091
|
/** Returns the resolved OTP options or throws `OTP_STORAGE_NOT_CONFIGURED`. */
|
|
1078
1092
|
private requireOtpOptions;
|
|
1079
1093
|
/** Builds an OTP audit entry with the recipient masked and never the code. */
|
|
@@ -1204,6 +1218,10 @@ declare class NotificationService {
|
|
|
1204
1218
|
* tenant id is taken from `global.tenantIdResolver(request)` when configured,
|
|
1205
1219
|
* which OVERRIDES any `tenantId` carried in the dispatch payload — closing the
|
|
1206
1220
|
* tenant-spoofing vector where a caller forges another tenant's id in the body.
|
|
1221
|
+
* A tenant id that is not a string, is empty or is whitespace-only is refused on every
|
|
1222
|
+
* path that produces it (the resolver, the payload with no resolver, the payload when
|
|
1223
|
+
* there is no HTTP request) instead of being recorded; a dispatch-shaped argument whose
|
|
1224
|
+
* payload `tenantId` is not a string is not recognised as a dispatch and is skipped.
|
|
1207
1225
|
*
|
|
1208
1226
|
* The entry NEVER carries the dispatched payload (an OTP `verify` payload holds a
|
|
1209
1227
|
* guessed code) — only the channel, the masked recipient, the purpose/template,
|
|
@@ -1253,8 +1271,21 @@ declare class NotificationAuditInterceptor implements NestInterceptor {
|
|
|
1253
1271
|
/** Narrows an unknown argument to a {@link DispatchInput} by structural shape. */
|
|
1254
1272
|
private isDispatchInput;
|
|
1255
1273
|
/**
|
|
1256
|
-
* Resolves the trusted tenant id
|
|
1257
|
-
*
|
|
1274
|
+
* Resolves the trusted tenant id from `tenantIdResolver(request)`.
|
|
1275
|
+
*
|
|
1276
|
+
* Falls back to the payload-supplied value only when no resolver is configured or
|
|
1277
|
+
* no request is available — the payload reaches the handler from the caller, so
|
|
1278
|
+
* that value is asserted rather than authenticated, and an entry built from it
|
|
1279
|
+
* attributes the event to whichever tenant the caller named. Configure a resolver
|
|
1280
|
+
* whenever this interceptor runs on an HTTP boundary.
|
|
1281
|
+
*
|
|
1282
|
+
* A value that identifies nobody is refused rather than recorded, on BOTH paths:
|
|
1283
|
+
* the resolver's answer and the payload fallback. The fallback is the default path
|
|
1284
|
+
* — most consumers configure no resolver — so guarding only the resolver would
|
|
1285
|
+
* leave the common case unchecked, which is where a blank tenant actually arrives.
|
|
1286
|
+
*
|
|
1287
|
+
* @throws Error When either the resolved or the fallback tenant id is not a
|
|
1288
|
+
* string, or is blank.
|
|
1258
1289
|
*/
|
|
1259
1290
|
private resolveTenantId;
|
|
1260
1291
|
/** Reads the HTTP request from the context, returning `null` outside an HTTP context. */
|
|
@@ -1974,7 +2005,7 @@ interface InMemoryStorageSize {
|
|
|
1974
2005
|
/** Process-local `IOtpStorage` for dev/test. */
|
|
1975
2006
|
declare class InMemoryOtpStorage implements IOtpStorage {
|
|
1976
2007
|
readonly name = "memory";
|
|
1977
|
-
/** OTP entries keyed by `${tenantId
|
|
2008
|
+
/** OTP entries keyed by `${hashTenantRecipient(tenantId, recipient)}::${purpose}`. */
|
|
1978
2009
|
private readonly store;
|
|
1979
2010
|
/** Cooldown expiries (epoch ms) keyed by the same composite key. */
|
|
1980
2011
|
private readonly cooldowns;
|
|
@@ -2066,7 +2097,15 @@ declare class InMemoryOtpStorage implements IOtpStorage {
|
|
|
2066
2097
|
* @returns The number of stored OTP entries and active cooldowns.
|
|
2067
2098
|
*/
|
|
2068
2099
|
size(): InMemoryStorageSize;
|
|
2069
|
-
/**
|
|
2100
|
+
/**
|
|
2101
|
+
* Collapses the `(tenantId, recipient, purpose)` tuple into one map key.
|
|
2102
|
+
*
|
|
2103
|
+
* The tenant and recipient go through {@link hashTenantRecipient}, which encodes
|
|
2104
|
+
* the pair unambiguously and rejects an empty component. Joining the raw values
|
|
2105
|
+
* around a delimiter would not: `('a::b', 'c', p)` and `('a', 'b::c', p)` produce
|
|
2106
|
+
* the same string. The digest is fixed-length, so appending the purpose after it
|
|
2107
|
+
* stays unambiguous too.
|
|
2108
|
+
*/
|
|
2070
2109
|
private key;
|
|
2071
2110
|
}
|
|
2072
2111
|
|
|
@@ -2081,9 +2120,11 @@ declare class InMemoryOtpStorage implements IOtpStorage {
|
|
|
2081
2120
|
* read → expiry/max check → increment → write sequence inside ONE Lua script
|
|
2082
2121
|
* (`EVAL`), so concurrent verifications cannot interleave and overshoot
|
|
2083
2122
|
* `maxAttempts`.
|
|
2084
|
-
* 2. **PII-free keys** — every key carries `sha256(tenantId:recipient)`,
|
|
2085
|
-
* plaintext email/phone, so an operator with `KEYS` access cannot
|
|
2086
|
-
* recipients
|
|
2123
|
+
* 2. **PII-free keys** — every key carries `sha256(sha256(tenantId):sha256(recipient))`,
|
|
2124
|
+
* never the plaintext email/phone, so an operator with `KEYS` access cannot
|
|
2125
|
+
* enumerate recipients. Each component is hashed before the join so the encoding
|
|
2126
|
+
* itself admits no ambiguity: joining the raw values would map `('a:b', 'c')` and
|
|
2127
|
+
* `('a', 'b:c')` onto one key, which no digest strength repairs.
|
|
2087
2128
|
*
|
|
2088
2129
|
* `ioredis` is an OPTIONAL peer dependency: this class forward-declares the slim
|
|
2089
2130
|
* {@link RedisLike} surface it needs (including `eval`/`pttl`) instead of importing
|
|
@@ -2206,9 +2247,9 @@ declare class RedisOtpStorage implements IOtpStorage {
|
|
|
2206
2247
|
* @param purpose - OTP purpose.
|
|
2207
2248
|
*/
|
|
2208
2249
|
clearCooldown(tenantId: string, recipient: string, purpose: string): Promise<void>;
|
|
2209
|
-
/** Builds the OTP entry key: `{namespace}:otp:{purpose}:{
|
|
2250
|
+
/** Builds the OTP entry key: `{namespace}:otp:{purpose}:{hashTenantRecipient(...)}`. */
|
|
2210
2251
|
private otpKey;
|
|
2211
|
-
/** Builds the cooldown key: `{namespace}:otp_cd:{purpose}:{
|
|
2252
|
+
/** Builds the cooldown key: `{namespace}:otp_cd:{purpose}:{hashTenantRecipient(...)}`. */
|
|
2212
2253
|
private cooldownKey;
|
|
2213
2254
|
}
|
|
2214
2255
|
|
|
@@ -2216,18 +2257,38 @@ declare class RedisOtpStorage implements IOtpStorage {
|
|
|
2216
2257
|
* @fileoverview SHA-256 hashing of `(tenantId, recipient)` for storage keys.
|
|
2217
2258
|
* @layer infrastructure
|
|
2218
2259
|
*
|
|
2219
|
-
* Storage keys are `sha256(
|
|
2260
|
+
* Storage keys are `sha256(sha256(tenantId):sha256(recipient))` for two reasons:
|
|
2220
2261
|
* 1. Privacy — an operator with `KEYS`-level access to the store cannot enumerate
|
|
2221
2262
|
* the email/phone of every recipient with a pending OTP.
|
|
2222
|
-
* 2. Multi-tenancy —
|
|
2223
|
-
*
|
|
2263
|
+
* 2. Multi-tenancy — each component is hashed to a fixed length BEFORE the two are
|
|
2264
|
+
* joined, so no two distinct pairs can produce the same input string.
|
|
2265
|
+
*
|
|
2266
|
+
* The second point is about the encoding, not about the digest, and no property of
|
|
2267
|
+
* SHA-256 substitutes for it. Joining the raw values around a delimiter makes
|
|
2268
|
+
* `('acme:bob', 'x')` and `('acme', 'bob:x')` produce the same input string — and
|
|
2269
|
+
* therefore the same key — while preimage and collision resistance hold perfectly.
|
|
2270
|
+
*
|
|
2271
|
+
* What that buys is precise, and overclaiming it would repeat the mistake it fixes:
|
|
2272
|
+
* distinct pairs are no longer *constructibly* mapped onto one key, because the
|
|
2273
|
+
* ambiguity was in the encoding and the encoding is now unique. The key itself stays
|
|
2274
|
+
* collision-RESISTANT rather than collision-free — an unbounded domain cannot map
|
|
2275
|
+
* injectively into 256 bits, and no construction changes that.
|
|
2276
|
+
*
|
|
2277
|
+
* A component that names no tenant and no recipient is refused rather than hashed:
|
|
2278
|
+
* empty, whitespace-only, or not a string at all. `('', r)` would put every caller
|
|
2279
|
+
* that omits a tenant into one shared namespace — the same isolation failure arrived
|
|
2280
|
+
* at from the other side.
|
|
2224
2281
|
*/
|
|
2225
2282
|
/**
|
|
2226
2283
|
* Derives a deterministic, PII-free storage key fragment from a tenant and recipient.
|
|
2227
2284
|
*
|
|
2228
|
-
* @param tenantId - Tenant isolation scope.
|
|
2229
|
-
* @param recipient - Pre-normalized recipient identifier.
|
|
2230
|
-
* @returns A 64-character lowercase hex SHA-256 digest. Order-sensitive
|
|
2285
|
+
* @param tenantId - Tenant isolation scope. Must be a non-blank string.
|
|
2286
|
+
* @param recipient - Pre-normalized recipient identifier. Must be a non-blank string.
|
|
2287
|
+
* @returns A 64-character lowercase hex SHA-256 digest. Order-sensitive, and unique
|
|
2288
|
+
* per pair up to SHA-256's collision resistance — the encoding contributes no
|
|
2289
|
+
* ambiguity of its own, so two distinct pairs never map onto one key by
|
|
2290
|
+
* construction.
|
|
2291
|
+
* @throws Error When either component is not a string, or is empty or whitespace-only.
|
|
2231
2292
|
*/
|
|
2232
2293
|
declare function hashTenantRecipient(tenantId: string, recipient: string): string;
|
|
2233
2294
|
|
|
@@ -2336,8 +2397,8 @@ declare function formatCooldown(remainingSeconds: number): string;
|
|
|
2336
2397
|
* Stable error-code identifiers, keyed by their symbolic name.
|
|
2337
2398
|
*
|
|
2338
2399
|
* Values are namespaced under `notification.*` and never change once published —
|
|
2339
|
-
* consumers branch on them to localize messages. The
|
|
2340
|
-
* template, OTP, SMS, push, audit,
|
|
2400
|
+
* consumers branch on them to localize messages. The 23 entries cover the email,
|
|
2401
|
+
* template, OTP, SMS, push, audit, channel-config and scoping failure surfaces.
|
|
2341
2402
|
*/
|
|
2342
2403
|
declare const NOTIFICATION_ERROR_CODES: {
|
|
2343
2404
|
readonly EMAIL_PROVIDER_NOT_CONFIGURED: "notification.email_provider_not_configured";
|
|
@@ -2363,6 +2424,13 @@ declare const NOTIFICATION_ERROR_CODES: {
|
|
|
2363
2424
|
readonly PUSH_SEND_FAILED: "notification.push_send_failed";
|
|
2364
2425
|
readonly AUDIT_LOG_FAILED: "notification.audit_log_failed";
|
|
2365
2426
|
readonly CHANNEL_DISABLED: "notification.channel_disabled";
|
|
2427
|
+
/**
|
|
2428
|
+
* A `tenantId` or `recipient` that identifies nobody reached a scoping
|
|
2429
|
+
* boundary — empty, whitespace-only, or not a string at all. Distinct from
|
|
2430
|
+
* {@link EMAIL_INVALID_RECIPIENT}, which is about a malformed address rather
|
|
2431
|
+
* than a value that cannot scope anything.
|
|
2432
|
+
*/
|
|
2433
|
+
readonly INVALID_SCOPE_IDENTIFIER: "notification.invalid_scope_identifier";
|
|
2366
2434
|
};
|
|
2367
2435
|
/**
|
|
2368
2436
|
* The literal-union type of every value in {@link NOTIFICATION_ERROR_CODES}.
|
|
@@ -2500,6 +2568,11 @@ declare const NOTIFICATION_ERROR_DEFINITIONS: {
|
|
|
2500
2568
|
readonly status: HttpStatus.NOT_IMPLEMENTED;
|
|
2501
2569
|
readonly message: "Channel not enabled in module config";
|
|
2502
2570
|
};
|
|
2571
|
+
readonly INVALID_SCOPE_IDENTIFIER: {
|
|
2572
|
+
readonly code: "notification.invalid_scope_identifier";
|
|
2573
|
+
readonly status: HttpStatus.BAD_REQUEST;
|
|
2574
|
+
readonly message: "Tenant or recipient identifier does not identify anybody";
|
|
2575
|
+
};
|
|
2503
2576
|
};
|
|
2504
2577
|
/** The symbolic key of any entry in {@link NOTIFICATION_ERROR_DEFINITIONS}. */
|
|
2505
2578
|
type NotificationErrorKey = keyof typeof NOTIFICATION_ERROR_DEFINITIONS;
|
|
@@ -2632,4 +2705,5 @@ declare const DEFAULT_TTLS: {
|
|
|
2632
2705
|
readonly OTP_GENERIC_SECONDS: 600;
|
|
2633
2706
|
};
|
|
2634
2707
|
|
|
2635
|
-
export {
|
|
2708
|
+
export { BYMAX_NOTIFICATION_EMAIL_PROVIDER, BYMAX_NOTIFICATION_LOG_REPOSITORY, BYMAX_NOTIFICATION_OPTIONS, BYMAX_NOTIFICATION_OTP_STORAGE, BYMAX_NOTIFICATION_PUSH_PROVIDER, BYMAX_NOTIFICATION_SMS_PROVIDER, BYMAX_NOTIFICATION_TEMPLATE_RENDERER, BymaxNotificationModule, CANONICAL_EMAIL_TEMPLATES, DEFAULT_TTLS, DefaultTemplateRenderer, EmailService, InMemoryOtpStorage, NOTIFICATION_ERROR_CODES, NOTIFICATION_ERROR_DEFINITIONS, NOTIFICATION_PURPOSES, NoOpEmailProvider, NoOpNotificationLogRepository, NotificationAuditInterceptor, NotificationException, NotificationService, OtpService, RedisOtpStorage, ResendEmailProvider, SmtpEmailProvider, cooldownExpiresAt, formatCooldown, generateOtpCode, hashTenantRecipient, safeCompare, toRetryAfterHeader };
|
|
2709
|
+
export type { AuditOptions, BymaxNotificationModuleAsyncOptions, BymaxNotificationModuleOptions, BymaxNotificationModuleOptionsFactory, CanonicalEmailTemplate, CanonicalNotificationPurpose, ConsumeAttemptResult, DefaultTemplateRendererOptions, DispatchInput, DispatchResult, EmailAttachment, EmailChannelOptions, EmailDispatchPayload, EmailSendInput, EmailSendOptions, EmailSendResult, EmailSendTemplateInput, GlobalOptions, IEmailProvider, IEmailTemplateRenderer, INotificationLogRepository, IOtpStorage, IPushProvider, ISmsProvider, InMemoryStorageSize, MissingVariableMode, NotificationChannel, NotificationErrorCode, NotificationErrorDefinition, NotificationErrorKey, NotificationErrorResponse, NotificationExceptionOptions, NotificationLogEntry, NotificationLogVerb, NotificationRequest, OtpChannelOptions, OtpConsumeInput, OtpDispatchPayload, OtpEntry, OtpGenerateInput, OtpGenerateResult, OtpPurpose, OtpPurposeConfig, OtpStatusInput, OtpStatusResult, OtpVerifyInput, OtpVerifyResult, PushChannelOptions, PushSendOptions, PushSendResult, RedisLike, RedisOtpStorageOptions, RenderedEmail, ResendEmailProviderOptions, ResolvedAuditOptions, ResolvedEmailOptions, ResolvedGlobalOptions, ResolvedNotificationOptions, ResolvedOtpOptions, SmsChannelOptions, SmsSendOptions, SmsSendResult, SmtpCredentials, SmtpEmailProviderOptions, SmtpTlsOptions, TemplateDefinition };
|
package/dist/server/index.d.ts
CHANGED
|
@@ -483,6 +483,10 @@ interface GlobalOptions {
|
|
|
483
483
|
* Resolves the tenant id from the request. When provided, the module prefers
|
|
484
484
|
* this source over a tenant id passed in the body/argument — preventing tenant
|
|
485
485
|
* spoofing.
|
|
486
|
+
*
|
|
487
|
+
* What it returns must identify a tenant: a value that is not a string, is empty or
|
|
488
|
+
* is whitespace-only is refused by the audit interceptor rather than recorded (see
|
|
489
|
+
* `audit.swallowErrors` for what the caller then sees).
|
|
486
490
|
*/
|
|
487
491
|
tenantIdResolver?: (req: NotificationRequest) => string | Promise<string>;
|
|
488
492
|
}
|
|
@@ -1074,6 +1078,16 @@ declare class OtpService {
|
|
|
1074
1078
|
private buildOtpEmail;
|
|
1075
1079
|
/** Finalizes a correct verification: consume-and-clear, or mark `validated`. */
|
|
1076
1080
|
private finalizeVerifySuccess;
|
|
1081
|
+
/**
|
|
1082
|
+
* Refuses a tenant id or recipient that identifies nobody, before any storage call.
|
|
1083
|
+
*
|
|
1084
|
+
* The reference storage already refuses these through `hashTenantRecipient`, but a
|
|
1085
|
+
* consumer-supplied {@link IOtpStorage} is not required to, and a blank scope would
|
|
1086
|
+
* then be shared by every caller that sent one.
|
|
1087
|
+
*
|
|
1088
|
+
* @throws NotificationException `INVALID_SCOPE_IDENTIFIER` when either is blank.
|
|
1089
|
+
*/
|
|
1090
|
+
private assertScope;
|
|
1077
1091
|
/** Returns the resolved OTP options or throws `OTP_STORAGE_NOT_CONFIGURED`. */
|
|
1078
1092
|
private requireOtpOptions;
|
|
1079
1093
|
/** Builds an OTP audit entry with the recipient masked and never the code. */
|
|
@@ -1204,6 +1218,10 @@ declare class NotificationService {
|
|
|
1204
1218
|
* tenant id is taken from `global.tenantIdResolver(request)` when configured,
|
|
1205
1219
|
* which OVERRIDES any `tenantId` carried in the dispatch payload — closing the
|
|
1206
1220
|
* tenant-spoofing vector where a caller forges another tenant's id in the body.
|
|
1221
|
+
* A tenant id that is not a string, is empty or is whitespace-only is refused on every
|
|
1222
|
+
* path that produces it (the resolver, the payload with no resolver, the payload when
|
|
1223
|
+
* there is no HTTP request) instead of being recorded; a dispatch-shaped argument whose
|
|
1224
|
+
* payload `tenantId` is not a string is not recognised as a dispatch and is skipped.
|
|
1207
1225
|
*
|
|
1208
1226
|
* The entry NEVER carries the dispatched payload (an OTP `verify` payload holds a
|
|
1209
1227
|
* guessed code) — only the channel, the masked recipient, the purpose/template,
|
|
@@ -1253,8 +1271,21 @@ declare class NotificationAuditInterceptor implements NestInterceptor {
|
|
|
1253
1271
|
/** Narrows an unknown argument to a {@link DispatchInput} by structural shape. */
|
|
1254
1272
|
private isDispatchInput;
|
|
1255
1273
|
/**
|
|
1256
|
-
* Resolves the trusted tenant id
|
|
1257
|
-
*
|
|
1274
|
+
* Resolves the trusted tenant id from `tenantIdResolver(request)`.
|
|
1275
|
+
*
|
|
1276
|
+
* Falls back to the payload-supplied value only when no resolver is configured or
|
|
1277
|
+
* no request is available — the payload reaches the handler from the caller, so
|
|
1278
|
+
* that value is asserted rather than authenticated, and an entry built from it
|
|
1279
|
+
* attributes the event to whichever tenant the caller named. Configure a resolver
|
|
1280
|
+
* whenever this interceptor runs on an HTTP boundary.
|
|
1281
|
+
*
|
|
1282
|
+
* A value that identifies nobody is refused rather than recorded, on BOTH paths:
|
|
1283
|
+
* the resolver's answer and the payload fallback. The fallback is the default path
|
|
1284
|
+
* — most consumers configure no resolver — so guarding only the resolver would
|
|
1285
|
+
* leave the common case unchecked, which is where a blank tenant actually arrives.
|
|
1286
|
+
*
|
|
1287
|
+
* @throws Error When either the resolved or the fallback tenant id is not a
|
|
1288
|
+
* string, or is blank.
|
|
1258
1289
|
*/
|
|
1259
1290
|
private resolveTenantId;
|
|
1260
1291
|
/** Reads the HTTP request from the context, returning `null` outside an HTTP context. */
|
|
@@ -1974,7 +2005,7 @@ interface InMemoryStorageSize {
|
|
|
1974
2005
|
/** Process-local `IOtpStorage` for dev/test. */
|
|
1975
2006
|
declare class InMemoryOtpStorage implements IOtpStorage {
|
|
1976
2007
|
readonly name = "memory";
|
|
1977
|
-
/** OTP entries keyed by `${tenantId
|
|
2008
|
+
/** OTP entries keyed by `${hashTenantRecipient(tenantId, recipient)}::${purpose}`. */
|
|
1978
2009
|
private readonly store;
|
|
1979
2010
|
/** Cooldown expiries (epoch ms) keyed by the same composite key. */
|
|
1980
2011
|
private readonly cooldowns;
|
|
@@ -2066,7 +2097,15 @@ declare class InMemoryOtpStorage implements IOtpStorage {
|
|
|
2066
2097
|
* @returns The number of stored OTP entries and active cooldowns.
|
|
2067
2098
|
*/
|
|
2068
2099
|
size(): InMemoryStorageSize;
|
|
2069
|
-
/**
|
|
2100
|
+
/**
|
|
2101
|
+
* Collapses the `(tenantId, recipient, purpose)` tuple into one map key.
|
|
2102
|
+
*
|
|
2103
|
+
* The tenant and recipient go through {@link hashTenantRecipient}, which encodes
|
|
2104
|
+
* the pair unambiguously and rejects an empty component. Joining the raw values
|
|
2105
|
+
* around a delimiter would not: `('a::b', 'c', p)` and `('a', 'b::c', p)` produce
|
|
2106
|
+
* the same string. The digest is fixed-length, so appending the purpose after it
|
|
2107
|
+
* stays unambiguous too.
|
|
2108
|
+
*/
|
|
2070
2109
|
private key;
|
|
2071
2110
|
}
|
|
2072
2111
|
|
|
@@ -2081,9 +2120,11 @@ declare class InMemoryOtpStorage implements IOtpStorage {
|
|
|
2081
2120
|
* read → expiry/max check → increment → write sequence inside ONE Lua script
|
|
2082
2121
|
* (`EVAL`), so concurrent verifications cannot interleave and overshoot
|
|
2083
2122
|
* `maxAttempts`.
|
|
2084
|
-
* 2. **PII-free keys** — every key carries `sha256(tenantId:recipient)`,
|
|
2085
|
-
* plaintext email/phone, so an operator with `KEYS` access cannot
|
|
2086
|
-
* recipients
|
|
2123
|
+
* 2. **PII-free keys** — every key carries `sha256(sha256(tenantId):sha256(recipient))`,
|
|
2124
|
+
* never the plaintext email/phone, so an operator with `KEYS` access cannot
|
|
2125
|
+
* enumerate recipients. Each component is hashed before the join so the encoding
|
|
2126
|
+
* itself admits no ambiguity: joining the raw values would map `('a:b', 'c')` and
|
|
2127
|
+
* `('a', 'b:c')` onto one key, which no digest strength repairs.
|
|
2087
2128
|
*
|
|
2088
2129
|
* `ioredis` is an OPTIONAL peer dependency: this class forward-declares the slim
|
|
2089
2130
|
* {@link RedisLike} surface it needs (including `eval`/`pttl`) instead of importing
|
|
@@ -2206,9 +2247,9 @@ declare class RedisOtpStorage implements IOtpStorage {
|
|
|
2206
2247
|
* @param purpose - OTP purpose.
|
|
2207
2248
|
*/
|
|
2208
2249
|
clearCooldown(tenantId: string, recipient: string, purpose: string): Promise<void>;
|
|
2209
|
-
/** Builds the OTP entry key: `{namespace}:otp:{purpose}:{
|
|
2250
|
+
/** Builds the OTP entry key: `{namespace}:otp:{purpose}:{hashTenantRecipient(...)}`. */
|
|
2210
2251
|
private otpKey;
|
|
2211
|
-
/** Builds the cooldown key: `{namespace}:otp_cd:{purpose}:{
|
|
2252
|
+
/** Builds the cooldown key: `{namespace}:otp_cd:{purpose}:{hashTenantRecipient(...)}`. */
|
|
2212
2253
|
private cooldownKey;
|
|
2213
2254
|
}
|
|
2214
2255
|
|
|
@@ -2216,18 +2257,38 @@ declare class RedisOtpStorage implements IOtpStorage {
|
|
|
2216
2257
|
* @fileoverview SHA-256 hashing of `(tenantId, recipient)` for storage keys.
|
|
2217
2258
|
* @layer infrastructure
|
|
2218
2259
|
*
|
|
2219
|
-
* Storage keys are `sha256(
|
|
2260
|
+
* Storage keys are `sha256(sha256(tenantId):sha256(recipient))` for two reasons:
|
|
2220
2261
|
* 1. Privacy — an operator with `KEYS`-level access to the store cannot enumerate
|
|
2221
2262
|
* the email/phone of every recipient with a pending OTP.
|
|
2222
|
-
* 2. Multi-tenancy —
|
|
2223
|
-
*
|
|
2263
|
+
* 2. Multi-tenancy — each component is hashed to a fixed length BEFORE the two are
|
|
2264
|
+
* joined, so no two distinct pairs can produce the same input string.
|
|
2265
|
+
*
|
|
2266
|
+
* The second point is about the encoding, not about the digest, and no property of
|
|
2267
|
+
* SHA-256 substitutes for it. Joining the raw values around a delimiter makes
|
|
2268
|
+
* `('acme:bob', 'x')` and `('acme', 'bob:x')` produce the same input string — and
|
|
2269
|
+
* therefore the same key — while preimage and collision resistance hold perfectly.
|
|
2270
|
+
*
|
|
2271
|
+
* What that buys is precise, and overclaiming it would repeat the mistake it fixes:
|
|
2272
|
+
* distinct pairs are no longer *constructibly* mapped onto one key, because the
|
|
2273
|
+
* ambiguity was in the encoding and the encoding is now unique. The key itself stays
|
|
2274
|
+
* collision-RESISTANT rather than collision-free — an unbounded domain cannot map
|
|
2275
|
+
* injectively into 256 bits, and no construction changes that.
|
|
2276
|
+
*
|
|
2277
|
+
* A component that names no tenant and no recipient is refused rather than hashed:
|
|
2278
|
+
* empty, whitespace-only, or not a string at all. `('', r)` would put every caller
|
|
2279
|
+
* that omits a tenant into one shared namespace — the same isolation failure arrived
|
|
2280
|
+
* at from the other side.
|
|
2224
2281
|
*/
|
|
2225
2282
|
/**
|
|
2226
2283
|
* Derives a deterministic, PII-free storage key fragment from a tenant and recipient.
|
|
2227
2284
|
*
|
|
2228
|
-
* @param tenantId - Tenant isolation scope.
|
|
2229
|
-
* @param recipient - Pre-normalized recipient identifier.
|
|
2230
|
-
* @returns A 64-character lowercase hex SHA-256 digest. Order-sensitive
|
|
2285
|
+
* @param tenantId - Tenant isolation scope. Must be a non-blank string.
|
|
2286
|
+
* @param recipient - Pre-normalized recipient identifier. Must be a non-blank string.
|
|
2287
|
+
* @returns A 64-character lowercase hex SHA-256 digest. Order-sensitive, and unique
|
|
2288
|
+
* per pair up to SHA-256's collision resistance — the encoding contributes no
|
|
2289
|
+
* ambiguity of its own, so two distinct pairs never map onto one key by
|
|
2290
|
+
* construction.
|
|
2291
|
+
* @throws Error When either component is not a string, or is empty or whitespace-only.
|
|
2231
2292
|
*/
|
|
2232
2293
|
declare function hashTenantRecipient(tenantId: string, recipient: string): string;
|
|
2233
2294
|
|
|
@@ -2336,8 +2397,8 @@ declare function formatCooldown(remainingSeconds: number): string;
|
|
|
2336
2397
|
* Stable error-code identifiers, keyed by their symbolic name.
|
|
2337
2398
|
*
|
|
2338
2399
|
* Values are namespaced under `notification.*` and never change once published —
|
|
2339
|
-
* consumers branch on them to localize messages. The
|
|
2340
|
-
* template, OTP, SMS, push, audit,
|
|
2400
|
+
* consumers branch on them to localize messages. The 23 entries cover the email,
|
|
2401
|
+
* template, OTP, SMS, push, audit, channel-config and scoping failure surfaces.
|
|
2341
2402
|
*/
|
|
2342
2403
|
declare const NOTIFICATION_ERROR_CODES: {
|
|
2343
2404
|
readonly EMAIL_PROVIDER_NOT_CONFIGURED: "notification.email_provider_not_configured";
|
|
@@ -2363,6 +2424,13 @@ declare const NOTIFICATION_ERROR_CODES: {
|
|
|
2363
2424
|
readonly PUSH_SEND_FAILED: "notification.push_send_failed";
|
|
2364
2425
|
readonly AUDIT_LOG_FAILED: "notification.audit_log_failed";
|
|
2365
2426
|
readonly CHANNEL_DISABLED: "notification.channel_disabled";
|
|
2427
|
+
/**
|
|
2428
|
+
* A `tenantId` or `recipient` that identifies nobody reached a scoping
|
|
2429
|
+
* boundary — empty, whitespace-only, or not a string at all. Distinct from
|
|
2430
|
+
* {@link EMAIL_INVALID_RECIPIENT}, which is about a malformed address rather
|
|
2431
|
+
* than a value that cannot scope anything.
|
|
2432
|
+
*/
|
|
2433
|
+
readonly INVALID_SCOPE_IDENTIFIER: "notification.invalid_scope_identifier";
|
|
2366
2434
|
};
|
|
2367
2435
|
/**
|
|
2368
2436
|
* The literal-union type of every value in {@link NOTIFICATION_ERROR_CODES}.
|
|
@@ -2500,6 +2568,11 @@ declare const NOTIFICATION_ERROR_DEFINITIONS: {
|
|
|
2500
2568
|
readonly status: HttpStatus.NOT_IMPLEMENTED;
|
|
2501
2569
|
readonly message: "Channel not enabled in module config";
|
|
2502
2570
|
};
|
|
2571
|
+
readonly INVALID_SCOPE_IDENTIFIER: {
|
|
2572
|
+
readonly code: "notification.invalid_scope_identifier";
|
|
2573
|
+
readonly status: HttpStatus.BAD_REQUEST;
|
|
2574
|
+
readonly message: "Tenant or recipient identifier does not identify anybody";
|
|
2575
|
+
};
|
|
2503
2576
|
};
|
|
2504
2577
|
/** The symbolic key of any entry in {@link NOTIFICATION_ERROR_DEFINITIONS}. */
|
|
2505
2578
|
type NotificationErrorKey = keyof typeof NOTIFICATION_ERROR_DEFINITIONS;
|
|
@@ -2632,4 +2705,5 @@ declare const DEFAULT_TTLS: {
|
|
|
2632
2705
|
readonly OTP_GENERIC_SECONDS: 600;
|
|
2633
2706
|
};
|
|
2634
2707
|
|
|
2635
|
-
export {
|
|
2708
|
+
export { BYMAX_NOTIFICATION_EMAIL_PROVIDER, BYMAX_NOTIFICATION_LOG_REPOSITORY, BYMAX_NOTIFICATION_OPTIONS, BYMAX_NOTIFICATION_OTP_STORAGE, BYMAX_NOTIFICATION_PUSH_PROVIDER, BYMAX_NOTIFICATION_SMS_PROVIDER, BYMAX_NOTIFICATION_TEMPLATE_RENDERER, BymaxNotificationModule, CANONICAL_EMAIL_TEMPLATES, DEFAULT_TTLS, DefaultTemplateRenderer, EmailService, InMemoryOtpStorage, NOTIFICATION_ERROR_CODES, NOTIFICATION_ERROR_DEFINITIONS, NOTIFICATION_PURPOSES, NoOpEmailProvider, NoOpNotificationLogRepository, NotificationAuditInterceptor, NotificationException, NotificationService, OtpService, RedisOtpStorage, ResendEmailProvider, SmtpEmailProvider, cooldownExpiresAt, formatCooldown, generateOtpCode, hashTenantRecipient, safeCompare, toRetryAfterHeader };
|
|
2709
|
+
export type { AuditOptions, BymaxNotificationModuleAsyncOptions, BymaxNotificationModuleOptions, BymaxNotificationModuleOptionsFactory, CanonicalEmailTemplate, CanonicalNotificationPurpose, ConsumeAttemptResult, DefaultTemplateRendererOptions, DispatchInput, DispatchResult, EmailAttachment, EmailChannelOptions, EmailDispatchPayload, EmailSendInput, EmailSendOptions, EmailSendResult, EmailSendTemplateInput, GlobalOptions, IEmailProvider, IEmailTemplateRenderer, INotificationLogRepository, IOtpStorage, IPushProvider, ISmsProvider, InMemoryStorageSize, MissingVariableMode, NotificationChannel, NotificationErrorCode, NotificationErrorDefinition, NotificationErrorKey, NotificationErrorResponse, NotificationExceptionOptions, NotificationLogEntry, NotificationLogVerb, NotificationRequest, OtpChannelOptions, OtpConsumeInput, OtpDispatchPayload, OtpEntry, OtpGenerateInput, OtpGenerateResult, OtpPurpose, OtpPurposeConfig, OtpStatusInput, OtpStatusResult, OtpVerifyInput, OtpVerifyResult, PushChannelOptions, PushSendOptions, PushSendResult, RedisLike, RedisOtpStorageOptions, RenderedEmail, ResendEmailProviderOptions, ResolvedAuditOptions, ResolvedEmailOptions, ResolvedGlobalOptions, ResolvedNotificationOptions, ResolvedOtpOptions, SmsChannelOptions, SmsSendOptions, SmsSendResult, SmtpCredentials, SmtpEmailProviderOptions, SmtpTlsOptions, TemplateDefinition };
|
package/dist/server/index.mjs
CHANGED
|
@@ -144,7 +144,14 @@ var NOTIFICATION_ERROR_CODES = {
|
|
|
144
144
|
PUSH_PROVIDER_NOT_CONFIGURED: "notification.push_provider_not_configured",
|
|
145
145
|
PUSH_SEND_FAILED: "notification.push_send_failed",
|
|
146
146
|
AUDIT_LOG_FAILED: "notification.audit_log_failed",
|
|
147
|
-
CHANNEL_DISABLED: "notification.channel_disabled"
|
|
147
|
+
CHANNEL_DISABLED: "notification.channel_disabled",
|
|
148
|
+
/**
|
|
149
|
+
* A `tenantId` or `recipient` that identifies nobody reached a scoping
|
|
150
|
+
* boundary — empty, whitespace-only, or not a string at all. Distinct from
|
|
151
|
+
* {@link EMAIL_INVALID_RECIPIENT}, which is about a malformed address rather
|
|
152
|
+
* than a value that cannot scope anything.
|
|
153
|
+
*/
|
|
154
|
+
INVALID_SCOPE_IDENTIFIER: "notification.invalid_scope_identifier"
|
|
148
155
|
};
|
|
149
156
|
|
|
150
157
|
// src/server/errors/notification-error-codes.ts
|
|
@@ -258,6 +265,11 @@ var NOTIFICATION_ERROR_DEFINITIONS = {
|
|
|
258
265
|
code: NOTIFICATION_ERROR_CODES.CHANNEL_DISABLED,
|
|
259
266
|
status: HttpStatus.NOT_IMPLEMENTED,
|
|
260
267
|
message: "Channel not enabled in module config"
|
|
268
|
+
},
|
|
269
|
+
INVALID_SCOPE_IDENTIFIER: {
|
|
270
|
+
code: NOTIFICATION_ERROR_CODES.INVALID_SCOPE_IDENTIFIER,
|
|
271
|
+
status: HttpStatus.BAD_REQUEST,
|
|
272
|
+
message: "Tenant or recipient identifier does not identify anybody"
|
|
261
273
|
}
|
|
262
274
|
};
|
|
263
275
|
var NOTIFICATION_ERROR_DEFINITION_MAP = new Map(
|
|
@@ -1184,6 +1196,23 @@ function formatCooldown(remainingSeconds) {
|
|
|
1184
1196
|
}
|
|
1185
1197
|
return parts.join(" ");
|
|
1186
1198
|
}
|
|
1199
|
+
|
|
1200
|
+
// src/server/utils/tenant-scope.ts
|
|
1201
|
+
function refuse(owner, label, reason) {
|
|
1202
|
+
return new NotificationException(
|
|
1203
|
+
"INVALID_SCOPE_IDENTIFIER",
|
|
1204
|
+
{ boundary: owner, parameter: label, reason },
|
|
1205
|
+
{ message: `[${owner}] ${label} ${reason}` }
|
|
1206
|
+
);
|
|
1207
|
+
}
|
|
1208
|
+
function assertIdentifies(owner, label, value) {
|
|
1209
|
+
if (typeof value !== "string") {
|
|
1210
|
+
throw refuse(owner, label, `expected a string, received ${typeof value}`);
|
|
1211
|
+
}
|
|
1212
|
+
if (value.trim() === "") {
|
|
1213
|
+
throw refuse(owner, label, "empty or whitespace-only");
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1187
1216
|
function safeCompare(expected, actual) {
|
|
1188
1217
|
const expectedBytes = Buffer.from(expected, "utf8");
|
|
1189
1218
|
const actualBytes = Buffer.from(actual, "utf8");
|
|
@@ -1229,6 +1258,7 @@ var OtpService = class {
|
|
|
1229
1258
|
*/
|
|
1230
1259
|
async generate(input) {
|
|
1231
1260
|
const otp = this.requireOtpOptions();
|
|
1261
|
+
this.assertScope(input);
|
|
1232
1262
|
const cfg = otp.resolveForPurpose(input.purpose);
|
|
1233
1263
|
await this.acquireCooldownOrThrow(input, cfg.resendCooldownSeconds);
|
|
1234
1264
|
const code = generateOtpCode(cfg.length, cfg.codeType);
|
|
@@ -1246,6 +1276,7 @@ var OtpService = class {
|
|
|
1246
1276
|
*/
|
|
1247
1277
|
async verify(input) {
|
|
1248
1278
|
const otp = this.requireOtpOptions();
|
|
1279
|
+
this.assertScope(input);
|
|
1249
1280
|
const result = await this.storage.consumeAttempt(input.tenantId, input.recipient, input.purpose);
|
|
1250
1281
|
if (result.status === "not_found") {
|
|
1251
1282
|
await this.audit(this.otpAuditEntry("failed", input, { reason: "not_found" }));
|
|
@@ -1277,6 +1308,7 @@ var OtpService = class {
|
|
|
1277
1308
|
*/
|
|
1278
1309
|
async consume(input) {
|
|
1279
1310
|
this.requireOtpOptions();
|
|
1311
|
+
this.assertScope(input);
|
|
1280
1312
|
await this.storage.delete(input.tenantId, input.recipient, input.purpose);
|
|
1281
1313
|
await this.storage.clearCooldown(input.tenantId, input.recipient, input.purpose);
|
|
1282
1314
|
}
|
|
@@ -1296,6 +1328,7 @@ var OtpService = class {
|
|
|
1296
1328
|
* @returns Existence, expiry, attempt counters, validation flag, and cooldown.
|
|
1297
1329
|
*/
|
|
1298
1330
|
async getStatus(input) {
|
|
1331
|
+
this.assertScope(input);
|
|
1299
1332
|
const [entry, cooldownSeconds] = await Promise.all([
|
|
1300
1333
|
this.storage.get(input.tenantId, input.recipient, input.purpose),
|
|
1301
1334
|
this.storage.getCooldown(input.tenantId, input.recipient, input.purpose)
|
|
@@ -1417,6 +1450,19 @@ var OtpService = class {
|
|
|
1417
1450
|
}
|
|
1418
1451
|
await this.audit(this.otpAuditEntry("verified", input, void 0));
|
|
1419
1452
|
}
|
|
1453
|
+
/**
|
|
1454
|
+
* Refuses a tenant id or recipient that identifies nobody, before any storage call.
|
|
1455
|
+
*
|
|
1456
|
+
* The reference storage already refuses these through `hashTenantRecipient`, but a
|
|
1457
|
+
* consumer-supplied {@link IOtpStorage} is not required to, and a blank scope would
|
|
1458
|
+
* then be shared by every caller that sent one.
|
|
1459
|
+
*
|
|
1460
|
+
* @throws NotificationException `INVALID_SCOPE_IDENTIFIER` when either is blank.
|
|
1461
|
+
*/
|
|
1462
|
+
assertScope(ref) {
|
|
1463
|
+
assertIdentifies("OtpService", "tenantId", ref.tenantId);
|
|
1464
|
+
assertIdentifies("OtpService", "recipient", ref.recipient);
|
|
1465
|
+
}
|
|
1420
1466
|
/** Returns the resolved OTP options or throws `OTP_STORAGE_NOT_CONFIGURED`. */
|
|
1421
1467
|
requireOtpOptions() {
|
|
1422
1468
|
if (!this.options.otp) {
|
|
@@ -1833,6 +1879,7 @@ function instantiate(valueOrClass) {
|
|
|
1833
1879
|
}
|
|
1834
1880
|
return new ctor();
|
|
1835
1881
|
}
|
|
1882
|
+
var INTERCEPTOR_NAME = "NotificationAuditInterceptor";
|
|
1836
1883
|
var INTERCEPTOR_PROVIDER_NAME = "__interceptor__";
|
|
1837
1884
|
function toErrorMessage(error) {
|
|
1838
1885
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -1908,19 +1955,36 @@ var NotificationAuditInterceptor = class {
|
|
|
1908
1955
|
return typeof candidate.payload === "object" && candidate.payload !== null;
|
|
1909
1956
|
}
|
|
1910
1957
|
/**
|
|
1911
|
-
* Resolves the trusted tenant id
|
|
1912
|
-
*
|
|
1958
|
+
* Resolves the trusted tenant id from `tenantIdResolver(request)`.
|
|
1959
|
+
*
|
|
1960
|
+
* Falls back to the payload-supplied value only when no resolver is configured or
|
|
1961
|
+
* no request is available — the payload reaches the handler from the caller, so
|
|
1962
|
+
* that value is asserted rather than authenticated, and an entry built from it
|
|
1963
|
+
* attributes the event to whichever tenant the caller named. Configure a resolver
|
|
1964
|
+
* whenever this interceptor runs on an HTTP boundary.
|
|
1965
|
+
*
|
|
1966
|
+
* A value that identifies nobody is refused rather than recorded, on BOTH paths:
|
|
1967
|
+
* the resolver's answer and the payload fallback. The fallback is the default path
|
|
1968
|
+
* — most consumers configure no resolver — so guarding only the resolver would
|
|
1969
|
+
* leave the common case unchecked, which is where a blank tenant actually arrives.
|
|
1970
|
+
*
|
|
1971
|
+
* @throws Error When either the resolved or the fallback tenant id is not a
|
|
1972
|
+
* string, or is blank.
|
|
1913
1973
|
*/
|
|
1914
1974
|
async resolveTenantId(context, fallback) {
|
|
1915
1975
|
const resolver = this.options.global.tenantIdResolver;
|
|
1916
1976
|
if (resolver === void 0) {
|
|
1977
|
+
assertIdentifies(INTERCEPTOR_NAME, "the payload tenant id", fallback);
|
|
1917
1978
|
return fallback;
|
|
1918
1979
|
}
|
|
1919
1980
|
const request = this.extractRequest(context);
|
|
1920
1981
|
if (request === null) {
|
|
1982
|
+
assertIdentifies(INTERCEPTOR_NAME, "the payload tenant id", fallback);
|
|
1921
1983
|
return fallback;
|
|
1922
1984
|
}
|
|
1923
|
-
|
|
1985
|
+
const resolved = await resolver(request);
|
|
1986
|
+
assertIdentifies(INTERCEPTOR_NAME, "the resolved tenant id", resolved);
|
|
1987
|
+
return resolved;
|
|
1924
1988
|
}
|
|
1925
1989
|
/** Reads the HTTP request from the context, returning `null` outside an HTTP context. */
|
|
1926
1990
|
extractRequest(context) {
|
|
@@ -2048,7 +2112,8 @@ async function loadOptionalPeer(moduleName) {
|
|
|
2048
2112
|
} catch (error) {
|
|
2049
2113
|
if (isModuleNotFound(error, moduleName)) {
|
|
2050
2114
|
throw new Error(
|
|
2051
|
-
`\`${moduleName}\` package is not installed. Run \`pnpm add ${moduleName}\` in the consumer app
|
|
2115
|
+
`\`${moduleName}\` package is not installed. Run \`pnpm add ${moduleName}\` in the consumer app.`,
|
|
2116
|
+
{ cause: error }
|
|
2052
2117
|
);
|
|
2053
2118
|
}
|
|
2054
2119
|
const reason = error instanceof Error ? error.message : String(error);
|
|
@@ -2657,11 +2722,21 @@ _options2 = new WeakMap();
|
|
|
2657
2722
|
SmtpEmailProvider = __decorateClass([
|
|
2658
2723
|
Injectable()
|
|
2659
2724
|
], SmtpEmailProvider);
|
|
2725
|
+
function sha256(value) {
|
|
2726
|
+
return createHash("sha256").update(value).digest("hex");
|
|
2727
|
+
}
|
|
2728
|
+
function hashTenantRecipient(tenantId, recipient) {
|
|
2729
|
+
assertIdentifies("hashTenantRecipient", "tenantId", tenantId);
|
|
2730
|
+
assertIdentifies("hashTenantRecipient", "recipient", recipient);
|
|
2731
|
+
return createHash("sha256").update(`${sha256(tenantId)}:${sha256(recipient)}`).digest("hex");
|
|
2732
|
+
}
|
|
2733
|
+
|
|
2734
|
+
// src/server/providers/in-memory-otp.storage.ts
|
|
2660
2735
|
var MS_PER_SECOND3 = 1e3;
|
|
2661
2736
|
var InMemoryOtpStorage = class {
|
|
2662
2737
|
constructor() {
|
|
2663
2738
|
this.name = "memory";
|
|
2664
|
-
/** OTP entries keyed by `${tenantId
|
|
2739
|
+
/** OTP entries keyed by `${hashTenantRecipient(tenantId, recipient)}::${purpose}`. */
|
|
2665
2740
|
this.store = /* @__PURE__ */ new Map();
|
|
2666
2741
|
/** Cooldown expiries (epoch ms) keyed by the same composite key. */
|
|
2667
2742
|
this.cooldowns = /* @__PURE__ */ new Map();
|
|
@@ -2820,19 +2895,22 @@ var InMemoryOtpStorage = class {
|
|
|
2820
2895
|
size() {
|
|
2821
2896
|
return { otps: this.store.size, cooldowns: this.cooldowns.size };
|
|
2822
2897
|
}
|
|
2823
|
-
/**
|
|
2898
|
+
/**
|
|
2899
|
+
* Collapses the `(tenantId, recipient, purpose)` tuple into one map key.
|
|
2900
|
+
*
|
|
2901
|
+
* The tenant and recipient go through {@link hashTenantRecipient}, which encodes
|
|
2902
|
+
* the pair unambiguously and rejects an empty component. Joining the raw values
|
|
2903
|
+
* around a delimiter would not: `('a::b', 'c', p)` and `('a', 'b::c', p)` produce
|
|
2904
|
+
* the same string. The digest is fixed-length, so appending the purpose after it
|
|
2905
|
+
* stays unambiguous too.
|
|
2906
|
+
*/
|
|
2824
2907
|
key(tenantId, recipient, purpose) {
|
|
2825
|
-
return `${tenantId
|
|
2908
|
+
return `${hashTenantRecipient(tenantId, recipient)}::${purpose}`;
|
|
2826
2909
|
}
|
|
2827
2910
|
};
|
|
2828
2911
|
InMemoryOtpStorage = __decorateClass([
|
|
2829
2912
|
Injectable()
|
|
2830
2913
|
], InMemoryOtpStorage);
|
|
2831
|
-
function hashTenantRecipient(tenantId, recipient) {
|
|
2832
|
-
return createHash("sha256").update(`${tenantId}:${recipient}`).digest("hex");
|
|
2833
|
-
}
|
|
2834
|
-
|
|
2835
|
-
// src/server/providers/redis-otp.storage.ts
|
|
2836
2914
|
var MS_PER_SECOND4 = 1e3;
|
|
2837
2915
|
var _redis;
|
|
2838
2916
|
var RedisOtpStorage = class {
|
|
@@ -2986,11 +3064,11 @@ var RedisOtpStorage = class {
|
|
|
2986
3064
|
async clearCooldown(tenantId, recipient, purpose) {
|
|
2987
3065
|
await __privateGet(this, _redis).del(this.cooldownKey(tenantId, recipient, purpose));
|
|
2988
3066
|
}
|
|
2989
|
-
/** Builds the OTP entry key: `{namespace}:otp:{purpose}:{
|
|
3067
|
+
/** Builds the OTP entry key: `{namespace}:otp:{purpose}:{hashTenantRecipient(...)}`. */
|
|
2990
3068
|
otpKey(tenantId, recipient, purpose) {
|
|
2991
3069
|
return `${this.namespace}:otp:${purpose}:${hashTenantRecipient(tenantId, recipient)}`;
|
|
2992
3070
|
}
|
|
2993
|
-
/** Builds the cooldown key: `{namespace}:otp_cd:{purpose}:{
|
|
3071
|
+
/** Builds the cooldown key: `{namespace}:otp_cd:{purpose}:{hashTenantRecipient(...)}`. */
|
|
2994
3072
|
cooldownKey(tenantId, recipient, purpose) {
|
|
2995
3073
|
return `${this.namespace}:otp_cd:${purpose}:${hashTenantRecipient(tenantId, recipient)}`;
|
|
2996
3074
|
}
|