@owf/eudi-registration-certificate 0.0.0-stage → 0.4.2
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/README.md +362 -2
- package/dist/index.cjs +1237 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +825 -0
- package/dist/index.d.mts +825 -0
- package/dist/index.mjs +1189 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +48 -4
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1189 @@
|
|
|
1
|
+
import { IdentityException, dateToSeconds, nowInSeconds, secondsToDate } from "@owf/identity-common";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { pemToDer } from "@owf/crypto";
|
|
4
|
+
import { JAdESProfile, Token, decode, validateProfile } from "@owf/eudi-jades";
|
|
5
|
+
/**
|
|
6
|
+
* Service_Provider - General service provider (A.2.1)
|
|
7
|
+
* OID: id-etsi-wrpa-entitlement 1
|
|
8
|
+
*/
|
|
9
|
+
const ENTITLEMENT_SERVICE_PROVIDER = "https://uri.etsi.org/19475/Entitlement/Service_Provider";
|
|
10
|
+
/**
|
|
11
|
+
* QEAA_Provider - Qualified trust service provider issuing qualified electronic attestations of attributes (A.2.2)
|
|
12
|
+
* OID: id-etsi-wrpa-entitlement 2
|
|
13
|
+
*/
|
|
14
|
+
const ENTITLEMENT_QEAA_PROVIDER = "https://uri.etsi.org/19475/Entitlement/QEAA_Provider";
|
|
15
|
+
/**
|
|
16
|
+
* Non_Q_EAA_Provider - Trust service provider issuing non-qualified electronic attestations of attributes (A.2.3)
|
|
17
|
+
* OID: id-etsi-wrpa-entitlement 3
|
|
18
|
+
*/
|
|
19
|
+
const ENTITLEMENT_NON_Q_EAA_PROVIDER = "https://uri.etsi.org/19475/Entitlement/Non_Q_EAA_Provider";
|
|
20
|
+
/**
|
|
21
|
+
* PUB_EAA_Provider - Public sector body or its agent issuing electronic attestations of attributes from authentic sources (A.2.4)
|
|
22
|
+
* OID: id-etsi-wrpa-entitlement 4
|
|
23
|
+
*/
|
|
24
|
+
const ENTITLEMENT_PUB_EAA_PROVIDER = "https://uri.etsi.org/19475/Entitlement/PUB_EAA_Provider";
|
|
25
|
+
/**
|
|
26
|
+
* PID_Provider - Provider of person identification data (A.2.5)
|
|
27
|
+
* OID: id-etsi-wrpa-entitlement 5
|
|
28
|
+
*/
|
|
29
|
+
const ENTITLEMENT_PID_PROVIDER = "https://uri.etsi.org/19475/Entitlement/PID_Provider";
|
|
30
|
+
/**
|
|
31
|
+
* All standard WRP entitlements defined in ETSI TS 119 475
|
|
32
|
+
*/
|
|
33
|
+
const WRP_ENTITLEMENTS = {
|
|
34
|
+
SERVICE_PROVIDER: ENTITLEMENT_SERVICE_PROVIDER,
|
|
35
|
+
QEAA_PROVIDER: ENTITLEMENT_QEAA_PROVIDER,
|
|
36
|
+
NON_Q_EAA_PROVIDER: ENTITLEMENT_NON_Q_EAA_PROVIDER,
|
|
37
|
+
PUB_EAA_PROVIDER: ENTITLEMENT_PUB_EAA_PROVIDER,
|
|
38
|
+
PID_PROVIDER: ENTITLEMENT_PID_PROVIDER,
|
|
39
|
+
QCERT_FOR_ESEAL_PROVIDER: "https://uri.etsi.org/19475/Entitlement/QCert_for_ESeal_Provider",
|
|
40
|
+
QCERT_FOR_ESIG_PROVIDER: "https://uri.etsi.org/19475/Entitlement/QCert_for_ESig_Provider",
|
|
41
|
+
RQSEALCDS_PROVIDER: "https://uri.etsi.org/19475/Entitlement/rQSealCDs_Provider",
|
|
42
|
+
RQSIGCDS_PROVIDER: "https://uri.etsi.org/19475/Entitlement/rQSigCDs_Provider",
|
|
43
|
+
ESIG_ESEAL_CREATION_PROVIDER: "https://uri.etsi.org/19475/Entitlement/ESig_ESeal_Creation_Provider"
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* List of all standard entitlement URIs
|
|
47
|
+
*/
|
|
48
|
+
const ALL_ENTITLEMENTS = Object.values(WRP_ENTITLEMENTS);
|
|
49
|
+
/**
|
|
50
|
+
* Entitlements for attestation providers (QEAA, Non-Q EAA, PUB EAA, PID)
|
|
51
|
+
*/
|
|
52
|
+
const ATTESTATION_PROVIDER_ENTITLEMENTS = [
|
|
53
|
+
ENTITLEMENT_QEAA_PROVIDER,
|
|
54
|
+
ENTITLEMENT_NON_Q_EAA_PROVIDER,
|
|
55
|
+
ENTITLEMENT_PUB_EAA_PROVIDER,
|
|
56
|
+
ENTITLEMENT_PID_PROVIDER
|
|
57
|
+
];
|
|
58
|
+
/**
|
|
59
|
+
* Payment Service Provider Sub-entitlements (A.3.1)
|
|
60
|
+
* As defined in ETSI TS 119 495
|
|
61
|
+
*/
|
|
62
|
+
const PSP_SUB_ENTITLEMENTS = {
|
|
63
|
+
/** Account Servicing Payment Service Provider */
|
|
64
|
+
ACCOUNT_SERVICING: "https://uri.etsi.org/19475/SubEntitlement/psp/psp-as",
|
|
65
|
+
/** Payment Initiation Service Provider */
|
|
66
|
+
PAYMENT_INITIATION: "https://uri.etsi.org/19475/SubEntitlement/psp/psp-pi",
|
|
67
|
+
/** Account Information Service Provider */
|
|
68
|
+
ACCOUNT_INFORMATION: "https://uri.etsi.org/19475/SubEntitlement/psp/psp-ai",
|
|
69
|
+
/** Payment Service Provider issuing card-based payment instruments */
|
|
70
|
+
CARD_BASED: "https://uri.etsi.org/19475/SubEntitlement/psp/psp-ic",
|
|
71
|
+
/** Unspecified Payment Service Provider */
|
|
72
|
+
UNSPECIFIED: "https://uri.etsi.org/19475/SubEntitlement/psp/unspecified"
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* All PSP sub-entitlement URIs
|
|
76
|
+
*/
|
|
77
|
+
const ALL_PSP_SUB_ENTITLEMENTS = Object.values(PSP_SUB_ENTITLEMENTS);
|
|
78
|
+
/**
|
|
79
|
+
* Identifier type URIs for legal person semantic identifiers
|
|
80
|
+
*/
|
|
81
|
+
const IDENTIFIER_TYPES = {
|
|
82
|
+
/** Economic Operator Registration and Identification Number (EORI-No) */
|
|
83
|
+
EORI: "http://data.europa.eu/eudi/id/EORI-No",
|
|
84
|
+
/** Legal Entity Identifier (LEI) */
|
|
85
|
+
LEI: "http://data.europa.eu/eudi/id/LEI",
|
|
86
|
+
/** European Unique Identifier (EUID) */
|
|
87
|
+
EUID: "http://data.europa.eu/eudi/id/EUID",
|
|
88
|
+
/** Value Added Tax Identification Number (VATIN) */
|
|
89
|
+
VATIN: "http://data.europa.eu/eudi/id/VATIN",
|
|
90
|
+
/** Taxpayer Identification Number (TIN) */
|
|
91
|
+
TIN: "http://data.europa.eu/eudi/id/TIN",
|
|
92
|
+
/** Excise Number */
|
|
93
|
+
EXCISE: "http://data.europa.eu/eudi/id/Excise"
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Mapping from identifier type URIs to semantic identifier initial characters
|
|
97
|
+
* for legal persons (Table 2, ETSI EN 319 412-1 clause 5.1.4).
|
|
98
|
+
*
|
|
99
|
+
* Note that the TIN type maps to the `VAT` initial characters for legal persons.
|
|
100
|
+
*/
|
|
101
|
+
const LEGAL_PERSON_IDENTIFIER_PREFIXES = {
|
|
102
|
+
[IDENTIFIER_TYPES.EORI]: "EOR",
|
|
103
|
+
[IDENTIFIER_TYPES.LEI]: "LEI",
|
|
104
|
+
[IDENTIFIER_TYPES.EUID]: "NTR",
|
|
105
|
+
[IDENTIFIER_TYPES.VATIN]: "VAT",
|
|
106
|
+
[IDENTIFIER_TYPES.TIN]: "VAT",
|
|
107
|
+
[IDENTIFIER_TYPES.EXCISE]: "EXC"
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Mapping from identifier type URIs to semantic identifier initial characters
|
|
111
|
+
* for natural persons (Table 4, ETSI EN 319 412-1 clause 5.1.3).
|
|
112
|
+
*/
|
|
113
|
+
const NATURAL_PERSON_IDENTIFIER_PREFIXES = {
|
|
114
|
+
[IDENTIFIER_TYPES.VATIN]: "TIN",
|
|
115
|
+
[IDENTIFIER_TYPES.TIN]: "TIN"
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Check if a given URI is a valid WRP entitlement
|
|
119
|
+
*/
|
|
120
|
+
function isValidEntitlement(uri) {
|
|
121
|
+
return ALL_ENTITLEMENTS.includes(uri);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Check if a given URI is a PSP sub-entitlement
|
|
125
|
+
*/
|
|
126
|
+
function isPSPSubEntitlement(uri) {
|
|
127
|
+
return ALL_PSP_SUB_ENTITLEMENTS.includes(uri);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Check if entitlements include an attestation provider role
|
|
131
|
+
*/
|
|
132
|
+
function hasAttestationProviderEntitlement(entitlements) {
|
|
133
|
+
return entitlements.some((e) => ATTESTATION_PROVIDER_ENTITLEMENTS.includes(e));
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Get the semantic identifier initial characters for a given identifier type URI
|
|
137
|
+
*
|
|
138
|
+
* @param subjectType - Which mapping to use: Table 2 for legal persons, Table 4 for natural persons
|
|
139
|
+
*/
|
|
140
|
+
function getIdentifierPrefix(typeUri, subjectType = "legal") {
|
|
141
|
+
return subjectType === "legal" ? LEGAL_PERSON_IDENTIFIER_PREFIXES[typeUri] : NATURAL_PERSON_IDENTIFIER_PREFIXES[typeUri];
|
|
142
|
+
}
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/schemas.ts
|
|
145
|
+
/**
|
|
146
|
+
* WRPRC Zod Schemas
|
|
147
|
+
*
|
|
148
|
+
* Zod schemas for ETSI TS 119 475 (Wallet-Relying Party Registration Certificates).
|
|
149
|
+
* Types are derived from these schemas via z.infer<>.
|
|
150
|
+
*
|
|
151
|
+
* @see https://www.etsi.org/deliver/etsi_ts/119400_119499/119475/01.02.01_60/ts_119475v010201p.pdf
|
|
152
|
+
*/
|
|
153
|
+
/**
|
|
154
|
+
* Multilingual string schema (B.2.6 Class MultiLangString)
|
|
155
|
+
*/
|
|
156
|
+
const MultiLangStringSchema = z.object({
|
|
157
|
+
/** Language code per BCP47/RFC 5646 */
|
|
158
|
+
lang: z.string().min(2),
|
|
159
|
+
/** Localized string value */
|
|
160
|
+
value: z.string().min(1)
|
|
161
|
+
});
|
|
162
|
+
/**
|
|
163
|
+
* Supervisory Authority schema for Data Protection Authority
|
|
164
|
+
*/
|
|
165
|
+
const SupervisoryAuthoritySchema = z.object({
|
|
166
|
+
/** Email address of the Data Protection Authority */
|
|
167
|
+
email: z.email().optional(),
|
|
168
|
+
/** Telephone number of the Data Protection Authority */
|
|
169
|
+
phone: z.string().optional(),
|
|
170
|
+
/** URL of web form provided by the Data Protection Authority */
|
|
171
|
+
uri: z.url().optional()
|
|
172
|
+
});
|
|
173
|
+
/**
|
|
174
|
+
* Claim schema for credential attribute specification (B.2.10 Class Claim).
|
|
175
|
+
*
|
|
176
|
+
* `path` follows the DCQL claims path pointer: string for an object key, integer for an
|
|
177
|
+
* array index, null to select every element of an array.
|
|
178
|
+
*/
|
|
179
|
+
const ClaimSchema = z.object({
|
|
180
|
+
/** Path pointer that specifies the path to a claim within the Credential */
|
|
181
|
+
path: z.array(z.union([
|
|
182
|
+
z.string(),
|
|
183
|
+
z.number().int(),
|
|
184
|
+
z.null()
|
|
185
|
+
])).min(1),
|
|
186
|
+
/** Array of expected values of the claim */
|
|
187
|
+
values: z.array(z.union([
|
|
188
|
+
z.string(),
|
|
189
|
+
z.number(),
|
|
190
|
+
z.boolean()
|
|
191
|
+
])).optional()
|
|
192
|
+
});
|
|
193
|
+
/**
|
|
194
|
+
* Credential schema for attestations (B.2.9 Class Credential).
|
|
195
|
+
*
|
|
196
|
+
* The subfield is named `claim` (singular) even though it holds an array. This follows
|
|
197
|
+
* ETSI TS 119 475 v1.2.1 Tables 8 and 9 and Annex C verbatim; see the package README.
|
|
198
|
+
*/
|
|
199
|
+
const CredentialSchema = z.object({
|
|
200
|
+
/** Format of the attestation (e.g., "dc+sd-jwt", "mso_mdoc") */
|
|
201
|
+
format: z.string().min(1),
|
|
202
|
+
/** Object defining additional properties per Credential Format */
|
|
203
|
+
meta: z.record(z.string(), z.unknown()),
|
|
204
|
+
/** Array of claim objects specifying requestable attributes */
|
|
205
|
+
claim: z.array(ClaimSchema).optional()
|
|
206
|
+
});
|
|
207
|
+
/** Credential issued by the WRP for `provides_attestations` (Table 8) */
|
|
208
|
+
const ProvidedAttestationSchema = CredentialSchema.omit({ claim: true }).strict();
|
|
209
|
+
/**
|
|
210
|
+
* Status list reference for certificate validity
|
|
211
|
+
*/
|
|
212
|
+
const StatusListSchema = z.object({
|
|
213
|
+
/** Index in the status list */
|
|
214
|
+
idx: z.number().int().nonnegative(),
|
|
215
|
+
/** URI to the status list */
|
|
216
|
+
uri: z.url()
|
|
217
|
+
});
|
|
218
|
+
/**
|
|
219
|
+
* Status schema for WRPRC validity
|
|
220
|
+
*/
|
|
221
|
+
const StatusSchema = z.object({ status_list: StatusListSchema });
|
|
222
|
+
/**
|
|
223
|
+
* Intermediary information when WRP acts through an intermediary (Table 10).
|
|
224
|
+
* The intermediary's semantic identifier comes from its own WRPAC.
|
|
225
|
+
*
|
|
226
|
+
* The common name subfield is `sname` per the normative Table 10. The informative
|
|
227
|
+
* Annex C example spells it `name`; see the package README.
|
|
228
|
+
*/
|
|
229
|
+
const IntermediarySchema = z.object({
|
|
230
|
+
/** Semantic identifier of the intermediary (from the intermediary's WRPAC) */
|
|
231
|
+
sub: z.string().min(1),
|
|
232
|
+
/** Common name of the intermediary */
|
|
233
|
+
sname: z.string().min(1)
|
|
234
|
+
});
|
|
235
|
+
/**
|
|
236
|
+
* WRPRC Payload schema according to ETSI TS 119 475 clause 5.2.4
|
|
237
|
+
*/
|
|
238
|
+
const WRPRCPayloadSchema = z.object({
|
|
239
|
+
/** The subject trade name of the WRPRC (B.2.1 tradeName) */
|
|
240
|
+
name: z.string().min(1),
|
|
241
|
+
/** Legal name for legal person (B.2.3 legalName) */
|
|
242
|
+
sub_ln: z.string().optional(),
|
|
243
|
+
/** Given name for natural person (B.2.4 givenName) */
|
|
244
|
+
sub_gn: z.string().optional(),
|
|
245
|
+
/** Family name for natural person (B.2.4 familyName) */
|
|
246
|
+
sub_fn: z.string().optional(),
|
|
247
|
+
/** WRP identifier following semantic identifier rules */
|
|
248
|
+
sub: z.string().min(1),
|
|
249
|
+
/** Country code (ISO 3166-1 Alpha-2) */
|
|
250
|
+
country: z.string().length(2),
|
|
251
|
+
/** URL pointing to the national registry API endpoint */
|
|
252
|
+
registry_uri: z.url(),
|
|
253
|
+
/** Descriptions of the services provided by the WRP (array of arrays of localized values) */
|
|
254
|
+
srv_description: z.array(z.array(MultiLangStringSchema)).optional(),
|
|
255
|
+
/** List of entitlements assigned to the WRP (Annex A.2 URI or OID form) */
|
|
256
|
+
entitlements: z.array(z.string().min(1)).min(1),
|
|
257
|
+
/** URL to the WRP's privacy policy */
|
|
258
|
+
privacy_policy: z.url().optional(),
|
|
259
|
+
/** URL general-purpose web address */
|
|
260
|
+
info_uri: z.url().optional(),
|
|
261
|
+
/** URL or email for data deletion/portability requests */
|
|
262
|
+
support_uri: z.url().or(z.email()).optional(),
|
|
263
|
+
/** Data Protection Authority supervising the WRP */
|
|
264
|
+
supervisory_authority: SupervisoryAuthoritySchema.optional(),
|
|
265
|
+
/** Policy identifier as defined in clause 6.1.3 */
|
|
266
|
+
policy_id: z.array(z.string()).optional(),
|
|
267
|
+
/** URL to the certificate policy and practice statement */
|
|
268
|
+
certificate_policy: z.url().optional(),
|
|
269
|
+
/**
|
|
270
|
+
* Unique identifier of this WRPRC (RFC 7519 `jti`).
|
|
271
|
+
*
|
|
272
|
+
* Not listed in ETSI TS 119 475, which leaves the certificate without an identifier
|
|
273
|
+
* to reference it by. Optional so that WRPRCs issued without one still validate.
|
|
274
|
+
*/
|
|
275
|
+
jti: z.string().min(1).optional(),
|
|
276
|
+
/** Issuance time as a Unix timestamp (Table 7) */
|
|
277
|
+
iat: z.number().int().positive(),
|
|
278
|
+
/** Status list for WRPRC validity */
|
|
279
|
+
status: StatusSchema.optional(),
|
|
280
|
+
/** Purpose of the intended data processing (localized values) */
|
|
281
|
+
purpose: z.array(MultiLangStringSchema).optional(),
|
|
282
|
+
/** Set of credentials intended to be requested by the WRP */
|
|
283
|
+
credentials: z.array(CredentialSchema).optional(),
|
|
284
|
+
/**
|
|
285
|
+
* Set of credentials issued by the WRP (for attestation providers, Table 8).
|
|
286
|
+
* These entries contain only `format` and `meta`; claim queries are not present.
|
|
287
|
+
*/
|
|
288
|
+
provides_attestations: z.array(ProvidedAttestationSchema).optional(),
|
|
289
|
+
/** Intermediary information when WRP acts through an intermediary (Table 10) */
|
|
290
|
+
intermediary: IntermediarySchema.optional(),
|
|
291
|
+
/** Whether the WRP is a public sector body (Table 10) */
|
|
292
|
+
public_body: z.boolean().optional(),
|
|
293
|
+
/** Expiry as a Unix timestamp; at most 12 months after `iat` (Table 10, GEN-5.2.4-08) */
|
|
294
|
+
exp: z.number().int().positive().optional(),
|
|
295
|
+
/** Identifier of the intended use, present only if provided by the registry (Table 9) */
|
|
296
|
+
intended_use_id: z.string().min(1).optional()
|
|
297
|
+
});
|
|
298
|
+
/**
|
|
299
|
+
* Signature algorithms permitted for JAdES B-B signatures (ETSI TS 119 182-1 clause 5.1.2),
|
|
300
|
+
* referenced by GEN-5.2.1-04.
|
|
301
|
+
*/
|
|
302
|
+
const WRPRC_JWS_ALGORITHMS = [
|
|
303
|
+
"ES256",
|
|
304
|
+
"ES384",
|
|
305
|
+
"ES512",
|
|
306
|
+
"PS256",
|
|
307
|
+
"PS384",
|
|
308
|
+
"PS512",
|
|
309
|
+
"RS256",
|
|
310
|
+
"RS384",
|
|
311
|
+
"RS512",
|
|
312
|
+
"EdDSA"
|
|
313
|
+
];
|
|
314
|
+
/**
|
|
315
|
+
* JWT Header schema according to ETSI TS 119 475 clause 5.2.2 (Table 5).
|
|
316
|
+
*
|
|
317
|
+
* Table 5 lists `typ`, `alg` and `x5c` as the minimum set. GEN-5.2.1-04 additionally
|
|
318
|
+
* requires a JAdES B-B signature, whose protected header carries `iat` as the claimed
|
|
319
|
+
* signing time (ETSI TS 119 182-1 clause 5.1.9). The informative Annex C header omits it.
|
|
320
|
+
*/
|
|
321
|
+
const WRPRCJWTHeaderSchema = z.object({
|
|
322
|
+
/** Type of the Web Token - must be "rc-wrp+jwt" for JWT */
|
|
323
|
+
typ: z.literal("rc-wrp+jwt"),
|
|
324
|
+
/** Algorithm used to sign the JWT */
|
|
325
|
+
alg: z.enum(WRPRC_JWS_ALGORITHMS),
|
|
326
|
+
/** Certificate chain to verify the JWT */
|
|
327
|
+
x5c: z.array(z.string()).min(1),
|
|
328
|
+
/** Key ID */
|
|
329
|
+
kid: z.string().optional(),
|
|
330
|
+
/** JAdES claimed signing time as a Unix timestamp */
|
|
331
|
+
iat: z.number().int().positive()
|
|
332
|
+
});
|
|
333
|
+
/**
|
|
334
|
+
* CWT Header schema according to ETSI TS 119 475 clause 5.2.3 (Table 6).
|
|
335
|
+
*
|
|
336
|
+
* GEN-5.2.1-05 requires an advanced electronic signature per RFC 9052 and RFC 9360
|
|
337
|
+
* rather than a JAdES profile, so no claimed signing time is imposed here.
|
|
338
|
+
*/
|
|
339
|
+
const WRPRCCWTHeaderSchema = z.object({
|
|
340
|
+
/** Type of the Web Token - must be "rc-wrp+cwt" for CWT */
|
|
341
|
+
typ: z.literal("rc-wrp+cwt"),
|
|
342
|
+
/** Algorithm used to sign the CWT per RFC 9052 */
|
|
343
|
+
alg: z.number().int(),
|
|
344
|
+
/** Certificate chain to verify the CWT per RFC 9360 */
|
|
345
|
+
x5chain: z.array(z.instanceof(Uint8Array)).min(1),
|
|
346
|
+
/** Unix timestamp indicating when the WRPRC was issued (payload field, not in Table 6) */
|
|
347
|
+
iat: z.number().int().positive().optional()
|
|
348
|
+
});
|
|
349
|
+
z.object({
|
|
350
|
+
header: WRPRCJWTHeaderSchema,
|
|
351
|
+
payload: WRPRCPayloadSchema
|
|
352
|
+
});
|
|
353
|
+
z.object({
|
|
354
|
+
header: WRPRCCWTHeaderSchema,
|
|
355
|
+
payload: WRPRCPayloadSchema
|
|
356
|
+
});
|
|
357
|
+
/**
|
|
358
|
+
* Schema for legal person WRPRC subject
|
|
359
|
+
*/
|
|
360
|
+
const LegalPersonSubjectSchema = WRPRCPayloadSchema.extend({ sub_ln: z.string().min(1) }).omit({
|
|
361
|
+
sub_gn: true,
|
|
362
|
+
sub_fn: true
|
|
363
|
+
});
|
|
364
|
+
/**
|
|
365
|
+
* Schema for natural person WRPRC subject
|
|
366
|
+
*/
|
|
367
|
+
const NaturalPersonSubjectSchema = WRPRCPayloadSchema.extend({
|
|
368
|
+
sub_gn: z.string().min(1),
|
|
369
|
+
sub_fn: z.string().min(1)
|
|
370
|
+
}).omit({ sub_ln: true });
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/wrprc-exception.ts
|
|
373
|
+
/**
|
|
374
|
+
* WRPRCException is a custom error class for WRPRC-related exceptions.
|
|
375
|
+
*/
|
|
376
|
+
var WRPRCException = class WRPRCException extends IdentityException {
|
|
377
|
+
constructor(message, details) {
|
|
378
|
+
super(message, details);
|
|
379
|
+
Object.setPrototypeOf(this, WRPRCException.prototype);
|
|
380
|
+
this.name = "WRPRCException";
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
//#endregion
|
|
384
|
+
//#region src/builders.ts
|
|
385
|
+
/**
|
|
386
|
+
* WRPRC Builders
|
|
387
|
+
*
|
|
388
|
+
* Fluent builders for creating ETSI TS 119 475 Wallet-Relying Party Registration Certificates.
|
|
389
|
+
*
|
|
390
|
+
* @see https://www.etsi.org/deliver/etsi_ts/119400_119499/119475/01.02.01_60/ts_119475v010201p.pdf
|
|
391
|
+
*/
|
|
392
|
+
/**
|
|
393
|
+
* Builder for creating WRPRC payloads with a fluent API
|
|
394
|
+
*/
|
|
395
|
+
var WRPRCBuilder = class {
|
|
396
|
+
constructor() {
|
|
397
|
+
this.payload = {};
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Set the trade name (display name) of the WRP
|
|
401
|
+
*/
|
|
402
|
+
name(value) {
|
|
403
|
+
this.payload.name = value;
|
|
404
|
+
return this;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Set the legal name for a legal person WRP
|
|
408
|
+
*/
|
|
409
|
+
legalName(value) {
|
|
410
|
+
this.payload.sub_ln = value;
|
|
411
|
+
return this;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Set the given name for a natural person WRP
|
|
415
|
+
*/
|
|
416
|
+
givenName(value) {
|
|
417
|
+
this.payload.sub_gn = value;
|
|
418
|
+
return this;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Set the family name for a natural person WRP
|
|
422
|
+
*/
|
|
423
|
+
familyName(value) {
|
|
424
|
+
this.payload.sub_fn = value;
|
|
425
|
+
return this;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Set the WRP identifier (semantic identifier)
|
|
429
|
+
*
|
|
430
|
+
* @param identifier - The semantic identifier following ETSI EN 319 412-1
|
|
431
|
+
* Format: PREFIX (3 chars) + COUNTRY (2 chars) + "-" + ID
|
|
432
|
+
* Examples: "LEIXG-529900T8BM49AURSDO55", "TINIT-RSSMRA85T10A562S"
|
|
433
|
+
*/
|
|
434
|
+
identifier(identifier) {
|
|
435
|
+
this.payload.sub = identifier;
|
|
436
|
+
return this;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Set the country code (ISO 3166-1 Alpha-2)
|
|
440
|
+
*/
|
|
441
|
+
country(code) {
|
|
442
|
+
this.payload.country = code;
|
|
443
|
+
return this;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Set the URL to the national registry API endpoint
|
|
447
|
+
*/
|
|
448
|
+
registryUri(uri) {
|
|
449
|
+
this.payload.registry_uri = uri;
|
|
450
|
+
return this;
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Add a service description in a specific language.
|
|
454
|
+
*
|
|
455
|
+
* `srv_description` is an array of services, each localized into one or more languages
|
|
456
|
+
* (B.2.1). Consecutive calls localize the same service; repeating a language starts the
|
|
457
|
+
* next one.
|
|
458
|
+
*/
|
|
459
|
+
serviceDescription(description, lang = "en") {
|
|
460
|
+
const groups = this.payload.srv_description ?? [];
|
|
461
|
+
this.payload.srv_description = groups;
|
|
462
|
+
const current = groups[groups.length - 1];
|
|
463
|
+
if (current && !current.some((entry) => entry.lang === lang)) current.push({
|
|
464
|
+
lang,
|
|
465
|
+
value: description
|
|
466
|
+
});
|
|
467
|
+
else groups.push([{
|
|
468
|
+
lang,
|
|
469
|
+
value: description
|
|
470
|
+
}]);
|
|
471
|
+
return this;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Add multiple service descriptions (one array per service, with multiple languages)
|
|
475
|
+
*/
|
|
476
|
+
addServiceDescriptions(descriptions) {
|
|
477
|
+
this.payload.srv_description = this.payload.srv_description ?? [];
|
|
478
|
+
this.payload.srv_description.push(descriptions);
|
|
479
|
+
return this;
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Add an entitlement
|
|
483
|
+
*
|
|
484
|
+
* @param entitlement - The entitlement URI or key from WRP_ENTITLEMENTS
|
|
485
|
+
*/
|
|
486
|
+
addEntitlement(entitlement) {
|
|
487
|
+
this.payload.entitlements = this.payload.entitlements ?? [];
|
|
488
|
+
if (!this.payload.entitlements.includes(entitlement)) this.payload.entitlements.push(entitlement);
|
|
489
|
+
return this;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Set all entitlements (replaces existing)
|
|
493
|
+
*/
|
|
494
|
+
entitlements(entitlements) {
|
|
495
|
+
this.payload.entitlements = [...entitlements];
|
|
496
|
+
return this;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Set the privacy policy URL
|
|
500
|
+
*/
|
|
501
|
+
privacyPolicy(uri) {
|
|
502
|
+
this.payload.privacy_policy = uri;
|
|
503
|
+
return this;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Set the info URI (general-purpose web address)
|
|
507
|
+
*/
|
|
508
|
+
infoUri(uri) {
|
|
509
|
+
this.payload.info_uri = uri;
|
|
510
|
+
return this;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Set the support URI for data requests
|
|
514
|
+
*/
|
|
515
|
+
supportUri(uri) {
|
|
516
|
+
this.payload.support_uri = uri;
|
|
517
|
+
return this;
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Set the supervisory authority (Data Protection Authority)
|
|
521
|
+
*/
|
|
522
|
+
supervisoryAuthority(authority) {
|
|
523
|
+
this.payload.supervisory_authority = authority;
|
|
524
|
+
return this;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Set the policy ID(s)
|
|
528
|
+
*/
|
|
529
|
+
policyId(ids) {
|
|
530
|
+
this.payload.policy_id = ids;
|
|
531
|
+
return this;
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Set the certificate policy URL
|
|
535
|
+
*/
|
|
536
|
+
certificatePolicy(uri) {
|
|
537
|
+
this.payload.certificate_policy = uri;
|
|
538
|
+
return this;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Set the unique identifier of the certificate (`jti`)
|
|
542
|
+
* If not set, the payload carries no identifier
|
|
543
|
+
*
|
|
544
|
+
* @param id - Identifier from the issuing registry's own scheme.
|
|
545
|
+
*/
|
|
546
|
+
certificateId(id) {
|
|
547
|
+
this.payload.jti = id;
|
|
548
|
+
return this;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Set the issued-at timestamp (Unix timestamp)
|
|
552
|
+
* If not set, will default to current time when building
|
|
553
|
+
*/
|
|
554
|
+
issuedAt(timestamp) {
|
|
555
|
+
this.payload.iat = typeof timestamp === "number" ? timestamp : dateToSeconds(timestamp);
|
|
556
|
+
return this;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Set the status reference for certificate validity
|
|
560
|
+
*/
|
|
561
|
+
status(status) {
|
|
562
|
+
this.payload.status = status;
|
|
563
|
+
return this;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Add a purpose description
|
|
567
|
+
*/
|
|
568
|
+
addPurpose(description, lang = "en") {
|
|
569
|
+
this.payload.purpose = this.payload.purpose ?? [];
|
|
570
|
+
this.payload.purpose.push({
|
|
571
|
+
lang,
|
|
572
|
+
value: description
|
|
573
|
+
});
|
|
574
|
+
return this;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Set all purposes (replaces existing)
|
|
578
|
+
*/
|
|
579
|
+
purposes(purposes) {
|
|
580
|
+
this.payload.purpose = purposes;
|
|
581
|
+
return this;
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Add a credential that the WRP intends to request
|
|
585
|
+
*/
|
|
586
|
+
addCredential(credential) {
|
|
587
|
+
this.payload.credentials = this.payload.credentials ?? [];
|
|
588
|
+
this.payload.credentials.push(credential);
|
|
589
|
+
return this;
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Add a credential issued by the WRP (Table 8, `provides_attestations`).
|
|
593
|
+
*/
|
|
594
|
+
addProvidedAttestation(attestation) {
|
|
595
|
+
this.payload.provides_attestations = this.payload.provides_attestations ?? [];
|
|
596
|
+
this.payload.provides_attestations.push(attestation);
|
|
597
|
+
return this;
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* Set intermediary information
|
|
601
|
+
*/
|
|
602
|
+
intermediary(intermediary) {
|
|
603
|
+
this.payload.intermediary = intermediary;
|
|
604
|
+
return this;
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Mark the WRP as a public sector body (Table 10)
|
|
608
|
+
*/
|
|
609
|
+
publicBody(value) {
|
|
610
|
+
this.payload.public_body = value;
|
|
611
|
+
return this;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Set the expiry timestamp (Table 10); must be at most 12 months after iat
|
|
615
|
+
*/
|
|
616
|
+
expiresAt(timestamp) {
|
|
617
|
+
this.payload.exp = typeof timestamp === "number" ? timestamp : dateToSeconds(timestamp);
|
|
618
|
+
return this;
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Set the intended-use identifier, used to fetch the intended use from the registry (Table 9)
|
|
622
|
+
*/
|
|
623
|
+
intendedUseId(id) {
|
|
624
|
+
this.payload.intended_use_id = id;
|
|
625
|
+
return this;
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Build the WRPRC payload
|
|
629
|
+
*
|
|
630
|
+
* @throws WRPRCException if the payload is invalid
|
|
631
|
+
*/
|
|
632
|
+
build() {
|
|
633
|
+
if (!this.payload.iat) this.payload.iat = nowInSeconds();
|
|
634
|
+
const result = WRPRCPayloadSchema.safeParse(this.payload);
|
|
635
|
+
if (!result.success) throw new WRPRCException(`Invalid WRPRC payload:\n${result.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("\n")}`, result.error.issues);
|
|
636
|
+
return result.data;
|
|
637
|
+
}
|
|
638
|
+
};
|
|
639
|
+
/**
|
|
640
|
+
* Builder for creating Credential objects
|
|
641
|
+
*/
|
|
642
|
+
var CredentialBuilder = class {
|
|
643
|
+
constructor() {
|
|
644
|
+
this.credential = {};
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Set the credential format
|
|
648
|
+
*
|
|
649
|
+
* @param format - The format identifier (e.g., "dc+sd-jwt", "mso_mdoc")
|
|
650
|
+
*/
|
|
651
|
+
format(format) {
|
|
652
|
+
this.credential.format = format;
|
|
653
|
+
return this;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Set the credential metadata
|
|
657
|
+
*
|
|
658
|
+
* @param meta - Metadata object per credential format specification
|
|
659
|
+
*/
|
|
660
|
+
meta(meta) {
|
|
661
|
+
this.credential.meta = meta;
|
|
662
|
+
return this;
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Set SD-JWT credential metadata (vct_values)
|
|
666
|
+
*/
|
|
667
|
+
sdJwtMeta(vctValues) {
|
|
668
|
+
this.credential.format = "dc+sd-jwt";
|
|
669
|
+
this.credential.meta = { vct_values: vctValues };
|
|
670
|
+
return this;
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Set mDL/mDoc credential metadata (doctype_value)
|
|
674
|
+
*/
|
|
675
|
+
mdocMeta(doctypeValue) {
|
|
676
|
+
this.credential.format = "mso_mdoc";
|
|
677
|
+
this.credential.meta = { doctype_value: doctypeValue };
|
|
678
|
+
return this;
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Add a claim to request
|
|
682
|
+
*/
|
|
683
|
+
addClaim(claim) {
|
|
684
|
+
this.credential.claim = this.credential.claim ?? [];
|
|
685
|
+
this.credential.claim.push(claim);
|
|
686
|
+
return this;
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Add a simple path claim. Use an integer for an array index and null to select
|
|
690
|
+
* every element of an array, per the DCQL claims path pointer.
|
|
691
|
+
*/
|
|
692
|
+
addPathClaim(...path) {
|
|
693
|
+
this.credential.claim = this.credential.claim ?? [];
|
|
694
|
+
this.credential.claim.push({ path });
|
|
695
|
+
return this;
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Build the Credential object
|
|
699
|
+
*/
|
|
700
|
+
build() {
|
|
701
|
+
if (!this.credential.format) throw new WRPRCException("Credential format is required");
|
|
702
|
+
if (!this.credential.meta) throw new WRPRCException("Credential meta is required");
|
|
703
|
+
return this.credential;
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
/**
|
|
707
|
+
* Create a new WRPRCBuilder
|
|
708
|
+
*/
|
|
709
|
+
function wrprc() {
|
|
710
|
+
return new WRPRCBuilder();
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Create a new CredentialBuilder
|
|
714
|
+
*/
|
|
715
|
+
function credential() {
|
|
716
|
+
return new CredentialBuilder();
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
* Create a WRPRC payload for a legal person
|
|
720
|
+
*/
|
|
721
|
+
function createLegalPersonWRPRC(input) {
|
|
722
|
+
return wrprc().name(input.name).legalName(input.legalName).identifier(input.identifier).country(input.country).registryUri(input.registryUri).entitlements(input.entitlements).build();
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Create a WRPRC payload for a natural person
|
|
726
|
+
*/
|
|
727
|
+
function createNaturalPersonWRPRC(input) {
|
|
728
|
+
return wrprc().name(input.name).givenName(input.givenName).familyName(input.familyName).identifier(input.identifier).country(input.country).registryUri(input.registryUri).entitlements(input.entitlements).build();
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Create a simple service provider WRPRC
|
|
732
|
+
*/
|
|
733
|
+
function createServiceProviderWRPRC(name, legalName, identifier, country, registryUri) {
|
|
734
|
+
return wrprc().name(name).legalName(legalName).identifier(identifier).country(country).registryUri(registryUri).addEntitlement(ENTITLEMENT_SERVICE_PROVIDER).build();
|
|
735
|
+
}
|
|
736
|
+
//#endregion
|
|
737
|
+
//#region src/dialect.ts
|
|
738
|
+
/**
|
|
739
|
+
* Wire dialects understood by this package.
|
|
740
|
+
*/
|
|
741
|
+
const WRPRC_DIALECTS = {
|
|
742
|
+
/** ETSI TS 119 475 v1.2.1 as published. The default for everything this package emits. */
|
|
743
|
+
CURRENT: "ts119475-1.2.1",
|
|
744
|
+
/**
|
|
745
|
+
* Anticipated corrections: `claims` instead of `claim`, `intermediary.name` instead of
|
|
746
|
+
* `intermediary.sname`.
|
|
747
|
+
*
|
|
748
|
+
* Unstable. No published edition defines this yet, so a certificate emitted in this
|
|
749
|
+
* dialect may match neither the current specification nor its eventual correction.
|
|
750
|
+
*/
|
|
751
|
+
DRAFT: "draft"
|
|
752
|
+
};
|
|
753
|
+
function isRecord(value) {
|
|
754
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
755
|
+
}
|
|
756
|
+
/** Rename a key, leaving the value alone if the target name is already taken. */
|
|
757
|
+
function renameKey(value, from, to) {
|
|
758
|
+
if (!isRecord(value) || !(from in value) || to in value) return value;
|
|
759
|
+
const { [from]: moved, ...rest } = value;
|
|
760
|
+
return {
|
|
761
|
+
...rest,
|
|
762
|
+
[to]: moved
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
function renameInCredentials(value, from, to) {
|
|
766
|
+
if (!Array.isArray(value)) return value;
|
|
767
|
+
return value.map((entry) => renameKey(entry, from, to));
|
|
768
|
+
}
|
|
769
|
+
function mapPayload(payload, claimFrom, claimTo, nameFrom, nameTo) {
|
|
770
|
+
if (!isRecord(payload)) return payload;
|
|
771
|
+
const result = { ...payload };
|
|
772
|
+
if ("credentials" in result) result.credentials = renameInCredentials(result.credentials, claimFrom, claimTo);
|
|
773
|
+
if ("intermediary" in result) result.intermediary = renameKey(result.intermediary, nameFrom, nameTo);
|
|
774
|
+
return result;
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Accept the anticipated `claims` and `intermediary.name` spellings when parsing, so a
|
|
778
|
+
* certificate from an SDK that already applies the corrections still validates.
|
|
779
|
+
*/
|
|
780
|
+
function normalizeWRPRCPayload(payload) {
|
|
781
|
+
return mapPayload(payload, "claims", "claim", "name", "sname");
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Serialize a canonical payload in the requested dialect.
|
|
785
|
+
*/
|
|
786
|
+
function toWRPRCDialect(payload, dialect) {
|
|
787
|
+
if (dialect === WRPRC_DIALECTS.CURRENT) return payload;
|
|
788
|
+
return mapPayload(payload, "claim", "claims", "sname", "name");
|
|
789
|
+
}
|
|
790
|
+
//#endregion
|
|
791
|
+
//#region src/validator.ts
|
|
792
|
+
/**
|
|
793
|
+
* WRPRC Validator
|
|
794
|
+
*
|
|
795
|
+
* Validation functions for ETSI TS 119 475 Wallet-Relying Party Registration Certificates.
|
|
796
|
+
*
|
|
797
|
+
* @see https://www.etsi.org/deliver/etsi_ts/119400_119499/119475/01.02.01_60/ts_119475v010201p.pdf
|
|
798
|
+
*/
|
|
799
|
+
/**
|
|
800
|
+
* Codes for the semantic checks this validator performs, beyond schema parsing.
|
|
801
|
+
*
|
|
802
|
+
* Exposed so a calling application can branch on a specific failure rather than
|
|
803
|
+
* matching on message text. Requirement identifiers from ETSI TS 119 475 v1.2.1
|
|
804
|
+
* are given where a code maps to one.
|
|
805
|
+
*/
|
|
806
|
+
const WRPRC_VALIDATION_CODES = {
|
|
807
|
+
/** No entitlement present (GEN-5.2.4-03) */
|
|
808
|
+
MISSING_ENTITLEMENT: "missing_entitlement",
|
|
809
|
+
/** Sub-entitlement present without its base entitlement (GEN-5.2.4-04) */
|
|
810
|
+
MISSING_BASE_ENTITLEMENT: "missing_base_entitlement",
|
|
811
|
+
/** Entitlement outside Annex A, which may be a national or EU extension */
|
|
812
|
+
UNKNOWN_ENTITLEMENT: "unknown_entitlement",
|
|
813
|
+
/** Attestation provider without provides_attestations (GEN-5.2.4-05) */
|
|
814
|
+
MISSING_PROVIDES_ATTESTATIONS: "missing_provides_attestations",
|
|
815
|
+
/** Service provider without credentials (GEN-5.2.4-06, Table 9) */
|
|
816
|
+
MISSING_CREDENTIALS: "missing_credentials",
|
|
817
|
+
/** Service provider without purpose (GEN-5.2.4-06, Table 9) */
|
|
818
|
+
MISSING_PURPOSE: "missing_purpose",
|
|
819
|
+
/** sub does not follow the semantic identifier format of clause 5.1 */
|
|
820
|
+
INVALID_SEMANTIC_IDENTIFIER: "invalid_semantic_identifier",
|
|
821
|
+
/** sub uses initial characters outside Tables 2 and 4, which may be a national scheme */
|
|
822
|
+
UNKNOWN_IDENTIFIER_PREFIX: "unknown_identifier_prefix",
|
|
823
|
+
/** sub parses but deviates from the expected identifier shape */
|
|
824
|
+
IDENTIFIER_FORMAT_WARNING: "identifier_format_warning",
|
|
825
|
+
/** exp more than 12 months after iat (GEN-5.2.4-08) */
|
|
826
|
+
EXP_TOO_LATE: "exp_too_late",
|
|
827
|
+
/** exp at or before iat */
|
|
828
|
+
EXP_BEFORE_IAT: "exp_before_iat",
|
|
829
|
+
/** Selected WRPRC identity does not match the access certificate subject */
|
|
830
|
+
ACCESS_CERTIFICATE_IDENTITY_MISMATCH: "access_certificate_identity_mismatch"
|
|
831
|
+
};
|
|
832
|
+
/**
|
|
833
|
+
* Entitlement-driven checks: GEN-5.2.4-03 to GEN-5.2.4-06.
|
|
834
|
+
*/
|
|
835
|
+
function validateEntitlementRequirements(payload, errors, warnings) {
|
|
836
|
+
for (const entitlement of payload.entitlements) if (!(ALL_ENTITLEMENTS.includes(entitlement) || ALL_PSP_SUB_ENTITLEMENTS.includes(entitlement))) warnings.push({
|
|
837
|
+
path: ["entitlements"],
|
|
838
|
+
message: `Unknown entitlement URI: ${entitlement}. This may be a national or EU-defined extension.`,
|
|
839
|
+
code: WRPRC_VALIDATION_CODES.UNKNOWN_ENTITLEMENT
|
|
840
|
+
});
|
|
841
|
+
if (payload.entitlements.length === 0) errors.push({
|
|
842
|
+
path: ["entitlements"],
|
|
843
|
+
message: "At least one entitlement must be specified (GEN-5.2.4-03)",
|
|
844
|
+
code: WRPRC_VALIDATION_CODES.MISSING_ENTITLEMENT
|
|
845
|
+
});
|
|
846
|
+
const hasSubEntitlement = payload.entitlements.some((e) => ALL_PSP_SUB_ENTITLEMENTS.includes(e));
|
|
847
|
+
const hasServiceProvider = payload.entitlements.includes("https://uri.etsi.org/19475/Entitlement/Service_Provider");
|
|
848
|
+
if (hasSubEntitlement && !hasServiceProvider) errors.push({
|
|
849
|
+
path: ["entitlements"],
|
|
850
|
+
message: "Service provider sub-entitlements require Service_Provider entitlement (GEN-5.2.4-04)",
|
|
851
|
+
code: WRPRC_VALIDATION_CODES.MISSING_BASE_ENTITLEMENT
|
|
852
|
+
});
|
|
853
|
+
if (payload.entitlements.some((e) => ATTESTATION_PROVIDER_ENTITLEMENTS.includes(e)) && !payload.provides_attestations) warnings.push({
|
|
854
|
+
path: ["provides_attestations"],
|
|
855
|
+
message: "Attestation providers should include provides_attestations field (GEN-5.2.4-05). This is recommended but not required.",
|
|
856
|
+
code: WRPRC_VALIDATION_CODES.MISSING_PROVIDES_ATTESTATIONS
|
|
857
|
+
});
|
|
858
|
+
if (hasServiceProvider && !payload.credentials) warnings.push({
|
|
859
|
+
path: ["credentials"],
|
|
860
|
+
message: "Service provider WRPRCs should include the credentials field (GEN-5.2.4-06)",
|
|
861
|
+
code: WRPRC_VALIDATION_CODES.MISSING_CREDENTIALS
|
|
862
|
+
});
|
|
863
|
+
if (hasServiceProvider && !payload.purpose) warnings.push({
|
|
864
|
+
path: ["purpose"],
|
|
865
|
+
message: "Service provider WRPRCs should include the purpose field (GEN-5.2.4-06)",
|
|
866
|
+
code: WRPRC_VALIDATION_CODES.MISSING_PURPOSE
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Validate a WRPRC payload against the schema and semantic rules.
|
|
871
|
+
*
|
|
872
|
+
* @param accessCertificateSub Required subject from the presented access certificate.
|
|
873
|
+
*/
|
|
874
|
+
function validateWRPRCPayload(payload, accessCertificateSub) {
|
|
875
|
+
const result = WRPRCPayloadSchema.safeParse(normalizeWRPRCPayload(payload));
|
|
876
|
+
const errors = [];
|
|
877
|
+
const warnings = [];
|
|
878
|
+
if (!result.success) {
|
|
879
|
+
for (const issue of result.error.issues) errors.push({
|
|
880
|
+
path: issue.path.map(String),
|
|
881
|
+
message: issue.message,
|
|
882
|
+
code: issue.code
|
|
883
|
+
});
|
|
884
|
+
return {
|
|
885
|
+
valid: false,
|
|
886
|
+
errors,
|
|
887
|
+
warnings
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
const validPayload = result.data;
|
|
891
|
+
validateEntitlementRequirements(validPayload, errors, warnings);
|
|
892
|
+
const subResult = validateSemanticIdentifier(validPayload.sub);
|
|
893
|
+
if (!subResult.valid) errors.push({
|
|
894
|
+
path: ["sub"],
|
|
895
|
+
message: subResult.message,
|
|
896
|
+
code: WRPRC_VALIDATION_CODES.INVALID_SEMANTIC_IDENTIFIER
|
|
897
|
+
});
|
|
898
|
+
else if (subResult.unknownPrefix) warnings.push({
|
|
899
|
+
path: ["sub"],
|
|
900
|
+
message: `Unknown semantic identifier initial characters: ${subResult.unknownPrefix}. This may be a national scheme (ETSI EN 319 412-1).`,
|
|
901
|
+
code: WRPRC_VALIDATION_CODES.UNKNOWN_IDENTIFIER_PREFIX
|
|
902
|
+
});
|
|
903
|
+
if (typeof validPayload.exp === "number") {
|
|
904
|
+
const maxExp = secondsToDate(validPayload.iat);
|
|
905
|
+
maxExp.setUTCFullYear(maxExp.getUTCFullYear() + 1);
|
|
906
|
+
if (secondsToDate(validPayload.exp) > maxExp) errors.push({
|
|
907
|
+
path: ["exp"],
|
|
908
|
+
message: "exp must be at most 12 months after iat (GEN-5.2.4-08)",
|
|
909
|
+
code: WRPRC_VALIDATION_CODES.EXP_TOO_LATE
|
|
910
|
+
});
|
|
911
|
+
if (validPayload.exp <= validPayload.iat) errors.push({
|
|
912
|
+
path: ["exp"],
|
|
913
|
+
message: "exp must be after iat",
|
|
914
|
+
code: WRPRC_VALIDATION_CODES.EXP_BEFORE_IAT
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
const expectedAccessCertificateSub = validPayload.intermediary?.sub ?? validPayload.sub;
|
|
918
|
+
const identityPath = validPayload.intermediary ? ["intermediary", "sub"] : ["sub"];
|
|
919
|
+
if (accessCertificateSub !== expectedAccessCertificateSub) errors.push({
|
|
920
|
+
path: identityPath,
|
|
921
|
+
message: `${validPayload.intermediary ? "intermediary.sub" : "sub"} must match the access certificate subject`,
|
|
922
|
+
code: WRPRC_VALIDATION_CODES.ACCESS_CERTIFICATE_IDENTITY_MISMATCH
|
|
923
|
+
});
|
|
924
|
+
return {
|
|
925
|
+
valid: errors.length === 0,
|
|
926
|
+
errors,
|
|
927
|
+
warnings
|
|
928
|
+
};
|
|
929
|
+
}
|
|
930
|
+
/**
|
|
931
|
+
* Validate a JWT header for WRPRC
|
|
932
|
+
*/
|
|
933
|
+
function validateWRPRCJWTHeader(header) {
|
|
934
|
+
const result = WRPRCJWTHeaderSchema.safeParse(header);
|
|
935
|
+
const errors = [];
|
|
936
|
+
const warnings = [];
|
|
937
|
+
if (!result.success) for (const issue of result.error.issues) errors.push({
|
|
938
|
+
path: issue.path.map(String),
|
|
939
|
+
message: issue.message,
|
|
940
|
+
code: issue.code
|
|
941
|
+
});
|
|
942
|
+
return {
|
|
943
|
+
valid: errors.length === 0,
|
|
944
|
+
errors,
|
|
945
|
+
warnings
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Validate a complete WRPRC (header + payload)
|
|
950
|
+
*/
|
|
951
|
+
function validateWRPRC(header, payload, accessCertificateSub) {
|
|
952
|
+
const headerResult = validateWRPRCJWTHeader(header);
|
|
953
|
+
const payloadResult = validateWRPRCPayload(payload, accessCertificateSub);
|
|
954
|
+
const errors = [...headerResult.errors.map((e) => ({
|
|
955
|
+
...e,
|
|
956
|
+
path: ["header", ...e.path]
|
|
957
|
+
})), ...payloadResult.errors.map((e) => ({
|
|
958
|
+
...e,
|
|
959
|
+
path: ["payload", ...e.path]
|
|
960
|
+
}))];
|
|
961
|
+
const warnings = [...headerResult.warnings.map((w) => ({
|
|
962
|
+
...w,
|
|
963
|
+
path: ["header", ...w.path]
|
|
964
|
+
})), ...payloadResult.warnings.map((w) => ({
|
|
965
|
+
...w,
|
|
966
|
+
path: ["payload", ...w.path]
|
|
967
|
+
}))];
|
|
968
|
+
return {
|
|
969
|
+
valid: errors.length === 0,
|
|
970
|
+
errors,
|
|
971
|
+
warnings
|
|
972
|
+
};
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
* Check if payload represents a legal person
|
|
976
|
+
*/
|
|
977
|
+
function isLegalPersonWRPRC(payload) {
|
|
978
|
+
return typeof payload.sub_ln === "string" && payload.sub_ln.length > 0;
|
|
979
|
+
}
|
|
980
|
+
/**
|
|
981
|
+
* Check if payload represents a natural person
|
|
982
|
+
*/
|
|
983
|
+
function isNaturalPersonWRPRC(payload) {
|
|
984
|
+
return typeof payload.sub_gn === "string" && payload.sub_gn.length > 0 && typeof payload.sub_fn === "string" && payload.sub_fn.length > 0;
|
|
985
|
+
}
|
|
986
|
+
/**
|
|
987
|
+
* Validate as legal person WRPRC
|
|
988
|
+
*/
|
|
989
|
+
function validateLegalPersonWRPRC(payload) {
|
|
990
|
+
const result = LegalPersonSubjectSchema.safeParse(normalizeWRPRCPayload(payload));
|
|
991
|
+
const errors = [];
|
|
992
|
+
const warnings = [];
|
|
993
|
+
if (!result.success) for (const issue of result.error.issues) errors.push({
|
|
994
|
+
path: issue.path.map(String),
|
|
995
|
+
message: issue.message,
|
|
996
|
+
code: issue.code
|
|
997
|
+
});
|
|
998
|
+
if (result.success) {
|
|
999
|
+
const sub = result.data.sub;
|
|
1000
|
+
if (!/^(?:[A-Z]{3}|[A-Z]{2}:)[A-Z]{2}-.+$/.test(sub)) warnings.push({
|
|
1001
|
+
path: ["sub"],
|
|
1002
|
+
message: "Legal person identifier should follow format: PREFIX + COUNTRY + \"-\" + ID (e.g., \"LEIXG-529900T8BM49AURSDO55\")",
|
|
1003
|
+
code: WRPRC_VALIDATION_CODES.IDENTIFIER_FORMAT_WARNING
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
return {
|
|
1007
|
+
valid: errors.length === 0,
|
|
1008
|
+
errors,
|
|
1009
|
+
warnings
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Validate as natural person WRPRC
|
|
1014
|
+
*/
|
|
1015
|
+
function validateNaturalPersonWRPRC(payload) {
|
|
1016
|
+
const result = NaturalPersonSubjectSchema.safeParse(normalizeWRPRCPayload(payload));
|
|
1017
|
+
const errors = [];
|
|
1018
|
+
const warnings = [];
|
|
1019
|
+
if (!result.success) for (const issue of result.error.issues) errors.push({
|
|
1020
|
+
path: issue.path.map(String),
|
|
1021
|
+
message: issue.message,
|
|
1022
|
+
code: issue.code
|
|
1023
|
+
});
|
|
1024
|
+
if (result.success) {
|
|
1025
|
+
const sub = result.data.sub;
|
|
1026
|
+
if (!/^(?:[A-Z]{3}|[A-Z]{2}:)[A-Z]{2}-.+$/.test(sub)) warnings.push({
|
|
1027
|
+
path: ["sub"],
|
|
1028
|
+
message: "Natural person identifier should follow format: PREFIX + COUNTRY + \"-\" + ID (e.g., \"TINIT-RSSMRA85T10A562S\")",
|
|
1029
|
+
code: WRPRC_VALIDATION_CODES.IDENTIFIER_FORMAT_WARNING
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
return {
|
|
1033
|
+
valid: errors.length === 0,
|
|
1034
|
+
errors,
|
|
1035
|
+
warnings
|
|
1036
|
+
};
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Valid semantic identifier initial characters for legal persons
|
|
1040
|
+
* (Table 2, ETSI EN 319 412-1 clause 5.1.4)
|
|
1041
|
+
*/
|
|
1042
|
+
const LEGAL_PERSON_PREFIXES = [
|
|
1043
|
+
"EOR",
|
|
1044
|
+
"LEI",
|
|
1045
|
+
"NTR",
|
|
1046
|
+
"VAT",
|
|
1047
|
+
"EXC"
|
|
1048
|
+
];
|
|
1049
|
+
/**
|
|
1050
|
+
* Valid semantic identifier initial characters for natural persons
|
|
1051
|
+
* (Table 4, ETSI EN 319 412-1 clause 5.1.3)
|
|
1052
|
+
*/
|
|
1053
|
+
const NATURAL_PERSON_PREFIXES = [
|
|
1054
|
+
"TIN",
|
|
1055
|
+
"PAS",
|
|
1056
|
+
"IDC",
|
|
1057
|
+
"PNO"
|
|
1058
|
+
];
|
|
1059
|
+
/**
|
|
1060
|
+
* Validate a semantic identifier format
|
|
1061
|
+
*/
|
|
1062
|
+
function validateSemanticIdentifier(identifier) {
|
|
1063
|
+
const match = identifier.match(/^([A-Z]{3}|[A-Z]{2}:)([A-Z]{2})-(.+)$/);
|
|
1064
|
+
if (!match) return {
|
|
1065
|
+
valid: false,
|
|
1066
|
+
message: "Semantic identifier must follow format: PREFIX (3 chars) + COUNTRY (2 chars) + \"-\" + ID"
|
|
1067
|
+
};
|
|
1068
|
+
const [, prefix] = match;
|
|
1069
|
+
return {
|
|
1070
|
+
valid: true,
|
|
1071
|
+
message: "",
|
|
1072
|
+
...LEGAL_PERSON_PREFIXES.includes(prefix) || NATURAL_PERSON_PREFIXES.includes(prefix) ? {} : { unknownPrefix: prefix }
|
|
1073
|
+
};
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* Assert that a WRPRC payload is valid, throws WRPRCException if not
|
|
1077
|
+
*/
|
|
1078
|
+
function assertValidWRPRCPayload(payload, accessCertificateSub) {
|
|
1079
|
+
const result = validateWRPRCPayload(payload, accessCertificateSub);
|
|
1080
|
+
if (!result.valid) throw new WRPRCException(`Invalid WRPRC payload:\n${result.errors.map((e) => `${e.path.join(".")}: ${e.message}`).join("\n")}`, result.errors);
|
|
1081
|
+
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Parse a WRPRC payload into its canonical form, accepting either dialect spelling.
|
|
1084
|
+
*
|
|
1085
|
+
* @throws WRPRCException if the payload is invalid
|
|
1086
|
+
*/
|
|
1087
|
+
function parseWRPRCPayload(payload, accessCertificateSub) {
|
|
1088
|
+
assertValidWRPRCPayload(payload, accessCertificateSub);
|
|
1089
|
+
return WRPRCPayloadSchema.parse(normalizeWRPRCPayload(payload));
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* Assert that a WRPRC is valid (header + payload), throws WRPRCException if not
|
|
1093
|
+
*/
|
|
1094
|
+
function assertValidWRPRC(header, payload, accessCertificateSub) {
|
|
1095
|
+
const result = validateWRPRC(header, payload, accessCertificateSub);
|
|
1096
|
+
if (!result.valid) throw new WRPRCException(`Invalid WRPRC:\n${result.errors.map((e) => `${e.path.join(".")}: ${e.message}`).join("\n")}`, result.errors);
|
|
1097
|
+
}
|
|
1098
|
+
//#endregion
|
|
1099
|
+
//#region src/signer.ts
|
|
1100
|
+
/**
|
|
1101
|
+
* WRPRC Signer
|
|
1102
|
+
*
|
|
1103
|
+
* Functions for creating and signing ETSI TS 119 475 Wallet-Relying Party Registration Certificates.
|
|
1104
|
+
*
|
|
1105
|
+
* GEN-5.2.1-04 requires the JWT to carry a JAdES signature with the B-B profile of
|
|
1106
|
+
* ETSI TS 119 182-1, so signing and decoding go through `@owf/eudi-jades`.
|
|
1107
|
+
*
|
|
1108
|
+
* @see https://www.etsi.org/deliver/etsi_ts/119400_119499/119475/01.02.01_60/ts_119475v010201p.pdf
|
|
1109
|
+
*/
|
|
1110
|
+
/**
|
|
1111
|
+
* Sign a WRPRC payload into a JAdES B-B signed JWT
|
|
1112
|
+
*
|
|
1113
|
+
* @param options - Signing options including payload, algorithm, certificates, and signer
|
|
1114
|
+
* @returns Signed WRPRC with JWS string and decoded parts
|
|
1115
|
+
*/
|
|
1116
|
+
async function signWRPRC(options) {
|
|
1117
|
+
const { payload, accessCertificateSub, algorithm = "ES256", certificates, keyId, signer, signingTime, dialect = WRPRC_DIALECTS.CURRENT } = options;
|
|
1118
|
+
assertValidWRPRCPayload(payload, accessCertificateSub);
|
|
1119
|
+
if (!certificates || certificates.length === 0) throw new WRPRCException("At least one certificate is required for x5c header");
|
|
1120
|
+
const x5c = certificates.map((cert) => {
|
|
1121
|
+
const content = pemToDer(cert);
|
|
1122
|
+
if (!content) throw new WRPRCException("Invalid PEM certificate format");
|
|
1123
|
+
return content;
|
|
1124
|
+
});
|
|
1125
|
+
const token = new Token(toWRPRCDialect(payload, dialect)).setProtectedHeader({
|
|
1126
|
+
typ: "rc-wrp+jwt",
|
|
1127
|
+
alg: algorithm,
|
|
1128
|
+
...keyId && { kid: keyId }
|
|
1129
|
+
}).setX5c(x5c).setSigningTime(signingTime);
|
|
1130
|
+
await token.sign(async (data) => signer(data));
|
|
1131
|
+
return {
|
|
1132
|
+
jws: token.toString(),
|
|
1133
|
+
header: token.getProtectedHeader(),
|
|
1134
|
+
payload
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
/**
|
|
1138
|
+
* Decode a signed WRPRC JWT (without cryptographic verification)
|
|
1139
|
+
*
|
|
1140
|
+
* @param jws - The compact JWS string
|
|
1141
|
+
* @returns Decoded WRPRC with header and payload
|
|
1142
|
+
*/
|
|
1143
|
+
function decodeWRPRC(jws, accessCertificateSub) {
|
|
1144
|
+
let decoded;
|
|
1145
|
+
try {
|
|
1146
|
+
decoded = decode(jws);
|
|
1147
|
+
} catch (error) {
|
|
1148
|
+
throw new WRPRCException(`WRPRC signature does not meet the JAdES B-B profile (GEN-5.2.1-04): ${error.message}`, error);
|
|
1149
|
+
}
|
|
1150
|
+
if (decoded.header.typ !== "rc-wrp+jwt") throw new WRPRCException(`Invalid WRPRC type: expected "rc-wrp+jwt", got "${decoded.header.typ}"`);
|
|
1151
|
+
const profile = validateProfile(decoded.header, JAdESProfile.B_B, decoded.unprotectedHeader);
|
|
1152
|
+
if (!profile.valid) throw new WRPRCException(`WRPRC signature does not meet the JAdES B-B profile (GEN-5.2.1-04): ${profile.missing?.join(", ")}`);
|
|
1153
|
+
const payload = parseWRPRCPayload(decoded.payload, accessCertificateSub);
|
|
1154
|
+
return {
|
|
1155
|
+
jws,
|
|
1156
|
+
header: decoded.header,
|
|
1157
|
+
payload
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
* Parse a WRPRC JWT without validation (for inspection purposes)
|
|
1162
|
+
*
|
|
1163
|
+
* @param jws - The compact JWS string
|
|
1164
|
+
* @returns Decoded parts without validation
|
|
1165
|
+
*/
|
|
1166
|
+
function parseWRPRC(jws) {
|
|
1167
|
+
const parts = jws.split(".");
|
|
1168
|
+
if (parts.length !== 3) throw new WRPRCException("Invalid compact JWS: expected 3 parts");
|
|
1169
|
+
const decoded = decode(jws);
|
|
1170
|
+
return {
|
|
1171
|
+
header: decoded.header,
|
|
1172
|
+
payload: decoded.payload,
|
|
1173
|
+
signature: parts[2]
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
/**
|
|
1177
|
+
* Create a WRPRC with automatic timestamp
|
|
1178
|
+
*
|
|
1179
|
+
* @param payload - Partial payload (iat will be set automatically if not provided)
|
|
1180
|
+
* @returns Complete payload with timestamp
|
|
1181
|
+
*/
|
|
1182
|
+
function createWRPRCPayload(payload, accessCertificateSub) {
|
|
1183
|
+
assertValidWRPRCPayload(payload, accessCertificateSub);
|
|
1184
|
+
return payload;
|
|
1185
|
+
}
|
|
1186
|
+
//#endregion
|
|
1187
|
+
export { ClaimSchema, CredentialBuilder, CredentialSchema, IDENTIFIER_TYPES, IntermediarySchema, LEGAL_PERSON_IDENTIFIER_PREFIXES, LegalPersonSubjectSchema, MultiLangStringSchema, NATURAL_PERSON_IDENTIFIER_PREFIXES, NaturalPersonSubjectSchema, PSP_SUB_ENTITLEMENTS, ProvidedAttestationSchema, StatusSchema, SupervisoryAuthoritySchema, WRPRCBuilder, WRPRCCWTHeaderSchema, WRPRCException, WRPRCJWTHeaderSchema, WRPRCPayloadSchema, WRPRC_DIALECTS, WRPRC_JWS_ALGORITHMS, WRPRC_VALIDATION_CODES, WRP_ENTITLEMENTS, assertValidWRPRC, assertValidWRPRCPayload, createLegalPersonWRPRC, createNaturalPersonWRPRC, createServiceProviderWRPRC, createWRPRCPayload, credential, decodeWRPRC, getIdentifierPrefix, hasAttestationProviderEntitlement, isLegalPersonWRPRC, isNaturalPersonWRPRC, isPSPSubEntitlement, isValidEntitlement, normalizeWRPRCPayload, parseWRPRC, parseWRPRCPayload, signWRPRC, toWRPRCDialect, validateLegalPersonWRPRC, validateNaturalPersonWRPRC, validateWRPRC, validateWRPRCJWTHeader, validateWRPRCPayload, wrprc };
|
|
1188
|
+
|
|
1189
|
+
//# sourceMappingURL=index.mjs.map
|