@pagopa/io-react-native-wallet 2.1.1 → 2.2.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.
Files changed (35) hide show
  1. package/lib/commonjs/credential/issuance/07-verify-and-parse-credential.js +82 -58
  2. package/lib/commonjs/credential/issuance/07-verify-and-parse-credential.js.map +1 -1
  3. package/lib/commonjs/index.js +3 -1
  4. package/lib/commonjs/index.js.map +1 -1
  5. package/lib/commonjs/mdoc/index.js +15 -0
  6. package/lib/commonjs/mdoc/index.js.map +1 -1
  7. package/lib/commonjs/mdoc/utils.js +37 -1
  8. package/lib/commonjs/mdoc/utils.js.map +1 -1
  9. package/lib/commonjs/utils/nestedProperty.js +21 -10
  10. package/lib/commonjs/utils/nestedProperty.js.map +1 -1
  11. package/lib/module/credential/issuance/07-verify-and-parse-credential.js +83 -59
  12. package/lib/module/credential/issuance/07-verify-and-parse-credential.js.map +1 -1
  13. package/lib/module/index.js +2 -1
  14. package/lib/module/index.js.map +1 -1
  15. package/lib/module/mdoc/index.js +1 -0
  16. package/lib/module/mdoc/index.js.map +1 -1
  17. package/lib/module/mdoc/utils.js +35 -0
  18. package/lib/module/mdoc/utils.js.map +1 -1
  19. package/lib/module/utils/nestedProperty.js +21 -10
  20. package/lib/module/utils/nestedProperty.js.map +1 -1
  21. package/lib/typescript/credential/issuance/07-verify-and-parse-credential.d.ts.map +1 -1
  22. package/lib/typescript/index.d.ts +2 -1
  23. package/lib/typescript/index.d.ts.map +1 -1
  24. package/lib/typescript/mdoc/index.d.ts +1 -0
  25. package/lib/typescript/mdoc/index.d.ts.map +1 -1
  26. package/lib/typescript/mdoc/utils.d.ts +50 -0
  27. package/lib/typescript/mdoc/utils.d.ts.map +1 -1
  28. package/lib/typescript/utils/nestedProperty.d.ts +2 -1
  29. package/lib/typescript/utils/nestedProperty.d.ts.map +1 -1
  30. package/package.json +1 -1
  31. package/src/credential/issuance/07-verify-and-parse-credential.ts +60 -26
  32. package/src/index.ts +2 -0
  33. package/src/mdoc/index.ts +1 -0
  34. package/src/mdoc/utils.ts +43 -0
  35. package/src/utils/nestedProperty.ts +35 -10
@@ -1,7 +1,57 @@
1
+ import type { VerifyAndParseCredential } from "../credential/issuance";
2
+ import type { Out } from "../utils/misc";
1
3
  /**
2
4
  * @param namespace The mdoc credential `namespace`
3
5
  * @param key The claim attribute key
4
6
  * @returns A string consisting of the concatenation of the namespace and the claim key, separated by a colon
5
7
  */
6
8
  export declare const getParsedCredentialClaimKey: (namespace: string, key: string) => string;
