@bymax-one/nest-notification 1.4.9 → 1.4.11
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 +134 -1
- package/README.md +58 -11
- package/dist/server/index.cjs +324 -73
- package/dist/server/index.d.cts +102 -37
- package/dist/server/index.d.ts +102 -37
- package/dist/server/index.mjs +324 -73
- package/dist/shared/index.cjs +4 -2
- package/dist/shared/index.d.cts +4 -2
- package/dist/shared/index.d.ts +4 -2
- package/dist/shared/index.mjs +4 -2
- package/dist/testing/index.cjs +3 -2
- package/dist/testing/index.mjs +3 -2
- package/package.json +1 -1
package/dist/server/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var common = require('@nestjs/common');
|
|
4
4
|
var crypto = require('crypto');
|
|
5
|
+
var url = require('url');
|
|
5
6
|
var rxjs = require('rxjs');
|
|
6
7
|
|
|
7
8
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -29,6 +30,7 @@ var BYMAX_NOTIFICATION_SMS_PROVIDER = /* @__PURE__ */ Symbol("BYMAX_NOTIFICATION
|
|
|
29
30
|
var BYMAX_NOTIFICATION_PUSH_PROVIDER = /* @__PURE__ */ Symbol("BYMAX_NOTIFICATION_PUSH_PROVIDER");
|
|
30
31
|
var BYMAX_NOTIFICATION_TEMPLATE_RENDERER = /* @__PURE__ */ Symbol("BYMAX_NOTIFICATION_TEMPLATE_RENDERER");
|
|
31
32
|
var BYMAX_NOTIFICATION_LOG_REPOSITORY = /* @__PURE__ */ Symbol("BYMAX_NOTIFICATION_LOG_REPOSITORY");
|
|
33
|
+
var BYMAX_NOTIFICATION_AUDIT_SINK = /* @__PURE__ */ Symbol("BYMAX_NOTIFICATION_AUDIT_SINK");
|
|
32
34
|
|
|
33
35
|
// src/server/constants/default-options.constants.ts
|
|
34
36
|
var DEFAULT_MAX_ATTACHMENT_BYTES = 10485760;
|
|
@@ -51,6 +53,15 @@ var DEFAULT_EMAIL_OPTIONS = {
|
|
|
51
53
|
var DEFAULT_AUDIT_OPTIONS = {
|
|
52
54
|
swallowErrors: true
|
|
53
55
|
};
|
|
56
|
+
|
|
57
|
+
// src/server/constants/notification-purposes.ts
|
|
58
|
+
var NOTIFICATION_PURPOSES = {
|
|
59
|
+
EMAIL_VERIFICATION: "email_verification",
|
|
60
|
+
PASSWORD_RESET: "password_reset",
|
|
61
|
+
MFA_OOB: "mfa_oob",
|
|
62
|
+
PHONE_VERIFICATION: "phone_verification",
|
|
63
|
+
MAGIC_LINK: "magic_link"
|
|
64
|
+
};
|
|
54
65
|
var CODE_DIGEST_PREFIX = "hmac-sha256:v1:";
|
|
55
66
|
var MIN_CODE_SECRET_BYTES = 32;
|
|
56
67
|
var CONTEXT = "bymax-notification:otp-code:v1\0";
|
|
@@ -102,6 +113,10 @@ function resolveOtp(otp) {
|
|
|
102
113
|
);
|
|
103
114
|
const perPurpose = Object.fromEntries(entries);
|
|
104
115
|
const perPurposeMap = new Map(entries);
|
|
116
|
+
const registered = /* @__PURE__ */ new Set([
|
|
117
|
+
...Object.values(NOTIFICATION_PURPOSES),
|
|
118
|
+
...perPurposeMap.keys()
|
|
119
|
+
]);
|
|
105
120
|
return {
|
|
106
121
|
defaultLength: base.length,
|
|
107
122
|
defaultCodeType: base.codeType,
|
|
@@ -111,7 +126,9 @@ function resolveOtp(otp) {
|
|
|
111
126
|
consumeOnVerify: otp.consumeOnVerify ?? DEFAULT_OTP_OPTIONS.consumeOnVerify,
|
|
112
127
|
...otp.codeSecret !== void 0 ? { digestCode: createCodeDigester(otp.codeSecret) } : {},
|
|
113
128
|
perPurpose,
|
|
114
|
-
resolveForPurpose: (purpose) => perPurposeMap.get(purpose) ?? base
|
|
129
|
+
resolveForPurpose: (purpose) => perPurposeMap.get(purpose) ?? base,
|
|
130
|
+
strictPurposes: otp.strictPurposes ?? false,
|
|
131
|
+
isRegisteredPurpose: (purpose) => registered.has(purpose)
|
|
115
132
|
};
|
|
116
133
|
}
|
|
117
134
|
function resolveAudit(options) {
|
|
@@ -169,8 +186,10 @@ var NOTIFICATION_ERROR_CODES = {
|
|
|
169
186
|
AUDIT_LOG_FAILED: "notification.audit_log_failed",
|
|
170
187
|
CHANNEL_DISABLED: "notification.channel_disabled",
|
|
171
188
|
/**
|
|
172
|
-
* A `tenantId` or `
|
|
173
|
-
* boundary
|
|
189
|
+
* A `tenantId`, `recipient` or `purpose` that cannot scope an OTP reached a
|
|
190
|
+
* scoping boundary: empty, whitespace-only, not a string at all, carrying a
|
|
191
|
+
* control character, or, for a purpose, longer than 64 characters or made of
|
|
192
|
+
* anything but `A-Z a-z 0-9 : _ -`. Distinct from
|
|
174
193
|
* {@link EMAIL_INVALID_RECIPIENT}, which is about a malformed address rather
|
|
175
194
|
* than a value that cannot scope anything.
|
|
176
195
|
*/
|
|
@@ -313,7 +332,7 @@ var NOTIFICATION_ERROR_DEFINITIONS = {
|
|
|
313
332
|
INVALID_SCOPE_IDENTIFIER: {
|
|
314
333
|
code: NOTIFICATION_ERROR_CODES.INVALID_SCOPE_IDENTIFIER,
|
|
315
334
|
status: common.HttpStatus.BAD_REQUEST,
|
|
316
|
-
message: "
|
|
335
|
+
message: "A tenant, recipient or purpose identifier is not valid"
|
|
317
336
|
}
|
|
318
337
|
};
|
|
319
338
|
var NOTIFICATION_ERROR_DEFINITION_MAP = new Map(
|
|
@@ -378,6 +397,52 @@ var NotificationException = class extends common.HttpException {
|
|
|
378
397
|
}
|
|
379
398
|
};
|
|
380
399
|
|
|
400
|
+
// src/server/utils/tenant-scope.ts
|
|
401
|
+
var CONTROL_CHARACTER = new RegExp("\\p{Cc}", "u");
|
|
402
|
+
function refuse(owner, label, reason) {
|
|
403
|
+
return new NotificationException(
|
|
404
|
+
"INVALID_SCOPE_IDENTIFIER",
|
|
405
|
+
{ boundary: owner, parameter: label, reason },
|
|
406
|
+
{ message: `[${owner}] ${label} ${reason}` }
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
function assertIdentifies(owner, label, value) {
|
|
410
|
+
if (typeof value !== "string") {
|
|
411
|
+
throw refuse(owner, label, `expected a string, received ${typeof value}`);
|
|
412
|
+
}
|
|
413
|
+
if (value.trim() === "") {
|
|
414
|
+
throw refuse(owner, label, "empty or whitespace-only");
|
|
415
|
+
}
|
|
416
|
+
if (CONTROL_CHARACTER.test(value)) {
|
|
417
|
+
throw refuse(owner, label, "contains a control character");
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
var MAX_PURPOSE_LENGTH = 64;
|
|
421
|
+
var PURPOSE_CHARACTERS = /^[A-Za-z0-9:_-]+$/;
|
|
422
|
+
function purposeProblem(value) {
|
|
423
|
+
if (typeof value !== "string") {
|
|
424
|
+
return `expected a string, received ${typeof value}`;
|
|
425
|
+
}
|
|
426
|
+
if (value === "") {
|
|
427
|
+
return "empty";
|
|
428
|
+
}
|
|
429
|
+
if (value.length > MAX_PURPOSE_LENGTH) {
|
|
430
|
+
return `longer than ${String(MAX_PURPOSE_LENGTH)} characters`;
|
|
431
|
+
}
|
|
432
|
+
return PURPOSE_CHARACTERS.test(value) ? void 0 : "contains a character outside A-Z a-z 0-9 : _ -";
|
|
433
|
+
}
|
|
434
|
+
function assertRegisteredPurpose(owner, value, isRegistered) {
|
|
435
|
+
if (!isRegistered(value)) {
|
|
436
|
+
throw refuse(owner, "purpose", "not a registered purpose");
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
function assertPurpose(owner, value) {
|
|
440
|
+
const problem = purposeProblem(value);
|
|
441
|
+
if (problem !== void 0) {
|
|
442
|
+
throw refuse(owner, "purpose", problem);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
381
446
|
// src/server/config/default-options.ts
|
|
382
447
|
var VALID_CODE_TYPES = ["numeric", "alpha", "alphanumeric"];
|
|
383
448
|
var OTP_MIN_LENGTH = 1;
|
|
@@ -472,6 +537,17 @@ function validateOtpOptions(otp) {
|
|
|
472
537
|
if (otp.defaultMaxAttempts !== void 0 && otp.defaultMaxAttempts < 1) {
|
|
473
538
|
throw new Error("[BymaxNotificationModule] options.otp.defaultMaxAttempts must be at least 1");
|
|
474
539
|
}
|
|
540
|
+
for (const key of Object.keys(otp.perPurpose ?? {})) {
|
|
541
|
+
const problem = purposeProblem(key);
|
|
542
|
+
if (problem !== void 0) {
|
|
543
|
+
throw new Error(
|
|
544
|
+
`[BymaxNotificationModule] options.otp.perPurpose has the key ${JSON.stringify(key)}, which is not a valid purpose: ${problem}`
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
if (otp.strictPurposes !== void 0 && typeof otp.strictPurposes !== "boolean") {
|
|
549
|
+
throw new Error("[BymaxNotificationModule] options.otp.strictPurposes must be a boolean");
|
|
550
|
+
}
|
|
475
551
|
if (otp.resendCooldownSeconds !== void 0 && otp.resendCooldownSeconds < 0) {
|
|
476
552
|
throw new Error(
|
|
477
553
|
"[BymaxNotificationModule] options.otp.resendCooldownSeconds must be 0 or greater"
|
|
@@ -554,14 +630,13 @@ exports.DefaultTemplateRenderer = class DefaultTemplateRenderer {
|
|
|
554
630
|
});
|
|
555
631
|
}
|
|
556
632
|
/**
|
|
557
|
-
* Builds a variable resolver for one render call.
|
|
558
|
-
*
|
|
559
|
-
*
|
|
633
|
+
* Builds a variable resolver for one render call. A key resolves only when the object holds it as
|
|
634
|
+
* an own enumerable property, so neither flat nor nested resolution can reach inherited or
|
|
635
|
+
* prototype keys, and only the property that is named is read.
|
|
560
636
|
*/
|
|
561
637
|
buildResolver(data) {
|
|
562
638
|
if (!this.enableNestedPaths) {
|
|
563
|
-
|
|
564
|
-
return (path) => values.get(path);
|
|
639
|
+
return (path) => this.ownValue(data, path);
|
|
565
640
|
}
|
|
566
641
|
return (path) => this.resolveNested(data, path);
|
|
567
642
|
}
|
|
@@ -572,10 +647,17 @@ exports.DefaultTemplateRenderer = class DefaultTemplateRenderer {
|
|
|
572
647
|
if (current === null || current === void 0 || typeof current !== "object") {
|
|
573
648
|
return void 0;
|
|
574
649
|
}
|
|
575
|
-
current =
|
|
650
|
+
current = this.ownValue(current, key);
|
|
576
651
|
}
|
|
577
652
|
return current;
|
|
578
653
|
}
|
|
654
|
+
/**
|
|
655
|
+
* The value of `key` when `object` holds it as an own enumerable property, otherwise `undefined`.
|
|
656
|
+
* Called through `Object.prototype` so an object without a prototype works too.
|
|
657
|
+
*/
|
|
658
|
+
ownValue(object, key) {
|
|
659
|
+
return Object.prototype.propertyIsEnumerable.call(object, key) ? Reflect.get(object, key) : void 0;
|
|
660
|
+
}
|
|
579
661
|
/**
|
|
580
662
|
* Validates a registered template shape, throwing on the offending key. The
|
|
581
663
|
* optional `text` body, when present, must be a string — a non-string `text`
|
|
@@ -600,6 +682,87 @@ exports.DefaultTemplateRenderer = class DefaultTemplateRenderer {
|
|
|
600
682
|
exports.DefaultTemplateRenderer = __decorateClass([
|
|
601
683
|
common.Injectable()
|
|
602
684
|
], exports.DefaultTemplateRenderer);
|
|
685
|
+
|
|
686
|
+
// src/server/utils/audit-escape.ts
|
|
687
|
+
var MAX_METADATA_DEPTH = 4;
|
|
688
|
+
var DEPTH_LIMIT_MARKER = "[depth limit]";
|
|
689
|
+
var CIRCULAR_MARKER = "[circular]";
|
|
690
|
+
var NON_PLAIN_MARKER = "[non-plain value]";
|
|
691
|
+
var ESCAPED = /[\p{Cc}\p{Zl}\p{Zp}\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/gu;
|
|
692
|
+
var NAMED = /* @__PURE__ */ new Map([
|
|
693
|
+
["\n", "\\n"],
|
|
694
|
+
["\r", "\\r"],
|
|
695
|
+
[" ", "\\t"]
|
|
696
|
+
]);
|
|
697
|
+
function sequenceOf(character) {
|
|
698
|
+
return NAMED.get(character) ?? `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`;
|
|
699
|
+
}
|
|
700
|
+
function escapeAuditString(value) {
|
|
701
|
+
return value.replace(ESCAPED, sequenceOf);
|
|
702
|
+
}
|
|
703
|
+
function isPlainObject(value) {
|
|
704
|
+
const prototype = Object.getPrototypeOf(value);
|
|
705
|
+
return prototype === Object.prototype || prototype === null;
|
|
706
|
+
}
|
|
707
|
+
function escapeRecord(record, level, ancestors) {
|
|
708
|
+
const escaped = /* @__PURE__ */ new Map();
|
|
709
|
+
for (const [key, value] of Object.entries(record)) {
|
|
710
|
+
const base = escapeAuditString(key);
|
|
711
|
+
let name = base;
|
|
712
|
+
for (let attempt = 2; escaped.has(name); attempt++) {
|
|
713
|
+
name = `${base}#${attempt}`;
|
|
714
|
+
}
|
|
715
|
+
escaped.set(name, escapeValue(value, level + 1, ancestors));
|
|
716
|
+
}
|
|
717
|
+
return Object.fromEntries(escaped);
|
|
718
|
+
}
|
|
719
|
+
function escapeValue(value, level, ancestors) {
|
|
720
|
+
if (typeof value === "string") {
|
|
721
|
+
return escapeAuditString(value);
|
|
722
|
+
}
|
|
723
|
+
if (typeof value === "function" || typeof value === "symbol") {
|
|
724
|
+
return NON_PLAIN_MARKER;
|
|
725
|
+
}
|
|
726
|
+
if (typeof value !== "object" || value === null) {
|
|
727
|
+
return value;
|
|
728
|
+
}
|
|
729
|
+
if (level > MAX_METADATA_DEPTH) {
|
|
730
|
+
return DEPTH_LIMIT_MARKER;
|
|
731
|
+
}
|
|
732
|
+
if (ancestors.has(value)) {
|
|
733
|
+
return CIRCULAR_MARKER;
|
|
734
|
+
}
|
|
735
|
+
const isArray = Array.isArray(value);
|
|
736
|
+
if (!isArray && !isPlainObject(value)) {
|
|
737
|
+
return NON_PLAIN_MARKER;
|
|
738
|
+
}
|
|
739
|
+
ancestors.add(value);
|
|
740
|
+
try {
|
|
741
|
+
return isArray ? value.map((item) => escapeValue(item, level + 1, ancestors)) : escapeRecord(value, level, ancestors);
|
|
742
|
+
} finally {
|
|
743
|
+
ancestors.delete(value);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
function escapeAuditEntry(entry) {
|
|
747
|
+
return escapeRecord(
|
|
748
|
+
entry,
|
|
749
|
+
0,
|
|
750
|
+
/* @__PURE__ */ new WeakSet()
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
// src/server/providers/escaping-log-repository.ts
|
|
755
|
+
var EscapingLogRepository = class {
|
|
756
|
+
/** @param inner - The host's repository that receives the escaped entries. */
|
|
757
|
+
constructor(inner) {
|
|
758
|
+
this.inner = inner;
|
|
759
|
+
this.name = inner.name;
|
|
760
|
+
}
|
|
761
|
+
/** Writes `entry` to the wrapped repository with its strings escaped. */
|
|
762
|
+
async create(entry) {
|
|
763
|
+
await this.inner.create(escapeAuditEntry(entry));
|
|
764
|
+
}
|
|
765
|
+
};
|
|
603
766
|
exports.NoOpNotificationLogRepository = class NoOpNotificationLogRepository {
|
|
604
767
|
constructor() {
|
|
605
768
|
this.name = "noop";
|
|
@@ -1190,27 +1353,6 @@ function buildRedactedCopy(name, message, stack) {
|
|
|
1190
1353
|
return copy;
|
|
1191
1354
|
}
|
|
1192
1355
|
|
|
1193
|
-
// src/server/utils/tenant-scope.ts
|
|
1194
|
-
var CONTROL_CHARACTER = new RegExp("\\p{Cc}", "u");
|
|
1195
|
-
function refuse(owner, label, reason) {
|
|
1196
|
-
return new NotificationException(
|
|
1197
|
-
"INVALID_SCOPE_IDENTIFIER",
|
|
1198
|
-
{ boundary: owner, parameter: label, reason },
|
|
1199
|
-
{ message: `[${owner}] ${label} ${reason}` }
|
|
1200
|
-
);
|
|
1201
|
-
}
|
|
1202
|
-
function assertIdentifies(owner, label, value) {
|
|
1203
|
-
if (typeof value !== "string") {
|
|
1204
|
-
throw refuse(owner, label, `expected a string, received ${typeof value}`);
|
|
1205
|
-
}
|
|
1206
|
-
if (value.trim() === "") {
|
|
1207
|
-
throw refuse(owner, label, "empty or whitespace-only");
|
|
1208
|
-
}
|
|
1209
|
-
if (CONTROL_CHARACTER.test(value)) {
|
|
1210
|
-
throw refuse(owner, label, "contains a control character");
|
|
1211
|
-
}
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1214
1356
|
// src/server/services/email.service.ts
|
|
1215
1357
|
var FALLBACK_LOCALE = "en";
|
|
1216
1358
|
var OTP_TEMPLATE_REMEDY = "register an 'otp_code' template (for example 'otp_code::en') in your template renderer, such as DefaultTemplateRenderer; the OTP email can use another template through the emailTemplate option of generate()";
|
|
@@ -1562,7 +1704,7 @@ exports.EmailService = __decorateClass([
|
|
|
1562
1704
|
__decorateParam(0, common.Inject(BYMAX_NOTIFICATION_OPTIONS)),
|
|
1563
1705
|
__decorateParam(1, common.Inject(BYMAX_NOTIFICATION_EMAIL_PROVIDER)),
|
|
1564
1706
|
__decorateParam(2, common.Inject(BYMAX_NOTIFICATION_TEMPLATE_RENDERER)),
|
|
1565
|
-
__decorateParam(3, common.Inject(
|
|
1707
|
+
__decorateParam(3, common.Inject(BYMAX_NOTIFICATION_AUDIT_SINK))
|
|
1566
1708
|
], exports.EmailService);
|
|
1567
1709
|
var NUMERIC = "0123456789";
|
|
1568
1710
|
var ALPHA = "ABCDEFGHJKLMNPQRSTUVWXYZ";
|
|
@@ -1626,11 +1768,31 @@ function formatCooldown(remainingSeconds) {
|
|
|
1626
1768
|
}
|
|
1627
1769
|
return parts.join(" ");
|
|
1628
1770
|
}
|
|
1629
|
-
|
|
1630
|
-
|
|
1771
|
+
var PLAIN_DOMAIN = /^[\p{L}\p{N}\p{M}_.-]+$/u;
|
|
1772
|
+
var ASCII_LABEL = new RegExp("^\\p{ASCII}*$", "u");
|
|
1773
|
+
function isLabelWiseMapping(folded, mapped) {
|
|
1774
|
+
const expected = folded.split(".").map((label) => ASCII_LABEL.test(label) ? label.toLowerCase() : null);
|
|
1775
|
+
const output = mapped.split(".");
|
|
1776
|
+
return expected.length === output.length && output.every((label, index) => {
|
|
1777
|
+
const want = expected.at(index);
|
|
1778
|
+
return want === null ? label.startsWith("xn--") : label === want;
|
|
1779
|
+
});
|
|
1780
|
+
}
|
|
1781
|
+
function normalizeDomain(domain) {
|
|
1782
|
+
const folded = domain.normalize("NFKC");
|
|
1783
|
+
if (!PLAIN_DOMAIN.test(folded)) {
|
|
1784
|
+
return domain.toLowerCase();
|
|
1785
|
+
}
|
|
1786
|
+
const mapped = url.domainToASCII(domain);
|
|
1787
|
+
return isLabelWiseMapping(folded, mapped) ? mapped : domain.toLowerCase();
|
|
1788
|
+
}
|
|
1631
1789
|
function normalizeRecipient(recipient) {
|
|
1632
1790
|
const trimmed = recipient.trim();
|
|
1633
|
-
|
|
1791
|
+
const at = trimmed.lastIndexOf("@");
|
|
1792
|
+
if (at === -1) {
|
|
1793
|
+
return trimmed;
|
|
1794
|
+
}
|
|
1795
|
+
return `${trimmed.slice(0, at).toLowerCase()}@${normalizeDomain(trimmed.slice(at + 1))}`;
|
|
1634
1796
|
}
|
|
1635
1797
|
|
|
1636
1798
|
// src/server/utils/storage-failures.ts
|
|
@@ -1971,7 +2133,7 @@ exports.OtpService = class OtpService {
|
|
|
1971
2133
|
);
|
|
1972
2134
|
} catch (error) {
|
|
1973
2135
|
this.logger.warn(
|
|
1974
|
-
`[${TEMPLATE_WARNING_CODE}] Could not check whether the email template '${DEFAULT_OTP_TEMPLATE}' resolves (${describeError(error)}); if it does not, the first generate() that delivers by email fails with TEMPLATE_NOT_FOUND.`
|
|
2136
|
+
`[${TEMPLATE_WARNING_CODE}] Could not check whether the email template '${DEFAULT_OTP_TEMPLATE}' resolves (${escapeAuditString(describeError(error))}); if it does not, the first generate() that delivers by email fails with TEMPLATE_NOT_FOUND.`
|
|
1975
2137
|
);
|
|
1976
2138
|
}
|
|
1977
2139
|
}
|
|
@@ -2106,11 +2268,16 @@ exports.OtpService = class OtpService {
|
|
|
2106
2268
|
* consumer-supplied {@link IOtpStorage} is not required to, and a blank scope would
|
|
2107
2269
|
* then be shared by every caller that sent one.
|
|
2108
2270
|
*
|
|
2109
|
-
* @throws NotificationException `INVALID_SCOPE_IDENTIFIER` when either is blank.
|
|
2271
|
+
* @throws NotificationException `INVALID_SCOPE_IDENTIFIER` when either is blank or the purpose is not a plain name.
|
|
2110
2272
|
*/
|
|
2111
2273
|
assertScope(ref) {
|
|
2112
2274
|
assertIdentifies("OtpService", "tenantId", ref.tenantId);
|
|
2113
2275
|
assertIdentifies("OtpService", "recipient", ref.recipient);
|
|
2276
|
+
assertPurpose("OtpService", ref.purpose);
|
|
2277
|
+
const otp = this.options.otp;
|
|
2278
|
+
if (otp?.strictPurposes === true) {
|
|
2279
|
+
assertRegisteredPurpose("OtpService", ref.purpose, otp.isRegisteredPurpose);
|
|
2280
|
+
}
|
|
2114
2281
|
}
|
|
2115
2282
|
/**
|
|
2116
2283
|
* Returns the input with its recipient replaced by the value the OTP state is keyed on.
|
|
@@ -2173,7 +2340,7 @@ exports.OtpService = __decorateClass([
|
|
|
2173
2340
|
common.Injectable(),
|
|
2174
2341
|
__decorateParam(0, common.Inject(BYMAX_NOTIFICATION_OPTIONS)),
|
|
2175
2342
|
__decorateParam(1, common.Inject(BYMAX_NOTIFICATION_OTP_STORAGE)),
|
|
2176
|
-
__decorateParam(2, common.Inject(
|
|
2343
|
+
__decorateParam(2, common.Inject(BYMAX_NOTIFICATION_AUDIT_SINK)),
|
|
2177
2344
|
__decorateParam(3, common.Optional()),
|
|
2178
2345
|
__decorateParam(3, common.Inject(exports.EmailService))
|
|
2179
2346
|
], exports.OtpService);
|
|
@@ -2432,6 +2599,7 @@ exports.BymaxNotificationModule = class BymaxNotificationModule {
|
|
|
2432
2599
|
exports: [
|
|
2433
2600
|
BYMAX_NOTIFICATION_OPTIONS,
|
|
2434
2601
|
BYMAX_NOTIFICATION_LOG_REPOSITORY,
|
|
2602
|
+
BYMAX_NOTIFICATION_AUDIT_SINK,
|
|
2435
2603
|
BYMAX_NOTIFICATION_EMAIL_PROVIDER,
|
|
2436
2604
|
BYMAX_NOTIFICATION_TEMPLATE_RENDERER,
|
|
2437
2605
|
BYMAX_NOTIFICATION_OTP_STORAGE,
|
|
@@ -2458,7 +2626,7 @@ exports.BymaxNotificationModule = class BymaxNotificationModule {
|
|
|
2458
2626
|
BYMAX_NOTIFICATION_OPTIONS,
|
|
2459
2627
|
BYMAX_NOTIFICATION_EMAIL_PROVIDER,
|
|
2460
2628
|
BYMAX_NOTIFICATION_TEMPLATE_RENDERER,
|
|
2461
|
-
|
|
2629
|
+
BYMAX_NOTIFICATION_AUDIT_SINK
|
|
2462
2630
|
]
|
|
2463
2631
|
},
|
|
2464
2632
|
{
|
|
@@ -2467,7 +2635,7 @@ exports.BymaxNotificationModule = class BymaxNotificationModule {
|
|
|
2467
2635
|
inject: [
|
|
2468
2636
|
BYMAX_NOTIFICATION_OPTIONS,
|
|
2469
2637
|
BYMAX_NOTIFICATION_OTP_STORAGE,
|
|
2470
|
-
|
|
2638
|
+
BYMAX_NOTIFICATION_AUDIT_SINK,
|
|
2471
2639
|
// Stryker disable next-line BooleanLiteral: the EmailService token is ALWAYS registered in async mode (as a factory that resolves to `undefined` when email is absent), so Nest resolves it whether optional is true or false — `optional: false` would still inject `undefined`, never throw, making the flag observationally inert here.
|
|
2472
2640
|
{ token: exports.EmailService, optional: true }
|
|
2473
2641
|
]
|
|
@@ -2498,6 +2666,7 @@ exports.BymaxNotificationModule = class BymaxNotificationModule {
|
|
|
2498
2666
|
useFactory: (raw) => raw.audit ? instantiate(raw.audit.repository) : new exports.NoOpNotificationLogRepository(),
|
|
2499
2667
|
inject: [RAW_NOTIFICATION_OPTIONS]
|
|
2500
2668
|
},
|
|
2669
|
+
auditSinkProvider(),
|
|
2501
2670
|
{
|
|
2502
2671
|
provide: BYMAX_NOTIFICATION_EMAIL_PROVIDER,
|
|
2503
2672
|
useFactory: (raw) => raw.email ? instantiate(raw.email.provider) : null,
|
|
@@ -2519,7 +2688,8 @@ exports.BymaxNotificationModule = class BymaxNotificationModule {
|
|
|
2519
2688
|
static buildProviders(options, resolved) {
|
|
2520
2689
|
const providers = [
|
|
2521
2690
|
{ provide: BYMAX_NOTIFICATION_OPTIONS, useValue: resolved },
|
|
2522
|
-
options.audit?.repository ? resolveAsProvider(BYMAX_NOTIFICATION_LOG_REPOSITORY, options.audit.repository) : { provide: BYMAX_NOTIFICATION_LOG_REPOSITORY, useClass: exports.NoOpNotificationLogRepository }
|
|
2691
|
+
options.audit?.repository ? resolveAsProvider(BYMAX_NOTIFICATION_LOG_REPOSITORY, options.audit.repository) : { provide: BYMAX_NOTIFICATION_LOG_REPOSITORY, useClass: exports.NoOpNotificationLogRepository },
|
|
2692
|
+
auditSinkProvider()
|
|
2523
2693
|
];
|
|
2524
2694
|
if (resolved.email && options.email) {
|
|
2525
2695
|
providers.push(resolveAsProvider(BYMAX_NOTIFICATION_EMAIL_PROVIDER, options.email.provider));
|
|
@@ -2561,6 +2731,13 @@ function activeChannels(resolved) {
|
|
|
2561
2731
|
}
|
|
2562
2732
|
return channels;
|
|
2563
2733
|
}
|
|
2734
|
+
function auditSinkProvider() {
|
|
2735
|
+
return {
|
|
2736
|
+
provide: BYMAX_NOTIFICATION_AUDIT_SINK,
|
|
2737
|
+
useFactory: (repository) => new EscapingLogRepository(repository),
|
|
2738
|
+
inject: [BYMAX_NOTIFICATION_LOG_REPOSITORY]
|
|
2739
|
+
};
|
|
2740
|
+
}
|
|
2564
2741
|
function resolveRenderer(renderer) {
|
|
2565
2742
|
if (renderer === void 0) {
|
|
2566
2743
|
return {
|
|
@@ -2745,25 +2922,24 @@ exports.NotificationAuditInterceptor = class NotificationAuditInterceptor {
|
|
|
2745
2922
|
}
|
|
2746
2923
|
return input.payload.recipient;
|
|
2747
2924
|
}
|
|
2748
|
-
/**
|
|
2925
|
+
/**
|
|
2926
|
+
* Extracts the audit `purpose`: the OTP purpose, or the email template name. The interceptor sees
|
|
2927
|
+
* the payload before the service refuses it and audits a refused call too, so an OTP purpose that
|
|
2928
|
+
* is not a plain name is left out of the entry: a control character in it would forge a log
|
|
2929
|
+
* record and its length is unbounded.
|
|
2930
|
+
*/
|
|
2749
2931
|
extractPurpose(input) {
|
|
2750
|
-
|
|
2932
|
+
if (input.channel !== "otp") {
|
|
2933
|
+
return input.payload.template;
|
|
2934
|
+
}
|
|
2935
|
+
return purposeProblem(input.payload.purpose) === void 0 ? input.payload.purpose : void 0;
|
|
2751
2936
|
}
|
|
2752
2937
|
};
|
|
2753
2938
|
exports.NotificationAuditInterceptor = __decorateClass([
|
|
2754
2939
|
common.Injectable(),
|
|
2755
2940
|
__decorateParam(0, common.Inject(BYMAX_NOTIFICATION_OPTIONS)),
|
|
2756
|
-
__decorateParam(1, common.Inject(
|
|
2941
|
+
__decorateParam(1, common.Inject(BYMAX_NOTIFICATION_AUDIT_SINK))
|
|
2757
2942
|
], exports.NotificationAuditInterceptor);
|
|
2758
|
-
|
|
2759
|
-
// src/server/constants/notification-purposes.ts
|
|
2760
|
-
var NOTIFICATION_PURPOSES = {
|
|
2761
|
-
EMAIL_VERIFICATION: "email_verification",
|
|
2762
|
-
PASSWORD_RESET: "password_reset",
|
|
2763
|
-
MFA_OOB: "mfa_oob",
|
|
2764
|
-
PHONE_VERIFICATION: "phone_verification",
|
|
2765
|
-
MAGIC_LINK: "magic_link"
|
|
2766
|
-
};
|
|
2767
2943
|
function maskEmail(address) {
|
|
2768
2944
|
const at = address.lastIndexOf("@");
|
|
2769
2945
|
if (at <= 0) {
|
|
@@ -2964,7 +3140,7 @@ exports.ResendEmailProvider = class ResendEmailProvider {
|
|
|
2964
3140
|
return withCode(new Error("Resend send failed"), code);
|
|
2965
3141
|
}
|
|
2966
3142
|
const reason = this.scrubSendError(rawMessage, options);
|
|
2967
|
-
this.logger.warn(`[RESEND_SEND_FAILED] ${reason}`);
|
|
3143
|
+
this.logger.warn(`[RESEND_SEND_FAILED] ${escapeAuditString(reason)}`);
|
|
2968
3144
|
return withCode(new Error(`Resend send failed: ${reason}`), code);
|
|
2969
3145
|
}
|
|
2970
3146
|
/**
|
|
@@ -3075,6 +3251,32 @@ function settleOrTimeout(call, timeoutMs) {
|
|
|
3075
3251
|
});
|
|
3076
3252
|
}
|
|
3077
3253
|
var LOOPBACK_HOSTS = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
|
|
3254
|
+
function assertPool(pool) {
|
|
3255
|
+
if (pool === void 0) {
|
|
3256
|
+
return;
|
|
3257
|
+
}
|
|
3258
|
+
if (typeof pool !== "object" || pool === null) {
|
|
3259
|
+
throw new Error("SmtpEmailProvider: pool must be an object");
|
|
3260
|
+
}
|
|
3261
|
+
for (const [name, value] of [
|
|
3262
|
+
["maxConnections", pool.maxConnections],
|
|
3263
|
+
["maxMessages", pool.maxMessages]
|
|
3264
|
+
]) {
|
|
3265
|
+
if (value !== void 0 && !(Number.isInteger(value) && value >= 1)) {
|
|
3266
|
+
throw new Error(`SmtpEmailProvider: pool.${name} must be a positive integer`);
|
|
3267
|
+
}
|
|
3268
|
+
}
|
|
3269
|
+
}
|
|
3270
|
+
function poolConfig(pool) {
|
|
3271
|
+
if (pool === void 0) {
|
|
3272
|
+
return {};
|
|
3273
|
+
}
|
|
3274
|
+
return {
|
|
3275
|
+
pool: true,
|
|
3276
|
+
...pool.maxConnections === void 0 ? {} : { maxConnections: pool.maxConnections },
|
|
3277
|
+
...pool.maxMessages === void 0 ? {} : { maxMessages: pool.maxMessages }
|
|
3278
|
+
};
|
|
3279
|
+
}
|
|
3078
3280
|
function areCredentialsComplete(credentials) {
|
|
3079
3281
|
return Boolean(credentials.user) && Boolean(credentials.pass);
|
|
3080
3282
|
}
|
|
@@ -3113,6 +3315,7 @@ exports.SmtpEmailProvider = class SmtpEmailProvider {
|
|
|
3113
3315
|
`SmtpEmailProvider: sendTimeoutMs must be a positive integer of at most ${String(MAX_SEND_TIMEOUT_MS)}`
|
|
3114
3316
|
);
|
|
3115
3317
|
}
|
|
3318
|
+
assertPool(options.pool);
|
|
3116
3319
|
__privateSet(this, _options2, options);
|
|
3117
3320
|
}
|
|
3118
3321
|
/**
|
|
@@ -3175,6 +3378,38 @@ exports.SmtpEmailProvider = class SmtpEmailProvider {
|
|
|
3175
3378
|
}
|
|
3176
3379
|
return { messageId: info.messageId };
|
|
3177
3380
|
}
|
|
3381
|
+
/**
|
|
3382
|
+
* Closes the transport this provider built, so a pooled connection does not keep the process
|
|
3383
|
+
* alive. A send already in flight on a pooled transport finishes; one still waiting for a
|
|
3384
|
+
* connection fails with `Connection pool was closed`. Idempotent, and it never builds a transport
|
|
3385
|
+
* to close it. A `send()` after it builds a new transport, which the host has to close again if it
|
|
3386
|
+
* keeps the provider alive.
|
|
3387
|
+
*
|
|
3388
|
+
* A close that throws is logged as a fixed line and not rethrown, so one failure cannot stop the
|
|
3389
|
+
* other shutdown hooks of the application.
|
|
3390
|
+
*/
|
|
3391
|
+
async close() {
|
|
3392
|
+
const pending = __privateGet(this, _transportPromise);
|
|
3393
|
+
__privateSet(this, _transportPromise, null);
|
|
3394
|
+
if (pending === null) {
|
|
3395
|
+
return;
|
|
3396
|
+
}
|
|
3397
|
+
let transport;
|
|
3398
|
+
try {
|
|
3399
|
+
transport = await pending;
|
|
3400
|
+
} catch {
|
|
3401
|
+
return;
|
|
3402
|
+
}
|
|
3403
|
+
try {
|
|
3404
|
+
transport.close?.();
|
|
3405
|
+
} catch {
|
|
3406
|
+
this.logger.warn("[SMTP_CLOSE_FAILED] The transport could not be closed.");
|
|
3407
|
+
}
|
|
3408
|
+
}
|
|
3409
|
+
/** Nest calls this when the application closes; it closes the transport. */
|
|
3410
|
+
async onModuleDestroy() {
|
|
3411
|
+
await this.close();
|
|
3412
|
+
}
|
|
3178
3413
|
/**
|
|
3179
3414
|
* Hands the payload to the transport, converting a transport failure into a plain
|
|
3180
3415
|
* `Error` whose message has the SMTP password scrubbed out. The email body is
|
|
@@ -3203,7 +3438,7 @@ exports.SmtpEmailProvider = class SmtpEmailProvider {
|
|
|
3203
3438
|
return this.withheldFailure(raw, code, redactValuesList);
|
|
3204
3439
|
}
|
|
3205
3440
|
const reason = this.scrubTransportError(raw, payload, redactValuesList);
|
|
3206
|
-
this.logger.warn(`[SMTP_SEND_FAILED] ${reason}`);
|
|
3441
|
+
this.logger.warn(`[SMTP_SEND_FAILED] ${escapeAuditString(reason)}`);
|
|
3207
3442
|
return Object.assign(
|
|
3208
3443
|
new Error(`SMTP send failed: ${reason}`),
|
|
3209
3444
|
code === void 0 ? {} : { code }
|
|
@@ -3379,31 +3614,37 @@ exports.SmtpEmailProvider = class SmtpEmailProvider {
|
|
|
3379
3614
|
* Returns the cached transport, lazily importing `nodemailer` on first use.
|
|
3380
3615
|
* Concurrent first calls share one in-flight initialization promise so the dynamic
|
|
3381
3616
|
* import and creation run exactly once; a failed init is dropped from the cache so
|
|
3382
|
-
* a later call can retry instead of being permanently bricked.
|
|
3617
|
+
* a later call can retry instead of being permanently bricked. It is dropped only while
|
|
3618
|
+
* it is still the cached build: `close()` may have taken it, and a newer send may have
|
|
3619
|
+
* started a build of its own, which a late failure here must not clear.
|
|
3383
3620
|
*
|
|
3384
3621
|
* @returns The initialized transport.
|
|
3385
3622
|
* @throws Error When the configuration is incomplete or `nodemailer` is missing.
|
|
3386
3623
|
*/
|
|
3387
3624
|
getTransport() {
|
|
3388
|
-
|
|
3389
|
-
|
|
3625
|
+
const cached = __privateGet(this, _transportPromise);
|
|
3626
|
+
if (cached !== null) {
|
|
3627
|
+
return cached;
|
|
3628
|
+
}
|
|
3629
|
+
const build = this.createTransport();
|
|
3630
|
+
__privateSet(this, _transportPromise, build);
|
|
3631
|
+
build.catch(() => {
|
|
3632
|
+
if (__privateGet(this, _transportPromise) === build) {
|
|
3633
|
+
__privateSet(this, _transportPromise, null);
|
|
3634
|
+
}
|
|
3635
|
+
});
|
|
3636
|
+
return build;
|
|
3390
3637
|
}
|
|
3391
3638
|
/**
|
|
3392
|
-
* Builds a fresh transport
|
|
3393
|
-
* provider is not permanently bricked by a transient init error.
|
|
3639
|
+
* Builds a fresh transport.
|
|
3394
3640
|
*
|
|
3395
3641
|
* @returns The initialized transport.
|
|
3396
3642
|
* @throws Error When the configuration is incomplete or `nodemailer` is missing.
|
|
3397
3643
|
*/
|
|
3398
3644
|
async createTransport() {
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
return this.instantiate(nodemailer, config);
|
|
3403
|
-
} catch (error) {
|
|
3404
|
-
__privateSet(this, _transportPromise, null);
|
|
3405
|
-
throw error;
|
|
3406
|
-
}
|
|
3645
|
+
const config = this.buildTransportConfig();
|
|
3646
|
+
const nodemailer = await this.loadNodemailer();
|
|
3647
|
+
return this.instantiate(nodemailer, config);
|
|
3407
3648
|
}
|
|
3408
3649
|
/**
|
|
3409
3650
|
* Calls into Nodemailer to build the transport, scrubbing the password out of a
|
|
@@ -3475,7 +3716,8 @@ exports.SmtpEmailProvider = class SmtpEmailProvider {
|
|
|
3475
3716
|
tls: options.tls,
|
|
3476
3717
|
connectionTimeout: options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT_MS,
|
|
3477
3718
|
greetingTimeout: options.greetingTimeout ?? DEFAULT_GREETING_TIMEOUT_MS,
|
|
3478
|
-
socketTimeout: options.socketTimeout ?? DEFAULT_SOCKET_TIMEOUT_MS
|
|
3719
|
+
socketTimeout: options.socketTimeout ?? DEFAULT_SOCKET_TIMEOUT_MS,
|
|
3720
|
+
...poolConfig(options.pool)
|
|
3479
3721
|
};
|
|
3480
3722
|
}
|
|
3481
3723
|
/**
|
|
@@ -3738,7 +3980,7 @@ exports.InMemoryOtpStorage = class InMemoryOtpStorage {
|
|
|
3738
3980
|
this.cooldowns.delete(this.key(tenantId, recipient, purpose));
|
|
3739
3981
|
}
|
|
3740
3982
|
/**
|
|
3741
|
-
* Test helper — wipes all entries and
|
|
3983
|
+
* Test helper — wipes all entries, cooldowns and failure counters. Not part of `IOtpStorage`.
|
|
3742
3984
|
*/
|
|
3743
3985
|
clear() {
|
|
3744
3986
|
this.store.clear();
|
|
@@ -3746,12 +3988,17 @@ exports.InMemoryOtpStorage = class InMemoryOtpStorage {
|
|
|
3746
3988
|
this.failures.clear();
|
|
3747
3989
|
}
|
|
3748
3990
|
/**
|
|
3749
|
-
* Test helper — current map sizes. Not part of `IOtpStorage`.
|
|
3991
|
+
* Test helper — current map sizes. Not part of `IOtpStorage`. Like the cooldowns, a failure
|
|
3992
|
+
* counter that has lapsed stays counted until it is read or cleared.
|
|
3750
3993
|
*
|
|
3751
|
-
* @returns The number of stored OTP entries and
|
|
3994
|
+
* @returns The number of stored OTP entries, cooldowns and failure counters.
|
|
3752
3995
|
*/
|
|
3753
3996
|
size() {
|
|
3754
|
-
return {
|
|
3997
|
+
return {
|
|
3998
|
+
otps: this.store.size,
|
|
3999
|
+
cooldowns: this.cooldowns.size,
|
|
4000
|
+
failures: this.failures.size
|
|
4001
|
+
};
|
|
3755
4002
|
}
|
|
3756
4003
|
/** The unexpired failure count for a key, dropping a lapsed counter. Synchronous. */
|
|
3757
4004
|
liveFailures(key) {
|
|
@@ -3775,6 +4022,7 @@ exports.InMemoryOtpStorage = class InMemoryOtpStorage {
|
|
|
3775
4022
|
* stays unambiguous too.
|
|
3776
4023
|
*/
|
|
3777
4024
|
key(tenantId, recipient, purpose) {
|
|
4025
|
+
assertPurpose("InMemoryOtpStorage", purpose);
|
|
3778
4026
|
return `${hashTenantRecipient(tenantId, recipient)}::${purpose}`;
|
|
3779
4027
|
}
|
|
3780
4028
|
};
|
|
@@ -3994,14 +4242,17 @@ exports.RedisOtpStorage = class RedisOtpStorage {
|
|
|
3994
4242
|
}
|
|
3995
4243
|
/** Builds the OTP entry key: `{namespace}:otp:{purpose}:{hashTenantRecipient(...)}`. */
|
|
3996
4244
|
otpKey(tenantId, recipient, purpose) {
|
|
4245
|
+
assertPurpose("RedisOtpStorage", purpose);
|
|
3997
4246
|
return `${this.namespace}:otp:${purpose}:${hashTenantRecipient(tenantId, recipient)}`;
|
|
3998
4247
|
}
|
|
3999
4248
|
/** Builds the failure-counter key: `{namespace}:otp_fail:{purpose}:{hashTenantRecipient(...)}`. */
|
|
4000
4249
|
failureKey(tenantId, recipient, purpose) {
|
|
4250
|
+
assertPurpose("RedisOtpStorage", purpose);
|
|
4001
4251
|
return `${this.namespace}:otp_fail:${purpose}:${hashTenantRecipient(tenantId, recipient)}`;
|
|
4002
4252
|
}
|
|
4003
4253
|
/** Builds the cooldown key: `{namespace}:otp_cd:{purpose}:{hashTenantRecipient(...)}`. */
|
|
4004
4254
|
cooldownKey(tenantId, recipient, purpose) {
|
|
4255
|
+
assertPurpose("RedisOtpStorage", purpose);
|
|
4005
4256
|
return `${this.namespace}:otp_cd:${purpose}:${hashTenantRecipient(tenantId, recipient)}`;
|
|
4006
4257
|
}
|
|
4007
4258
|
};
|