@sirosfoundation/wmp-js 0.5.2 → 0.7.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/dist/completion.d.ts +107 -0
- package/dist/completion.js +544 -0
- package/dist/consent.d.ts +53 -0
- package/dist/consent.js +136 -0
- package/dist/discovery.d.ts +48 -8
- package/dist/discovery.js +119 -38
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +10 -0
- package/dist/evidence.d.ts +1 -0
- package/dist/evidence.js +1 -0
- package/dist/index.d.ts +14 -6
- package/dist/index.js +12 -3
- package/dist/jsonrpc.d.ts +2 -0
- package/dist/jsonrpc.js +4 -1
- package/dist/openid4x.d.ts +165 -17
- package/dist/openid4x.js +176 -3
- package/dist/peer.d.ts +234 -3
- package/dist/peer.js +1007 -51
- package/dist/profile.d.ts +39 -0
- package/dist/random.d.ts +2 -0
- package/dist/random.js +7 -0
- package/dist/relay.js +4 -2
- package/dist/session-store.d.ts +75 -3
- package/dist/session-store.js +105 -4
- package/dist/signflow.d.ts +173 -0
- package/dist/signflow.js +464 -0
- package/dist/transport.d.ts +45 -4
- package/dist/transport.js +209 -42
- package/dist/types.d.ts +2 -0
- package/dist/types.js +2 -0
- package/dist/version.d.ts +30 -0
- package/dist/version.js +61 -0
- package/package.json +3 -3
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Completion-result rules of the WMP OpenID4x profile (Sections 3.1.4, 3.1.5.1,
|
|
3
|
+
* 3.3 and 7.7) — mirrors go-wmp/pkg/wmp/openid4x/completion.go and replay.go
|
|
4
|
+
* (v0.11.0).
|
|
5
|
+
*
|
|
6
|
+
* A client MUST refuse a wmp.flow.complete whose result breaks these rules, and
|
|
7
|
+
* MUST treat every member listed in `redacted_members` as unrecoverable. None of
|
|
8
|
+
* the functions here reads or returns a member VALUE in an error: errors name
|
|
9
|
+
* the member or rule only, so a refusal never echoes a secret.
|
|
10
|
+
*/
|
|
11
|
+
/** Largest credential_count a client may report from a decrypted response. */
|
|
12
|
+
export declare const MAX_ENCRYPTED_CREDENTIAL_COUNT = 256;
|
|
13
|
+
/** Largest response_jwe an orchestrator relays, in characters. */
|
|
14
|
+
export declare const MAX_RESPONSE_JWE_LEN: number;
|
|
15
|
+
/**
|
|
16
|
+
* credential_metadata members that carry a Secret Value and are never relayed
|
|
17
|
+
* in a completion (OpenID4VCI Credential Auxiliary Data). Default for the
|
|
18
|
+
* `secretMembers` argument of {@link validateCompletionResult}.
|
|
19
|
+
*/
|
|
20
|
+
export declare const DEFAULT_SECRET_MEMBERS: readonly string[];
|
|
21
|
+
/**
|
|
22
|
+
* The only result members a replayed wmp.flow.complete withholds, and the only
|
|
23
|
+
* names `redacted_members` may list (Section 3.1.4): the OID4VCI refresh token
|
|
24
|
+
* and the DPoP key it is bound to. Sorted.
|
|
25
|
+
*/
|
|
26
|
+
export declare const REPLAY_SECRET_MEMBERS: readonly string[];
|
|
27
|
+
/** Raised for every non-conforming completion or match result. */
|
|
28
|
+
export declare class CompletionResultError extends Error {
|
|
29
|
+
constructor(message: string);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Checks the `result` of a wmp.flow.complete (an oid4vci completion or a sign
|
|
33
|
+
* sub-flow result). Throws {@link CompletionResultError} on the first
|
|
34
|
+
* violation. Members it does not know are ignored (Core Section 9.5).
|
|
35
|
+
*
|
|
36
|
+
* The rules:
|
|
37
|
+
* - credential_metadata is an object and carries none of `secretMembers`;
|
|
38
|
+
* - credential_response_encrypted is a boolean; when true there is no
|
|
39
|
+
* credential_metadata and no credentials[].credential value;
|
|
40
|
+
* - redacted_members is a non-empty array of distinct {@link REPLAY_SECRET_MEMBERS}
|
|
41
|
+
* names in alphabetical order, and none of the members it lists is present in
|
|
42
|
+
* the same result;
|
|
43
|
+
* - a decrypt_credential_response result has credential_count (1 to 256) or
|
|
44
|
+
* transaction_id, never both, and carries no credentials or
|
|
45
|
+
* credential_metadata; interval only accompanies transaction_id;
|
|
46
|
+
* - a vp_token, when present, is a presentation form {@link validVpToken} allows;
|
|
47
|
+
* presentation_submission is an object and response_code a string (Section 3.2.4);
|
|
48
|
+
* - a response_jwe is a compact JWE of five base64url segments (only the
|
|
49
|
+
* encrypted key may be empty), at most {@link MAX_RESPONSE_JWE_LEN} long, and
|
|
50
|
+
* never accompanies a vp_token.
|
|
51
|
+
*/
|
|
52
|
+
export declare function validateCompletionResult(result: unknown, secretMembers?: readonly string[]): void;
|
|
53
|
+
/**
|
|
54
|
+
* A vp_token the profile allows (table in profile 3.2.5, DCQL form in 7.x): a
|
|
55
|
+
* non-empty string, a non-empty array of non-empty strings, or an object mapping
|
|
56
|
+
* query ids to non-empty arrays of non-empty strings. Anything else (booleans,
|
|
57
|
+
* numbers, null, empty or mixed values) is not a presentation. The predicate
|
|
58
|
+
* form of {@link validateVpToken} — the same rule, exposed for the flows.
|
|
59
|
+
*/
|
|
60
|
+
export declare function validVpToken(v: unknown): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Checks the `result` of a wmp.flow.complete for a flow of type match
|
|
63
|
+
* (Section 3.3.2). The result is a closed set: nothing but satisfiable,
|
|
64
|
+
* satisfiable_query_ids, selection_handle and code may be present, so no claim,
|
|
65
|
+
* disclosure, credential, identifier or free text can ride along (Section
|
|
66
|
+
* 3.3.3). The caller selects this check by the flow type it recorded for the
|
|
67
|
+
* flow_id, never by members of the result.
|
|
68
|
+
*
|
|
69
|
+
* @param queryIds the `id` values of the verifier's DCQL query when known; every
|
|
70
|
+
* satisfiable_query_ids element must then be one of them.
|
|
71
|
+
*/
|
|
72
|
+
export declare function validateMatchResult(result: unknown, queryIds?: readonly string[]): void;
|
|
73
|
+
/** The `id` of each credential query of a DCQL query (for {@link validateMatchResult}). */
|
|
74
|
+
export declare function dcqlQueryIds(query: unknown): string[];
|
|
75
|
+
/**
|
|
76
|
+
* The secret members a replayed completion reports as withheld, or [] when it
|
|
77
|
+
* reports none. A client MUST treat each as unrecoverable: it did not
|
|
78
|
+
* necessarily not exist, and no value may be fabricated or substituted for it.
|
|
79
|
+
* Call it on a result that passed {@link validateCompletionResult}.
|
|
80
|
+
*/
|
|
81
|
+
export declare function redactedMembers(result: unknown): string[];
|
|
82
|
+
export interface ReplaySplit {
|
|
83
|
+
/** False when the message mentions a secret member but cannot be parsed as such a completion: do not retain it. */
|
|
84
|
+
ok: boolean;
|
|
85
|
+
/** The form safe to retain for replay. Empty when !ok. */
|
|
86
|
+
replay: string;
|
|
87
|
+
/** The live form, set only when the replay form differs from the input. */
|
|
88
|
+
live?: string;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Separates a serialised outbound JSON-RPC message into the form safe to retain
|
|
92
|
+
* for replay and the live form (Section 3.1.4). For a wmp.flow.complete whose
|
|
93
|
+
* result carries any of {@link REPLAY_SECRET_MEMBERS}, replay is the
|
|
94
|
+
* notification with exactly those members removed and `"redacted_members":
|
|
95
|
+
* [<names withheld, sorted>]` added to the result, and live is the input
|
|
96
|
+
* unchanged. Every other member, credential_metadata included, is preserved. For
|
|
97
|
+
* any other message, or a completion with nothing to withhold, replay is the
|
|
98
|
+
* input and live is absent.
|
|
99
|
+
*
|
|
100
|
+
* ok is false when the message mentions a secret member but is not a
|
|
101
|
+
* well-formed completion, has a duplicate member name (a reader would see
|
|
102
|
+
* either value), has a name that differs from a structural name only in letter
|
|
103
|
+
* case, or its result already carries redacted_members: the caller must not
|
|
104
|
+
* retain it. The replay copy of a conforming input satisfies
|
|
105
|
+
* {@link validateCompletionResult}.
|
|
106
|
+
*/
|
|
107
|
+
export declare function splitReplaySecrets(data: string | Uint8Array): ReplaySplit;
|
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Completion-result rules of the WMP OpenID4x profile (Sections 3.1.4, 3.1.5.1,
|
|
3
|
+
* 3.3 and 7.7) — mirrors go-wmp/pkg/wmp/openid4x/completion.go and replay.go
|
|
4
|
+
* (v0.11.0).
|
|
5
|
+
*
|
|
6
|
+
* A client MUST refuse a wmp.flow.complete whose result breaks these rules, and
|
|
7
|
+
* MUST treat every member listed in `redacted_members` as unrecoverable. None of
|
|
8
|
+
* the functions here reads or returns a member VALUE in an error: errors name
|
|
9
|
+
* the member or rule only, so a refusal never echoes a secret.
|
|
10
|
+
*/
|
|
11
|
+
/** Largest credential_count a client may report from a decrypted response. */
|
|
12
|
+
export const MAX_ENCRYPTED_CREDENTIAL_COUNT = 256;
|
|
13
|
+
/** Largest response_jwe an orchestrator relays, in characters. */
|
|
14
|
+
export const MAX_RESPONSE_JWE_LEN = 4 << 20;
|
|
15
|
+
/**
|
|
16
|
+
* credential_metadata members that carry a Secret Value and are never relayed
|
|
17
|
+
* in a completion (OpenID4VCI Credential Auxiliary Data). Default for the
|
|
18
|
+
* `secretMembers` argument of {@link validateCompletionResult}.
|
|
19
|
+
*/
|
|
20
|
+
export const DEFAULT_SECRET_MEMBERS = Object.freeze(["auxiliary_data"]);
|
|
21
|
+
/**
|
|
22
|
+
* The only result members a replayed wmp.flow.complete withholds, and the only
|
|
23
|
+
* names `redacted_members` may list (Section 3.1.4): the OID4VCI refresh token
|
|
24
|
+
* and the DPoP key it is bound to. Sorted.
|
|
25
|
+
*/
|
|
26
|
+
export const REPLAY_SECRET_MEMBERS = Object.freeze([
|
|
27
|
+
"dpop_jwk",
|
|
28
|
+
"dpop_key_id",
|
|
29
|
+
"refresh_token",
|
|
30
|
+
]);
|
|
31
|
+
const REDACTED_MEMBERS_KEY = "redacted_members";
|
|
32
|
+
const METHOD_FLOW_COMPLETE = "wmp.flow.complete";
|
|
33
|
+
/** Raised for every non-conforming completion or match result. */
|
|
34
|
+
export class CompletionResultError extends Error {
|
|
35
|
+
constructor(message) {
|
|
36
|
+
super(`non-conforming flow result: ${message}`);
|
|
37
|
+
this.name = "CompletionResultError";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function isObj(v) {
|
|
41
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
42
|
+
}
|
|
43
|
+
function has(o, k) {
|
|
44
|
+
return Object.hasOwn(o, k);
|
|
45
|
+
}
|
|
46
|
+
function fail(msg) {
|
|
47
|
+
throw new CompletionResultError(msg);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Checks the `result` of a wmp.flow.complete (an oid4vci completion or a sign
|
|
51
|
+
* sub-flow result). Throws {@link CompletionResultError} on the first
|
|
52
|
+
* violation. Members it does not know are ignored (Core Section 9.5).
|
|
53
|
+
*
|
|
54
|
+
* The rules:
|
|
55
|
+
* - credential_metadata is an object and carries none of `secretMembers`;
|
|
56
|
+
* - credential_response_encrypted is a boolean; when true there is no
|
|
57
|
+
* credential_metadata and no credentials[].credential value;
|
|
58
|
+
* - redacted_members is a non-empty array of distinct {@link REPLAY_SECRET_MEMBERS}
|
|
59
|
+
* names in alphabetical order, and none of the members it lists is present in
|
|
60
|
+
* the same result;
|
|
61
|
+
* - a decrypt_credential_response result has credential_count (1 to 256) or
|
|
62
|
+
* transaction_id, never both, and carries no credentials or
|
|
63
|
+
* credential_metadata; interval only accompanies transaction_id;
|
|
64
|
+
* - a vp_token, when present, is a presentation form {@link validVpToken} allows;
|
|
65
|
+
* presentation_submission is an object and response_code a string (Section 3.2.4);
|
|
66
|
+
* - a response_jwe is a compact JWE of five base64url segments (only the
|
|
67
|
+
* encrypted key may be empty), at most {@link MAX_RESPONSE_JWE_LEN} long, and
|
|
68
|
+
* never accompanies a vp_token.
|
|
69
|
+
*/
|
|
70
|
+
export function validateCompletionResult(result, secretMembers = DEFAULT_SECRET_MEMBERS) {
|
|
71
|
+
if (!isObj(result))
|
|
72
|
+
fail("result is not an object");
|
|
73
|
+
const encrypted = validateEncryptedFlag(result);
|
|
74
|
+
validateCredentialMetadata(result, encrypted, secretMembers);
|
|
75
|
+
if (encrypted)
|
|
76
|
+
validateNoCredentialValues(result);
|
|
77
|
+
validateRedactedMembers(result);
|
|
78
|
+
validateDecryptResult(result);
|
|
79
|
+
validateResponseJWE(result);
|
|
80
|
+
validateVpToken(result);
|
|
81
|
+
validatePresentationMembers(result);
|
|
82
|
+
}
|
|
83
|
+
// OpenID4x §3.2.4: a sign result's vp_token is a non-empty string, a non-empty
|
|
84
|
+
// array of non-empty strings, or a non-empty object of non-empty arrays of
|
|
85
|
+
// non-empty strings. Anything else is refused; the value is not logged or echoed.
|
|
86
|
+
function validateVpToken(m) {
|
|
87
|
+
if (!has(m, "vp_token"))
|
|
88
|
+
return;
|
|
89
|
+
const v = m.vp_token;
|
|
90
|
+
if (typeof v === "string") {
|
|
91
|
+
if (v.length === 0)
|
|
92
|
+
fail("vp_token is the empty string");
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (Array.isArray(v)) {
|
|
96
|
+
if (v.length === 0)
|
|
97
|
+
fail("vp_token is the empty array");
|
|
98
|
+
v.forEach((entry, i) => {
|
|
99
|
+
if (typeof entry !== "string" || entry.length === 0) {
|
|
100
|
+
fail(`vp_token[${i}] is not a non-empty string`);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (isObj(v)) {
|
|
106
|
+
const keys = Object.keys(v);
|
|
107
|
+
if (keys.length === 0)
|
|
108
|
+
fail("vp_token is the empty object");
|
|
109
|
+
for (const key of keys) {
|
|
110
|
+
const arr = v[key];
|
|
111
|
+
if (!Array.isArray(arr) || arr.length === 0) {
|
|
112
|
+
fail(`vp_token[${JSON.stringify(key)}] is not a non-empty array`);
|
|
113
|
+
}
|
|
114
|
+
arr.forEach((entry, i) => {
|
|
115
|
+
if (typeof entry !== "string" || entry.length === 0) {
|
|
116
|
+
fail(`vp_token[${JSON.stringify(key)}][${i}] is not a non-empty string`);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
fail("vp_token is not a string, array, or object");
|
|
123
|
+
}
|
|
124
|
+
/** The result members of an oid4vp completion (Section 3.2.4): each, when present, has its type.
|
|
125
|
+
* The vp_token form itself is {@link validateVpToken}'s (the same check, landed with it). */
|
|
126
|
+
function validatePresentationMembers(m) {
|
|
127
|
+
if (has(m, "presentation_submission") && !isObj(m.presentation_submission)) {
|
|
128
|
+
fail("presentation_submission is not an object");
|
|
129
|
+
}
|
|
130
|
+
if (has(m, "response_code") && typeof m.response_code !== "string") {
|
|
131
|
+
fail("response_code is not a string");
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* A vp_token the profile allows (table in profile 3.2.5, DCQL form in 7.x): a
|
|
136
|
+
* non-empty string, a non-empty array of non-empty strings, or an object mapping
|
|
137
|
+
* query ids to non-empty arrays of non-empty strings. Anything else (booleans,
|
|
138
|
+
* numbers, null, empty or mixed values) is not a presentation. The predicate
|
|
139
|
+
* form of {@link validateVpToken} — the same rule, exposed for the flows.
|
|
140
|
+
*/
|
|
141
|
+
export function validVpToken(v) {
|
|
142
|
+
const str = (x) => typeof x === "string" && x !== "";
|
|
143
|
+
const strs = (x) => Array.isArray(x) && x.length > 0 && x.every(str);
|
|
144
|
+
if (str(v) || strs(v))
|
|
145
|
+
return true;
|
|
146
|
+
if (typeof v !== "object" || v === null || Array.isArray(v))
|
|
147
|
+
return false;
|
|
148
|
+
const vals = Object.values(v);
|
|
149
|
+
return vals.length > 0 && vals.every(strs);
|
|
150
|
+
}
|
|
151
|
+
function validateEncryptedFlag(m) {
|
|
152
|
+
if (!has(m, "credential_response_encrypted"))
|
|
153
|
+
return false;
|
|
154
|
+
const v = m.credential_response_encrypted;
|
|
155
|
+
if (typeof v !== "boolean")
|
|
156
|
+
fail("credential_response_encrypted is not a boolean");
|
|
157
|
+
return v;
|
|
158
|
+
}
|
|
159
|
+
function validateCredentialMetadata(m, encrypted, secretMembers) {
|
|
160
|
+
if (!has(m, "credential_metadata"))
|
|
161
|
+
return;
|
|
162
|
+
const meta = m.credential_metadata;
|
|
163
|
+
if (!isObj(meta))
|
|
164
|
+
fail("credential_metadata is not an object");
|
|
165
|
+
for (const secret of secretMembers) {
|
|
166
|
+
if (has(meta, secret))
|
|
167
|
+
fail(`credential_metadata carries ${secret}, which is never relayed`);
|
|
168
|
+
}
|
|
169
|
+
if (encrypted)
|
|
170
|
+
fail("credential_metadata present although credential_response_encrypted is true");
|
|
171
|
+
}
|
|
172
|
+
function validateNoCredentialValues(m) {
|
|
173
|
+
if (!has(m, "credentials"))
|
|
174
|
+
return;
|
|
175
|
+
const creds = m.credentials;
|
|
176
|
+
if (!Array.isArray(creds))
|
|
177
|
+
fail("credentials is not an array of objects");
|
|
178
|
+
creds.forEach((c, i) => {
|
|
179
|
+
if (!isObj(c))
|
|
180
|
+
fail(`credentials[${i}] is not an object`);
|
|
181
|
+
if (has(c, "credential")) {
|
|
182
|
+
fail(`credentials[${i}] carries a credential value although credential_response_encrypted is true`);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
function validateRedactedMembers(m) {
|
|
187
|
+
if (!has(m, REDACTED_MEMBERS_KEY))
|
|
188
|
+
return;
|
|
189
|
+
const names = m[REDACTED_MEMBERS_KEY];
|
|
190
|
+
if (!Array.isArray(names) || names.length === 0 || names.some((n) => typeof n !== "string")) {
|
|
191
|
+
fail("redacted_members is not a non-empty array of strings");
|
|
192
|
+
}
|
|
193
|
+
const seen = new Set();
|
|
194
|
+
let previous;
|
|
195
|
+
for (const n of names) {
|
|
196
|
+
if (!REPLAY_SECRET_MEMBERS.includes(n))
|
|
197
|
+
fail("redacted_members lists a member that is not a replay secret member");
|
|
198
|
+
if (seen.has(n))
|
|
199
|
+
fail("redacted_members lists a member twice");
|
|
200
|
+
// Section 3.1.4: the names are listed in alphabetical order.
|
|
201
|
+
if (previous !== undefined && n < previous)
|
|
202
|
+
fail("redacted_members is not in alphabetical order");
|
|
203
|
+
previous = n;
|
|
204
|
+
seen.add(n);
|
|
205
|
+
// A member cannot be both withheld and delivered: a client that read the
|
|
206
|
+
// delivered value would contradict the marker it was told to honour.
|
|
207
|
+
if (has(m, n))
|
|
208
|
+
fail("redacted_members lists a member that the result also carries");
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function validateDecryptResult(m) {
|
|
212
|
+
const hasCount = has(m, "credential_count");
|
|
213
|
+
const hasTx = has(m, "transaction_id");
|
|
214
|
+
if (hasCount && hasTx)
|
|
215
|
+
fail("both transaction_id and credential_count");
|
|
216
|
+
if (!hasCount && !hasTx) {
|
|
217
|
+
validateAbsentInterval(m);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
for (const k of ["credentials", "credential_metadata"]) {
|
|
221
|
+
if (has(m, k))
|
|
222
|
+
fail(`decryption result carries ${k}`);
|
|
223
|
+
}
|
|
224
|
+
if (hasCount) {
|
|
225
|
+
const n = m.credential_count;
|
|
226
|
+
if (!Number.isInteger(n) || n < 1 || n > MAX_ENCRYPTED_CREDENTIAL_COUNT) {
|
|
227
|
+
fail(`credential_count is not an integer from 1 to ${MAX_ENCRYPTED_CREDENTIAL_COUNT}`);
|
|
228
|
+
}
|
|
229
|
+
// An interval belongs only to a deferred (transaction_id) result.
|
|
230
|
+
validateAbsentInterval(m);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const tx = m.transaction_id;
|
|
234
|
+
if (typeof tx !== "string" || tx === "")
|
|
235
|
+
fail("transaction_id is not a non-empty string");
|
|
236
|
+
if (has(m, "interval")) {
|
|
237
|
+
const iv = m.interval;
|
|
238
|
+
if (!Number.isInteger(iv) || iv < 0)
|
|
239
|
+
fail("interval is not a non-negative integer");
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function validateAbsentInterval(m) {
|
|
243
|
+
if (has(m, "interval"))
|
|
244
|
+
fail("interval without transaction_id");
|
|
245
|
+
}
|
|
246
|
+
function validateResponseJWE(m) {
|
|
247
|
+
if (!has(m, "response_jwe"))
|
|
248
|
+
return;
|
|
249
|
+
if (has(m, "vp_token"))
|
|
250
|
+
fail("response_jwe together with vp_token");
|
|
251
|
+
const jwe = m.response_jwe;
|
|
252
|
+
if (typeof jwe !== "string" || jwe === "" || jwe.length > MAX_RESPONSE_JWE_LEN) {
|
|
253
|
+
fail(`response_jwe is not a non-empty string of at most ${MAX_RESPONSE_JWE_LEN} bytes`);
|
|
254
|
+
}
|
|
255
|
+
const parts = jwe.split(".");
|
|
256
|
+
if (parts.length !== 5)
|
|
257
|
+
fail("response_jwe is not a compact JWE of five segments");
|
|
258
|
+
parts.forEach((p, i) => {
|
|
259
|
+
if (p === "" && i !== 1)
|
|
260
|
+
fail(`response_jwe segment ${i + 1} is empty`);
|
|
261
|
+
if (!/^[A-Za-z0-9_-]*$/.test(p))
|
|
262
|
+
fail(`response_jwe segment ${i + 1} is not base64url`);
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
// ---------------------------------------------------------------------------
|
|
266
|
+
// match flow result (Section 3.3)
|
|
267
|
+
// ---------------------------------------------------------------------------
|
|
268
|
+
const MATCH_MEMBERS = new Set(["satisfiable", "satisfiable_query_ids", "selection_handle", "code"]);
|
|
269
|
+
const MATCH_CODES = new Set(["no_match", "insufficient", "error"]);
|
|
270
|
+
// 22 or more base64url characters, no padding, canonical length (4n, 4n+2, 4n+3).
|
|
271
|
+
const MATCH_HANDLE = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2,3})?$/;
|
|
272
|
+
/**
|
|
273
|
+
* Checks the `result` of a wmp.flow.complete for a flow of type match
|
|
274
|
+
* (Section 3.3.2). The result is a closed set: nothing but satisfiable,
|
|
275
|
+
* satisfiable_query_ids, selection_handle and code may be present, so no claim,
|
|
276
|
+
* disclosure, credential, identifier or free text can ride along (Section
|
|
277
|
+
* 3.3.3). The caller selects this check by the flow type it recorded for the
|
|
278
|
+
* flow_id, never by members of the result.
|
|
279
|
+
*
|
|
280
|
+
* @param queryIds the `id` values of the verifier's DCQL query when known; every
|
|
281
|
+
* satisfiable_query_ids element must then be one of them.
|
|
282
|
+
*/
|
|
283
|
+
export function validateMatchResult(result, queryIds) {
|
|
284
|
+
if (!isObj(result))
|
|
285
|
+
fail("match result is not an object");
|
|
286
|
+
for (const k of Object.keys(result)) {
|
|
287
|
+
if (!MATCH_MEMBERS.has(k))
|
|
288
|
+
fail("match result carries a member outside the closed set");
|
|
289
|
+
}
|
|
290
|
+
validateMatchSatisfiable(result);
|
|
291
|
+
validateMatchIds(result, queryIds);
|
|
292
|
+
if (has(result, "selection_handle")) {
|
|
293
|
+
const h = result.selection_handle;
|
|
294
|
+
if (typeof h !== "string" || h.length < 22 || !MATCH_HANDLE.test(h))
|
|
295
|
+
fail("selection_handle is not a base64url string of at least 22 characters");
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function validateMatchSatisfiable(result) {
|
|
299
|
+
if (typeof result.satisfiable !== "boolean")
|
|
300
|
+
fail("satisfiable is not a boolean");
|
|
301
|
+
if (result.satisfiable && has(result, "code"))
|
|
302
|
+
fail("code present although satisfiable is true");
|
|
303
|
+
if (!result.satisfiable && !has(result, "code"))
|
|
304
|
+
fail("code absent although satisfiable is false");
|
|
305
|
+
if (has(result, "code") && !MATCH_CODES.has(result.code)) {
|
|
306
|
+
fail("code is not one of no_match, insufficient, error");
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
function validateMatchIds(result, queryIds) {
|
|
310
|
+
if (!has(result, "satisfiable_query_ids"))
|
|
311
|
+
return;
|
|
312
|
+
const ids = result.satisfiable_query_ids;
|
|
313
|
+
if (!Array.isArray(ids) || ids.some((x) => typeof x !== "string"))
|
|
314
|
+
fail("satisfiable_query_ids is not an array of strings");
|
|
315
|
+
const seen = new Set();
|
|
316
|
+
for (const id of ids) {
|
|
317
|
+
if (seen.has(id))
|
|
318
|
+
fail("satisfiable_query_ids repeats an element");
|
|
319
|
+
seen.add(id);
|
|
320
|
+
if (queryIds && !queryIds.includes(id))
|
|
321
|
+
fail("satisfiable_query_ids has an element that is not an id of the query");
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/** The `id` of each credential query of a DCQL query (for {@link validateMatchResult}). */
|
|
325
|
+
export function dcqlQueryIds(query) {
|
|
326
|
+
const creds = isObj(query) ? query.credentials : undefined;
|
|
327
|
+
return Array.isArray(creds) ? creds.filter((c) => isObj(c) && typeof c.id === "string").map((c) => c.id) : [];
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* The secret members a replayed completion reports as withheld, or [] when it
|
|
331
|
+
* reports none. A client MUST treat each as unrecoverable: it did not
|
|
332
|
+
* necessarily not exist, and no value may be fabricated or substituted for it.
|
|
333
|
+
* Call it on a result that passed {@link validateCompletionResult}.
|
|
334
|
+
*/
|
|
335
|
+
export function redactedMembers(result) {
|
|
336
|
+
if (!isObj(result) || !Array.isArray(result[REDACTED_MEMBERS_KEY]))
|
|
337
|
+
return [];
|
|
338
|
+
return result[REDACTED_MEMBERS_KEY].filter((n) => typeof n === "string" && REPLAY_SECRET_MEMBERS.includes(n));
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Separates a serialised outbound JSON-RPC message into the form safe to retain
|
|
342
|
+
* for replay and the live form (Section 3.1.4). For a wmp.flow.complete whose
|
|
343
|
+
* result carries any of {@link REPLAY_SECRET_MEMBERS}, replay is the
|
|
344
|
+
* notification with exactly those members removed and `"redacted_members":
|
|
345
|
+
* [<names withheld, sorted>]` added to the result, and live is the input
|
|
346
|
+
* unchanged. Every other member, credential_metadata included, is preserved. For
|
|
347
|
+
* any other message, or a completion with nothing to withhold, replay is the
|
|
348
|
+
* input and live is absent.
|
|
349
|
+
*
|
|
350
|
+
* ok is false when the message mentions a secret member but is not a
|
|
351
|
+
* well-formed completion, has a duplicate member name (a reader would see
|
|
352
|
+
* either value), has a name that differs from a structural name only in letter
|
|
353
|
+
* case, or its result already carries redacted_members: the caller must not
|
|
354
|
+
* retain it. The replay copy of a conforming input satisfies
|
|
355
|
+
* {@link validateCompletionResult}.
|
|
356
|
+
*/
|
|
357
|
+
export function splitReplaySecrets(data) {
|
|
358
|
+
const text = typeof data === "string" ? data : new TextDecoder("utf-8", { fatal: false }).decode(data);
|
|
359
|
+
if (!mayNeedRedaction(text))
|
|
360
|
+
return { ok: true, replay: text };
|
|
361
|
+
const env = scanObject(text, ["method", "params"]);
|
|
362
|
+
if (!env)
|
|
363
|
+
return REFUSED;
|
|
364
|
+
const rawMethod = env.get("method");
|
|
365
|
+
if (rawMethod === undefined)
|
|
366
|
+
return { ok: true, replay: text }; // a response carries no completion
|
|
367
|
+
const method = parseJson(rawMethod);
|
|
368
|
+
if (typeof method !== "string")
|
|
369
|
+
return REFUSED;
|
|
370
|
+
if (method !== METHOD_FLOW_COMPLETE)
|
|
371
|
+
return { ok: true, replay: text };
|
|
372
|
+
const params = scanObject(env.get("params") ?? "", ["result"]);
|
|
373
|
+
if (!params)
|
|
374
|
+
return REFUSED;
|
|
375
|
+
const rawResult = params.get("result");
|
|
376
|
+
if (rawResult === undefined)
|
|
377
|
+
return { ok: true, replay: text }; // result is optional
|
|
378
|
+
const result = scanObject(rawResult, [REDACTED_MEMBERS_KEY, ...REPLAY_SECRET_MEMBERS]);
|
|
379
|
+
if (!result)
|
|
380
|
+
return REFUSED;
|
|
381
|
+
if (result.has(REDACTED_MEMBERS_KEY))
|
|
382
|
+
return REFUSED;
|
|
383
|
+
const withheld = [];
|
|
384
|
+
for (const k of REPLAY_SECRET_MEMBERS) {
|
|
385
|
+
if (result.has(k)) {
|
|
386
|
+
withheld.push(k);
|
|
387
|
+
result.delete(k);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
if (withheld.length === 0)
|
|
391
|
+
return { ok: true, replay: text };
|
|
392
|
+
result.set(REDACTED_MEMBERS_KEY, JSON.stringify(withheld));
|
|
393
|
+
params.set("result", assemble(result));
|
|
394
|
+
env.set("params", assemble(params));
|
|
395
|
+
return { ok: true, replay: assemble(env), live: text };
|
|
396
|
+
}
|
|
397
|
+
/** JSON.parse that yields undefined for text that is not JSON (undefined is no JSON value). */
|
|
398
|
+
function parseJson(text) {
|
|
399
|
+
try {
|
|
400
|
+
return JSON.parse(text);
|
|
401
|
+
}
|
|
402
|
+
catch {
|
|
403
|
+
return undefined;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const REFUSED = { ok: false, replay: "" };
|
|
407
|
+
function assemble(m) {
|
|
408
|
+
return "{" + [...m].map(([k, v]) => `${JSON.stringify(k)}:${v}`).join(",") + "}";
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* True when text could carry a secret member or a redacted_members member. A
|
|
412
|
+
* JSON key may be spelled with escapes ("refresh_token") or with non-ASCII
|
|
413
|
+
* letters that case-fold to ASCII (U+017F, U+212A), so any escape or non-ASCII
|
|
414
|
+
* character counts; redacted_members counts because a live completion never
|
|
415
|
+
* carries it.
|
|
416
|
+
*/
|
|
417
|
+
function mayNeedRedaction(text) {
|
|
418
|
+
const lower = text.toLowerCase();
|
|
419
|
+
// eslint-disable-next-line no-control-regex
|
|
420
|
+
if (/[^\u0000-\u007f]/.test(text) || lower.includes(String.raw `\u`) || lower.includes(`"${REDACTED_MEMBERS_KEY}"`)) {
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
return REPLAY_SECRET_MEMBERS.some((k) => lower.includes(`"${k}"`));
|
|
424
|
+
}
|
|
425
|
+
function foldCase(s) {
|
|
426
|
+
return s.replaceAll("\u017f", "s").replaceAll("\u212a", "k").toLowerCase();
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Splits a JSON text that is exactly one object into its members, in order,
|
|
430
|
+
* keyed by the DECODED name, each value the verbatim source text. Returns
|
|
431
|
+
* undefined for anything but exactly one object whose names are distinct after
|
|
432
|
+
* decoding, or that has a name equal to one of `exact` only up to letter case.
|
|
433
|
+
*/
|
|
434
|
+
function scanObject(text, exact) {
|
|
435
|
+
const n = text.length;
|
|
436
|
+
let i = skipWs(text, 0);
|
|
437
|
+
if (text[i] !== "{")
|
|
438
|
+
return undefined;
|
|
439
|
+
i = skipWs(text, i + 1);
|
|
440
|
+
const members = new Map();
|
|
441
|
+
if (text[i] === "}")
|
|
442
|
+
return skipWs(text, i + 1) === n ? members : undefined;
|
|
443
|
+
for (;;) {
|
|
444
|
+
const next = scanMember(text, skipWs(text, i), members, exact);
|
|
445
|
+
if (next < 0)
|
|
446
|
+
return undefined;
|
|
447
|
+
i = skipWs(text, next);
|
|
448
|
+
if (text[i] === ",") {
|
|
449
|
+
i++;
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
if (text[i] !== "}")
|
|
453
|
+
return undefined;
|
|
454
|
+
return skipWs(text, i + 1) === n ? members : undefined;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
function skipWs(text, from) {
|
|
458
|
+
let i = from;
|
|
459
|
+
while (i < text.length && (text[i] === " " || text[i] === "\t" || text[i] === "\n" || text[i] === "\r"))
|
|
460
|
+
i++;
|
|
461
|
+
return i;
|
|
462
|
+
}
|
|
463
|
+
/** Returns the end index (exclusive) of the string literal starting at `start`, or -1. */
|
|
464
|
+
function stringEnd(text, start) {
|
|
465
|
+
let j = start + 1;
|
|
466
|
+
while (j < text.length) {
|
|
467
|
+
const c = text[j];
|
|
468
|
+
if (c === "\\")
|
|
469
|
+
j += 2;
|
|
470
|
+
else if (c === '"')
|
|
471
|
+
return j + 1;
|
|
472
|
+
else
|
|
473
|
+
j++;
|
|
474
|
+
}
|
|
475
|
+
return -1;
|
|
476
|
+
}
|
|
477
|
+
/** Returns the end index of the bracketed value (object or array) starting at `start`, or -1. */
|
|
478
|
+
function bracketedEnd(text, start) {
|
|
479
|
+
let depth = 0;
|
|
480
|
+
let j = start;
|
|
481
|
+
while (j < text.length) {
|
|
482
|
+
const d = text[j];
|
|
483
|
+
if (d === '"') {
|
|
484
|
+
const e = stringEnd(text, j);
|
|
485
|
+
if (e < 0)
|
|
486
|
+
return -1;
|
|
487
|
+
j = e;
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
if (d === "{" || d === "[")
|
|
491
|
+
depth++;
|
|
492
|
+
else if (d === "}" || d === "]") {
|
|
493
|
+
depth--;
|
|
494
|
+
if (depth === 0)
|
|
495
|
+
return j + 1;
|
|
496
|
+
}
|
|
497
|
+
j++;
|
|
498
|
+
}
|
|
499
|
+
return -1;
|
|
500
|
+
}
|
|
501
|
+
/** Returns the end index of the JSON value starting at `start`, or -1. */
|
|
502
|
+
function valueEnd(text, start) {
|
|
503
|
+
const c = text[start];
|
|
504
|
+
if (c === '"')
|
|
505
|
+
return stringEnd(text, start);
|
|
506
|
+
if (c === "{" || c === "[")
|
|
507
|
+
return bracketedEnd(text, start);
|
|
508
|
+
let j = start;
|
|
509
|
+
while (j < text.length && !/[,}\]\s]/.test(text[j]))
|
|
510
|
+
j++;
|
|
511
|
+
return j > start ? j : -1;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Scans one `"name": value` member starting at `start` and records it in
|
|
515
|
+
* `members`. Returns the index after the value, or -1 when the member is
|
|
516
|
+
* malformed, repeated, or its name equals one of `exact` only up to letter case.
|
|
517
|
+
*/
|
|
518
|
+
function scanMember(text, start, members, exact) {
|
|
519
|
+
if (text[start] !== '"')
|
|
520
|
+
return -1;
|
|
521
|
+
const ke = stringEnd(text, start);
|
|
522
|
+
if (ke < 0)
|
|
523
|
+
return -1;
|
|
524
|
+
const key = parseJson(text.slice(start, ke));
|
|
525
|
+
if (typeof key !== "string")
|
|
526
|
+
return -1;
|
|
527
|
+
const colon = skipWs(text, ke);
|
|
528
|
+
if (text[colon] !== ":")
|
|
529
|
+
return -1;
|
|
530
|
+
const vs = skipWs(text, colon + 1);
|
|
531
|
+
const ve = valueEnd(text, vs);
|
|
532
|
+
if (ve < 0)
|
|
533
|
+
return -1;
|
|
534
|
+
const raw = text.slice(vs, ve);
|
|
535
|
+
if (parseJson(raw) === undefined)
|
|
536
|
+
return -1; // the value must itself be well-formed JSON
|
|
537
|
+
if (members.has(key))
|
|
538
|
+
return -1;
|
|
539
|
+
const folded = foldCase(key);
|
|
540
|
+
if (exact.some((e) => key !== e && folded === e))
|
|
541
|
+
return -1;
|
|
542
|
+
members.set(key, raw);
|
|
543
|
+
return ve;
|
|
544
|
+
}
|