9
+ /**
10
+ * Extract and validate the `verification` claim from an mdoc parsed credential.
11
+ *
12
+ * This method is **synchronous**, so it requires a credential that was already parsed.
13
+ *
14
+ * @param parsedCredential The parsed mdoc credential
15
+ * @returns The verification claim or undefined if it wasn't found
16
+ */
17
+ export declare const getVerificationFromParsedCredential: (parsedCredential: Out<VerifyAndParseCredential>["parsedCredential"]) => {
18
+ trust_framework: string;
19
+ assurance_level: string;
20
+ evidence: {
21
+ type: "vouch";
22
+ time: string | number;
23
+ attestation: {
24
+ type: "digital_attestation";
25
+ reference_number: string;
26
+ date_of_issuance: string;
27
+ voucher: {
28
+ organization: string;
29
+ };
30
+ };
31
+ }[];
32
+ } | undefined;
33
+ /**
34
+ * Extract and validate the `verification` claim from an MDOC credential.
35
+ *
36
+ * This method is **asynchronous**. See {@link getVerificationFromParsedCredential} for the synchronous version.
37
+ *
38
+ * @param token The raw MDOC credential
39
+ * @returns The verification claim or undefined if it wasn't found
40
+ */
41
+ export declare const getVerification: (token: string) => Promise<{
42
+ trust_framework: string;
43
+ assurance_level: string;
44
+ evidence: {
45
+ type: "vouch";
46
+ time: string | number;
47
+ attestation: {
48
+ type: "digital_attestation";
49
+ reference_number: string;
50
+ date_of_issuance: string;
51
+ voucher: {
52
+ organization: string;
53
+ };
54
+ };
55
+ }[];
56
+ } | undefined>;
7
57
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/mdoc/utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,2BAA2B,cAAe,MAAM,OAAO,MAAM,WACnD,CAAC"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/mdoc/utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AACvE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGzC;;;;GAIG;AACH,eAAO,MAAM,2BAA2B,cAAe,MAAM,OAAO,MAAM,WACnD,CAAC;AAExB;;;;;;;GAOG;AACH,eAAO,MAAM,mCAAmC,qBAC5B,IAAI,wBAAwB,CAAC,CAAC,kBAAkB,CAAC;;;;;;;;;;;;;;;aAQpE,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,UAAiB,MAAM;;;;;;;;;;;;;;;cAQlD,CAAC"}
@@ -16,8 +16,9 @@ type NodeOrStructure = Partial<PropertyNode<any>> | Record<string, any> | any[];
16
16
  * @param path - The path segments to follow.
17
17
  * @param sourceValue - The raw value to place at the end of the path.
18
18
  * @param displayData - The data for generating localized names.
19
+ * @param skipMissingLeaves - If true, skips optional keys when mapping over arrays.
19
20
  * @returns The new object or array structure.
20
21
  */
21
- export declare const createNestedProperty: (currentObject: NodeOrStructure, path: Path, sourceValue: unknown, displayData: DisplayData) => NodeOrStructure;
22
+ export declare const createNestedProperty: (currentObject: NodeOrStructure, path: Path, sourceValue: unknown, displayData: DisplayData, skipMissingLeaves?: boolean) => NodeOrStructure;
22
23
  export {};
23
24
  //# sourceMappingURL=nestedProperty.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"nestedProperty.d.ts","sourceRoot":"","sources":["../../../src/utils/nestedProperty.ts"],"names":[],"mappings":"AAGA,KAAK,WAAW,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CA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"name": "@pagopa/io-react-native-wallet",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "Provide data structures, helpers and API for IO Wallet",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
@@ -9,7 +9,7 @@ import type { ObtainCredential } from "./06-obtain-credential";
9
9
  import { verify as verifyMdoc } from "../../mdoc";
10
10
  import { MDOC_DEFAULT_NAMESPACE } from "../../mdoc/const";
11
11
  import { getParsedCredentialClaimKey } from "../../mdoc/utils";
12
- import { LogLevel, Logger } from "../../utils/logging";
12
+ import { Logger, LogLevel } from "../../utils/logging";
13
13
  import { extractElementValueAsDate } from "../../mdoc/converter";
14
14
  import type { CBOR } from "@pagopa/io-react-native-iso18013";
15
15
  import type { PublicKey } from "@pagopa/io-react-native-crypto";
