@oneaddress/partner-sdk 2.7.0 → 2.9.0
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 +172 -0
- package/README.md +203 -62
- package/dist/{chunk-OKAJO4T6.mjs → chunk-OKPEOGR5.mjs} +588 -51
- package/dist/{chunk-CLV6TLSP.mjs → chunk-PP2TM7HN.mjs} +80 -0
- package/dist/edge.d.mts +1 -1
- package/dist/edge.d.ts +1 -1
- package/dist/edge.js +591 -42
- package/dist/edge.mjs +28 -2
- package/dist/index.d.mts +452 -53
- package/dist/index.d.ts +452 -53
- package/dist/index.js +691 -49
- package/dist/index.mjs +52 -2
- package/dist/{src-FPYBM6X6.mjs → src-P3LR7F4A.mjs} +1 -1
- package/package.json +2 -2
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
isFreshTimestamp,
|
|
6
6
|
isSafeWebhookUrl,
|
|
7
7
|
verifyRegistryHead
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-PP2TM7HN.mjs";
|
|
9
9
|
|
|
10
10
|
// src/index.ts
|
|
11
11
|
import crypto from "crypto";
|
|
@@ -14,6 +14,19 @@ import crypto from "crypto";
|
|
|
14
14
|
var CURRENT_VERSION = "2026.1";
|
|
15
15
|
var SUPPORTED_VERSIONS = ["2026.1"];
|
|
16
16
|
|
|
17
|
+
// ../protocol/src/notice-period.ts
|
|
18
|
+
var ISO_DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
19
|
+
function isIsoDate(value) {
|
|
20
|
+
if (typeof value !== "string") return false;
|
|
21
|
+
const m = ISO_DATE.exec(value);
|
|
22
|
+
if (!m) return false;
|
|
23
|
+
const d = new Date(Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3])));
|
|
24
|
+
return toIso(d) === value;
|
|
25
|
+
}
|
|
26
|
+
function toIso(d) {
|
|
27
|
+
return d.toISOString().slice(0, 10);
|
|
28
|
+
}
|
|
29
|
+
|
|
17
30
|
// ../protocol/src/d5-body.ts
|
|
18
31
|
var D5_BODY_ALWAYS = [
|
|
19
32
|
"event",
|
|
@@ -23,7 +36,6 @@ var D5_BODY_ALWAYS = [
|
|
|
23
36
|
"partner_id",
|
|
24
37
|
"session_envelope",
|
|
25
38
|
"session_key_share",
|
|
26
|
-
"stripe_session_id",
|
|
27
39
|
"timestamp",
|
|
28
40
|
"update_id",
|
|
29
41
|
"user_id"
|
|
@@ -44,7 +56,18 @@ var D5_BODY_CONDITIONAL = {
|
|
|
44
56
|
*/
|
|
45
57
|
id: "key-crossover copy only",
|
|
46
58
|
/** The sibling marker naming the other key_ids this update was sealed to. */
|
|
47
|
-
crossover_key_ids: "key-crossover copy only"
|
|
59
|
+
crossover_key_ids: "key-crossover copy only",
|
|
60
|
+
/**
|
|
61
|
+
* The day the change takes effect, `YYYY-MM-DD` (31 Oct 2026). Set on every
|
|
62
|
+
* dispatch made for a SCHEDULED MOVE, which is its move date. A scheduled
|
|
63
|
+
* move sends each recipient early enough for its own notice period
|
|
64
|
+
* (`resolveRecipientSendPlan` in `notice-period.ts`), so the update can
|
|
65
|
+
* arrive days before the customer moves; a recipient that treated it as
|
|
66
|
+
* immediate could start charging at the new address from the day it arrived.
|
|
67
|
+
* Absent on an immediate update, which takes effect on receipt. Optional for
|
|
68
|
+
* receivers: an SDK that does not know the field ignores it.
|
|
69
|
+
*/
|
|
70
|
+
effective_date: "scheduled move only"
|
|
48
71
|
};
|
|
49
72
|
var D5_BODY_DRILL_ONLY = ["conformance"];
|
|
50
73
|
var D5_BODY_ADDED_AT_SEND = ["dispatch_id"];
|
|
@@ -79,6 +102,110 @@ function pullSigningStringV2(timestamp, method, pathAndQuery, body = "") {
|
|
|
79
102
|
return `v2.${timestamp}.${method.toUpperCase()}.${pathAndQuery}.${body}`;
|
|
80
103
|
}
|
|
81
104
|
|
|
105
|
+
// ../protocol/src/loa-v2.ts
|
|
106
|
+
var LOA_V2_VERSION_TAG = "oneaddress-loa/2";
|
|
107
|
+
var LOA_V2_LEGACY_ENVELOPE_TAG = "oneaddress-legacy-envelope/1";
|
|
108
|
+
var LOA_V2_FIELD_ORDER = [
|
|
109
|
+
"consumer_name",
|
|
110
|
+
"authorisation_date",
|
|
111
|
+
"scope",
|
|
112
|
+
"update_uid",
|
|
113
|
+
"partner_id",
|
|
114
|
+
"envelope_sha256"
|
|
115
|
+
];
|
|
116
|
+
var LOA_MAX_TYPED_ALIASES = 3;
|
|
117
|
+
var LoaV2FieldError = class extends Error {
|
|
118
|
+
constructor(field, reason) {
|
|
119
|
+
super(`LOA v2 field ${field} ${reason}`);
|
|
120
|
+
this.field = field;
|
|
121
|
+
this.name = "LoaV2FieldError";
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
var CONTROL_RE = /[\u0000-\u001F\u007F]/;
|
|
125
|
+
var LONE_SURROGATE_RE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
|
|
126
|
+
function utf8Length(s) {
|
|
127
|
+
return new TextEncoder().encode(s).length;
|
|
128
|
+
}
|
|
129
|
+
function encodeField(name, value) {
|
|
130
|
+
if (typeof value !== "string") throw new LoaV2FieldError(name, "is not a string");
|
|
131
|
+
if (CONTROL_RE.test(value)) throw new LoaV2FieldError(name, "contains a control character");
|
|
132
|
+
if (LONE_SURROGATE_RE.test(value)) throw new LoaV2FieldError(name, "is not well-formed Unicode");
|
|
133
|
+
return `${utf8Length(value)}:${value}
|
|
134
|
+
`;
|
|
135
|
+
}
|
|
136
|
+
function canonicalLoaV2(fields) {
|
|
137
|
+
let out = encodeField("version", LOA_V2_VERSION_TAG);
|
|
138
|
+
for (const f of LOA_V2_FIELD_ORDER) out += encodeField(f, fields[f]);
|
|
139
|
+
const aliases = fields.consumer_typed_aliases;
|
|
140
|
+
if (!Array.isArray(aliases)) throw new LoaV2FieldError("consumer_typed_aliases", "is not a list");
|
|
141
|
+
if (aliases.length > LOA_MAX_TYPED_ALIASES) {
|
|
142
|
+
throw new LoaV2FieldError("consumer_typed_aliases", `has more than ${LOA_MAX_TYPED_ALIASES} entries`);
|
|
143
|
+
}
|
|
144
|
+
out += encodeField("consumer_typed_aliases", String(aliases.length));
|
|
145
|
+
for (const a of aliases) out += encodeField("consumer_typed_aliases", a);
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
function loaV2EnvelopeDigestInput(envelope) {
|
|
149
|
+
if (typeof envelope === "string") return envelope;
|
|
150
|
+
if (!envelope || typeof envelope !== "object") {
|
|
151
|
+
throw new LoaV2FieldError("envelope", "is neither a session_envelope string nor an address_encrypted object");
|
|
152
|
+
}
|
|
153
|
+
return encodeField("tag", LOA_V2_LEGACY_ENVELOPE_TAG) + encodeField("ephemeralPublicKey", envelope.ephemeralPublicKey) + encodeField("iv", envelope.iv) + encodeField("ciphertext", envelope.ciphertext) + encodeField("hkdfSalt", envelope.hkdfSalt ?? "");
|
|
154
|
+
}
|
|
155
|
+
var ASCII_WS_RE = /[ \t\n\v\f\r]+/;
|
|
156
|
+
var MARK_RE = /\p{M}/gu;
|
|
157
|
+
function normaliseLoaName(name) {
|
|
158
|
+
return name.normalize("NFKD").replace(MARK_RE, "").toLowerCase().replace(/\u03C2/g, "\u03C3").split(ASCII_WS_RE).filter((t) => t.length > 0).join(" ");
|
|
159
|
+
}
|
|
160
|
+
function loaSurname(name) {
|
|
161
|
+
const tokens = normaliseLoaName(name).split(" ").filter((t) => t.length > 0);
|
|
162
|
+
return tokens.length > 0 ? tokens[tokens.length - 1] : "";
|
|
163
|
+
}
|
|
164
|
+
function loaFirstName(name) {
|
|
165
|
+
const tokens = normaliseLoaName(name).split(" ").filter((t) => t.length > 0);
|
|
166
|
+
return tokens.length > 0 ? tokens[0] : "";
|
|
167
|
+
}
|
|
168
|
+
function loaNamesMatch(a, b) {
|
|
169
|
+
const na = normaliseLoaName(a);
|
|
170
|
+
return na.length > 0 && na === normaliseLoaName(b);
|
|
171
|
+
}
|
|
172
|
+
var LOA_MIN_SHORT_FORM_LETTERS = 3;
|
|
173
|
+
function codePoints(s) {
|
|
174
|
+
return Array.from(s).length;
|
|
175
|
+
}
|
|
176
|
+
function isAcceptableAlias(verifiedName, alias) {
|
|
177
|
+
const vFirst = loaFirstName(verifiedName);
|
|
178
|
+
const aFirst = loaFirstName(alias);
|
|
179
|
+
if (!vFirst || !aFirst) return false;
|
|
180
|
+
if (aFirst === vFirst) return true;
|
|
181
|
+
const [shorter, longer] = codePoints(aFirst) <= codePoints(vFirst) ? [aFirst, vFirst] : [vFirst, aFirst];
|
|
182
|
+
return codePoints(shorter) >= LOA_MIN_SHORT_FORM_LETTERS && longer.startsWith(shorter);
|
|
183
|
+
}
|
|
184
|
+
function acceptTypedAliases(verifiedName, aliases) {
|
|
185
|
+
const out = [];
|
|
186
|
+
if (!Array.isArray(aliases)) return out;
|
|
187
|
+
const seen = /* @__PURE__ */ new Set([normaliseLoaName(verifiedName)]);
|
|
188
|
+
for (const a of aliases) {
|
|
189
|
+
if (out.length >= LOA_MAX_TYPED_ALIASES) break;
|
|
190
|
+
if (typeof a !== "string") continue;
|
|
191
|
+
if (CONTROL_RE.test(a) || LONE_SURROGATE_RE.test(a)) continue;
|
|
192
|
+
const n = normaliseLoaName(a);
|
|
193
|
+
if (!n || seen.has(n)) continue;
|
|
194
|
+
if (!isAcceptableAlias(verifiedName, a)) continue;
|
|
195
|
+
seen.add(n);
|
|
196
|
+
out.push(a);
|
|
197
|
+
}
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
var LOA_V2_REFUSALS = [
|
|
201
|
+
"loa_v2_malformed",
|
|
202
|
+
"loa_v2_signature_invalid",
|
|
203
|
+
"loa_v2_partner_mismatch",
|
|
204
|
+
"loa_v2_update_mismatch",
|
|
205
|
+
"loa_v2_envelope_mismatch",
|
|
206
|
+
"loa_v2_name_mismatch"
|
|
207
|
+
];
|
|
208
|
+
|
|
82
209
|
// src/app-hosts.ts
|
|
83
210
|
function allowedAppHosts() {
|
|
84
211
|
const hosts = ["oneaddress.io", "app.oneaddress.io"];
|
|
@@ -87,7 +214,7 @@ function allowedAppHosts() {
|
|
|
87
214
|
}
|
|
88
215
|
return new Set(hosts);
|
|
89
216
|
}
|
|
90
|
-
function resolveAppOrigin(baseUrl, label) {
|
|
217
|
+
function resolveAppOrigin(baseUrl, label, extraHosts) {
|
|
91
218
|
let parsed;
|
|
92
219
|
try {
|
|
93
220
|
parsed = new URL(baseUrl);
|
|
@@ -95,6 +222,9 @@ function resolveAppOrigin(baseUrl, label) {
|
|
|
95
222
|
throw new Error(`[OneAddress SDK] ${label}: appBaseUrl is not a valid URL`);
|
|
96
223
|
}
|
|
97
224
|
const allowed = allowedAppHosts();
|
|
225
|
+
for (const h of extraHosts ?? []) {
|
|
226
|
+
if (typeof h === "string" && h.trim() !== "") allowed.add(h.trim().toLowerCase());
|
|
227
|
+
}
|
|
98
228
|
if (!allowed.has(parsed.hostname)) {
|
|
99
229
|
throw new Error(
|
|
100
230
|
`[OneAddress SDK] ${label}: appBaseUrl host must be one of: ${[...allowed].join(", ")}`
|
|
@@ -126,26 +256,38 @@ function surnameVariants(token) {
|
|
|
126
256
|
if (!token.includes("-")) return [token];
|
|
127
257
|
return [token, ...token.split("-").filter(Boolean)];
|
|
128
258
|
}
|
|
259
|
+
var MAX_ALIASES = 10;
|
|
260
|
+
var MAX_TOKENS_PER_NAME = 8;
|
|
261
|
+
var MAX_POOL_TOKENS = 24;
|
|
129
262
|
function namePoolMatches(providedNames, storedName) {
|
|
130
263
|
if (isEmailLike(storedName)) return false;
|
|
131
|
-
const provided = providedNames.filter((n) =>
|
|
264
|
+
const provided = providedNames.filter((n) => typeof n === "string" && n !== "" && !isEmailLike(n));
|
|
132
265
|
const primary = tokenize(provided[0] ?? "");
|
|
133
|
-
if (primary.length === 0) return false;
|
|
266
|
+
if (primary.length === 0 || primary.length > MAX_TOKENS_PER_NAME) return false;
|
|
267
|
+
const aliases = provided.slice(1, 1 + MAX_ALIASES).map(tokenize).filter((t) => t.length > 0 && t.length <= MAX_TOKENS_PER_NAME);
|
|
268
|
+
const distinct = new Set(primary);
|
|
269
|
+
for (const t of aliases) for (const tok of t) distinct.add(tok);
|
|
270
|
+
if (distinct.size > MAX_POOL_TOKENS) return false;
|
|
134
271
|
const surnames = new Set(surnameVariants(primary[primary.length - 1]));
|
|
135
|
-
for (
|
|
136
|
-
const
|
|
137
|
-
if (t.length >= 2) for (const v of surnameVariants(
|
|
138
|
-
}
|
|
139
|
-
const
|
|
140
|
-
for (const
|
|
141
|
-
const
|
|
272
|
+
for (const t of aliases) {
|
|
273
|
+
const last = t[t.length - 1];
|
|
274
|
+
if (t.length >= 2 && last.length >= 2) for (const v of surnameVariants(last)) surnames.add(v);
|
|
275
|
+
}
|
|
276
|
+
const givenSet = /* @__PURE__ */ new Set();
|
|
277
|
+
for (const tok of primary) if (!surnames.has(tok)) givenSet.add(tok);
|
|
278
|
+
for (const t of aliases) for (const tok of t) if (tok.length >= 2 && !surnames.has(tok)) givenSet.add(tok);
|
|
279
|
+
const given = [...givenSet];
|
|
280
|
+
const fullGiven = given.filter((g) => g.length >= 2);
|
|
142
281
|
const stored = tokenize(storedName);
|
|
143
|
-
if (stored.length
|
|
282
|
+
if (stored.length < 2) return false;
|
|
144
283
|
const storedSurname = stored[stored.length - 1];
|
|
145
284
|
const storedGiven = stored.slice(0, -1);
|
|
146
285
|
if (!surnameVariants(storedSurname).some((v) => surnames.has(v))) return false;
|
|
147
286
|
const covered = (tok) => given.includes(tok) || tok.length === 1 && given.some((g) => g[0] === tok) || given.some((g) => g.length === 1 && g === tok[0]);
|
|
148
|
-
|
|
287
|
+
if (!storedGiven.every(covered)) return false;
|
|
288
|
+
return storedGiven.some(
|
|
289
|
+
(tok) => fullGiven.includes(tok) || tok.length === 1 && fullGiven.some((g) => g[0] === tok)
|
|
290
|
+
);
|
|
149
291
|
}
|
|
150
292
|
function findMatchingStoredName(providedNames, storedCandidates) {
|
|
151
293
|
return storedCandidates.find((candidate) => namePoolMatches(providedNames, candidate));
|
|
@@ -576,6 +718,18 @@ async function checkRegistryHead(opts) {
|
|
|
576
718
|
headExpiresAt: expires
|
|
577
719
|
};
|
|
578
720
|
}
|
|
721
|
+
if (expectedKeyId) {
|
|
722
|
+
return {
|
|
723
|
+
verdict: "names-key-we-lack",
|
|
724
|
+
checked: true,
|
|
725
|
+
alarm: true,
|
|
726
|
+
detail: `the head names ${named} as the current key for this partner id, not ${expectedKeyId}, the key this recipient is configured with. If you did not rotate to ${named}, address envelopes are being sealed to a key you cannot read: contact OneAddress through a channel that is not oneaddress.io. If you did rotate, update keyId, or pass holdsKey so a key you hold is recognised.`,
|
|
727
|
+
namedKeyId: named,
|
|
728
|
+
expectedKeyId,
|
|
729
|
+
headIssuedAt: issued,
|
|
730
|
+
headExpiresAt: expires
|
|
731
|
+
};
|
|
732
|
+
}
|
|
579
733
|
return {
|
|
580
734
|
verdict: "key-identity-unknown",
|
|
581
735
|
checked: false,
|
|
@@ -590,14 +744,21 @@ async function checkRegistryHead(opts) {
|
|
|
590
744
|
|
|
591
745
|
// src/index.ts
|
|
592
746
|
var IDEMPOTENCY_DIALECTS = ["postgres", "sqlite", "mysql", "mssql"];
|
|
747
|
+
var DEFAULT_IDEMPOTENCY_LEASE_SECS = 120;
|
|
593
748
|
function createSqlIdempotencyStore(opts) {
|
|
594
749
|
const table = opts.table ?? "oneaddress_dispatches";
|
|
595
750
|
const dialect = opts.dialect ?? "postgres";
|
|
751
|
+
const leaseSeconds = opts.leaseSeconds ?? DEFAULT_IDEMPOTENCY_LEASE_SECS;
|
|
596
752
|
if (!IDEMPOTENCY_DIALECTS.includes(dialect)) {
|
|
597
753
|
throw new Error(
|
|
598
754
|
`[OneAddress SDK] createSqlIdempotencyStore: unknown dialect "${dialect}". Supported: ${IDEMPOTENCY_DIALECTS.join(", ")}.`
|
|
599
755
|
);
|
|
600
756
|
}
|
|
757
|
+
if (typeof leaseSeconds !== "number" || !Number.isSafeInteger(leaseSeconds) || leaseSeconds < 0 || leaseSeconds > 86400) {
|
|
758
|
+
throw new Error(
|
|
759
|
+
`[OneAddress SDK] createSqlIdempotencyStore: leaseSeconds must be a whole number from 0 to 86400, got ${String(leaseSeconds)}`
|
|
760
|
+
);
|
|
761
|
+
}
|
|
601
762
|
const defaultPh = dialect === "postgres" ? (n) => `$${n}` : dialect === "mssql" ? (n) => `@p${n}` : () => "?";
|
|
602
763
|
const ph = opts.placeholder ?? defaultPh;
|
|
603
764
|
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)) {
|
|
@@ -616,7 +777,26 @@ function createSqlIdempotencyStore(opts) {
|
|
|
616
777
|
[dispatchId]
|
|
617
778
|
);
|
|
618
779
|
if (tokenOf(rows[0]) === token) return "fresh";
|
|
619
|
-
|
|
780
|
+
if (completedOf(rows[0])) return "duplicate";
|
|
781
|
+
const takeover = crypto.randomBytes(16).toString("hex");
|
|
782
|
+
await opts.query(
|
|
783
|
+
`UPDATE ${table} SET received_at = CURRENT_TIMESTAMP, claim_token = ${ph(1)} WHERE dispatch_id = ${ph(2)} AND completed_at IS NULL AND ${staleClaim(3)}`,
|
|
784
|
+
leaseParams([takeover, dispatchId])
|
|
785
|
+
);
|
|
786
|
+
const after = await opts.query(
|
|
787
|
+
`SELECT claim_token, completed_at FROM ${table} WHERE dispatch_id = ${ph(1)}`,
|
|
788
|
+
[dispatchId]
|
|
789
|
+
);
|
|
790
|
+
if (tokenOf(after[0]) === takeover) return "retry-after-incomplete";
|
|
791
|
+
return completedOf(after[0]) ? "duplicate" : "in-progress";
|
|
792
|
+
}
|
|
793
|
+
function staleClaim(n) {
|
|
794
|
+
if (leaseSeconds === 0) return "1 = 1";
|
|
795
|
+
const cutoff = dialect === "postgres" ? `received_at < CURRENT_TIMESTAMP - make_interval(secs => ${ph(n)})` : dialect === "sqlite" ? `julianday(received_at) < julianday('now') - (${ph(n)} / 86400.0)` : dialect === "mysql" ? `received_at < CURRENT_TIMESTAMP - INTERVAL ${ph(n)} SECOND` : `received_at < DATEADD(second, -${ph(n)}, CURRENT_TIMESTAMP)`;
|
|
796
|
+
return `(received_at IS NULL OR ${cutoff})`;
|
|
797
|
+
}
|
|
798
|
+
function leaseParams(params) {
|
|
799
|
+
return leaseSeconds === 0 ? params : [...params, leaseSeconds];
|
|
620
800
|
}
|
|
621
801
|
async function setByUpsert(dispatchId) {
|
|
622
802
|
if (dialect === "mysql") {
|
|
@@ -650,11 +830,22 @@ function createSqlIdempotencyStore(opts) {
|
|
|
650
830
|
[dispatchId]
|
|
651
831
|
);
|
|
652
832
|
if (inserted.length > 0) return "fresh";
|
|
833
|
+
const took = await opts.query(
|
|
834
|
+
`UPDATE ${table} SET received_at = CURRENT_TIMESTAMP WHERE dispatch_id = ${ph(1)} AND completed_at IS NULL AND ${staleClaim(2)} RETURNING dispatch_id`,
|
|
835
|
+
leaseParams([dispatchId])
|
|
836
|
+
);
|
|
837
|
+
if (took.length > 0) return "retry-after-incomplete";
|
|
653
838
|
const rows = await opts.query(
|
|
654
839
|
`SELECT completed_at FROM ${table} WHERE dispatch_id = ${ph(1)}`,
|
|
655
840
|
[dispatchId]
|
|
656
841
|
);
|
|
657
|
-
return completedOf(rows[0]) ? "duplicate" : "
|
|
842
|
+
return completedOf(rows[0]) ? "duplicate" : "in-progress";
|
|
843
|
+
},
|
|
844
|
+
async release(dispatchId) {
|
|
845
|
+
await opts.query(
|
|
846
|
+
`DELETE FROM ${table} WHERE dispatch_id = ${ph(1)} AND completed_at IS NULL`,
|
|
847
|
+
[dispatchId]
|
|
848
|
+
);
|
|
658
849
|
},
|
|
659
850
|
async complete(dispatchId) {
|
|
660
851
|
await opts.query(
|
|
@@ -772,7 +963,7 @@ async function decryptAddress(payload, privateKeyPem, partnerId) {
|
|
|
772
963
|
return JSON.parse(new TextDecoder().decode(plaintext));
|
|
773
964
|
}
|
|
774
965
|
async function decryptSession(share, envelopeB64, privateKeyPem, partnerId, expectedAlg) {
|
|
775
|
-
const { decryptSession: decryptSessionCrypto } = await import("./src-
|
|
966
|
+
const { decryptSession: decryptSessionCrypto } = await import("./src-P3LR7F4A.mjs");
|
|
776
967
|
return decryptSessionCrypto(
|
|
777
968
|
share,
|
|
778
969
|
envelopeB64,
|
|
@@ -840,6 +1031,7 @@ async function transformSession(share, envelopeB64, privateKeyPem, partnerId, re
|
|
|
840
1031
|
);
|
|
841
1032
|
}
|
|
842
1033
|
var OA_APP_BASE_URL2 = "https://oneaddress.io";
|
|
1034
|
+
var CONFIRM_PATH = "/api/confirm";
|
|
843
1035
|
async function confirmUpdate(opts) {
|
|
844
1036
|
const {
|
|
845
1037
|
partnerId,
|
|
@@ -854,6 +1046,13 @@ async function confirmUpdate(opts) {
|
|
|
854
1046
|
} = opts;
|
|
855
1047
|
if (!partnerId) return { ok: false, status: 0, error: "partnerId is required" };
|
|
856
1048
|
if (!secret) return { ok: false, status: 0, error: "secret is required" };
|
|
1049
|
+
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(partnerId)) {
|
|
1050
|
+
return {
|
|
1051
|
+
ok: false,
|
|
1052
|
+
status: 0,
|
|
1053
|
+
error: `partnerId must be your partner UUID from the portal, received ${JSON.stringify(partnerId)}`
|
|
1054
|
+
};
|
|
1055
|
+
}
|
|
857
1056
|
const id = typeof dispatchId === "string" ? Number(dispatchId) : dispatchId;
|
|
858
1057
|
if (!Number.isInteger(id) || id <= 0) {
|
|
859
1058
|
return {
|
|
@@ -871,18 +1070,22 @@ async function confirmUpdate(opts) {
|
|
|
871
1070
|
...memberName !== void 0 ? { member_name: memberName } : {}
|
|
872
1071
|
});
|
|
873
1072
|
const timestamp = String(Math.floor(Date.now() / 1e3));
|
|
874
|
-
const signature = crypto.createHmac("sha256", secret).update(
|
|
875
|
-
let
|
|
876
|
-
|
|
1073
|
+
const signature = crypto.createHmac("sha256", secret).update(pullSigningStringV2(timestamp, "POST", CONFIRM_PATH, body)).digest("hex");
|
|
1074
|
+
let origin;
|
|
1075
|
+
try {
|
|
1076
|
+
origin = resolveAppOrigin(appBaseUrl, "confirmUpdate", opts.allowedHosts);
|
|
1077
|
+
} catch (err) {
|
|
1078
|
+
return { ok: false, status: 0, error: err instanceof Error ? err.message : String(err) };
|
|
1079
|
+
}
|
|
877
1080
|
let res;
|
|
878
1081
|
try {
|
|
879
|
-
res = await fetchImpl(`${
|
|
1082
|
+
res = await fetchImpl(`${origin}${CONFIRM_PATH}`, {
|
|
880
1083
|
method: "POST",
|
|
881
1084
|
headers: {
|
|
882
1085
|
"Content-Type": "application/json",
|
|
883
|
-
"Authorization": `Bearer ${secret}`,
|
|
884
1086
|
"X-OneAddress-Timestamp": timestamp,
|
|
885
|
-
"X-OneAddress-Signature": signature
|
|
1087
|
+
"X-OneAddress-Signature": signature,
|
|
1088
|
+
[PULL_SIGNATURE_VERSION_HEADER]: "2"
|
|
886
1089
|
},
|
|
887
1090
|
// The SAME string that was signed. Not an object, and not re-stringified.
|
|
888
1091
|
body
|
|
@@ -908,6 +1111,48 @@ async function fetchLoaPublicKey(appBaseUrl = OA_APP_BASE_URL2) {
|
|
|
908
1111
|
}
|
|
909
1112
|
return res.text();
|
|
910
1113
|
}
|
|
1114
|
+
var LOA_PUBLIC_KEY_CACHE_MS = 24 * 60 * 60 * 1e3;
|
|
1115
|
+
var loaKeyCache = /* @__PURE__ */ new Map();
|
|
1116
|
+
var loaKeyInFlight = /* @__PURE__ */ new Map();
|
|
1117
|
+
var LoaPublicKeyUnavailableError = class extends Error {
|
|
1118
|
+
constructor(from, cause) {
|
|
1119
|
+
super(
|
|
1120
|
+
`[OneAddress SDK] could not fetch OneAddress's LOA public key from ${from} (${cause}). requireLoa is on (the default since 2.8.0), so this dispatch is refused and OneAddress will retry it. Pin the key with loaPublicKeyPem (from fetchLoaPublicKey), or set requireLoa: false to process dispatches without verifying consent.`
|
|
1121
|
+
);
|
|
1122
|
+
this.from = from;
|
|
1123
|
+
this.name = "LoaPublicKeyUnavailableError";
|
|
1124
|
+
}
|
|
1125
|
+
};
|
|
1126
|
+
async function cachedLoaPublicKey(appBaseUrl = OA_APP_BASE_URL2, now = Date.now()) {
|
|
1127
|
+
const origin = resolveAppOrigin(appBaseUrl, "cachedLoaPublicKey");
|
|
1128
|
+
const held = loaKeyCache.get(origin);
|
|
1129
|
+
if (held && now - held.at < LOA_PUBLIC_KEY_CACHE_MS) return held.pem;
|
|
1130
|
+
let pending = loaKeyInFlight.get(origin);
|
|
1131
|
+
if (!pending) {
|
|
1132
|
+
pending = (async () => {
|
|
1133
|
+
const pem = (await fetchLoaPublicKey(origin)).trim();
|
|
1134
|
+
if (!pem.includes("-----BEGIN PUBLIC KEY-----")) {
|
|
1135
|
+
throw new Error("the response is not a PEM public key");
|
|
1136
|
+
}
|
|
1137
|
+
loaKeyCache.set(origin, { pem, at: Date.now() });
|
|
1138
|
+
return pem;
|
|
1139
|
+
})().finally(() => loaKeyInFlight.delete(origin));
|
|
1140
|
+
loaKeyInFlight.set(origin, pending);
|
|
1141
|
+
}
|
|
1142
|
+
try {
|
|
1143
|
+
return await pending;
|
|
1144
|
+
} catch (err) {
|
|
1145
|
+
if (held) return held.pem;
|
|
1146
|
+
throw new LoaPublicKeyUnavailableError(
|
|
1147
|
+
`${origin}/.well-known/oneaddress-loa-key.pem`,
|
|
1148
|
+
err instanceof Error ? err.message : String(err)
|
|
1149
|
+
);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
function _resetLoaKeyCacheForTests() {
|
|
1153
|
+
loaKeyCache.clear();
|
|
1154
|
+
loaKeyInFlight.clear();
|
|
1155
|
+
}
|
|
911
1156
|
async function verifyLOA(loa, publicKeyPem) {
|
|
912
1157
|
if (loa.algorithm !== "ECDSA-P256") {
|
|
913
1158
|
console.warn(
|
|
@@ -987,6 +1232,91 @@ async function decryptLoaEncrypted(loaEncrypted, privateKeyPem, partnerId, expec
|
|
|
987
1232
|
);
|
|
988
1233
|
return data;
|
|
989
1234
|
}
|
|
1235
|
+
function hasLoaV2(loa) {
|
|
1236
|
+
return typeof loa.oneaddress_signature_v2 === "string";
|
|
1237
|
+
}
|
|
1238
|
+
function canonicalD5LoaFormV2(loa) {
|
|
1239
|
+
return canonicalLoaV2({
|
|
1240
|
+
consumer_name: loa.consumer_name,
|
|
1241
|
+
authorisation_date: loa.authorisation_date,
|
|
1242
|
+
scope: loa.scope,
|
|
1243
|
+
update_uid: loa.update_uid,
|
|
1244
|
+
partner_id: loa.partner_id,
|
|
1245
|
+
envelope_sha256: loa.envelope_sha256,
|
|
1246
|
+
consumer_typed_aliases: loa.consumer_typed_aliases
|
|
1247
|
+
});
|
|
1248
|
+
}
|
|
1249
|
+
function loaEnvelopeSha256(envelope) {
|
|
1250
|
+
return crypto.createHash("sha256").update(loaV2EnvelopeDigestInput(envelope), "utf8").digest("hex");
|
|
1251
|
+
}
|
|
1252
|
+
async function verifyD5LOAv2(loa, publicKeyPem) {
|
|
1253
|
+
if (loa.signature_algorithm !== "ECDSA-P256" || !hasLoaV2(loa)) return false;
|
|
1254
|
+
let text;
|
|
1255
|
+
try {
|
|
1256
|
+
text = canonicalD5LoaFormV2(loa);
|
|
1257
|
+
} catch {
|
|
1258
|
+
return false;
|
|
1259
|
+
}
|
|
1260
|
+
try {
|
|
1261
|
+
const key = await crypto.subtle.importKey(
|
|
1262
|
+
"spki",
|
|
1263
|
+
pemToBuffer(publicKeyPem),
|
|
1264
|
+
{ name: "ECDSA", namedCurve: "P-256" },
|
|
1265
|
+
false,
|
|
1266
|
+
["verify"]
|
|
1267
|
+
);
|
|
1268
|
+
return await crypto.subtle.verify(
|
|
1269
|
+
{ name: "ECDSA", hash: "SHA-256" },
|
|
1270
|
+
key,
|
|
1271
|
+
Buffer.from(loa.oneaddress_signature_v2, "hex"),
|
|
1272
|
+
new TextEncoder().encode(text)
|
|
1273
|
+
);
|
|
1274
|
+
} catch {
|
|
1275
|
+
return false;
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
function typedAliasesOf(loa) {
|
|
1279
|
+
if (!hasLoaV2(loa)) return [];
|
|
1280
|
+
return acceptTypedAliases(loa.consumer_name, loa.consumer_typed_aliases);
|
|
1281
|
+
}
|
|
1282
|
+
function bodyUpdateIdString(v) {
|
|
1283
|
+
if (typeof v === "string") return v;
|
|
1284
|
+
if (typeof v === "number" && Number.isSafeInteger(v)) return String(v);
|
|
1285
|
+
return null;
|
|
1286
|
+
}
|
|
1287
|
+
async function checkLoaV2(loa, ctx) {
|
|
1288
|
+
if (!hasLoaV2(loa) || typeof loa.partner_id !== "string" || typeof loa.envelope_sha256 !== "string") {
|
|
1289
|
+
return "loa_v2_malformed";
|
|
1290
|
+
}
|
|
1291
|
+
try {
|
|
1292
|
+
canonicalD5LoaFormV2(loa);
|
|
1293
|
+
} catch {
|
|
1294
|
+
return "loa_v2_malformed";
|
|
1295
|
+
}
|
|
1296
|
+
if (ctx.publicKeyPem !== void 0 && !await verifyD5LOAv2(loa, ctx.publicKeyPem)) {
|
|
1297
|
+
return "loa_v2_signature_invalid";
|
|
1298
|
+
}
|
|
1299
|
+
if (loa.partner_id !== ctx.partnerId) return "loa_v2_partner_mismatch";
|
|
1300
|
+
if (bodyUpdateIdString(ctx.updateId) !== loa.update_uid) return "loa_v2_update_mismatch";
|
|
1301
|
+
let digest;
|
|
1302
|
+
try {
|
|
1303
|
+
digest = loaEnvelopeSha256(ctx.envelope);
|
|
1304
|
+
} catch {
|
|
1305
|
+
return "loa_v2_envelope_mismatch";
|
|
1306
|
+
}
|
|
1307
|
+
if (digest !== loa.envelope_sha256) return "loa_v2_envelope_mismatch";
|
|
1308
|
+
if (typeof ctx.envelopeVerifiedName !== "string" || !loaNamesMatch(ctx.envelopeVerifiedName, loa.consumer_name)) {
|
|
1309
|
+
return "loa_v2_name_mismatch";
|
|
1310
|
+
}
|
|
1311
|
+
return null;
|
|
1312
|
+
}
|
|
1313
|
+
var LoaV2RefusedError = class extends Error {
|
|
1314
|
+
constructor(code) {
|
|
1315
|
+
super(`[OneAddress SDK] D5 LOA v2 refused (${code}); refusing rather than applying an update the consent record does not cover`);
|
|
1316
|
+
this.code = code;
|
|
1317
|
+
this.name = "LoaV2RefusedError";
|
|
1318
|
+
}
|
|
1319
|
+
};
|
|
990
1320
|
var ADDRESS_REQUIRED_FIELDS = [
|
|
991
1321
|
"street",
|
|
992
1322
|
"suburb",
|
|
@@ -994,19 +1324,60 @@ var ADDRESS_REQUIRED_FIELDS = [
|
|
|
994
1324
|
"postcode",
|
|
995
1325
|
"country"
|
|
996
1326
|
];
|
|
1327
|
+
var MAX_ADDRESS_FIELD_CHARS = 512;
|
|
997
1328
|
function validateAddress(raw) {
|
|
998
|
-
if (typeof raw !== "object" || raw === null) {
|
|
1329
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
999
1330
|
throw new Error("[OneAddress SDK] Decrypted address payload is not an object");
|
|
1000
1331
|
}
|
|
1001
1332
|
const obj = raw;
|
|
1002
1333
|
for (const field of ADDRESS_REQUIRED_FIELDS) {
|
|
1003
|
-
|
|
1334
|
+
const value = obj[field];
|
|
1335
|
+
if (typeof value !== "string" || value === "") {
|
|
1336
|
+
throw new Error(
|
|
1337
|
+
`[OneAddress SDK] Decrypted address is missing required field "${field}". This may indicate a decryption error or a schema change - update @oneaddress/partner-sdk.`
|
|
1338
|
+
);
|
|
1339
|
+
}
|
|
1340
|
+
if (value.length > MAX_ADDRESS_FIELD_CHARS) {
|
|
1004
1341
|
throw new Error(
|
|
1005
|
-
`[OneAddress SDK] Decrypted address
|
|
1342
|
+
`[OneAddress SDK] Decrypted address field "${field}" is ${value.length} characters, over the ${MAX_ADDRESS_FIELD_CHARS} this SDK accepts; refusing rather than truncating`
|
|
1006
1343
|
);
|
|
1007
1344
|
}
|
|
1008
1345
|
}
|
|
1009
|
-
|
|
1346
|
+
const rawUpdatedAt = obj.updated_at;
|
|
1347
|
+
let updatedAt;
|
|
1348
|
+
if (rawUpdatedAt === void 0 || rawUpdatedAt === null) updatedAt = "";
|
|
1349
|
+
else if (typeof rawUpdatedAt === "number" && Number.isFinite(rawUpdatedAt)) updatedAt = String(rawUpdatedAt);
|
|
1350
|
+
else if (typeof rawUpdatedAt === "string") updatedAt = rawUpdatedAt;
|
|
1351
|
+
else throw new Error('[OneAddress SDK] Decrypted address field "updated_at" is not a string');
|
|
1352
|
+
if (updatedAt.length > MAX_ADDRESS_FIELD_CHARS) {
|
|
1353
|
+
throw new Error(
|
|
1354
|
+
`[OneAddress SDK] Decrypted address field "updated_at" is over the ${MAX_ADDRESS_FIELD_CHARS} characters this SDK accepts`
|
|
1355
|
+
);
|
|
1356
|
+
}
|
|
1357
|
+
return {
|
|
1358
|
+
street: obj.street,
|
|
1359
|
+
suburb: obj.suburb,
|
|
1360
|
+
state: obj.state,
|
|
1361
|
+
postcode: obj.postcode,
|
|
1362
|
+
country: obj.country,
|
|
1363
|
+
updated_at: updatedAt
|
|
1364
|
+
};
|
|
1365
|
+
}
|
|
1366
|
+
function legacyAddressOf(blob, body) {
|
|
1367
|
+
return validateAddress({
|
|
1368
|
+
street: blob.street,
|
|
1369
|
+
suburb: blob.suburb,
|
|
1370
|
+
state: blob.state,
|
|
1371
|
+
postcode: blob.postcode,
|
|
1372
|
+
country: blob.country,
|
|
1373
|
+
updated_at: typeof blob.updated_at === "string" ? blob.updated_at : body.timestamp ?? ""
|
|
1374
|
+
});
|
|
1375
|
+
}
|
|
1376
|
+
function stringOr(value, fallback) {
|
|
1377
|
+
return typeof value === "string" ? value : fallback;
|
|
1378
|
+
}
|
|
1379
|
+
function stringsOf(value) {
|
|
1380
|
+
return Array.isArray(value) ? value.filter((v) => typeof v === "string") : [];
|
|
1010
1381
|
}
|
|
1011
1382
|
var ADDRESS_ALIASES = {
|
|
1012
1383
|
line1: ["line1", "address_line1", "street", "streetAddress"],
|
|
@@ -1190,6 +1561,41 @@ function _resetOnceWarningsForTests() {
|
|
|
1190
1561
|
warnedNoIdempotencyStore = false;
|
|
1191
1562
|
}
|
|
1192
1563
|
var MAX_TIMESTAMP_TOLERANCE_SECS = 86400;
|
|
1564
|
+
var DEFAULT_MAX_BODY_BYTES = 1048576;
|
|
1565
|
+
var IN_PROGRESS_RETRY_AFTER_SECS = 30;
|
|
1566
|
+
async function readBodyCapped(req, max) {
|
|
1567
|
+
const declaredLength = req.headers.get("content-length");
|
|
1568
|
+
if (declaredLength !== null && /^\s*\d+\s*$/.test(declaredLength) && Number(declaredLength) > max) {
|
|
1569
|
+
return null;
|
|
1570
|
+
}
|
|
1571
|
+
const stream = req.body;
|
|
1572
|
+
if (stream && typeof stream.getReader === "function") {
|
|
1573
|
+
const reader = stream.getReader();
|
|
1574
|
+
const chunks = [];
|
|
1575
|
+
let total = 0;
|
|
1576
|
+
for (; ; ) {
|
|
1577
|
+
const { done, value } = await reader.read();
|
|
1578
|
+
if (done) break;
|
|
1579
|
+
if (!value) continue;
|
|
1580
|
+
total += value.byteLength;
|
|
1581
|
+
if (total > max) {
|
|
1582
|
+
await reader.cancel().catch(() => void 0);
|
|
1583
|
+
return null;
|
|
1584
|
+
}
|
|
1585
|
+
chunks.push(value);
|
|
1586
|
+
}
|
|
1587
|
+
const joined = new Uint8Array(total);
|
|
1588
|
+
let offset = 0;
|
|
1589
|
+
for (const c of chunks) {
|
|
1590
|
+
joined.set(c, offset);
|
|
1591
|
+
offset += c.byteLength;
|
|
1592
|
+
}
|
|
1593
|
+
return new TextDecoder("utf-8").decode(joined);
|
|
1594
|
+
}
|
|
1595
|
+
const text = await req.text();
|
|
1596
|
+
if (text.length > max || new TextEncoder().encode(text).byteLength > max) return null;
|
|
1597
|
+
return text;
|
|
1598
|
+
}
|
|
1193
1599
|
var XWING_SEED_BYTES = 32;
|
|
1194
1600
|
function assertUsablePrivateKey(privateKeyPem) {
|
|
1195
1601
|
if (typeof privateKeyPem !== "string") {
|
|
@@ -1219,6 +1625,21 @@ function requirePemFor(purpose, key) {
|
|
|
1219
1625
|
`[OneAddress SDK] ${purpose} needs the raw private key; a PrivateKeyOperations delegate ("${key.name}") covers session envelopes only`
|
|
1220
1626
|
);
|
|
1221
1627
|
}
|
|
1628
|
+
var EffectiveDateMismatchError = class extends Error {
|
|
1629
|
+
constructor(sealed, body) {
|
|
1630
|
+
super(
|
|
1631
|
+
`[OneAddress SDK] effective_date in the body (${JSON.stringify(body ?? null)}) does not match the one sealed in the envelope (${JSON.stringify(sealed ?? null)}); refusing rather than applying a date the consumer did not seal`
|
|
1632
|
+
);
|
|
1633
|
+
this.sealed = sealed;
|
|
1634
|
+
this.body = body;
|
|
1635
|
+
this.name = "EffectiveDateMismatchError";
|
|
1636
|
+
}
|
|
1637
|
+
};
|
|
1638
|
+
function assertEffectiveDateBound(sealed, body) {
|
|
1639
|
+
if (sealed === void 0 && body === void 0) return;
|
|
1640
|
+
if (sealed !== void 0 && body !== void 0 && sealed === body) return;
|
|
1641
|
+
throw new EffectiveDateMismatchError(sealed, body);
|
|
1642
|
+
}
|
|
1222
1643
|
function assertLoaBoundToEnvelope(loa, envelopeUpdateUid) {
|
|
1223
1644
|
if (typeof loa.update_uid !== "string" || typeof envelopeUpdateUid !== "string") return;
|
|
1224
1645
|
if (loa.update_uid !== envelopeUpdateUid) {
|
|
@@ -1236,13 +1657,16 @@ function createOneAddressHandler(options) {
|
|
|
1236
1657
|
expectedAlg,
|
|
1237
1658
|
keyId,
|
|
1238
1659
|
loaPublicKeyPem,
|
|
1239
|
-
requireLoa =
|
|
1660
|
+
requireLoa = true,
|
|
1661
|
+
loaPublicKeyBaseUrl,
|
|
1240
1662
|
confirm,
|
|
1663
|
+
partnerId: configuredPartnerId,
|
|
1241
1664
|
onUpdate,
|
|
1242
1665
|
onVerify,
|
|
1243
1666
|
onAccountVerify,
|
|
1244
1667
|
onTest,
|
|
1245
1668
|
timestampToleranceSecs = 300,
|
|
1669
|
+
maxBodyBytes = DEFAULT_MAX_BODY_BYTES,
|
|
1246
1670
|
idempotencyStore,
|
|
1247
1671
|
acceptedUpdateTypes: acceptedUpdateTypesRaw,
|
|
1248
1672
|
allowedCallbackHosts
|
|
@@ -1256,6 +1680,9 @@ function createOneAddressHandler(options) {
|
|
|
1256
1680
|
`[OneAddress SDK] timestampToleranceSecs must be a positive number of seconds up to ${MAX_TIMESTAMP_TOLERANCE_SECS}, got ${String(timestampToleranceSecs)}`
|
|
1257
1681
|
);
|
|
1258
1682
|
}
|
|
1683
|
+
if (typeof maxBodyBytes !== "number" || !Number.isSafeInteger(maxBodyBytes) || maxBodyBytes <= 0) {
|
|
1684
|
+
throw new Error(`[OneAddress SDK] maxBodyBytes must be a positive whole number of bytes, got ${String(maxBodyBytes)}`);
|
|
1685
|
+
}
|
|
1259
1686
|
if (webhookSecretGraceUntil !== void 0) {
|
|
1260
1687
|
const until = typeof webhookSecretGraceUntil === "number" ? webhookSecretGraceUntil : Date.parse(webhookSecretGraceUntil);
|
|
1261
1688
|
if (Number.isNaN(until)) {
|
|
@@ -1265,6 +1692,15 @@ function createOneAddressHandler(options) {
|
|
|
1265
1692
|
if (confirm && (!confirm.partnerId || !confirm.confirmSecret)) {
|
|
1266
1693
|
throw new Error("[OneAddress SDK] confirm needs both partnerId and confirmSecret");
|
|
1267
1694
|
}
|
|
1695
|
+
if (typeof requireLoa !== "boolean") {
|
|
1696
|
+
throw new Error(`[OneAddress SDK] requireLoa must be true or false, got ${String(requireLoa)}`);
|
|
1697
|
+
}
|
|
1698
|
+
if (loaPublicKeyBaseUrl !== void 0) resolveAppOrigin(loaPublicKeyBaseUrl, "loaPublicKeyBaseUrl");
|
|
1699
|
+
async function loaVerificationKey() {
|
|
1700
|
+
if (loaPublicKeyPem) return loaPublicKeyPem;
|
|
1701
|
+
if (!requireLoa) return void 0;
|
|
1702
|
+
return cachedLoaPublicKey(loaPublicKeyBaseUrl ?? OA_APP_BASE_URL2);
|
|
1703
|
+
}
|
|
1268
1704
|
function enforceLoaPresence(body) {
|
|
1269
1705
|
const present = !!body.loa_encrypted && typeof body.loa_encrypted === "object";
|
|
1270
1706
|
if (present) return;
|
|
@@ -1277,7 +1713,7 @@ function createOneAddressHandler(options) {
|
|
|
1277
1713
|
warnedConsentUnverified = true;
|
|
1278
1714
|
console.warn(
|
|
1279
1715
|
// NOSONAR - intentional SDK diagnostic, not user-controlled data
|
|
1280
|
-
"[OneAddress SDK] a dispatch arrived with no loa_encrypted and requireLoa is off, so consent for this update is UNVERIFIED.
|
|
1716
|
+
"[OneAddress SDK] a dispatch arrived with no loa_encrypted and requireLoa is off, so consent for this update is UNVERIFIED. Remove requireLoa: false (it is on by default) to refuse such dispatches. Logged once per process."
|
|
1281
1717
|
);
|
|
1282
1718
|
}
|
|
1283
1719
|
}
|
|
@@ -1312,9 +1748,9 @@ function createOneAddressHandler(options) {
|
|
|
1312
1748
|
const newEmail = updateTypes.includes("email") ? requireContactValue("email", data.new_email) : void 0;
|
|
1313
1749
|
const customer = {
|
|
1314
1750
|
email: "",
|
|
1315
|
-
name: data.verified_name
|
|
1316
|
-
account_number: data.account_number
|
|
1317
|
-
known_names:
|
|
1751
|
+
name: stringOr(data.verified_name, ""),
|
|
1752
|
+
account_number: stringOr(data.account_number, null),
|
|
1753
|
+
known_names: stringsOf(data.known_names)
|
|
1318
1754
|
};
|
|
1319
1755
|
let loa;
|
|
1320
1756
|
let loa_ref;
|
|
@@ -1326,12 +1762,25 @@ function createOneAddressHandler(options) {
|
|
|
1326
1762
|
expectedAlg
|
|
1327
1763
|
);
|
|
1328
1764
|
loa_ref = d5LoaRef(loa);
|
|
1329
|
-
|
|
1330
|
-
|
|
1765
|
+
const loaKey = await loaVerificationKey();
|
|
1766
|
+
if (loaKey) {
|
|
1767
|
+
const valid = await verifyD5LOA(loa, loaKey);
|
|
1331
1768
|
if (!valid) throw new Error("D5 LOA signature verification failed");
|
|
1332
1769
|
}
|
|
1333
1770
|
assertLoaBoundToEnvelope(loa, data.update_uid);
|
|
1771
|
+
if (hasLoaV2(loa)) {
|
|
1772
|
+
const refusal = await checkLoaV2(loa, {
|
|
1773
|
+
partnerId: configuredPartnerId ?? confirm?.partnerId ?? partnerId,
|
|
1774
|
+
updateId: body.update_id,
|
|
1775
|
+
envelope: body.session_envelope,
|
|
1776
|
+
envelopeVerifiedName: data.verified_name,
|
|
1777
|
+
...loaKey ? { publicKeyPem: loaKey } : {}
|
|
1778
|
+
});
|
|
1779
|
+
if (refusal) throw new LoaV2RefusedError(refusal);
|
|
1780
|
+
customer.known_names = typedAliasesOf(loa);
|
|
1781
|
+
}
|
|
1334
1782
|
}
|
|
1783
|
+
assertEffectiveDateBound(data.effective_date, body.effective_date);
|
|
1335
1784
|
return { updateTypes, address, newPhone, newEmail, customer, loa, loa_ref, raw: data };
|
|
1336
1785
|
}
|
|
1337
1786
|
function isD5Body(body) {
|
|
@@ -1346,7 +1795,13 @@ function createOneAddressHandler(options) {
|
|
|
1346
1795
|
return Array.isArray(siblings) && siblings.includes(keyId);
|
|
1347
1796
|
}
|
|
1348
1797
|
return async function handler(req) {
|
|
1349
|
-
const rawBody = await req
|
|
1798
|
+
const rawBody = await readBodyCapped(req, maxBodyBytes);
|
|
1799
|
+
if (rawBody === null) {
|
|
1800
|
+
return new Response(JSON.stringify({ error: "Payload too large" }), {
|
|
1801
|
+
status: 413,
|
|
1802
|
+
headers: { "Content-Type": "application/json" }
|
|
1803
|
+
});
|
|
1804
|
+
}
|
|
1350
1805
|
const sig = req.headers.get("x-oneaddress-signature") ?? "";
|
|
1351
1806
|
const tsHeader = req.headers.get("x-oneaddress-timestamp") ?? "";
|
|
1352
1807
|
if (!isFreshTimestamp(tsHeader, timestampToleranceSecs)) {
|
|
@@ -1395,6 +1850,8 @@ function createOneAddressHandler(options) {
|
|
|
1395
1850
|
if (isCrossoverSiblingToSkip(body)) {
|
|
1396
1851
|
return declared(200, { ok: true, skipped: true, crossover: true });
|
|
1397
1852
|
}
|
|
1853
|
+
let claimed = false;
|
|
1854
|
+
let completed = false;
|
|
1398
1855
|
if (dispatchId) {
|
|
1399
1856
|
if (!idempotencyStore) {
|
|
1400
1857
|
const nodeEnv = typeof process !== "undefined" ? process.env?.NODE_ENV : void 0;
|
|
@@ -1415,6 +1872,17 @@ function createOneAddressHandler(options) {
|
|
|
1415
1872
|
if (claim === "duplicate") {
|
|
1416
1873
|
return declared(200, { ok: true, duplicate: true });
|
|
1417
1874
|
}
|
|
1875
|
+
if (claim === "in-progress") {
|
|
1876
|
+
return new Response(JSON.stringify({
|
|
1877
|
+
error: "dispatch_in_progress",
|
|
1878
|
+
detail: "Another delivery of this dispatch is being processed. Retry later.",
|
|
1879
|
+
accepted_update_types: acceptedUpdateTypes
|
|
1880
|
+
}), {
|
|
1881
|
+
status: 503,
|
|
1882
|
+
headers: { "Content-Type": "application/json", "Retry-After": String(IN_PROGRESS_RETRY_AFTER_SECS) }
|
|
1883
|
+
});
|
|
1884
|
+
}
|
|
1885
|
+
claimed = true;
|
|
1418
1886
|
} else {
|
|
1419
1887
|
const alreadySeen = await idempotencyStore.has(dispatchId);
|
|
1420
1888
|
if (alreadySeen) {
|
|
@@ -1428,6 +1896,12 @@ function createOneAddressHandler(options) {
|
|
|
1428
1896
|
let event;
|
|
1429
1897
|
enforceLoaPresence(body);
|
|
1430
1898
|
if (isD5Body(body)) {
|
|
1899
|
+
if (body.effective_date !== void 0 && !isIsoDate(body.effective_date)) {
|
|
1900
|
+
return declared(400, {
|
|
1901
|
+
error: "invalid_effective_date",
|
|
1902
|
+
detail: "effective_date must be a YYYY-MM-DD calendar date. Nothing was processed."
|
|
1903
|
+
});
|
|
1904
|
+
}
|
|
1431
1905
|
let d5;
|
|
1432
1906
|
try {
|
|
1433
1907
|
d5 = await buildD5Pieces(body, partnerId);
|
|
@@ -1479,6 +1953,7 @@ function createOneAddressHandler(options) {
|
|
|
1479
1953
|
loa: d5.loa,
|
|
1480
1954
|
loa_ref: d5.loa_ref,
|
|
1481
1955
|
id_verification: body.id_verification,
|
|
1956
|
+
...body.effective_date !== void 0 ? { effective_date: body.effective_date } : {},
|
|
1482
1957
|
raw: d5.raw
|
|
1483
1958
|
};
|
|
1484
1959
|
} else {
|
|
@@ -1510,16 +1985,27 @@ function createOneAddressHandler(options) {
|
|
|
1510
1985
|
expectedAlg
|
|
1511
1986
|
);
|
|
1512
1987
|
loa_ref = d5LoaRef(loa);
|
|
1513
|
-
|
|
1988
|
+
const loaKey = await loaVerificationKey();
|
|
1989
|
+
if (loaKey && !await verifyD5LOA(loa, loaKey)) {
|
|
1514
1990
|
throw new Error("D5 LOA signature verification failed");
|
|
1515
1991
|
}
|
|
1516
1992
|
assertLoaBoundToEnvelope(loa, rawAddress.update_uid);
|
|
1993
|
+
if (hasLoaV2(loa)) {
|
|
1994
|
+
const refusal = await checkLoaV2(loa, {
|
|
1995
|
+
partnerId: configuredPartnerId ?? confirm?.partnerId ?? partnerId,
|
|
1996
|
+
updateId: body.update_id,
|
|
1997
|
+
envelope: encrypted,
|
|
1998
|
+
envelopeVerifiedName: rawAddress.fullName,
|
|
1999
|
+
...loaKey ? { publicKeyPem: loaKey } : {}
|
|
2000
|
+
});
|
|
2001
|
+
if (refusal) throw new LoaV2RefusedError(refusal);
|
|
2002
|
+
}
|
|
1517
2003
|
}
|
|
1518
2004
|
const customer = {
|
|
1519
2005
|
email: "",
|
|
1520
|
-
name: rawAddress.fullName
|
|
1521
|
-
account_number: rawAddress.accountReference
|
|
1522
|
-
known_names:
|
|
2006
|
+
name: stringOr(rawAddress.fullName, ""),
|
|
2007
|
+
account_number: stringOr(rawAddress.accountReference, null),
|
|
2008
|
+
known_names: loa && hasLoaV2(loa) ? typedAliasesOf(loa) : stringsOf(rawAddress.knownNames)
|
|
1523
2009
|
};
|
|
1524
2010
|
event = {
|
|
1525
2011
|
event: "address.updated",
|
|
@@ -1531,7 +2017,8 @@ function createOneAddressHandler(options) {
|
|
|
1531
2017
|
customer,
|
|
1532
2018
|
// The legacy blob predates kinds and always carries an address.
|
|
1533
2019
|
update_types: ["address"],
|
|
1534
|
-
|
|
2020
|
+
// Six named fields, never the blob: see validateAddress.
|
|
2021
|
+
address: legacyAddressOf(rawAddress, body),
|
|
1535
2022
|
loa,
|
|
1536
2023
|
loa_ref,
|
|
1537
2024
|
id_verification: body.id_verification,
|
|
@@ -1539,6 +2026,7 @@ function createOneAddressHandler(options) {
|
|
|
1539
2026
|
};
|
|
1540
2027
|
}
|
|
1541
2028
|
const result = await onUpdate(event);
|
|
2029
|
+
completed = true;
|
|
1542
2030
|
if (dispatchId && idempotencyStore) {
|
|
1543
2031
|
const record = idempotencyStore.complete ? idempotencyStore.complete(dispatchId) : idempotencyStore.set(dispatchId);
|
|
1544
2032
|
await record.catch(
|
|
@@ -1548,13 +2036,16 @@ function createOneAddressHandler(options) {
|
|
|
1548
2036
|
}
|
|
1549
2037
|
if (confirm && dispatchId) {
|
|
1550
2038
|
const status = result && typeof result === "object" && result.status ? result.status : "confirmed";
|
|
2039
|
+
const note = result && typeof result === "object" && typeof result.note === "string" ? result.note : void 0;
|
|
1551
2040
|
void confirmUpdate({
|
|
1552
2041
|
partnerId: confirm.partnerId,
|
|
1553
2042
|
secret: confirm.confirmSecret,
|
|
1554
2043
|
dispatchId,
|
|
1555
2044
|
status,
|
|
2045
|
+
...note !== void 0 ? { note } : {},
|
|
1556
2046
|
loaRef: event.loa_ref,
|
|
1557
2047
|
...confirm.oneAddressApi ? { appBaseUrl: confirm.oneAddressApi } : {},
|
|
2048
|
+
...confirm.allowedHosts ? { allowedHosts: confirm.allowedHosts } : {},
|
|
1558
2049
|
...confirm.fetchImpl ? { fetchImpl: confirm.fetchImpl } : {}
|
|
1559
2050
|
}).then(
|
|
1560
2051
|
(r) => {
|
|
@@ -1591,7 +2082,7 @@ function createOneAddressHandler(options) {
|
|
|
1591
2082
|
user_id: body.user_id ?? "",
|
|
1592
2083
|
customer: body.customer,
|
|
1593
2084
|
customer_match: body.customer_match,
|
|
1594
|
-
address:
|
|
2085
|
+
address: legacyAddressOf(rawAddress, body),
|
|
1595
2086
|
loa: body.loa,
|
|
1596
2087
|
id_verification: body.id_verification,
|
|
1597
2088
|
raw: rawAddress
|
|
@@ -1630,10 +2121,10 @@ function createOneAddressHandler(options) {
|
|
|
1630
2121
|
partnerId
|
|
1631
2122
|
);
|
|
1632
2123
|
customer = {
|
|
1633
|
-
email: raw.email
|
|
1634
|
-
name: raw.name
|
|
1635
|
-
account_number: raw.account_number
|
|
1636
|
-
known_names:
|
|
2124
|
+
email: stringOr(raw.email, ""),
|
|
2125
|
+
name: stringOr(raw.name, ""),
|
|
2126
|
+
account_number: stringOr(raw.account_number, null),
|
|
2127
|
+
known_names: stringsOf(raw.known_names)
|
|
1637
2128
|
};
|
|
1638
2129
|
} else {
|
|
1639
2130
|
customer = body.customer ?? {};
|
|
@@ -1720,6 +2211,13 @@ function createOneAddressHandler(options) {
|
|
|
1720
2211
|
status: 500,
|
|
1721
2212
|
headers: { "Content-Type": "application/json" }
|
|
1722
2213
|
});
|
|
2214
|
+
} finally {
|
|
2215
|
+
if (claimed && !completed && idempotencyStore?.release) {
|
|
2216
|
+
await idempotencyStore.release(dispatchId).catch(
|
|
2217
|
+
(err) => console.error("[OneAddress SDK] idempotency release failed:", err)
|
|
2218
|
+
// NOSONAR
|
|
2219
|
+
);
|
|
2220
|
+
}
|
|
1723
2221
|
}
|
|
1724
2222
|
};
|
|
1725
2223
|
}
|
|
@@ -1779,6 +2277,9 @@ async function buildSignedTestWebhook(opts) {
|
|
|
1779
2277
|
account_number: "TEST-0001",
|
|
1780
2278
|
id_verification_ref: crypto.randomUUID(),
|
|
1781
2279
|
known_names: [],
|
|
2280
|
+
// Sealed beside the body copy, as the relay's pay step does, so a
|
|
2281
|
+
// rehearsed scheduled move passes the handler's binding check.
|
|
2282
|
+
...opts.effectiveDate !== void 0 ? { effective_date: opts.effectiveDate } : {},
|
|
1782
2283
|
...opts.session ?? {}
|
|
1783
2284
|
};
|
|
1784
2285
|
const envelope = await encryptSessionForPartner(sessionData, partnerKey);
|
|
@@ -1793,6 +2294,10 @@ async function buildSignedTestWebhook(opts) {
|
|
|
1793
2294
|
oneaddress_acn: "696 078 869",
|
|
1794
2295
|
oneaddress_signature: "00".repeat(64),
|
|
1795
2296
|
signature_algorithm: "ECDSA-P256",
|
|
2297
|
+
partner_id: opts.partnerId,
|
|
2298
|
+
envelope_sha256: loaEnvelopeSha256(envelope.session_envelope),
|
|
2299
|
+
consumer_typed_aliases: acceptTypedAliases(sessionData.verified_name, sessionData.known_names ?? []),
|
|
2300
|
+
oneaddress_signature_v2: "00".repeat(64),
|
|
1796
2301
|
...opts.loa ?? {}
|
|
1797
2302
|
};
|
|
1798
2303
|
if (opts.loaSigningKeyPem) {
|
|
@@ -1803,12 +2308,19 @@ async function buildSignedTestWebhook(opts) {
|
|
|
1803
2308
|
false,
|
|
1804
2309
|
["sign"]
|
|
1805
2310
|
);
|
|
1806
|
-
const
|
|
2311
|
+
const signHex = async (text) => Buffer.from(await crypto.subtle.sign(
|
|
1807
2312
|
{ name: "ECDSA", hash: "SHA-256" },
|
|
1808
2313
|
key,
|
|
1809
|
-
new TextEncoder().encode(
|
|
1810
|
-
);
|
|
1811
|
-
loa.oneaddress_signature =
|
|
2314
|
+
new TextEncoder().encode(text)
|
|
2315
|
+
)).toString("hex");
|
|
2316
|
+
loa.oneaddress_signature = await signHex(canonicalD5LoaForm(loa));
|
|
2317
|
+
let v2Text;
|
|
2318
|
+
try {
|
|
2319
|
+
v2Text = canonicalD5LoaFormV2(loa);
|
|
2320
|
+
} catch {
|
|
2321
|
+
v2Text = null;
|
|
2322
|
+
}
|
|
2323
|
+
if (hasLoaV2(loa) && v2Text !== null) loa.oneaddress_signature_v2 = await signHex(v2Text);
|
|
1812
2324
|
}
|
|
1813
2325
|
const sealed = await encryptEnvelopeForPartner(loa, partnerKey);
|
|
1814
2326
|
loa_encrypted = { session_envelope: sealed.session_envelope, session_key_share: sealed.session_key_share };
|
|
@@ -1830,9 +2342,9 @@ async function buildSignedTestWebhook(opts) {
|
|
|
1830
2342
|
key_share_expiry: keyShareExpiryIso(new Date(nowSecs * 1e3), opts.retentionPeriodDays),
|
|
1831
2343
|
id_verification: { method: "identity_document", verified_at: nowIso, verified: true, platform: "oneaddress/v1" },
|
|
1832
2344
|
...loa_encrypted ? { loa_encrypted } : {},
|
|
2345
|
+
...opts.effectiveDate !== void 0 ? { effective_date: opts.effectiveDate } : {},
|
|
1833
2346
|
// On the wire with a `null` value when no PaymentIntent backed the update,
|
|
1834
2347
|
// so the KEY is present either way.
|
|
1835
|
-
stripe_session_id: opts.stripeSessionId ?? null,
|
|
1836
2348
|
// `id` and `crossover_key_ids` are crossover-only and therefore NOT emitted
|
|
1837
2349
|
// by default. A test that needs them passes them through `extraBody`, which
|
|
1838
2350
|
// is exactly the circumstance the relay emits them in.
|
|
@@ -1867,6 +2379,15 @@ async function buildSignedTestWebhook(opts) {
|
|
|
1867
2379
|
export {
|
|
1868
2380
|
CURRENT_VERSION,
|
|
1869
2381
|
SUPPORTED_VERSIONS,
|
|
2382
|
+
LOA_MAX_TYPED_ALIASES,
|
|
2383
|
+
normaliseLoaName,
|
|
2384
|
+
loaSurname,
|
|
2385
|
+
loaFirstName,
|
|
2386
|
+
loaNamesMatch,
|
|
2387
|
+
LOA_MIN_SHORT_FORM_LETTERS,
|
|
2388
|
+
isAcceptableAlias,
|
|
2389
|
+
acceptTypedAliases,
|
|
2390
|
+
LOA_V2_REFUSALS,
|
|
1870
2391
|
isEmailLike,
|
|
1871
2392
|
namePoolMatches,
|
|
1872
2393
|
findMatchingStoredName,
|
|
@@ -1876,6 +2397,7 @@ export {
|
|
|
1876
2397
|
createOutboxConsumer,
|
|
1877
2398
|
parsePinnedRegistryRoots,
|
|
1878
2399
|
checkRegistryHead,
|
|
2400
|
+
DEFAULT_IDEMPOTENCY_LEASE_SECS,
|
|
1879
2401
|
createSqlIdempotencyStore,
|
|
1880
2402
|
verifySignature,
|
|
1881
2403
|
signPullRequest,
|
|
@@ -1886,17 +2408,32 @@ export {
|
|
|
1886
2408
|
transformSession,
|
|
1887
2409
|
confirmUpdate,
|
|
1888
2410
|
fetchLoaPublicKey,
|
|
2411
|
+
LOA_PUBLIC_KEY_CACHE_MS,
|
|
2412
|
+
LoaPublicKeyUnavailableError,
|
|
2413
|
+
cachedLoaPublicKey,
|
|
2414
|
+
_resetLoaKeyCacheForTests,
|
|
1889
2415
|
verifyLOA,
|
|
1890
2416
|
canonicalD5LoaForm,
|
|
1891
2417
|
d5LoaRef,
|
|
1892
2418
|
verifyD5LOA,
|
|
1893
2419
|
decryptLoaEncrypted,
|
|
2420
|
+
hasLoaV2,
|
|
2421
|
+
canonicalD5LoaFormV2,
|
|
2422
|
+
loaEnvelopeSha256,
|
|
2423
|
+
verifyD5LOAv2,
|
|
2424
|
+
typedAliasesOf,
|
|
2425
|
+
checkLoaV2,
|
|
2426
|
+
LoaV2RefusedError,
|
|
2427
|
+
MAX_ADDRESS_FIELD_CHARS,
|
|
1894
2428
|
validateAddress,
|
|
1895
2429
|
readAddressParts,
|
|
1896
2430
|
formatAddressParts,
|
|
1897
2431
|
compareAddresses,
|
|
1898
2432
|
sendVerifyResult,
|
|
1899
2433
|
_resetOnceWarningsForTests,
|
|
2434
|
+
DEFAULT_MAX_BODY_BYTES,
|
|
2435
|
+
EffectiveDateMismatchError,
|
|
2436
|
+
assertEffectiveDateBound,
|
|
1900
2437
|
createOneAddressHandler,
|
|
1901
2438
|
toExpressHandler,
|
|
1902
2439
|
buildSignedTestWebhook
|