@tinycloud/node-sdk 3.8.1-beta.2 → 3.9.0-beta.4
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/dist/credential-pending-store.cjs +46 -8
- package/dist/credential-pending-store.cjs.map +1 -1
- package/dist/credential-pending-store.d.cts +2 -1
- package/dist/credential-pending-store.d.ts +2 -1
- package/dist/credential-pending-store.js +55 -16
- package/dist/credential-pending-store.js.map +1 -1
- package/dist/credentials-core.cjs +140 -56
- package/dist/credentials-core.cjs.map +1 -1
- package/dist/credentials-core.d.cts +21 -1
- package/dist/credentials-core.d.ts +21 -1
- package/dist/credentials-core.js +138 -54
- package/dist/credentials-core.js.map +1 -1
- package/dist/credentials.cjs +303 -114
- package/dist/credentials.cjs.map +1 -1
- package/dist/credentials.d.cts +33 -1
- package/dist/credentials.d.ts +33 -1
- package/dist/credentials.js +291 -104
- package/dist/credentials.js.map +1 -1
- package/dist/{email-proof-GXXQhIff.d.cts → email-proof-BaOqVlB-.d.cts} +25 -7
- package/dist/{email-proof-GXXQhIff.d.ts → email-proof-BaOqVlB-.d.ts} +25 -7
- package/dist/share-receive.cjs +289 -112
- package/dist/share-receive.cjs.map +1 -1
- package/dist/share-receive.d.cts +2 -1
- package/dist/share-receive.d.ts +2 -1
- package/dist/share-receive.js +273 -95
- package/dist/share-receive.js.map +1 -1
- package/package.json +2 -2
package/dist/share-receive.cjs
CHANGED
|
@@ -24,14 +24,57 @@ __export(share_receive_exports, {
|
|
|
24
24
|
requestAgentShareEmailProof: () => requestAgentShareEmailProof
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(share_receive_exports);
|
|
27
|
-
var
|
|
27
|
+
var import_sdk_core7 = require("@tinycloud/sdk-core");
|
|
28
28
|
var import_share_sdk2 = require("@tinycloud/share-sdk");
|
|
29
29
|
|
|
30
30
|
// src/credentials/email-proof.ts
|
|
31
|
-
var
|
|
31
|
+
var import_sdk_core5 = require("@tinycloud/sdk-core");
|
|
32
|
+
var import_held_credentials = require("@tinycloud/sdk-core/held-credentials");
|
|
32
33
|
|
|
33
34
|
// src/credentials/interpreter.ts
|
|
35
|
+
var import_sdk_core3 = require("@tinycloud/sdk-core");
|
|
36
|
+
|
|
37
|
+
// src/credentials/held-binding.ts
|
|
38
|
+
var import_sdk_core2 = require("@tinycloud/sdk-core");
|
|
39
|
+
|
|
40
|
+
// src/credentials/pins.ts
|
|
34
41
|
var import_sdk_core = require("@tinycloud/sdk-core");
|
|
42
|
+
var EMAIL_PROOF_PINS = Object.freeze({
|
|
43
|
+
witnessOrigin: "https://witness.credentials.org",
|
|
44
|
+
issuerDid: "did:web:issuer.credentials.org",
|
|
45
|
+
issuerKid: "did:web:issuer.credentials.org#controller",
|
|
46
|
+
issuerX: "eEJI4xEobto4HtQ7Pg9R1vBOwcpfGRTlXDG5QfqLnGQ",
|
|
47
|
+
descriptorDigest: "1tg-qphmKBVtNwzVg9xyz-xxqt_xtMXAsQyXw46m8S0"
|
|
48
|
+
});
|
|
49
|
+
var HELD_ACCOUNT_BINDING_PINS = Object.freeze({
|
|
50
|
+
type: import_sdk_core.ACCOUNT_HOLDER_BINDING_DOMAIN,
|
|
51
|
+
protocol: "tinycloud.credentials/acquisition/v1",
|
|
52
|
+
profile: "tinycloud.email-held/v1",
|
|
53
|
+
profileVersion: 1,
|
|
54
|
+
descriptorDigest: "dc3_DuvzipXwYkTfjKqTaK1c3VS0YSPnaBsYSQriC0w",
|
|
55
|
+
issuer: EMAIL_PROOF_PINS.issuerDid,
|
|
56
|
+
issuerKid: EMAIL_PROOF_PINS.issuerKid,
|
|
57
|
+
audience: "tinycloud://credentials",
|
|
58
|
+
openerOrigin: "https://localhost",
|
|
59
|
+
completionOrigin: "https://localhost",
|
|
60
|
+
completionContext: "sdk-acquisition",
|
|
61
|
+
credentialLifetimeSeconds: 31536e3
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// src/credentials/held-binding.ts
|
|
65
|
+
var REQUEST_TTL_MS = 6e5;
|
|
66
|
+
var substituted = () => new import_sdk_core2.CredentialError("REQUEST_SUBSTITUTED", "Holder binding was substituted");
|
|
67
|
+
async function assertHeldBinding(binding, expected) {
|
|
68
|
+
const { descriptor } = expected;
|
|
69
|
+
if (descriptor.holderBinding.alg !== "EIP191" || binding.type !== descriptor.holderBinding.domain || binding.requestId !== expected.requestId || binding.descriptorDigest !== expected.descriptorDigest || binding.requirementDigest !== expected.requirementDigest || binding.holderDid !== expected.holderDid || binding.issuer !== descriptor.issuer.did || binding.issuerKid !== descriptor.issuer.kid || binding.profile !== descriptor.profile || binding.openerOrigin !== expected.openerOrigin || binding.completionOrigin !== expected.openerOrigin || binding.audience !== HELD_ACCOUNT_BINDING_PINS.audience || binding.completionContext !== HELD_ACCOUNT_BINDING_PINS.completionContext || // The static fields, against the one frozen object the request body and the account signer also use (A2.4).
|
|
70
|
+
binding.type !== HELD_ACCOUNT_BINDING_PINS.type || binding.protocol !== HELD_ACCOUNT_BINDING_PINS.protocol || binding.profile !== HELD_ACCOUNT_BINDING_PINS.profile || binding.profileVersion !== HELD_ACCOUNT_BINDING_PINS.profileVersion || binding.descriptorDigest !== HELD_ACCOUNT_BINDING_PINS.descriptorDigest || binding.issuer !== HELD_ACCOUNT_BINDING_PINS.issuer || binding.issuerKid !== HELD_ACCOUNT_BINDING_PINS.issuerKid || binding.openerOrigin !== HELD_ACCOUNT_BINDING_PINS.openerOrigin || binding.completionOrigin !== HELD_ACCOUNT_BINDING_PINS.completionOrigin || binding.credentialLifetimeSeconds !== HELD_ACCOUNT_BINDING_PINS.credentialLifetimeSeconds) throw substituted();
|
|
71
|
+
if (expected.challengeNonce === void 0 || binding.challengeNonce !== expected.challengeNonce) throw substituted();
|
|
72
|
+
if (binding.normalizedClaimsDigest !== await (0, import_sdk_core2.canonicalDigest)(expected.requirement.claims)) throw substituted();
|
|
73
|
+
const expires = Date.parse(binding.expiresAt);
|
|
74
|
+
if (expected.serverExpiresAt === void 0 || expires !== Date.parse(expected.serverExpiresAt) || Date.parse(binding.issuedAt) !== expires - REQUEST_TTL_MS) throw substituted();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/credentials/interpreter.ts
|
|
35
78
|
var delay = (ms, signal) => new Promise((resolve, reject) => {
|
|
36
79
|
const timer = setTimeout(resolve, ms);
|
|
37
80
|
signal?.addEventListener("abort", () => {
|
|
@@ -40,18 +83,36 @@ var delay = (ms, signal) => new Promise((resolve, reject) => {
|
|
|
40
83
|
}, { once: true });
|
|
41
84
|
});
|
|
42
85
|
async function interpretCredentialFlow(input) {
|
|
86
|
+
const held = input.descriptor.subjectRelationship === "account_is_subject";
|
|
87
|
+
if (held && input.allowAccountSubject !== true) throw new import_sdk_core3.CredentialError("UNSUPPORTED_PROFILE", "The held credential profile requires an explicit opt-in");
|
|
43
88
|
const steps = new Map(input.descriptor.steps.map((step) => [step.type, step]));
|
|
44
89
|
const completed = /* @__PURE__ */ new Set();
|
|
45
90
|
let lastTransition = "";
|
|
91
|
+
const checkBinding = async (value) => {
|
|
92
|
+
const binding = (0, import_sdk_core3.validateCredentialHolderBinding)(value);
|
|
93
|
+
if (binding.requestId !== input.requestId || binding.descriptorDigest !== input.descriptorDigest || binding.requirementDigest !== input.requirementDigest || binding.holderDid !== input.holderDid || binding.issuer !== input.descriptor.issuer.did || binding.issuerKid !== input.descriptor.issuer.kid || binding.profile !== input.descriptor.profile || binding.openerOrigin !== input.openerOrigin || binding.completionOrigin !== input.openerOrigin || binding.audience !== "tinycloud://credentials") throw new import_sdk_core3.CredentialError("REQUEST_SUBSTITUTED", "Holder binding was substituted");
|
|
94
|
+
const alg = input.descriptor.holderBinding.alg;
|
|
95
|
+
if (binding.type !== input.descriptor.holderBinding.domain || alg === "EIP191" && !input.transport.challengeNonce) throw new import_sdk_core3.CredentialError("REQUEST_SUBSTITUTED", "Holder binding was substituted");
|
|
96
|
+
if (alg === "EIP191") await assertHeldBinding(binding, { descriptor: input.descriptor, requirement: input.requirement, requestId: input.requestId, descriptorDigest: input.descriptorDigest, requirementDigest: input.requirementDigest, holderDid: input.holderDid, openerOrigin: input.openerOrigin, challengeNonce: input.transport.challengeNonce(input.requestId), serverExpiresAt: input.expiresAt });
|
|
97
|
+
return binding;
|
|
98
|
+
};
|
|
99
|
+
const ensureHeldBinding = async () => {
|
|
100
|
+
if (!held || input.transport.signedBinding?.(input.requestId) !== void 0) return;
|
|
101
|
+
await checkBinding(await input.transport.holderBinding(input.requestId, input.verifier, input.signal));
|
|
102
|
+
};
|
|
46
103
|
for (; ; ) {
|
|
47
104
|
input.signal?.throwIfAborted();
|
|
48
105
|
const state = await input.transport.state(input.requestId, input.verifier, input.signal);
|
|
49
|
-
if (state.requestId !== input.requestId) throw new
|
|
50
|
-
if (state.state === "expired") throw new
|
|
51
|
-
if (state.state === "canceled") throw new
|
|
52
|
-
if (state.state === "issuer_unready") throw new
|
|
53
|
-
if (state.state === "complete")
|
|
106
|
+
if (state.requestId !== input.requestId) throw new import_sdk_core3.CredentialError("REQUEST_SUBSTITUTED", "Credential request identity changed");
|
|
107
|
+
if (state.state === "expired") throw new import_sdk_core3.CredentialError("REQUEST_EXPIRED", "Credential request expired", { state: state.state, correlationId: state.correlationId });
|
|
108
|
+
if (state.state === "canceled") throw new import_sdk_core3.CredentialError("CANCELED", "Credential request was canceled", { state: state.state, correlationId: state.correlationId });
|
|
109
|
+
if (state.state === "issuer_unready") throw new import_sdk_core3.CredentialError("ISSUER_UNREADY", "Credential issuer is not ready", { retryAfterMs: state.retryAfterMs, state: state.state, correlationId: state.correlationId });
|
|
110
|
+
if (state.state === "complete") {
|
|
111
|
+
await ensureHeldBinding();
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
54
114
|
if (state.state === "ready_to_issue" || state.state === "issued") {
|
|
115
|
+
await ensureHeldBinding();
|
|
55
116
|
if (state.state === "ready_to_issue" && !completed.has("issue")) {
|
|
56
117
|
await input.transport.issue(input.requestId, input.verifier, input.signal);
|
|
57
118
|
completed.add("issue");
|
|
@@ -64,7 +125,7 @@ async function interpretCredentialFlow(input) {
|
|
|
64
125
|
continue;
|
|
65
126
|
}
|
|
66
127
|
const declared = steps.get(next.type);
|
|
67
|
-
if (next.id !== next.type || !declared || declared.type !== next.type || declared.version !== next.version) throw new
|
|
128
|
+
if (next.id !== next.type || !declared || declared.type !== next.type || declared.version !== next.version) throw new import_sdk_core3.CredentialError("REQUEST_SUBSTITUTED", "Server selected an undeclared credential step");
|
|
68
129
|
if (completed.has(next.id) || state.transitionId === lastTransition) {
|
|
69
130
|
await (input.onWait?.() ?? delay(state.retryAfterMs ?? 50, input.signal));
|
|
70
131
|
continue;
|
|
@@ -72,21 +133,22 @@ async function interpretCredentialFlow(input) {
|
|
|
72
133
|
lastTransition = state.transitionId;
|
|
73
134
|
if (next.type === "holder_signature") {
|
|
74
135
|
input.onProgress?.({ state: "signing", stepId: next.id, correlationId: state.correlationId });
|
|
75
|
-
const binding =
|
|
76
|
-
|
|
77
|
-
|
|
136
|
+
const binding = await checkBinding(await input.transport.holderBinding(input.requestId, input.verifier, input.signal));
|
|
137
|
+
const alg = input.descriptor.holderBinding.alg;
|
|
138
|
+
if (alg === "EIP191" !== (input.signing.holderKey?.alg === "EIP191")) throw new import_sdk_core3.CredentialError("REQUEST_SUBSTITUTED", "Holder binding was substituted");
|
|
139
|
+
const bytes = (0, import_sdk_core3.holderBindingSigningBytes)(binding);
|
|
78
140
|
let signature = await input.signing.autoSign?.(binding, bytes, input.signal);
|
|
79
141
|
if (signature === void 0) {
|
|
80
|
-
if (!input.signing.requestApproval) throw new
|
|
142
|
+
if (!input.signing.requestApproval) throw new import_sdk_core3.CredentialError("SIGNATURE_REJECTED", "OpenKey approval is required");
|
|
81
143
|
try {
|
|
82
144
|
signature = await input.signing.requestApproval(binding, bytes, input.signal);
|
|
83
145
|
} catch (cause) {
|
|
84
|
-
if (cause instanceof
|
|
85
|
-
throw new
|
|
146
|
+
if (cause instanceof import_sdk_core3.CredentialError) throw cause;
|
|
147
|
+
throw new import_sdk_core3.CredentialError("SIGNATURE_REJECTED", "OpenKey approval was rejected", { cause });
|
|
86
148
|
}
|
|
87
149
|
}
|
|
88
|
-
if (!(signature instanceof Uint8Array) || signature.length === 0) throw new
|
|
89
|
-
await input.transport.submitHolderSignature(input.requestId, input.verifier, (0,
|
|
150
|
+
if (!(signature instanceof Uint8Array) || signature.length === 0) throw new import_sdk_core3.CredentialError("SIGNATURE_REJECTED", "OpenKey signature is invalid");
|
|
151
|
+
await input.transport.submitHolderSignature(input.requestId, input.verifier, alg === "EIP191" ? `0x${Array.from(signature, (byte) => byte.toString(16).padStart(2, "0")).join("")}` : (0, import_sdk_core3.encodeBase64Url)(signature), input.signal, input.signing.holderKey);
|
|
90
152
|
} else {
|
|
91
153
|
input.onProgress?.({ state: next.type === "collect_input" ? "collecting" : "proving", stepId: next.id, correlationId: state.correlationId });
|
|
92
154
|
const handler = input.handlers?.[next.type];
|
|
@@ -120,9 +182,9 @@ async function interpretCredentialFlow(input) {
|
|
|
120
182
|
await input.transport.submitStep(input.requestId, input.verifier, next.id, proof, input.signal);
|
|
121
183
|
break;
|
|
122
184
|
} catch (cause) {
|
|
123
|
-
if (!(cause instanceof
|
|
185
|
+
if (!(cause instanceof import_sdk_core3.CredentialError) || cause.code !== "PROOF_REJECTED") throw cause;
|
|
124
186
|
const attemptsRemaining = input.descriptor.lifecycle.maxProofAttempts - attempt;
|
|
125
|
-
if (next.type !== "mailbox_otp" || attemptsRemaining <= 0) throw new
|
|
187
|
+
if (next.type !== "mailbox_otp" || attemptsRemaining <= 0) throw new import_sdk_core3.CredentialError("VERIFICATION_FAILED", "The proof was not accepted", { state: attemptsRemaining <= 0 ? "proof_attempts_exhausted" : "proof_rejected", cause });
|
|
126
188
|
feedback = { kind: "rejected", attemptsRemaining };
|
|
127
189
|
}
|
|
128
190
|
}
|
|
@@ -133,27 +195,27 @@ async function interpretCredentialFlow(input) {
|
|
|
133
195
|
}
|
|
134
196
|
|
|
135
197
|
// src/credentials/transport.ts
|
|
136
|
-
var
|
|
198
|
+
var import_sdk_core4 = require("@tinycloud/sdk-core");
|
|
137
199
|
function object(value, label) {
|
|
138
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new
|
|
200
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new import_sdk_core4.CredentialError("VERIFICATION_FAILED", `${label} is invalid`);
|
|
139
201
|
return value;
|
|
140
202
|
}
|
|
141
203
|
function opaque(value, label) {
|
|
142
|
-
if (typeof value !== "string" || !/^[A-Za-z0-9_-]{16,128}$/.test(value)) throw new
|
|
204
|
+
if (typeof value !== "string" || !/^[A-Za-z0-9_-]{16,128}$/.test(value)) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", `${label} is invalid`);
|
|
143
205
|
return value;
|
|
144
206
|
}
|
|
145
207
|
function timestamp(value, label) {
|
|
146
|
-
if (typeof value !== "string" || !Number.isFinite(Date.parse(value))) throw new
|
|
208
|
+
if (typeof value !== "string" || !Number.isFinite(Date.parse(value))) throw new import_sdk_core4.CredentialError("VERIFICATION_FAILED", `${label} is invalid`);
|
|
147
209
|
return new Date(value).toISOString();
|
|
148
210
|
}
|
|
149
211
|
function jwtPayload(credential) {
|
|
150
212
|
const part = credential.split("~", 1)[0]?.split(".")[1];
|
|
151
|
-
if (!part) throw new
|
|
213
|
+
if (!part) throw new import_sdk_core4.CredentialError("VERIFICATION_FAILED", "Issued SD-JWT is invalid");
|
|
152
214
|
try {
|
|
153
|
-
return object(JSON.parse(new TextDecoder().decode((0,
|
|
215
|
+
return object(JSON.parse(new TextDecoder().decode((0, import_sdk_core4.decodeBase64Url)(part))), "SD-JWT payload");
|
|
154
216
|
} catch (cause) {
|
|
155
|
-
if (cause instanceof
|
|
156
|
-
throw new
|
|
217
|
+
if (cause instanceof import_sdk_core4.CredentialError) throw cause;
|
|
218
|
+
throw new import_sdk_core4.CredentialError("VERIFICATION_FAILED", "Issued SD-JWT is invalid", { cause });
|
|
157
219
|
}
|
|
158
220
|
}
|
|
159
221
|
var SERVER_CODES = /* @__PURE__ */ new Set(["REQUEST_EXPIRED", "ISSUER_UNREADY", "UNSUPPORTED_PROFILE", "UNSUPPORTED_VERSION", "SIGNATURE_REJECTED"]);
|
|
@@ -162,12 +224,13 @@ function serverError(value, response, honorServerRecoverable) {
|
|
|
162
224
|
const body = value;
|
|
163
225
|
const keys = Object.keys(body).sort();
|
|
164
226
|
if (keys.join(",") !== "code,correlationId,recoverable,state,type" || body.type !== "tinycloud.credentials/error/v1" || typeof body.code !== "string" || typeof body.recoverable !== "boolean" || typeof body.state !== "string" || typeof body.correlationId !== "string") return void 0;
|
|
165
|
-
if (body.code === "PROOF_REJECTED") return body.recoverable ? new
|
|
166
|
-
if (body.code === "RATE_LIMITED") return new
|
|
167
|
-
if (body.code === "CHALLENGE_EXPIRED") return new
|
|
227
|
+
if (body.code === "PROOF_REJECTED") return body.recoverable ? new import_sdk_core4.CredentialError("PROOF_REJECTED", "The proof was not accepted", { state: body.state, correlationId: body.correlationId }) : new import_sdk_core4.CredentialError("VERIFICATION_FAILED", "The proof attempt limit was reached", { state: "proof_attempts_exhausted", correlationId: body.correlationId });
|
|
228
|
+
if (body.code === "RATE_LIMITED") return new import_sdk_core4.CredentialError("ISSUER_UNREADY", "Too many verification codes were requested", { state: "rate_limited", correlationId: body.correlationId, retryAfterMs: Number(response.headers.get("retry-after")) * 1e3 || void 0, recoverable: honorServerRecoverable ? body.recoverable : void 0 });
|
|
229
|
+
if (body.code === "CHALLENGE_EXPIRED") return new import_sdk_core4.CredentialError("REQUEST_EXPIRED", "The credential challenge expired", { state: body.state, correlationId: body.correlationId, recoverable: honorServerRecoverable ? body.recoverable : void 0 });
|
|
230
|
+
if (body.code === "DESCRIPTOR_MISMATCH") body.code = "UNSUPPORTED_PROFILE";
|
|
168
231
|
if (!SERVER_CODES.has(body.code)) return void 0;
|
|
169
232
|
const code = body.code;
|
|
170
|
-
return new
|
|
233
|
+
return new import_sdk_core4.CredentialError(code, `OpenCredentials rejected the request: ${code}`, {
|
|
171
234
|
state: body.state,
|
|
172
235
|
correlationId: body.correlationId,
|
|
173
236
|
retryAfterMs: Number(response.headers.get("retry-after")) * 1e3 || void 0,
|
|
@@ -175,15 +238,20 @@ function serverError(value, response, honorServerRecoverable) {
|
|
|
175
238
|
});
|
|
176
239
|
}
|
|
177
240
|
var OpenCredentialsHttpTransport = class {
|
|
241
|
+
/**
|
|
242
|
+
* `allowAccountSubject` is passed only by the email-proof functions, after the per-call held opt-in was accepted. Without it an account-bound
|
|
243
|
+
* (held) descriptor is refused here, before any request (TC-2254 A2.5).
|
|
244
|
+
*/
|
|
178
245
|
constructor(descriptor, fetchFn = globalThis.fetch.bind(globalThis), options = {}) {
|
|
179
246
|
this.descriptor = descriptor;
|
|
180
247
|
this.fetchFn = fetchFn;
|
|
181
248
|
this.options = options;
|
|
182
249
|
this.nonces = /* @__PURE__ */ new Map();
|
|
183
250
|
this.bindings = /* @__PURE__ */ new Map();
|
|
251
|
+
if (descriptor.subjectRelationship === "account_is_subject" && options.allowAccountSubject !== true) throw new import_sdk_core4.CredentialError("UNSUPPORTED_PROFILE", "The held credential profile requires an explicit opt-in");
|
|
184
252
|
}
|
|
185
253
|
url(endpoint, id) {
|
|
186
|
-
return new URL((0,
|
|
254
|
+
return new URL((0, import_sdk_core4.credentialEndpointPath)(endpoint, id), this.descriptor.issuer.origin).href;
|
|
187
255
|
}
|
|
188
256
|
async request(endpoint, input) {
|
|
189
257
|
let response;
|
|
@@ -191,8 +259,8 @@ var OpenCredentialsHttpTransport = class {
|
|
|
191
259
|
const init = { method: input.method ?? "GET", credentials: "omit", cache: "no-store", redirect: "error", referrerPolicy: "no-referrer", signal: input.signal, headers: { accept: "application/json", ...input.body === void 0 ? {} : { "content-type": "application/json" }, ...input.verifier === void 0 ? {} : { authorization: `Bearer ${input.verifier}` } }, ...input.body === void 0 ? {} : { body: JSON.stringify(input.body) } };
|
|
192
260
|
response = await this.fetchFn(this.url(endpoint, input.id), init);
|
|
193
261
|
} catch (cause) {
|
|
194
|
-
if (cause instanceof DOMException && cause.name === "AbortError") throw new
|
|
195
|
-
throw new
|
|
262
|
+
if (cause instanceof DOMException && cause.name === "AbortError") throw new import_sdk_core4.CredentialError("CANCELED", "Credential acquisition was canceled", { cause });
|
|
263
|
+
throw new import_sdk_core4.CredentialError("OFFLINE", "OpenCredentials is unavailable", { cause });
|
|
196
264
|
}
|
|
197
265
|
if (!response.ok) {
|
|
198
266
|
let parsed;
|
|
@@ -203,95 +271,112 @@ var OpenCredentialsHttpTransport = class {
|
|
|
203
271
|
}
|
|
204
272
|
const typed = serverError(parsed, response, this.options.honorServerRecoverable === true);
|
|
205
273
|
if (typed) throw typed;
|
|
206
|
-
if (response.status === 410) throw new
|
|
207
|
-
if (response.status === 409) throw new
|
|
208
|
-
if (response.status === 503) throw new
|
|
209
|
-
throw new
|
|
274
|
+
if (response.status === 410) throw new import_sdk_core4.CredentialError("REQUEST_EXPIRED", "Credential request expired");
|
|
275
|
+
if (response.status === 409) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Credential request was replayed or substituted");
|
|
276
|
+
if (response.status === 503) throw new import_sdk_core4.CredentialError("ISSUER_UNREADY", "Credential issuer is not ready", { retryAfterMs: Number(response.headers.get("retry-after")) * 1e3 || void 0 });
|
|
277
|
+
throw new import_sdk_core4.CredentialError("VERIFICATION_FAILED", "OpenCredentials rejected the request");
|
|
210
278
|
}
|
|
211
279
|
if (response.status === 204) return void 0;
|
|
212
280
|
try {
|
|
213
281
|
return await response.json();
|
|
214
282
|
} catch (cause) {
|
|
215
|
-
throw new
|
|
283
|
+
throw new import_sdk_core4.CredentialError("VERIFICATION_FAILED", "OpenCredentials response is invalid", { cause });
|
|
216
284
|
}
|
|
217
285
|
}
|
|
218
286
|
async create(input) {
|
|
219
|
-
const body = object(await this.request("request", { method: "POST", body: { protocol: input.descriptor.protocol, profile: input.descriptor.profile, profileVersion: input.descriptor.profileVersion, descriptorDigest: input.descriptorDigest, requirementDigest: input.requirementDigest, holderDid: input.holderDid, inputs: input.inputs ?? input.requirement.claims, audience:
|
|
220
|
-
if (body.type !== "tinycloud.credentials/acquisition-request/v1" || body.protocol !== input.descriptor.protocol || body.next !== "challenge" || body.endpoint !== "challenge") throw new
|
|
287
|
+
const body = object(await this.request("request", { method: "POST", body: { protocol: input.descriptor.protocol, profile: input.descriptor.profile, profileVersion: input.descriptor.profileVersion, descriptorDigest: input.descriptorDigest, requirementDigest: input.requirementDigest, holderDid: input.holderDid, inputs: input.inputs ?? input.requirement.claims, audience: HELD_ACCOUNT_BINDING_PINS.audience, openerOrigin: input.openerOrigin, completionOrigin: input.openerOrigin, completionContext: HELD_ACCOUNT_BINDING_PINS.completionContext, completionVerifierChallenge: input.completionVerifierChallenge }, signal: input.signal }), "create response");
|
|
288
|
+
if (body.type !== "tinycloud.credentials/acquisition-request/v1" || body.protocol !== input.descriptor.protocol || body.next !== "challenge" || body.endpoint !== "challenge") throw new import_sdk_core4.CredentialError("VERIFICATION_FAILED", "create response is invalid");
|
|
221
289
|
const requestId = opaque(body.requestId, "requestId");
|
|
222
290
|
return { requestId, locator: requestId, expiresAt: timestamp(body.expiresAt, "expiresAt"), correlationId: requestId };
|
|
223
291
|
}
|
|
292
|
+
/** The nonce stored from R3 or R2 for this request. Held acquisition compares the signed binding with it. */
|
|
293
|
+
challengeNonce(id) {
|
|
294
|
+
return this.nonces.get(id);
|
|
295
|
+
}
|
|
296
|
+
/** The binding fetched and validated for this request, as the account signed it. */
|
|
297
|
+
signedBinding(id) {
|
|
298
|
+
return this.bindings.get(id);
|
|
299
|
+
}
|
|
224
300
|
proofStep() {
|
|
225
301
|
return this.descriptor.steps.some((step) => step.type === "mailbox_otp") ? "mailbox_otp" : "collect_input";
|
|
226
302
|
}
|
|
227
303
|
async state(id, verifier, signal) {
|
|
228
304
|
const raw = object(await this.request("state", { id, verifier, signal }), "state response");
|
|
229
|
-
if (raw.type !== "tinycloud.credentials/acquisition-state/v1" || raw.requestId !== id || raw.profile !== this.descriptor.profile || raw.profileVersion !== 1) throw new
|
|
305
|
+
if (raw.type !== "tinycloud.credentials/acquisition-state/v1" || raw.requestId !== id || raw.profile !== this.descriptor.profile || raw.profileVersion !== 1) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Credential state response is substituted");
|
|
230
306
|
const base = { type: "OpenCredentialsAcquisitionState", version: 1, requestId: id, transitionId: `${String(raw.state)}:${String(raw.resultPreviouslyRead ?? false)}`, correlationId: id };
|
|
231
307
|
if (raw.state === "challenge_required") {
|
|
232
308
|
const type = this.proofStep();
|
|
233
309
|
return { ...base, state: "pending", nextStep: { id: type, type, version: 1, constraints: { challengeRequired: true } } };
|
|
234
310
|
}
|
|
311
|
+
if (this.descriptor.subjectRelationship === "account_is_subject" && (raw.state === "proof_required" || raw.state === "holder_binding_required" || raw.state === "holder_signature_required" || raw.state === "ready_to_issue" || raw.state === "issued")) {
|
|
312
|
+
const nonce = raw.challengeNonce;
|
|
313
|
+
const prior = this.nonces.get(id);
|
|
314
|
+
if (typeof nonce !== "string" || nonce === "" || prior !== void 0 && prior !== nonce) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Credential challenge nonce is missing or changed");
|
|
315
|
+
this.nonces.set(id, opaque(nonce, "challengeNonce"));
|
|
316
|
+
}
|
|
235
317
|
if (raw.state === "proof_required") {
|
|
236
318
|
const type = this.proofStep();
|
|
237
|
-
this.nonces.set(id, opaque(raw.challengeNonce, "challengeNonce"));
|
|
319
|
+
if (this.descriptor.subjectRelationship !== "account_is_subject") this.nonces.set(id, opaque(raw.challengeNonce, "challengeNonce"));
|
|
238
320
|
return { ...base, state: "pending", nextStep: { id: type, type, version: 1, constraints: {} } };
|
|
239
321
|
}
|
|
240
322
|
if (raw.state === "holder_binding_required" || raw.state === "holder_signature_required") return { ...base, state: "pending", nextStep: { id: "holder_signature", type: "holder_signature", version: 1, constraints: {} } };
|
|
241
323
|
if (raw.state === "ready_to_issue") return { ...base, state: "ready_to_issue" };
|
|
242
324
|
if (raw.state === "issued") return { ...base, state: "complete" };
|
|
243
|
-
throw new
|
|
325
|
+
throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Credential state is unsupported");
|
|
244
326
|
}
|
|
245
327
|
async beginStep(id, verifier, stepId, signal) {
|
|
246
328
|
const step = this.proofStep();
|
|
247
|
-
if (stepId !== step) throw new
|
|
329
|
+
if (stepId !== step) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Credential challenge step is substituted");
|
|
248
330
|
const challenge = object(await this.request("challenge", { id, verifier, method: "POST", body: { step, stepVersion: 1 }, signal }), "challenge response");
|
|
249
|
-
if (challenge.type !== "tinycloud.credentials/challenge/v1" || challenge.step !== step || challenge.stepVersion !== 1) throw new
|
|
331
|
+
if (challenge.type !== "tinycloud.credentials/challenge/v1" || challenge.step !== step || challenge.stepVersion !== 1) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Credential challenge is substituted");
|
|
250
332
|
this.nonces.set(id, opaque(challenge.challengeNonce, "challengeNonce"));
|
|
251
333
|
}
|
|
252
334
|
async submitStep(id, verifier, stepId, proof, signal) {
|
|
253
335
|
const step = this.proofStep();
|
|
254
|
-
if (stepId !== step) throw new
|
|
336
|
+
if (stepId !== step) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Credential proof step is substituted");
|
|
255
337
|
const nonce = this.nonces.get(id);
|
|
256
|
-
if (!nonce) throw new
|
|
338
|
+
if (!nonce) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Credential challenge is missing");
|
|
257
339
|
await this.request("proof", { id, verifier, method: "POST", body: { step, stepVersion: 1, challengeNonce: nonce, proof }, signal });
|
|
258
340
|
}
|
|
259
341
|
async holderBinding(id, verifier, signal) {
|
|
260
342
|
const raw = object(await this.request("holder_binding", { id, verifier, signal }), "holder binding response");
|
|
261
|
-
if (raw.type !== "tinycloud.credentials/holder-binding-request/v1" || raw.signingDomain !==
|
|
262
|
-
const binding = (0,
|
|
263
|
-
if (
|
|
343
|
+
if (raw.type !== "tinycloud.credentials/holder-binding-request/v1" || raw.signingDomain !== this.descriptor.holderBinding.domain || raw.alg !== this.descriptor.holderBinding.alg || typeof raw.canonical !== "string") throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Holder binding response is invalid");
|
|
344
|
+
const binding = (0, import_sdk_core4.validateCredentialHolderBinding)(raw.binding);
|
|
345
|
+
if (binding.type !== raw.signingDomain) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Holder binding type does not match its signing domain");
|
|
346
|
+
if ((0, import_sdk_core4.encodeBase64Url)((0, import_sdk_core4.holderBindingCanonicalBytes)(binding)) !== raw.canonical) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Holder binding canonical bytes were substituted");
|
|
264
347
|
this.bindings.set(id, binding);
|
|
265
348
|
return binding;
|
|
266
349
|
}
|
|
267
350
|
async submitHolderSignature(id, verifier, signature, signal, key) {
|
|
268
351
|
const binding = this.bindings.get(id);
|
|
269
|
-
if (!binding) throw new
|
|
352
|
+
if (!binding) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "Holder binding was not retrieved");
|
|
353
|
+
if (this.descriptor.holderBinding.alg === "EIP191" && !key) throw new import_sdk_core4.CredentialError("REQUEST_SUBSTITUTED", "An account holder must name its key and algorithm");
|
|
354
|
+
const alg = key?.alg ?? "EdDSA";
|
|
270
355
|
const kid = key?.kid ?? `${binding.holderDid}#${binding.holderDid.replace("did:key:", "")}`;
|
|
271
|
-
await this.request("holder_signature", { id, verifier, method: "POST", body: { alg
|
|
356
|
+
await this.request("holder_signature", { id, verifier, method: "POST", body: { alg, kid, signature }, signal });
|
|
272
357
|
}
|
|
273
358
|
async issue(id, verifier, signal) {
|
|
274
359
|
await this.request("issue", { id, verifier, method: "POST", signal });
|
|
275
360
|
}
|
|
276
361
|
async result(id, verifier, signal) {
|
|
277
362
|
const raw = object(await this.request("result", { id, verifier, signal }), "result response");
|
|
278
|
-
if (raw.type !== "tinycloud.credentials/result/v1" || raw.protocol !== this.descriptor.protocol || raw.profile !== this.descriptor.profile || raw.profileVersion !== 1 || raw.format !== "vc+sd-jwt" || raw.descriptorDigest !== await (0,
|
|
363
|
+
if (raw.type !== "tinycloud.credentials/result/v1" || raw.protocol !== this.descriptor.protocol || raw.profile !== this.descriptor.profile || raw.profileVersion !== 1 || raw.format !== "vc+sd-jwt" || raw.descriptorDigest !== await (0, import_sdk_core4.canonicalDigest)(this.descriptor) || raw.issuer !== this.descriptor.issuer.did || raw.issuerKid !== this.descriptor.issuer.kid || typeof raw.credential !== "string" || await (0, import_sdk_core4.sha256Base64Url)(raw.credential) !== raw.credentialDigest) throw new import_sdk_core4.CredentialError("VERIFICATION_FAILED", "Issued result metadata is invalid");
|
|
279
364
|
const holder = object(raw.holderBinding, "result holder binding");
|
|
280
|
-
if (raw.subject !== holder.did || holder.signingDomain !==
|
|
365
|
+
if (raw.subject !== holder.did || holder.signingDomain !== this.descriptor.holderBinding.domain) throw new import_sdk_core4.CredentialError("HOLDER_MISMATCH", "Issued holder binding is invalid");
|
|
281
366
|
const claimsRaw = object(raw.claims, "result claims");
|
|
282
367
|
const claims = {};
|
|
283
368
|
for (const [name, value] of Object.entries(claimsRaw)) {
|
|
284
|
-
if (typeof value !== "string") throw new
|
|
369
|
+
if (typeof value !== "string") throw new import_sdk_core4.CredentialError("VERIFICATION_FAILED", "Issued claims are invalid");
|
|
285
370
|
claims[name] = value;
|
|
286
371
|
}
|
|
287
372
|
const validity = object(raw.validity, "result validity");
|
|
288
373
|
const payload = jwtPayload(raw.credential);
|
|
289
|
-
if (typeof payload.jti !== "string") throw new
|
|
374
|
+
if (typeof payload.jti !== "string") throw new import_sdk_core4.CredentialError("VERIFICATION_FAILED", "Credential id is invalid");
|
|
290
375
|
const issuedAt = timestamp(validity.issuedAt, "issuedAt");
|
|
291
376
|
const expiresAt = timestamp(validity.expiresAt, "expiresAt");
|
|
292
377
|
const status = object(raw.status, "result status");
|
|
293
|
-
if (status.type !== "none" || status.freshnessSeconds !== this.descriptor.status.freshnessSeconds) throw new
|
|
294
|
-
return { type: "OpenCredentialsIssuedCredential", version: 1, protocol: this.descriptor.protocol, profile: { id: this.descriptor.profile, version: 1 }, credentialType: { id: this.descriptor.format.vct, version: 1 }, schema: this.descriptor.format.vct, format: "vc+sd-jwt", issuerDid: raw.issuer, issuerKid: raw.issuerKid, subjectDid: raw.subject, holderDid: raw.subject, claims, claimsDigest: await (0,
|
|
378
|
+
if (status.type !== "none" || status.freshnessSeconds !== this.descriptor.status.freshnessSeconds) throw new import_sdk_core4.CredentialError("VERIFICATION_FAILED", "Credential status is invalid");
|
|
379
|
+
return { type: "OpenCredentialsIssuedCredential", version: 1, protocol: this.descriptor.protocol, profile: { id: this.descriptor.profile, version: 1 }, credentialType: { id: this.descriptor.format.vct, version: 1 }, schema: this.descriptor.format.vct, format: "vc+sd-jwt", issuerDid: raw.issuer, issuerKid: raw.issuerKid, subjectDid: raw.subject, holderDid: raw.subject, claims, claimsDigest: await (0, import_sdk_core4.canonicalDigest)(claims), descriptorDigest: raw.descriptorDigest, credentialId: payload.jti, issuedAt, notBefore: issuedAt, expiresAt, status: { method: "none", freshnessSeconds: status.freshnessSeconds }, credential: raw.credential };
|
|
295
380
|
}
|
|
296
381
|
async issuerMetadata(signal) {
|
|
297
382
|
return await this.request("issuer_metadata", { signal });
|
|
@@ -302,13 +387,6 @@ var OpenCredentialsHttpTransport = class {
|
|
|
302
387
|
};
|
|
303
388
|
|
|
304
389
|
// src/credentials/email-proof.ts
|
|
305
|
-
var EMAIL_PROOF_PINS = Object.freeze({
|
|
306
|
-
witnessOrigin: "https://witness.credentials.org",
|
|
307
|
-
issuerDid: "did:web:issuer.credentials.org",
|
|
308
|
-
issuerKid: "did:web:issuer.credentials.org#controller",
|
|
309
|
-
issuerX: "eEJI4xEobto4HtQ7Pg9R1vBOwcpfGRTlXDG5QfqLnGQ",
|
|
310
|
-
descriptorDigest: "1tg-qphmKBVtNwzVg9xyz-xxqt_xtMXAsQyXw46m8S0"
|
|
311
|
-
});
|
|
312
390
|
var EMAIL_PROOF_CREDENTIAL_TYPE = { id: "opencredentials.email/v1", version: 1 };
|
|
313
391
|
var EMAIL_PROOF_PENDING_MAX_MS = 15 * 6e4;
|
|
314
392
|
var DISCOVERY_URL = `${EMAIL_PROOF_PINS.witnessOrigin}/.well-known/opencredentials`;
|
|
@@ -323,9 +401,17 @@ var EMAIL_PROOF_ISSUER_KEYS = [
|
|
|
323
401
|
];
|
|
324
402
|
var DEFAULT_EMAIL_PROOF_PROFILE = "tinycloud.email-proof/v1";
|
|
325
403
|
var PROFILE_PINS = {
|
|
326
|
-
"tinycloud.email-proof/v1": { descriptorDigest: EMAIL_PROOF_PINS.descriptorDigest, requiredStep: "mailbox_otp", enabled: true }
|
|
404
|
+
"tinycloud.email-proof/v1": { descriptorDigest: EMAIL_PROOF_PINS.descriptorDigest, requiredStep: "mailbox_otp", enabled: true },
|
|
405
|
+
// Held profile: disabled by default. A call enables it only with the per-call opt-in `allowHeldProfile: true` (see `profilePin`).
|
|
406
|
+
// Without it, requests refuse with PROFILE_UNAVAILABLE before any network call.
|
|
407
|
+
"tinycloud.email-held/v1": { descriptorDigest: HELD_ACCOUNT_BINDING_PINS.descriptorDigest, requiredStep: "mailbox_otp", enabled: false }
|
|
327
408
|
};
|
|
328
|
-
var OPENER_ORIGIN =
|
|
409
|
+
var OPENER_ORIGIN = HELD_ACCOUNT_BINDING_PINS.openerOrigin;
|
|
410
|
+
var HELD_PROFILE = "tinycloud.email-held/v1";
|
|
411
|
+
var HELD_KID_FRAGMENT = "blockchainAccountId";
|
|
412
|
+
var HELD_MAX_AGE_SECONDS = 31536e3;
|
|
413
|
+
var HELD_REQUEST_TTL_MS = 6e5;
|
|
414
|
+
var CLOCK_SKEW_BOUND_MS = 3e5;
|
|
329
415
|
var STEP_TIMEOUT_MS = 6e4;
|
|
330
416
|
var HOLDER_DID_PREFIX = "did:key:";
|
|
331
417
|
var HOLDER_DID_PATTERN = /^did:key:z6Mk[^#]+$/;
|
|
@@ -345,6 +431,7 @@ var RECOVERABILITY = {
|
|
|
345
431
|
CREDENTIAL_INVALID: false,
|
|
346
432
|
SESSION_EXPIRED: false,
|
|
347
433
|
SESSION_INVALID: false,
|
|
434
|
+
CLIENT_CLOCK_SKEW: false,
|
|
348
435
|
CANCELED: false,
|
|
349
436
|
FAILED: false
|
|
350
437
|
};
|
|
@@ -361,6 +448,7 @@ var MESSAGES = {
|
|
|
361
448
|
CREDENTIAL_INVALID: "Issued credential failed verification.",
|
|
362
449
|
SESSION_EXPIRED: "Email verification request expired. Request a new code.",
|
|
363
450
|
SESSION_INVALID: "Email verification request is no longer valid. Request a new code.",
|
|
451
|
+
CLIENT_CLOCK_SKEW: "Your device clock is more than 5 minutes behind. Correct it and try again.",
|
|
364
452
|
CANCELED: "Email verification was canceled.",
|
|
365
453
|
FAILED: "Email verification failed."
|
|
366
454
|
};
|
|
@@ -392,7 +480,8 @@ function restoreEmailProofPending(fields) {
|
|
|
392
480
|
requirementDigest: fields.requirementDigest,
|
|
393
481
|
openerOrigin: fields.openerOrigin,
|
|
394
482
|
createdAt: fields.createdAt,
|
|
395
|
-
expiresAt: fields.expiresAt
|
|
483
|
+
expiresAt: fields.expiresAt,
|
|
484
|
+
...fields.serverExpiresAt === void 0 ? {} : { serverExpiresAt: fields.serverExpiresAt }
|
|
396
485
|
};
|
|
397
486
|
const pending = { ...shown };
|
|
398
487
|
hide(pending, "verifier", fields.verifier);
|
|
@@ -406,6 +495,9 @@ function hide(target, key, value) {
|
|
|
406
495
|
function pendingDeadlineMs(pending) {
|
|
407
496
|
return Math.min(Date.parse(pending.expiresAt), Date.parse(pending.createdAt) + EMAIL_PROOF_PENDING_MAX_MS);
|
|
408
497
|
}
|
|
498
|
+
function isTicketEmailProofResult(result) {
|
|
499
|
+
return result.envelope.profile.id !== HELD_PROFILE;
|
|
500
|
+
}
|
|
409
501
|
var CodeRequested = class extends Error {
|
|
410
502
|
};
|
|
411
503
|
var pauseForCode = async () => {
|
|
@@ -413,17 +505,19 @@ var pauseForCode = async () => {
|
|
|
413
505
|
};
|
|
414
506
|
async function requestEmailProofCode(input) {
|
|
415
507
|
const profile = input.profile ?? DEFAULT_EMAIL_PROOF_PROFILE;
|
|
416
|
-
profilePin(profile, "request");
|
|
417
|
-
const
|
|
418
|
-
assertHolder(input.holder, "request");
|
|
508
|
+
profilePin(profile, "request", input.allowHeldProfile);
|
|
509
|
+
const held = profile === HELD_PROFILE;
|
|
510
|
+
if (held) assertHolder(input.holder, "request", profile);
|
|
511
|
+
const address = normalizeAddress(input.address, profile);
|
|
512
|
+
if (!held) assertHolder(input.holder, "request", profile);
|
|
419
513
|
const now = input.now ?? (() => /* @__PURE__ */ new Date());
|
|
420
514
|
const signal = input.signal ?? AbortSignal.timeout(STEP_TIMEOUT_MS);
|
|
421
515
|
const requirement = emailRequirement(address, profile);
|
|
422
|
-
const requirementDigest = await (0,
|
|
423
|
-
const descriptor = await witnessDescriptor(profile, requirement, { fetch: input.fetch, signal }, "request");
|
|
424
|
-
const descriptorDigest = await (0,
|
|
516
|
+
const requirementDigest = await (0, import_sdk_core5.credentialRequirementDigest)(requirement);
|
|
517
|
+
const descriptor = await witnessDescriptor(profile, requirement, { fetch: input.fetch, signal }, "request", input.allowHeldProfile);
|
|
518
|
+
const descriptorDigest = await (0, import_sdk_core5.canonicalDigest)(descriptor);
|
|
425
519
|
const verifier = randomVerifier();
|
|
426
|
-
const transport = new OpenCredentialsHttpTransport(descriptor, input.fetch, { honorServerRecoverable: true });
|
|
520
|
+
const transport = new OpenCredentialsHttpTransport(descriptor, input.fetch, { honorServerRecoverable: true, ...held ? { allowAccountSubject: true } : {} });
|
|
427
521
|
const createdAtMs = now().getTime();
|
|
428
522
|
let created;
|
|
429
523
|
try {
|
|
@@ -434,7 +528,7 @@ async function requestEmailProofCode(input) {
|
|
|
434
528
|
requirementDigest,
|
|
435
529
|
holderDid: input.holder.did,
|
|
436
530
|
openerOrigin: OPENER_ORIGIN,
|
|
437
|
-
completionVerifierChallenge: await (0,
|
|
531
|
+
completionVerifierChallenge: await (0, import_sdk_core5.sha256Base64Url)(verifier),
|
|
438
532
|
signal
|
|
439
533
|
});
|
|
440
534
|
} catch (error) {
|
|
@@ -442,6 +536,8 @@ async function requestEmailProofCode(input) {
|
|
|
442
536
|
}
|
|
443
537
|
const serverExpiry = Date.parse(created.expiresAt);
|
|
444
538
|
if (!Number.isFinite(serverExpiry)) throw new EmailProofError("request", "FAILED");
|
|
539
|
+
const serverNowMs = serverExpiry - HELD_REQUEST_TTL_MS;
|
|
540
|
+
if (held && serverNowMs - now().getTime() > CLOCK_SKEW_BOUND_MS) throw new EmailProofError("request", "CLIENT_CLOCK_SKEW");
|
|
445
541
|
const pending = restoreEmailProofPending({
|
|
446
542
|
type: "TinyCloudEmailProofPending",
|
|
447
543
|
version: 1,
|
|
@@ -454,11 +550,14 @@ async function requestEmailProofCode(input) {
|
|
|
454
550
|
requirementDigest,
|
|
455
551
|
openerOrigin: OPENER_ORIGIN,
|
|
456
552
|
createdAt: new Date(createdAtMs).toISOString(),
|
|
457
|
-
|
|
553
|
+
// Held: the deadline is measured on the client clock (a fast client must not lose window), the witness enforces its own 600 s.
|
|
554
|
+
expiresAt: new Date(held ? createdAtMs + Math.min(serverExpiry - serverNowMs, EMAIL_PROOF_PENDING_MAX_MS) : Math.min(serverExpiry, createdAtMs + EMAIL_PROOF_PENDING_MAX_MS)).toISOString(),
|
|
555
|
+
...held ? { serverExpiresAt: created.expiresAt } : {}
|
|
458
556
|
});
|
|
459
557
|
try {
|
|
460
558
|
await interpretCredentialFlow({
|
|
461
559
|
descriptor,
|
|
560
|
+
...held ? { allowAccountSubject: true } : {},
|
|
462
561
|
requirement,
|
|
463
562
|
requestId: created.requestId,
|
|
464
563
|
verifier,
|
|
@@ -466,8 +565,9 @@ async function requestEmailProofCode(input) {
|
|
|
466
565
|
descriptorDigest,
|
|
467
566
|
requirementDigest,
|
|
468
567
|
openerOrigin: OPENER_ORIGIN,
|
|
568
|
+
...held ? { expiresAt: created.expiresAt } : {},
|
|
469
569
|
transport,
|
|
470
|
-
signing: holderSigning(input.holder),
|
|
570
|
+
signing: holderSigning(input.holder, held),
|
|
471
571
|
handlers: { mailbox_otp: pauseForCode },
|
|
472
572
|
signal
|
|
473
573
|
});
|
|
@@ -484,22 +584,26 @@ async function completeWithIssuerKeys(input, issuerKeys) {
|
|
|
484
584
|
const { pending } = input;
|
|
485
585
|
const now = input.now ?? (() => /* @__PURE__ */ new Date());
|
|
486
586
|
if (!isPending(pending)) throw new EmailProofError("session", "SESSION_INVALID");
|
|
587
|
+
profilePin(pending.profile, "session", input.allowHeldProfile);
|
|
588
|
+
const held = pending.profile === HELD_PROFILE;
|
|
589
|
+
if (held && !Number.isFinite(Date.parse(pending.serverExpiresAt ?? ""))) throw new EmailProofError("session", "SESSION_INVALID");
|
|
487
590
|
if (pendingDeadlineMs(pending) <= now().getTime()) throw new EmailProofError("session", "SESSION_EXPIRED");
|
|
488
591
|
if (!/^\d{8}$/.test(input.code)) throw new EmailProofError("code", "CODE_INVALID");
|
|
489
|
-
assertHolder(input.holder, "code");
|
|
592
|
+
assertHolder(input.holder, "code", pending.profile);
|
|
490
593
|
if (input.holder.did !== pending.holderDid) throw new EmailProofError("session", "SESSION_INVALID");
|
|
491
594
|
const signal = input.signal ?? AbortSignal.timeout(STEP_TIMEOUT_MS);
|
|
492
595
|
const requirement = emailRequirement(pending.address, pending.profile);
|
|
493
|
-
if (await (0,
|
|
494
|
-
const descriptor = await witnessDescriptor(pending.profile, requirement, { fetch: input.fetch, signal }, "session");
|
|
495
|
-
const descriptorDigest = await (0,
|
|
596
|
+
if (await (0, import_sdk_core5.credentialRequirementDigest)(requirement) !== pending.requirementDigest) throw new EmailProofError("session", "SESSION_INVALID");
|
|
597
|
+
const descriptor = await witnessDescriptor(pending.profile, requirement, { fetch: input.fetch, signal }, "session", input.allowHeldProfile);
|
|
598
|
+
const descriptorDigest = await (0, import_sdk_core5.canonicalDigest)(descriptor);
|
|
496
599
|
if (descriptorDigest !== pending.descriptorDigest) throw new EmailProofError("session", "SESSION_INVALID");
|
|
497
|
-
const transport = new OpenCredentialsHttpTransport(descriptor, input.fetch, { honorServerRecoverable: true });
|
|
600
|
+
const transport = new OpenCredentialsHttpTransport(descriptor, input.fetch, { honorServerRecoverable: true, ...held ? { allowAccountSubject: true } : {} });
|
|
498
601
|
try {
|
|
499
602
|
const state = await transport.state(pending.requestId, pending.verifier, signal);
|
|
500
603
|
if (state.nextStep?.constraints.challengeRequired === true) throw new EmailProofError("session", "SESSION_INVALID");
|
|
501
604
|
await interpretCredentialFlow({
|
|
502
605
|
descriptor,
|
|
606
|
+
...held ? { allowAccountSubject: true } : {},
|
|
503
607
|
requirement,
|
|
504
608
|
requestId: pending.requestId,
|
|
505
609
|
verifier: pending.verifier,
|
|
@@ -507,8 +611,9 @@ async function completeWithIssuerKeys(input, issuerKeys) {
|
|
|
507
611
|
descriptorDigest,
|
|
508
612
|
requirementDigest: pending.requirementDigest,
|
|
509
613
|
openerOrigin: pending.openerOrigin,
|
|
614
|
+
...held ? { expiresAt: pending.serverExpiresAt } : {},
|
|
510
615
|
transport,
|
|
511
|
-
signing: holderSigning(input.holder),
|
|
616
|
+
signing: holderSigning(input.holder, held),
|
|
512
617
|
handlers: { mailbox_otp: async () => ({ otp: input.code }) },
|
|
513
618
|
signal
|
|
514
619
|
});
|
|
@@ -517,7 +622,11 @@ async function completeWithIssuerKeys(input, issuerKeys) {
|
|
|
517
622
|
}
|
|
518
623
|
try {
|
|
519
624
|
const envelope = await transport.result(pending.requestId, pending.verifier, signal);
|
|
520
|
-
|
|
625
|
+
if (held) {
|
|
626
|
+
const verified2 = await verifyHeldResult({ envelope, pending, descriptor, descriptorDigest, transport, issuerKeys, now });
|
|
627
|
+
return { envelope, verified: verified2, credentialId: verified2.credentialId, issuedAt: verified2.issuedAt.toISOString(), expiresAt: verified2.expiresAt.toISOString(), descriptor };
|
|
628
|
+
}
|
|
629
|
+
const verified = await (0, import_sdk_core5.verifyIssuedCredential)({
|
|
521
630
|
envelope,
|
|
522
631
|
descriptor,
|
|
523
632
|
descriptorDigest,
|
|
@@ -533,27 +642,49 @@ async function completeWithIssuerKeys(input, issuerKeys) {
|
|
|
533
642
|
throw mapError(error, "credential", signal);
|
|
534
643
|
}
|
|
535
644
|
}
|
|
536
|
-
function normalizeAddress(address) {
|
|
645
|
+
function normalizeAddress(address, profile) {
|
|
646
|
+
if (profile === HELD_PROFILE) return normalizeHeldAddress(address);
|
|
537
647
|
const value = address.trim();
|
|
538
648
|
if (value.length > 254 || !EMAIL_PATTERN.test(value)) throw new EmailProofError("request", "ADDRESS_INVALID");
|
|
539
649
|
return value;
|
|
540
650
|
}
|
|
651
|
+
var HELD_LOCAL_ATOM = /^[a-z0-9!#$%&'*+\-/=?^_`{|}~]+(\.[a-z0-9!#$%&'*+\-/=?^_`{|}~]+)*$/;
|
|
652
|
+
var HELD_DOMAIN_LABEL = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/;
|
|
653
|
+
function normalizeHeldAddress(address) {
|
|
654
|
+
const invalid = () => new EmailProofError("request", "ADDRESS_INVALID");
|
|
655
|
+
const trimmed = address.trim();
|
|
656
|
+
if (/[^\x00-\x7f]/.test(trimmed)) throw invalid();
|
|
657
|
+
const value = trimmed.toLowerCase();
|
|
658
|
+
const parts = value.split("@");
|
|
659
|
+
if (value.length < 3 || value.length > 254 || parts.length !== 2 || /[^\x21-\x7e]/.test(value)) throw invalid();
|
|
660
|
+
const [local, domain] = parts;
|
|
661
|
+
if (local.length > 64 || !HELD_LOCAL_ATOM.test(local) || local.startsWith("+")) throw invalid();
|
|
662
|
+
if (domain.length > 253 || !domain.includes(".") || !domain.split(".").every((label) => label.length <= 63 && HELD_DOMAIN_LABEL.test(label))) throw invalid();
|
|
663
|
+
return value;
|
|
664
|
+
}
|
|
541
665
|
function holderKid(did) {
|
|
542
666
|
return `${did}#${did.slice(HOLDER_DID_PREFIX.length)}`;
|
|
543
667
|
}
|
|
544
|
-
function
|
|
668
|
+
function assertHeldHolder(holder, step) {
|
|
669
|
+
const parsed = (0, import_held_credentials.parsePkhDid)(holder.did);
|
|
670
|
+
const canonical = parsed !== null && parsed.chainId === "1" ? `did:pkh:eip155:1:${(0, import_held_credentials.toChecksumAddress)(parsed.address)}` : void 0;
|
|
671
|
+
if (canonical === void 0 || holder.did !== canonical || holder.alg !== "EIP191" || holder.kid !== `${canonical}#${HELD_KID_FRAGMENT}`) throw new EmailProofError(step, "HOLDER_INVALID");
|
|
672
|
+
}
|
|
673
|
+
function assertHolder(holder, step, profile) {
|
|
674
|
+
if (profile === HELD_PROFILE) return assertHeldHolder(holder, step);
|
|
545
675
|
if (!HOLDER_DID_PATTERN.test(holder.did) || holder.alg !== "EdDSA" || holder.kid !== holderKid(holder.did)) throw new EmailProofError(step, "HOLDER_INVALID");
|
|
546
676
|
}
|
|
547
|
-
function profilePin(profile, step) {
|
|
677
|
+
function profilePin(profile, step, allowHeld) {
|
|
548
678
|
const entry = Object.prototype.hasOwnProperty.call(PROFILE_PINS, profile) ? PROFILE_PINS[profile] : void 0;
|
|
549
|
-
if (!entry || !entry.enabled) throw new EmailProofError(step, "PROFILE_UNAVAILABLE");
|
|
679
|
+
if (!entry || !entry.enabled && !(allowHeld === true && profile === HELD_PROFILE)) throw new EmailProofError(step, "PROFILE_UNAVAILABLE");
|
|
550
680
|
return entry;
|
|
551
681
|
}
|
|
552
682
|
function emailRequirement(address, profile) {
|
|
553
|
-
return (0,
|
|
683
|
+
if (profile === HELD_PROFILE) return (0, import_sdk_core5.validateCredentialRequirement)({ type: "TinyCloudCredentialRequirement", version: 1, profile: { id: profile, version: 1 }, credentialType: EMAIL_PROOF_CREDENTIAL_TYPE, claims: { email: address }, maxAgeSeconds: HELD_MAX_AGE_SECONDS });
|
|
684
|
+
return (0, import_sdk_core5.createEmailCredentialRequirement)({ email: address, profile: { id: profile, version: 1 }, credentialType: EMAIL_PROOF_CREDENTIAL_TYPE });
|
|
554
685
|
}
|
|
555
|
-
async function witnessDescriptor(profile, requirement, options, step) {
|
|
556
|
-
const pin = profilePin(profile, step);
|
|
686
|
+
async function witnessDescriptor(profile, requirement, options, step, allowHeld) {
|
|
687
|
+
const pin = profilePin(profile, step, allowHeld);
|
|
557
688
|
let descriptor;
|
|
558
689
|
try {
|
|
559
690
|
descriptor = await discoverCatalogDescriptor(requirement, DISCOVERY_URL, { fetch: options.fetch, signal: options.signal });
|
|
@@ -563,7 +694,7 @@ async function witnessDescriptor(profile, requirement, options, step) {
|
|
|
563
694
|
if (descriptor.issuer.origin !== EMAIL_PROOF_PINS.witnessOrigin || descriptor.issuer.did !== EMAIL_PROOF_PINS.issuerDid || !descriptor.steps.some((candidate) => candidate.type === pin.requiredStep)) {
|
|
564
695
|
throw new EmailProofError(step, "PROFILE_UNAVAILABLE");
|
|
565
696
|
}
|
|
566
|
-
if (await (0,
|
|
697
|
+
if (await (0, import_sdk_core5.canonicalDigest)(descriptor) !== pin.descriptorDigest) throw new EmailProofError(step, "PROFILE_UNAVAILABLE");
|
|
567
698
|
return descriptor;
|
|
568
699
|
}
|
|
569
700
|
async function discoverCatalogDescriptor(requirement, discoveryUrl, options = {}) {
|
|
@@ -571,30 +702,30 @@ async function discoverCatalogDescriptor(requirement, discoveryUrl, options = {}
|
|
|
571
702
|
try {
|
|
572
703
|
url = new URL(discoveryUrl);
|
|
573
704
|
} catch {
|
|
574
|
-
throw new
|
|
705
|
+
throw new import_sdk_core5.CredentialError("UNSUPPORTED_PROFILE", "Discovery URL is invalid");
|
|
575
706
|
}
|
|
576
|
-
if (url.protocol !== "https:" || !["/.well-known/opencredentials", "/v1/credential-types"].includes(url.pathname) || url.search || url.hash) throw new
|
|
707
|
+
if (url.protocol !== "https:" || !["/.well-known/opencredentials", "/v1/credential-types"].includes(url.pathname) || url.search || url.hash) throw new import_sdk_core5.CredentialError("UNSUPPORTED_PROFILE", "Discovery URL is not an allowed catalog endpoint");
|
|
577
708
|
const init = { credentials: "omit", cache: "no-store", redirect: "error", referrerPolicy: "no-referrer", signal: options.signal };
|
|
578
709
|
let response;
|
|
579
710
|
try {
|
|
580
711
|
response = await (options.fetch ?? globalThis.fetch.bind(globalThis))(url, init);
|
|
581
712
|
} catch (cause) {
|
|
582
|
-
throw new
|
|
713
|
+
throw new import_sdk_core5.CredentialError("OFFLINE", "Credential discovery is unavailable", { cause });
|
|
583
714
|
}
|
|
584
|
-
if (!response.ok) throw new
|
|
715
|
+
if (!response.ok) throw new import_sdk_core5.CredentialError(response.status === 503 ? "ISSUER_UNREADY" : "OFFLINE", "Credential discovery is unavailable");
|
|
585
716
|
const catalog = await response.json();
|
|
586
|
-
if (catalog.type !== "tinycloud.credentials/catalog/v1" || catalog.protocol !== "tinycloud.credentials/acquisition/v1" || catalog.catalogVersion !== 1 || !Array.isArray(catalog.profiles)) throw new
|
|
717
|
+
if (catalog.type !== "tinycloud.credentials/catalog/v1" || catalog.protocol !== "tinycloud.credentials/acquisition/v1" || catalog.catalogVersion !== 1 || !Array.isArray(catalog.profiles)) throw new import_sdk_core5.CredentialError("DESCRIPTOR_INVALID", "Credential catalog is invalid");
|
|
587
718
|
for (const entryValue of catalog.profiles) {
|
|
588
719
|
if (typeof entryValue !== "object" || entryValue === null || Array.isArray(entryValue)) continue;
|
|
589
720
|
const entry = entryValue;
|
|
590
721
|
if (entry.supported !== true || entry.enabled !== true || entry.readiness !== "ready" && entry.readiness !== "degraded") continue;
|
|
591
722
|
try {
|
|
592
|
-
const candidate = (0,
|
|
593
|
-
if ((0,
|
|
723
|
+
const candidate = (0, import_sdk_core5.validateCredentialFlowDescriptor)(entry.descriptor);
|
|
724
|
+
if ((0, import_sdk_core5.descriptorSatisfiesRequirement)(candidate, requirement) && entry.descriptorDigest === await (0, import_sdk_core5.canonicalDigest)(candidate)) return candidate;
|
|
594
725
|
} catch {
|
|
595
726
|
}
|
|
596
727
|
}
|
|
597
|
-
throw new
|
|
728
|
+
throw new import_sdk_core5.CredentialError("UNSUPPORTED_PROFILE", "No available credential profile satisfies the requirement");
|
|
598
729
|
}
|
|
599
730
|
function discoveryFailure(error, signal, step) {
|
|
600
731
|
if (signal.aborted && signal.reason?.name !== "TimeoutError") return new EmailProofError(step, "CANCELED");
|
|
@@ -603,11 +734,55 @@ function discoveryFailure(error, signal, step) {
|
|
|
603
734
|
function pinnedIssuerMetadata(keys) {
|
|
604
735
|
return { type: "OpenCredentialsIssuerMetadata", version: 1, origin: EMAIL_PROOF_PINS.witnessOrigin, issuerDid: EMAIL_PROOF_PINS.issuerDid, keys, cache: { maxAgeSeconds: 3600, etag: "pinned" } };
|
|
605
736
|
}
|
|
606
|
-
function
|
|
607
|
-
|
|
737
|
+
async function verifyHeldResult(input) {
|
|
738
|
+
const { envelope, pending, descriptor } = input;
|
|
739
|
+
const refuse = (message) => new import_sdk_core5.CredentialError("VERIFICATION_FAILED", message);
|
|
740
|
+
const iatMs = Date.parse(envelope.issuedAt);
|
|
741
|
+
const clientMs = input.now().getTime();
|
|
742
|
+
if (!Number.isFinite(iatMs) || iatMs - clientMs > CLOCK_SKEW_BOUND_MS) throw refuse("client clock is more than 5 minutes behind the issuer");
|
|
743
|
+
const key = input.issuerKeys.find((candidate) => candidate.kid === EMAIL_PROOF_PINS.issuerKid);
|
|
744
|
+
if (!key) throw refuse("no pinned issuer key");
|
|
745
|
+
const issuerSet = (0, import_held_credentials.parseIssuerSet)(
|
|
746
|
+
{ format: "tinycloud.credentials-issuers/v1", version: 1, issuers: [{ issuerDid: EMAIL_PROOF_PINS.issuerDid, kid: EMAIL_PROOF_PINS.issuerKid, alg: "EdDSA", publicKey: key.jwk.x, vcts: [EMAIL_PROOF_CREDENTIAL_TYPE.id], profiles: [HELD_PROFILE], notBefore: "2020-01-01T00:00:00Z", retiredAt: null, notAfter: null }] },
|
|
747
|
+
{ didMethods: { issuer: ["web"] } }
|
|
748
|
+
);
|
|
749
|
+
const verified = (0, import_held_credentials.verifyHeldEmailCredential)(envelope.credential, {
|
|
750
|
+
now: new Date(Math.max(clientMs, iatMs)),
|
|
751
|
+
owner: pending.holderDid,
|
|
752
|
+
didMethods: { issuer: ["web"] },
|
|
753
|
+
acceptedIssuers: [{ issuerDid: EMAIL_PROOF_PINS.issuerDid, kid: EMAIL_PROOF_PINS.issuerKid }],
|
|
754
|
+
issuerSet
|
|
755
|
+
});
|
|
756
|
+
const signed = input.transport.signedBinding(pending.requestId);
|
|
757
|
+
const claims = Object.keys(envelope.claims).sort().join();
|
|
758
|
+
const toSecond = (ms) => Math.floor(ms / 1e3);
|
|
759
|
+
if (
|
|
760
|
+
// (i) the holder, as the one EIP-55 string
|
|
761
|
+
envelope.subjectDid !== pending.holderDid || envelope.holderDid !== pending.holderDid || // (ii) exactly {email, emailDomain}, equal to the credential's, and the requested mailbox
|
|
762
|
+
claims !== "email,emailDomain" || envelope.claims.email !== verified.claims.email || envelope.claims.emailDomain !== verified.claims.emailDomain || envelope.claims.email !== pending.address || // (iii) validity to the second
|
|
763
|
+
toSecond(Date.parse(envelope.issuedAt)) !== toSecond(verified.issuedAt.getTime()) || toSecond(Date.parse(envelope.expiresAt)) !== toSecond(verified.expiresAt.getTime()) || // (v) the pins
|
|
764
|
+
envelope.descriptorDigest !== input.descriptorDigest || envelope.profile.id !== HELD_PROFILE || envelope.profile.version !== 1 || envelope.issuerDid !== EMAIL_PROOF_PINS.issuerDid || envelope.issuerKid !== EMAIL_PROOF_PINS.issuerKid || envelope.format !== "vc+sd-jwt" || envelope.status.method !== "none" || envelope.status.freshnessSeconds !== descriptor.status.freshnessSeconds
|
|
765
|
+
) throw refuse("issued credential does not match the request");
|
|
766
|
+
if (signed?.type !== descriptor.holderBinding.domain || !("credentialLifetimeSeconds" in signed)) throw refuse("signed binding is missing");
|
|
767
|
+
if (verified.credentialId !== signed.jti || (verified.expiresAt.getTime() - verified.issuedAt.getTime()) / 1e3 !== signed.credentialLifetimeSeconds) throw refuse("credential does not match the binding the account signed");
|
|
768
|
+
return verified;
|
|
769
|
+
}
|
|
770
|
+
function holderSigning(holder, held) {
|
|
771
|
+
return {
|
|
772
|
+
autoSign: async (_binding, bytes) => {
|
|
773
|
+
try {
|
|
774
|
+
return await holder.sign(bytes);
|
|
775
|
+
} catch (error) {
|
|
776
|
+
const aborted = error instanceof DOMException && (error.name === "TimeoutError" || error.name === "AbortError");
|
|
777
|
+
if (!held || error instanceof EmailProofError || aborted) throw error;
|
|
778
|
+
throw new EmailProofError("credential", "SIGNATURE_REJECTED");
|
|
779
|
+
}
|
|
780
|
+
},
|
|
781
|
+
holderKey: { alg: holder.alg, kid: holder.kid }
|
|
782
|
+
};
|
|
608
783
|
}
|
|
609
784
|
function randomVerifier() {
|
|
610
|
-
return (0,
|
|
785
|
+
return (0, import_sdk_core5.encodeBase64Url)(crypto.getRandomValues(new Uint8Array(32)));
|
|
611
786
|
}
|
|
612
787
|
function isPending(value) {
|
|
613
788
|
if (typeof value !== "object" || value === null) return false;
|
|
@@ -617,7 +792,7 @@ function isPending(value) {
|
|
|
617
792
|
function mapError(value, step, signal) {
|
|
618
793
|
if (value instanceof EmailProofError) return value;
|
|
619
794
|
if (value instanceof DOMException && value.name === "TimeoutError") return new EmailProofError(step, "NETWORK");
|
|
620
|
-
const error = (0,
|
|
795
|
+
const error = (0, import_sdk_core5.credentialError)(value);
|
|
621
796
|
const details = error.details;
|
|
622
797
|
switch (error.code) {
|
|
623
798
|
case "ISSUER_UNREADY":
|
|
@@ -626,6 +801,7 @@ function mapError(value, step, signal) {
|
|
|
626
801
|
if (signal?.aborted && signal.reason?.name !== "TimeoutError") return new EmailProofError(step, "CANCELED");
|
|
627
802
|
return new EmailProofError(step, "NETWORK");
|
|
628
803
|
case "UNSUPPORTED_PROFILE":
|
|
804
|
+
case "UNSUPPORTED_VERSION":
|
|
629
805
|
case "DESCRIPTOR_INVALID":
|
|
630
806
|
return new EmailProofError(step, "PROFILE_UNAVAILABLE");
|
|
631
807
|
case "PROOF_REJECTED":
|
|
@@ -651,18 +827,19 @@ function mapError(value, step, signal) {
|
|
|
651
827
|
}
|
|
652
828
|
|
|
653
829
|
// src/share-receive-access.ts
|
|
654
|
-
var
|
|
830
|
+
var import_sdk_core6 = require("@tinycloud/sdk-core");
|
|
655
831
|
var import_share_sdk = require("@tinycloud/share-sdk");
|
|
656
832
|
async function openVerifiedAgentShare(pending, result, requirement, options) {
|
|
657
833
|
const { envelope } = pending;
|
|
658
|
-
if (!(
|
|
834
|
+
if (!isTicketEmailProofResult(result)) throw new import_share_sdk.ShareReceiveError("authorization-denied", "email credential does not match this share");
|
|
835
|
+
if (!(0, import_sdk_core6.credentialMatchesRequirement)(requirement, result.verified, options.holder.did, new Date(options.now?.() ?? Date.now()))) {
|
|
659
836
|
throw new import_share_sdk.ShareReceiveError("authorization-denied", "email credential does not match this share");
|
|
660
837
|
}
|
|
661
838
|
if (envelope.target.nodeAudience !== envelope.attestedEnforcerBinding.nodeAudience) {
|
|
662
839
|
throw new import_share_sdk.ShareReceiveError("envelope-invalid", "share Node binding is invalid");
|
|
663
840
|
}
|
|
664
841
|
try {
|
|
665
|
-
await (0,
|
|
842
|
+
await (0, import_sdk_core6.verifyOwnerNodeBinding)({
|
|
666
843
|
registryUrl: options.registryOrigin,
|
|
667
844
|
ownerDid: envelope.policy.ownerDid,
|
|
668
845
|
nodeOrigin: envelope.target.origin,
|
|
@@ -675,7 +852,7 @@ async function openVerifiedAgentShare(pending, result, requirement, options) {
|
|
|
675
852
|
}
|
|
676
853
|
let admitted;
|
|
677
854
|
try {
|
|
678
|
-
admitted = await (0,
|
|
855
|
+
admitted = await (0, import_sdk_core6.admitPolicyCredentialV4)({
|
|
679
856
|
policy: envelope.policy,
|
|
680
857
|
policyCid: envelope.policyCid,
|
|
681
858
|
policyRootCid: envelope.policyRoot.cid,
|
|
@@ -729,7 +906,7 @@ function requirementFor(envelope) {
|
|
|
729
906
|
throw new import_share_sdk2.ShareReceiveError("unsupported-target", "this receive path accepts exact-email shares only");
|
|
730
907
|
}
|
|
731
908
|
const policy = envelope.policy;
|
|
732
|
-
return (0,
|
|
909
|
+
return (0, import_sdk_core7.createEmailCredentialRequirement)({
|
|
733
910
|
email: envelope.recipientMatcher.value,
|
|
734
911
|
profile: policy.credentialRequirement.profile,
|
|
735
912
|
credentialType: policy.credentialRequirement.credentialType
|
|
@@ -748,7 +925,7 @@ async function requestAgentShareEmailProof(link, options) {
|
|
|
748
925
|
})).metadata;
|
|
749
926
|
if (envelope === void 0) throw new import_share_sdk2.ShareReceiveError("envelope-invalid", "share envelope is invalid");
|
|
750
927
|
const requirement = requirementFor(envelope);
|
|
751
|
-
if (await (0,
|
|
928
|
+
if (await (0, import_sdk_core7.credentialRequirementDigest)(requirement) !== envelope.policy.credentialRequirement.requirementDigest) {
|
|
752
929
|
throw new import_share_sdk2.ShareReceiveError("envelope-invalid", "share credential requirement is invalid");
|
|
753
930
|
}
|
|
754
931
|
const proof = await requestEmailProofCode({ address: requirement.claims.email, holder: options.holder, fetch: options.fetch, signal: options.signal });
|
|
@@ -758,7 +935,7 @@ async function completeAgentShareReceive(pending, code, options) {
|
|
|
758
935
|
const { envelope } = pending;
|
|
759
936
|
active(envelope, options.now?.() ?? Date.now());
|
|
760
937
|
const requirement = requirementFor(envelope);
|
|
761
|
-
if (await (0,
|
|
938
|
+
if (await (0, import_sdk_core7.credentialRequirementDigest)(requirement) !== envelope.policy.credentialRequirement.requirementDigest) {
|
|
762
939
|
throw new import_share_sdk2.ShareReceiveError("envelope-invalid", "share credential requirement is invalid");
|
|
763
940
|
}
|
|
764
941
|
if (pending.proof.address !== requirement.claims.email || pending.proof.holderDid !== options.holder.did) {
|