@@ -85,36 +85,69 @@ const parseCredentialSdJwt = (
85
85
 
86
86
  const attrDefinitions = credentialConfig.claims;
87
87
 
88
- // Validate that all attributes from the config exist in the disclosures
89
- const attrsNotInDisclosures = attrDefinitions.filter(
90
- (definition) => !disclosures.some(([, name]) => name === definition.path[0])
91
- );
88
+ // Validate that all attributes from the config exist in either disclosures OR payload
89
+ if (!ignoreMissingAttributes) {
90
+ const disclosedKeys = new Set(disclosures.map(([, name]) => name));
91
+ const payloadKeys = new Set(Object.keys(sdJwt.payload ?? {}));
92
92
 
93
- if (attrsNotInDisclosures.length > 0 && !ignoreMissingAttributes) {
94
- const missing = attrsNotInDisclosures.map((_) => _.path[0]).join(", ");
95
- const received = disclosures.map((_) => _[1]).join(", ");
96
- const message = `Some attributes are missing in the credential. Missing: [${missing}], received: [${received}]`;
97
- Logger.log(LogLevel.ERROR, message);
98
- throw new IoWalletError(message);
93
+ const definedTopLevelKeys = new Set(
94
+ attrDefinitions.map((def) => def.path[0] as string)
95
+ );
96
+
97
+ const missingKeys = [...definedTopLevelKeys].filter(
98
+ (key) => !disclosedKeys.has(key) && !payloadKeys.has(key)
99
+ );
100
+
101
+ if (missingKeys.length > 0) {
102
+ throw new IoWalletError(
103
+ `Some attributes are missing in the credential. Missing: [${missingKeys.join(", ")}]`
104
+ );
105
+ }
99
106
  }
100
107
 
101
108
  const definedValues: ParsedCredential = {};
102
109
 
103
- for (const { path, display } of attrDefinitions) {
104
- const attrKey = path[0];
105
- const disclosureValue = disclosures.find(
106
- ([, name]) => name === attrKey
107
- )?.[2];
108
-
109
- if (disclosureValue !== undefined) {
110
- const enriched = createNestedProperty(
111
- definedValues,
112
- path,
113
- disclosureValue,
114
- display
115
- );
116
- Object.assign(definedValues, enriched);
110
+ // Group all schema definitions by their top-level key
111
+ const groupedDefinitions = attrDefinitions.reduce(
112
+ (acc, def) => {
113
+ const key = def.path[0] as string;
114
+ const group = acc[key];
115
+ if (group) {
116
+ group.push(def);
117
+ } else {
118
+ acc[key] = [def];
119
+ }
120
+ return acc;
121
+ },
122
+ {} as Record<string, typeof attrDefinitions>
123
+ );
124
+
125
+ // Loop through each group
126
+ for (const topLevelKey in groupedDefinitions) {
127
+ const definitionsForThisKey = groupedDefinitions[topLevelKey];
128
+
129
+ if (!definitionsForThisKey) {
130
+ continue;
117
131
  }
132
+
133
+ const disclosureForThisKey = disclosures.find(
134
+ ([, name]) => name === topLevelKey
135
+ );
136
+
137
+ if (!disclosureForThisKey) {
138
+ continue;
139
+ }
140
+
141
+ const disclosureValue = disclosureForThisKey[2];
142
+
143
+ const tempObjectForGroup = definitionsForThisKey.reduce(
144
+ (acc, { path, display }) =>
145
+ createNestedProperty(acc, path, disclosureValue, display),
146
+ {}
147
+ );
148
+
149
+ // Merge the fully constructed object into the final result
150
+ Object.assign(definedValues, tempObjectForGroup);
118
151
  }
119
152
 
120
153
  if (includeUndefinedAttributes) {
@@ -134,6 +167,7 @@ const parseCredentialSdJwt = (
134
167
 
135
168
  return definedValues;
136
169
  };
170
+
137
171
  const parseCredentialMDoc = (
138
172
  // the list of supported credentials, as defined in the issuer configuration
139
173
  credentialConfig: CredentialConf,
@@ -294,7 +328,7 @@ async function verifyCredentialSdJwt(
294
328
  * and it's bound to the given key
295
329
  *
296
330
  * @param rawCredential The received credential
297
- * @param issuerKeys The set of public keys of the issuer,
331
+ * @param x509CertRoot The root certificate of the issuer,
298
332
  * which will be used to verify the signature
299
333
  * @param holderBindingContext The access to the holder's key
300
334
  *
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ import "react-native-url-polyfill/auto";
7
7
  import * as Credential from "./credential";
8
8
  import * as PID from "./pid";
9
9
  import * as SdJwt from "./sd-jwt";
10
+ import * as Mdoc from "./mdoc";
10
11
  import * as Errors from "./utils/errors";
11
12
  import * as WalletInstanceAttestation from "./wallet-instance-attestation";
12
13
  import * as Trust from "./trust";
@@ -18,6 +19,7 @@ import type { IntegrityContext } from "./utils/integrity";
18
19
 
19
20
  export {
20
21
  SdJwt,
22
+ Mdoc,
21
23
  PID,
22
24
  Credential,
23
25
  WalletInstanceAttestation,
package/src/mdoc/index.ts CHANGED
@@ -9,6 +9,7 @@ import {
9
9
  import { MissingX509CertsError, X509ValidationError } from "../trust/errors";
10
10
  import { IoWalletError } from "../utils/errors";
11
11
  import { convertBase64DerToPem, getSigninJwkFromCert } from "../utils/crypto";
12
+ export * from "./utils";
12
13
 
13
14
  export const verify = async (
14
15
  token: string,
package/src/mdoc/utils.ts CHANGED
@@ -1,3 +1,9 @@
1
+ import { CBOR } from "@pagopa/io-react-native-iso18013";
2
+ import { Verification } from "../sd-jwt/types";
3
+ import type { VerifyAndParseCredential } from "../credential/issuance";
4
+ import type { Out } from "../utils/misc";
5
+ import { MDOC_DEFAULT_NAMESPACE } from "./const";
6
+
1
7
  /**
2
8
  * @param namespace The mdoc credential `namespace`
3
9
  * @param key The claim attribute key
@@ -5,3 +11,40 @@
5
11
  */
6
12
  export const getParsedCredentialClaimKey = (namespace: string, key: string) =>
7
13
  `${namespace}:${key}`;
14
+
15
+ /**
16
+ * Extract and validate the `verification` claim from an mdoc parsed credential.
17
+ *
18
+ * This method is **synchronous**, so it requires a credential that was already parsed.
19
+ *
20
+ * @param parsedCredential The parsed mdoc credential
21
+ * @returns The verification claim or undefined if it wasn't found
22
+ */
23
+ export const getVerificationFromParsedCredential = (
24
+ parsedCredential: Out<VerifyAndParseCredential>["parsedCredential"]
25
+ ) => {
26
+ const verificationKey = getParsedCredentialClaimKey(
27
+ `${MDOC_DEFAULT_NAMESPACE}.IT`,
28
+ "verification"
29
+ );
30
+ const verification = parsedCredential[verificationKey]?.value;
31
+ return verification ? Verification.parse(verification) : undefined;
32
+ };
33
+
34
+ /**
35
+ * Extract and validate the `verification` claim from an MDOC credential.
36
+ *
37
+ * This method is **asynchronous**. See {@link getVerificationFromParsedCredential} for the synchronous version.
38
+ *
39
+ * @param token The raw MDOC credential
40
+ * @returns The verification claim or undefined if it wasn't found
41
+ */
42
+ export const getVerification = async (token: string) => {
43
+ const issuerSigned = await CBOR.decodeIssuerSigned(token);
44
+ const namespace = issuerSigned.nameSpaces[`${MDOC_DEFAULT_NAMESPACE}.IT`];
45
+ const verification = namespace?.find(
46
+ (x) => x.elementIdentifier === "verification"
47
+ )?.elementValue;
48
+
49
+ return verification ? Verification.parse(verification) : undefined;
50
+ };
@@ -25,7 +25,7 @@ const buildName = (display: DisplayData): LocalizedNames =>
25
25
  {}
26
26
  );
27
27
 
28
- // Handles the case where the path key is `null`
28
+ // Handles the case where the path key is `null` (indicating an array)
29
29
  const handleNullKeyCase = (
30
30
  currentObject: NodeOrStructure,
31
31
  rest: Path,
@@ -39,7 +39,15 @@ const handleNullKeyCase = (
39
39
  const existingValue = Array.isArray(node.value) ? node.value : [];
40
40
 
41
41
  const mappedArray = sourceValue.map((item, idx) =>
42
- createNestedProperty(existingValue[idx] || {}, rest, item, displayData)
42
+ // When mapping over an array, recursively call with `skipMissingLeaves` set to `true`.
43
+ // This tells the function to skip optional keys inside these array objects.
44
+ createNestedProperty(
45
+ existingValue[idx] || {},
46
+ rest,
47
+ item,
48
+ displayData,
49
+ true
50
+ )
43
51
  );
44
52
 
45
53
  return {
@@ -55,7 +63,8 @@ const handleStringKeyCase = (
55
63
  key: string,
56
64
  rest: Path,
57
65
  sourceValue: unknown,
58
- displayData: DisplayData
66
+ displayData: DisplayData,
67
+ skipMissingLeaves: boolean
59
68
  ): NodeOrStructure => {
60
69
  let nextSourceValue = sourceValue;
61
70
  const isLeaf = rest.length === 0;
@@ -73,7 +82,13 @@ const handleStringKeyCase = (
73
82
 
74
83
  // Skip processing when the key is not found within the claim object
75
84
  if (!(key in sourceValue)) {
76
- // Leaf node: create a node with an empty value and display name
85
+ // If the flag is set (we're inside an array), skip the missing key completely.
86
+ if (skipMissingLeaves) {
87
+ return currentObject;
88
+ }
89
+
90
+ // If the flag is NOT set, create the empty placeholder
91
+ // so that its children can be attached later.
77
92
  if (isLeaf) {
78
93
  return {
79
94
  ...currentObject,
@@ -101,7 +116,13 @@ const handleStringKeyCase = (
101
116
 
102
117
  return {
103
118
  ...currentObject,
104
- [key]: createNestedProperty(nextObject, rest, nextSourceValue, displayData),
119
+ [key]: createNestedProperty(
120
+ nextObject,
121
+ rest,
122
+ nextSourceValue,
123
+ displayData,
124
+ skipMissingLeaves
125
+ ),
105
126
  };
106
127
  };
107
128
 
@@ -132,13 +153,15 @@ const handleNumberKeyCase = (
132
153
  * @param path - The path segments to follow.
133
154
  * @param sourceValue - The raw value to place at the end of the path.
134
155
  * @param displayData - The data for generating localized names.
156
+ * @param skipMissingLeaves - If true, skips optional keys when mapping over arrays.
135
157
  * @returns The new object or array structure.
136
158
  */
137
159
  export const createNestedProperty = (
138
160
  currentObject: NodeOrStructure,
139
161
  path: Path,
140
- sourceValue: unknown, // Use `unknown` for type-safe input
141
- displayData: DisplayData
162
+ sourceValue: unknown,
163
+ displayData: DisplayData,
164
+ skipMissingLeaves: boolean = false
142
165
  ): NodeOrStructure => {
143
166
  const [key, ...rest] = path;
144
167
 
@@ -152,7 +175,8 @@ export const createNestedProperty = (
152
175
  key as string,
153
176
  rest,
154
177
  sourceValue,
155
- displayData
178
+ displayData,
179
+ skipMissingLeaves
156
180
  );
157
181
 
158
182
  case typeof key === "number":
@@ -178,11 +202,12 @@ const handleRestKey = (
178
202
  displayData: DisplayData
179
203
  ): NodeOrStructure => {
180
204
  const currentNode = currentObject[key] ?? {};
181
- // Take the first key in the remaining path
182
205
  const restKey = rest[0] as string;
183
206
  const nextSourceValue = sourceValue[restKey];
207
+ if (typeof nextSourceValue === "undefined") {
208
+ return currentObject;
209
+ }
184
210
 
185
- // Merge the current node with the updated nested property for the remaining path.
186
211
  return {
187
212
  ...currentObject,
188
213
  [key]: {