@pagopa/io-react-native-wallet 2.0.0-next.3 → 2.0.0-next.5

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 (131) hide show
  1. package/lib/commonjs/credential/issuance/04-complete-user-authorization.js +75 -57
  2. package/lib/commonjs/credential/issuance/04-complete-user-authorization.js.map +1 -1
  3. package/lib/commonjs/credential/issuance/06-obtain-credential.js.map +1 -1
  4. package/lib/commonjs/credential/issuance/07-verify-and-parse-credential.js +3 -3
  5. package/lib/commonjs/credential/issuance/07-verify-and-parse-credential.js.map +1 -1
  6. package/lib/commonjs/credential/issuance/README.md +45 -34
  7. package/lib/commonjs/credential/issuance/types.js +1 -0
  8. package/lib/commonjs/credential/issuance/types.js.map +1 -1
  9. package/lib/commonjs/credential/presentation/07-evaluate-dcql-query.js +6 -13
  10. package/lib/commonjs/credential/presentation/07-evaluate-dcql-query.js.map +1 -1
  11. package/lib/commonjs/credential/presentation/07-evaluate-input-descriptor.js +7 -8
  12. package/lib/commonjs/credential/presentation/07-evaluate-input-descriptor.js.map +1 -1
  13. package/lib/commonjs/credential/presentation/types.js +1 -1
  14. package/lib/commonjs/credential/presentation/types.js.map +1 -1
  15. package/lib/commonjs/credential/status/{02-status-attestation.js → 02-status-assertion.js} +28 -22
  16. package/lib/commonjs/credential/status/02-status-assertion.js.map +1 -0
  17. package/lib/commonjs/credential/status/03-verify-and-parse-status-assertion.js +85 -0
  18. package/lib/commonjs/credential/status/03-verify-and-parse-status-assertion.js.map +1 -0
  19. package/lib/commonjs/credential/status/README.md +22 -20
  20. package/lib/commonjs/credential/status/index.js +6 -6
  21. package/lib/commonjs/credential/status/index.js.map +1 -1
  22. package/lib/commonjs/credential/status/types.js +48 -15
  23. package/lib/commonjs/credential/status/types.js.map +1 -1
  24. package/lib/commonjs/sd-jwt/index.js +6 -1
  25. package/lib/commonjs/sd-jwt/index.js.map +1 -1
  26. package/lib/commonjs/sd-jwt/types.js +25 -9
  27. package/lib/commonjs/sd-jwt/types.js.map +1 -1
  28. package/lib/commonjs/utils/credentials.js +33 -0
  29. package/lib/commonjs/utils/credentials.js.map +1 -0
  30. package/lib/commonjs/utils/crypto.js +1 -7
  31. package/lib/commonjs/utils/crypto.js.map +1 -1
  32. package/lib/commonjs/utils/jwk.js +12 -0
  33. package/lib/commonjs/utils/jwk.js.map +1 -1
  34. package/lib/commonjs/wallet-instance-attestation/types.js +1 -2
  35. package/lib/commonjs/wallet-instance-attestation/types.js.map +1 -1
  36. package/lib/module/credential/issuance/04-complete-user-authorization.js +76 -58
  37. package/lib/module/credential/issuance/04-complete-user-authorization.js.map +1 -1
  38. package/lib/module/credential/issuance/06-obtain-credential.js.map +1 -1
  39. package/lib/module/credential/issuance/07-verify-and-parse-credential.js +4 -4
  40. package/lib/module/credential/issuance/07-verify-and-parse-credential.js.map +1 -1
  41. package/lib/module/credential/issuance/README.md +45 -34
  42. package/lib/module/credential/issuance/types.js +1 -0
  43. package/lib/module/credential/issuance/types.js.map +1 -1
  44. package/lib/module/credential/presentation/07-evaluate-dcql-query.js +6 -13
  45. package/lib/module/credential/presentation/07-evaluate-dcql-query.js.map +1 -1
  46. package/lib/module/credential/presentation/07-evaluate-input-descriptor.js +7 -8
  47. package/lib/module/credential/presentation/07-evaluate-input-descriptor.js.map +1 -1
  48. package/lib/module/credential/presentation/types.js +1 -1
  49. package/lib/module/credential/presentation/types.js.map +1 -1
  50. package/lib/module/credential/status/{02-status-attestation.js → 02-status-assertion.js} +28 -22
  51. package/lib/module/credential/status/02-status-assertion.js.map +1 -0
  52. package/lib/module/credential/status/03-verify-and-parse-status-assertion.js +78 -0
  53. package/lib/module/credential/status/03-verify-and-parse-status-assertion.js.map +1 -0
  54. package/lib/module/credential/status/README.md +22 -20
  55. package/lib/module/credential/status/index.js +3 -3
  56. package/lib/module/credential/status/index.js.map +1 -1
  57. package/lib/module/credential/status/types.js +43 -12
  58. package/lib/module/credential/status/types.js.map +1 -1
  59. package/lib/module/sd-jwt/index.js +6 -1
  60. package/lib/module/sd-jwt/index.js.map +1 -1
  61. package/lib/module/sd-jwt/types.js +25 -9
  62. package/lib/module/sd-jwt/types.js.map +1 -1
  63. package/lib/module/utils/credentials.js +26 -0
  64. package/lib/module/utils/credentials.js.map +1 -0
  65. package/lib/module/utils/crypto.js +2 -8
  66. package/lib/module/utils/crypto.js.map +1 -1
  67. package/lib/module/utils/jwk.js +11 -1
  68. package/lib/module/utils/jwk.js.map +1 -1
  69. package/lib/module/wallet-instance-attestation/types.js +1 -2
  70. package/lib/module/wallet-instance-attestation/types.js.map +1 -1
  71. package/lib/typescript/credential/issuance/04-complete-user-authorization.d.ts +7 -14
  72. package/lib/typescript/credential/issuance/04-complete-user-authorization.d.ts.map +1 -1
  73. package/lib/typescript/credential/issuance/06-obtain-credential.d.ts.map +1 -1
  74. package/lib/typescript/credential/issuance/07-verify-and-parse-credential.d.ts.map +1 -1
  75. package/lib/typescript/credential/issuance/types.d.ts +3 -0
  76. package/lib/typescript/credential/issuance/types.d.ts.map +1 -1
  77. package/lib/typescript/credential/presentation/01-start-flow.d.ts +2 -2
  78. package/lib/typescript/credential/presentation/07-evaluate-dcql-query.d.ts +4 -3
  79. package/lib/typescript/credential/presentation/07-evaluate-dcql-query.d.ts.map +1 -1
  80. package/lib/typescript/credential/presentation/07-evaluate-input-descriptor.d.ts +9 -5
  81. package/lib/typescript/credential/presentation/07-evaluate-input-descriptor.d.ts.map +1 -1
  82. package/lib/typescript/credential/presentation/types.d.ts +3 -4
  83. package/lib/typescript/credential/presentation/types.d.ts.map +1 -1
  84. package/lib/typescript/credential/status/02-status-assertion.d.ts +23 -0
  85. package/lib/typescript/credential/status/02-status-assertion.d.ts.map +1 -0
  86. package/lib/typescript/credential/status/03-verify-and-parse-status-assertion.d.ts +21 -0
  87. package/lib/typescript/credential/status/03-verify-and-parse-status-assertion.d.ts.map +1 -0
  88. package/lib/typescript/credential/status/index.d.ts +4 -4
  89. package/lib/typescript/credential/status/index.d.ts.map +1 -1
  90. package/lib/typescript/credential/status/types.d.ts +499 -22
  91. package/lib/typescript/credential/status/types.d.ts.map +1 -1
  92. package/lib/typescript/sd-jwt/index.d.ts +68 -40
  93. package/lib/typescript/sd-jwt/index.d.ts.map +1 -1
  94. package/lib/typescript/sd-jwt/types.d.ts +97 -46
  95. package/lib/typescript/sd-jwt/types.d.ts.map +1 -1
  96. package/lib/typescript/utils/credentials.d.ts +11 -0
  97. package/lib/typescript/utils/credentials.d.ts.map +1 -0
  98. package/lib/typescript/utils/crypto.d.ts.map +1 -1
  99. package/lib/typescript/utils/jwk.d.ts +7 -0
  100. package/lib/typescript/utils/jwk.d.ts.map +1 -1
  101. package/package.json +1 -1
  102. package/src/credential/issuance/04-complete-user-authorization.ts +79 -85
  103. package/src/credential/issuance/06-obtain-credential.ts +4 -1
  104. package/src/credential/issuance/07-verify-and-parse-credential.ts +4 -6
  105. package/src/credential/issuance/README.md +45 -34
  106. package/src/credential/issuance/types.ts +1 -0
  107. package/src/credential/presentation/07-evaluate-dcql-query.ts +16 -17
  108. package/src/credential/presentation/07-evaluate-input-descriptor.ts +16 -13
  109. package/src/credential/presentation/types.ts +1 -2
  110. package/src/credential/status/{02-status-attestation.ts → 02-status-assertion.ts} +37 -28
  111. package/src/credential/status/03-verify-and-parse-status-assertion.ts +109 -0
  112. package/src/credential/status/README.md +22 -20
  113. package/src/credential/status/index.ts +7 -14
  114. package/src/credential/status/types.ts +62 -15
  115. package/src/sd-jwt/index.ts +5 -1
  116. package/src/sd-jwt/types.ts +24 -10
  117. package/src/utils/credentials.ts +29 -0
  118. package/src/utils/crypto.ts +12 -20
  119. package/src/utils/jwk.ts +15 -1
  120. package/src/wallet-instance-attestation/types.ts +1 -1
  121. package/lib/commonjs/credential/status/02-status-attestation.js.map +0 -1
  122. package/lib/commonjs/credential/status/03-verify-and-parse-status-attestation.js +0 -55
  123. package/lib/commonjs/credential/status/03-verify-and-parse-status-attestation.js.map +0 -1
  124. package/lib/module/credential/status/02-status-attestation.js.map +0 -1
  125. package/lib/module/credential/status/03-verify-and-parse-status-attestation.js +0 -49
  126. package/lib/module/credential/status/03-verify-and-parse-status-attestation.js.map +0 -1
  127. package/lib/typescript/credential/status/02-status-attestation.d.ts +0 -19
  128. package/lib/typescript/credential/status/02-status-attestation.d.ts.map +0 -1
  129. package/lib/typescript/credential/status/03-verify-and-parse-status-attestation.d.ts +0 -24
  130. package/lib/typescript/credential/status/03-verify-and-parse-status-attestation.d.ts.map +0 -1
  131. package/src/credential/status/03-verify-and-parse-status-attestation.ts +0 -70
@@ -6,54 +6,65 @@ import {
6
6
  import type { EvaluateIssuerTrust, ObtainCredential } from "../issuance";
7
7
  import { type CryptoContext, SignJWT } from "@pagopa/io-react-native-jwt";
8
8
  import { v4 as uuidv4 } from "uuid";
9
- import { StatusAttestationResponse } from "./types";
9
+ import { StatusAssertionResponse } from "./types";
10
10
  import {
11
11
  IssuerResponseError,
12
12
  IssuerResponseErrorCodes,
13
13
  ResponseErrorBuilder,
14
14
  UnexpectedStatusCodeError,
15
15
  } from "../../utils/errors";
16
- import { LogLevel, Logger } from "../../utils/logging";
16
+ import { Logger, LogLevel } from "../../utils/logging";
17
+ import { extractJwkFromCredential } from "../../utils/credentials";
17
18
 
18
- export type StatusAttestation = (
19
+ export type StatusAssertion = (
19
20
  issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
20
21
  credential: Out<ObtainCredential>["credential"],
21
- credentialCryptoContext: CryptoContext,
22
- appFetch?: GlobalFetch["fetch"]
22
+ format: Out<ObtainCredential>["format"],
23
+ context: {
24
+ credentialCryptoContext: CryptoContext;
25
+ wiaCryptoContext: CryptoContext;
26
+ appFetch?: GlobalFetch["fetch"];
27
+ }
23
28
  ) => Promise<{
24
- statusAttestation: StatusAttestationResponse["status_attestation"];
29
+ statusAssertion: string;
25
30
  }>;
26
31
 
27
32
  /**
28
- * WARNING: This function must be called after {@link startFlow}.
29
- * Verify the status of the credential attestation.
33
+ * Get the status assertion of a digital credential.
30
34
  * @param issuerConf - The issuer's configuration
31
35
  * @param credential - The credential to be verified
32
- * @param credentialCryptoContext - The credential's crypto context
36
+ * @param format - The format of the credential, e.g. "sd-jwt"
37
+ * @param context.credentialCryptoContext - The credential's crypto context
38
+ * @param context.wiaCryptoContext - The Wallet Attestation's crypto context
33
39
  * @param context.appFetch (optional) fetch api implementation. Default: built-in fetch
34
40
  * @throws {IssuerResponseError} with a specific code for more context
35
- * @returns The credential status attestation
41
+ * @returns The credential status assertion
36
42
  */
37
- export const statusAttestation: StatusAttestation = async (
43
+ export const statusAssertion: StatusAssertion = async (
38
44
  issuerConf,
39
45
  credential,
40
- credentialCryptoContext,
41
- appFetch: GlobalFetch["fetch"] = fetch
46
+ format,
47
+ ctx
42
48
  ) => {
43
- const jwk = await credentialCryptoContext.getPublicKey();
49
+ const { credentialCryptoContext, wiaCryptoContext, appFetch = fetch } = ctx;
50
+
51
+ const jwk = await extractJwkFromCredential(credential, format);
52
+ const issuerJwk = await wiaCryptoContext.getPublicKey();
44
53
  const credentialHash = await getCredentialHashWithouDiscloures(credential);
45
54
  const statusAttUrl =
46
55
  issuerConf.openid_credential_issuer.status_attestation_endpoint;
56
+
47
57
  const credentialPop = await new SignJWT(credentialCryptoContext)
48
58
  .setPayload({
59
+ iss: issuerJwk.kid,
49
60
  aud: statusAttUrl,
50
61
  jti: uuidv4().toString(),
51
62
  credential_hash: credentialHash,
52
- credential_hash_alg: "S256",
63
+ credential_hash_alg: "sha-256",
53
64
  })
54
65
  .setProtectedHeader({
55
66
  alg: "ES256",
56
- typ: "status-attestation-request+jwt",
67
+ typ: "status-assertion-request+jwt",
57
68
  kid: jwk.kid,
58
69
  })
59
70
  .setIssuedAt()
@@ -61,7 +72,7 @@ export const statusAttestation: StatusAttestation = async (
61
72
  .sign();
62
73
 
63
74
  const body = {
64
- credential_pop: credentialPop,
75
+ status_assertion_requests: [credentialPop],
65
76
  };
66
77
 
67
78
  Logger.log(LogLevel.DEBUG, `Credential pop: ${credentialPop}`);
@@ -73,33 +84,31 @@ export const statusAttestation: StatusAttestation = async (
73
84
  },
74
85
  body: JSON.stringify(body),
75
86
  })
76
- .then(hasStatusOrThrow(201))
87
+ .then(hasStatusOrThrow(200))
77
88
  .then((raw) => raw.json())
78
- .then((json) => StatusAttestationResponse.parse(json))
79
- .catch(handleStatusAttestationError);
89
+ .then((json) => StatusAssertionResponse.parse(json))
90
+ .catch(handleStatusAssertionError);
80
91
 
81
- return { statusAttestation: result.status_attestation };
92
+ const [statusAttestationJwt] = result.status_assertion_responses;
93
+
94
+ return { statusAssertion: statusAttestationJwt! };
82
95
  };
83
96
 
84
97
  /**
85
- * Handle the status attestation error by mapping it to a custom exception.
98
+ * Handle the status assertion error by mapping it to a custom exception.
86
99
  * If the error is not an instance of {@link UnexpectedStatusCodeError}, it is thrown as is.
87
100
  * @param e - The error to be handled
88
101
  * @throws {IssuerResponseError} with a specific code for more context
89
102
  */
90
- const handleStatusAttestationError = (e: unknown) => {
103
+ const handleStatusAssertionError = (e: unknown) => {
91
104
  if (!(e instanceof UnexpectedStatusCodeError)) {
92
105
  throw e;
93
106
  }
94
107
 
95
108
  throw new ResponseErrorBuilder(IssuerResponseError)
96
- .handle(404, {
97
- code: IssuerResponseErrorCodes.CredentialInvalidStatus,
98
- message: "Invalid status found for the given credential",
99
- })
100
109
  .handle("*", {
101
110
  code: IssuerResponseErrorCodes.StatusAttestationRequestFailed,
102
- message: `Unable to obtain the status attestation for the given credential`,
111
+ message: `Unable to obtain the status assertion for the given credential`,
103
112
  })
104
113
  .buildFrom(e);
105
114
  };
@@ -0,0 +1,109 @@
1
+ import type { Out } from "../../utils/misc";
2
+ import {
3
+ IoWalletError,
4
+ IssuerResponseError,
5
+ IssuerResponseErrorCodes,
6
+ } from "../../utils/errors";
7
+ import { decode as decodeJwt, verify } from "@pagopa/io-react-native-jwt";
8
+ import type { EvaluateIssuerTrust, StatusAssertion } from ".";
9
+ import {
10
+ type InvalidStatusErrorReason,
11
+ ParsedStatusAssertion,
12
+ ParsedStatusAssertionError,
13
+ ParsedStatusAssertionResponse,
14
+ StatusType,
15
+ } from "./types";
16
+ import { Logger, LogLevel } from "../../utils/logging";
17
+ import type { ObtainCredential } from "../issuance";
18
+ import { extractJwkFromCredential } from "../../utils/credentials";
19
+ import { isSameThumbprint } from "../../utils/jwk";
20
+
21
+ export type VerifyAndParseStatusAssertion = (
22
+ issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
23
+ statusAssertion: Out<StatusAssertion>,
24
+ credential: Out<ObtainCredential>["credential"],
25
+ format: Out<ObtainCredential>["format"]
26
+ ) => Promise<{ parsedStatusAssertion: ParsedStatusAssertion }>;
27
+
28
+ /**
29
+ * Given a status assertion, verifies that:
30
+ * - It's in the supported format;
31
+ * - The assertion is correctly signed;
32
+ * - It's bound to the given key.
33
+ * @param issuerConf The Issuer configuration returned by {@link evaluateIssuerTrust}
34
+ * @param statusAssertion The encoded status assertion returned by {@link statusAssertion}
35
+ * @param context.credentialCryptoContext The crypto context used to obtain the credential in {@link obtainCredential}
36
+ * @returns A parsed status assertion
37
+ * @throws {IoWalletError} If the credential signature is not verified with the Issuer key set
38
+ * @throws {IssuerResponseError} If the status assertion contains an error or the credential status is invalid
39
+ */
40
+ export const verifyAndParseStatusAssertion: VerifyAndParseStatusAssertion =
41
+ async (issuerConf, rawStatusAssertion, credential, format) => {
42
+ const { statusAssertion } = rawStatusAssertion;
43
+
44
+ await verify(
45
+ statusAssertion,
46
+ issuerConf.openid_credential_issuer.jwks.keys
47
+ );
48
+
49
+ const decodedJwt = decodeJwt(statusAssertion);
50
+ const parsedStatusAssertion = ParsedStatusAssertionResponse.parse({
51
+ header: decodedJwt.protectedHeader,
52
+ payload: decodedJwt.payload,
53
+ });
54
+
55
+ Logger.log(
56
+ LogLevel.DEBUG,
57
+ `Parsed status assertion: ${JSON.stringify(parsedStatusAssertion)}`
58
+ );
59
+
60
+ // Errors are transmitted in the JWT and use a 200 HTTP status code
61
+ if (isStatusAssertionError(parsedStatusAssertion)) {
62
+ throw new IssuerResponseError({
63
+ code: IssuerResponseErrorCodes.CredentialInvalidStatus,
64
+ message: "The status assertion contains an error",
65
+ statusCode: 200,
66
+ reason: buildErrorReason(parsedStatusAssertion),
67
+ });
68
+ }
69
+
70
+ const { cnf, credential_status_type } = parsedStatusAssertion.payload;
71
+ const holderBindingKey = await extractJwkFromCredential(credential, format);
72
+
73
+ if (!(await isSameThumbprint(cnf.jwk, holderBindingKey))) {
74
+ const errorMessage = `Failed to verify holder binding for status assertion: the thumbprints of keys ${cnf.jwk.kid} and ${holderBindingKey.kid} do not match`;
75
+ Logger.log(LogLevel.ERROR, errorMessage);
76
+ throw new IoWalletError(errorMessage);
77
+ }
78
+
79
+ if (credential_status_type !== StatusType.VALID) {
80
+ throw new IssuerResponseError({
81
+ code: IssuerResponseErrorCodes.CredentialInvalidStatus,
82
+ message: "Invalid status found for the given credential",
83
+ statusCode: 200,
84
+ reason: buildErrorReason(parsedStatusAssertion),
85
+ });
86
+ }
87
+
88
+ return { parsedStatusAssertion };
89
+ };
90
+
91
+ const isStatusAssertionError = (
92
+ assertion: ParsedStatusAssertionResponse
93
+ ): assertion is ParsedStatusAssertionError =>
94
+ assertion.header.typ === "status-assertion-error+jwt";
95
+
96
+ /**
97
+ * Build an object containing the details on the error to use as the IssuerResponseError's reason
98
+ * @param assertion The status assertion response, both success or failure
99
+ * @returns The error's reason object
100
+ */
101
+ const buildErrorReason = ({
102
+ payload,
103
+ }: ParsedStatusAssertionResponse): InvalidStatusErrorReason =>
104
+ "error" in payload
105
+ ? payload
106
+ : {
107
+ error: payload.credential_status_detail!.state,
108
+ error_description: payload.credential_status_detail!.description,
109
+ };
@@ -1,16 +1,16 @@
1
- # Credential Status Attestation
1
+ # Credential Status Assertion
2
2
 
3
- This flow is used to obtain a credential status attestation from its credential issuer. Each step in the flow is imported from the related file which is named with a sequential number.
4
- The credential status attestation is a JWT which contains the credential status which indicates if the credential is valid or not.
5
- The status attestation is supposed to be stored securely along with the credential. It has a limited lifetime and should be refreshed periodically according to the `exp` field in the JWT payload.
3
+ This flow is used to obtain a credential status assertion from its credential issuer. Each step in the flow is imported from the related file which is named with a sequential number.
4
+ The credential status assertion is a JWT which contains the credential status which indicates if the credential is valid or not (see [OAuth Status Assertions](https://italia.github.io/eid-wallet-it-docs/versione-corrente/en/credential-revocation.html#oauth-status-assertions)).
5
+ The status assertion is supposed to be stored securely along with the credential. It has a limited lifetime and should be refreshed periodically according to the `exp` field in the JWT payload.
6
6
 
7
7
  ## Sequence Diagram
8
8
 
9
9
  ```mermaid
10
10
  graph TD;
11
11
  0[startFlow]
12
- 1[statusAttestation]
13
- 2[verifyAndParseStatusAttestation]
12
+ 1[statusAssertion]
13
+ 2[verifyAndParseStatusAssertion]
14
14
 
15
15
  0 --> 1
16
16
  1 --> 2
@@ -21,14 +21,14 @@ graph TD;
21
21
 
22
22
  The following errors are mapped to a `IssuerResponseError` with specific codes.
23
23
 
24
- |HTTP Status|Error Code|Description|
25
- |-----------|----------|-----------|
26
- |`404 Not Found`|`ERR_CREDENTIAL_INVALID_STATUS`|This response is returned by the credential issuer when the status attestation is invalid. It might contain more details in the `reason` property.|
24
+ |Error Code|Description|
25
+ |----------|-----------|
26
+ |`ERR_CREDENTIAL_INVALID_STATUS`|This error is thrown when the status assertion for a given credential is invalid. It might contain more details in the `reason` property.|
27
27
 
28
28
  ## Example
29
29
 
30
30
  <details>
31
- <summary>Credential status attestation flow</summary>
31
+ <summary>Credential status assertion flow</summary>
32
32
 
33
33
  ```ts
34
34
  // Start the issuance flow
@@ -42,24 +42,26 @@ const { issuerUrl } = startFlow();
42
42
  // Evaluate issuer trust
43
43
  const { issuerConf } = await Credential.Status.evaluateIssuerTrust(issuerUrl);
44
44
 
45
- // Get the credential attestation
46
- const res = await Credential.Status.statusAttestation(
45
+ // Get the credential assertion
46
+ const res = await Credential.Status.statusAssertion(
47
47
  issuerConf,
48
48
  credential,
49
- credentialCryptoContext
49
+ format,
50
+ { credentialCryptoContext, wiaCryptoContext }
50
51
  );
51
52
 
52
- // Verify and parse the status attestation
53
- const { parsedStatusAttestation } =
54
- await Credential.Status.verifyAndParseStatusAttestation(
53
+ // Verify and parse the status assertion
54
+ const { parsedStatusAssertion } =
55
+ await Credential.Status.verifyAndParseStatusAssertion(
55
56
  issuerConf,
56
- res.statusAttestation,
57
- { credentialCryptoContext }
57
+ res.statusAssertion,
58
+ credential,
59
+ format
58
60
  );
59
61
 
60
62
  return {
61
- statusAttestation: res.statusAttestation,
62
- parsedStatusAttestation,
63
+ statusAssertion: res.statusAssertion,
64
+ parsedStatusAssertion,
63
65
  };
64
66
  ```
65
67
 
@@ -1,22 +1,15 @@
1
1
  import { type StartFlow } from "./01-start-flow";
2
- import {
3
- statusAttestation,
4
- type StatusAttestation,
5
- } from "./02-status-attestation";
2
+ import { statusAssertion, type StatusAssertion } from "./02-status-assertion";
6
3
  import { evaluateIssuerTrust, type EvaluateIssuerTrust } from "../issuance";
7
4
  import {
8
- verifyAndParseStatusAttestation,
9
- type VerifyAndParseStatusAttestation,
10
- } from "./03-verify-and-parse-status-attestation";
5
+ verifyAndParseStatusAssertion,
6
+ type VerifyAndParseStatusAssertion,
7
+ } from "./03-verify-and-parse-status-assertion";
11
8
 
12
- export {
13
- evaluateIssuerTrust,
14
- statusAttestation,
15
- verifyAndParseStatusAttestation,
16
- };
9
+ export { evaluateIssuerTrust, statusAssertion, verifyAndParseStatusAssertion };
17
10
  export type {
18
11
  StartFlow,
19
12
  EvaluateIssuerTrust,
20
- StatusAttestation,
21
- VerifyAndParseStatusAttestation,
13
+ StatusAssertion,
14
+ VerifyAndParseStatusAssertion,
22
15
  };
@@ -3,35 +3,38 @@ import { JWK } from "../../utils/jwk";
3
3
  import * as z from "zod";
4
4
 
5
5
  /**
6
- * Shape from parsing a status attestation response in case of 201.
6
+ * Shape from parsing a status assertion response in case of 201.
7
7
  */
8
- export const StatusAttestationResponse = z.object({
9
- status_attestation: z.string(),
8
+ export const StatusAssertionResponse = z.object({
9
+ status_assertion_responses: z.array(z.string()),
10
10
  });
11
11
 
12
12
  /**
13
- * Type from parsing a status attestation response in case of 201.
14
- * Inferred from {@link StatusAttestationResponse}.
13
+ * Type from parsing a status assertion response in case of 201.
14
+ * Inferred from {@link StatusAssertionResponse}.
15
15
  */
16
- export type StatusAttestationResponse = z.infer<
17
- typeof StatusAttestationResponse
18
- >;
16
+ export type StatusAssertionResponse = z.infer<typeof StatusAssertionResponse>;
19
17
 
20
- /**
21
- * Type for a parsed status attestation.
22
- */
23
- export type ParsedStatusAttestation = z.infer<typeof ParsedStatusAttestation>;
18
+ export type ParsedStatusAssertion = z.infer<typeof ParsedStatusAssertion>;
24
19
 
25
20
  /**
26
- * Shape for parsing a status attestation in a JWT.
21
+ * Shape for parsing a successful status assertion in a JWT.
27
22
  */
28
- export const ParsedStatusAttestation = z.object({
23
+ export const ParsedStatusAssertion = z.object({
29
24
  header: z.object({
30
- typ: z.literal("status-attestation+jwt"),
25
+ typ: z.literal("status-assertion+jwt"),
31
26
  alg: z.string(),
32
27
  kid: z.string().optional(),
33
28
  }),
34
29
  payload: z.object({
30
+ iss: z.string(),
31
+ credential_status_type: z.string(),
32
+ credential_status_detail: z
33
+ .object({
34
+ state: z.string(),
35
+ description: z.string(),
36
+ })
37
+ .optional(),
35
38
  credential_hash_alg: z.string(),
36
39
  credential_hash: z.string(),
37
40
  cnf: z.object({
@@ -41,3 +44,47 @@ export const ParsedStatusAttestation = z.object({
41
44
  iat: UnixTime,
42
45
  }),
43
46
  });
47
+
48
+ export type ParsedStatusAssertionError = z.infer<
49
+ typeof ParsedStatusAssertionError
50
+ >;
51
+
52
+ /**
53
+ * The JWT that contains the errors occurred for the status assertion request.
54
+ * @see https://italia.github.io/eid-wallet-it-docs/versione-corrente/en/credential-revocation.html#http-status-assertion-response
55
+ */
56
+ export const ParsedStatusAssertionError = z.object({
57
+ header: z.object({
58
+ typ: z.literal("status-assertion-error+jwt"),
59
+ alg: z.string(),
60
+ kid: z.string().optional(),
61
+ }),
62
+ payload: z.object({
63
+ credential_hash_alg: z.string(),
64
+ credential_hash: z.string(),
65
+ error: z.string(),
66
+ error_description: z.string(),
67
+ }),
68
+ });
69
+
70
+ /**
71
+ * The status assertion response that might include either a successful assertion or an error
72
+ */
73
+ export type ParsedStatusAssertionResponse = z.infer<
74
+ typeof ParsedStatusAssertionResponse
75
+ >;
76
+ export const ParsedStatusAssertionResponse = z.union([
77
+ ParsedStatusAssertion,
78
+ ParsedStatusAssertionError,
79
+ ]);
80
+
81
+ export enum StatusType {
82
+ VALID = "0x00",
83
+ INVALID = "0x01",
84
+ SUSPENDED = "0x02",
85
+ }
86
+
87
+ export type InvalidStatusErrorReason = {
88
+ error: string;
89
+ error_description: string;
90
+ };
@@ -110,7 +110,11 @@ export const disclose = async (
110
110
  })
111
111
  );
112
112
 
113
- const filteredDisclosures = rawDisclosures.filter((d) => {
113
+ // The disclosures in the new SD-JWT aligned with version 1.0
114
+ // include a trailing "~" character.
115
+ // To avoid parsing errors, it is necessary to filter the array
116
+ // to remove any empty strings
117
+ const filteredDisclosures = rawDisclosures.filter(Boolean).filter((d) => {
114
118
  const {
115
119
  decoded: [, name],
116
120
  } = decodeDisclosure(d);
@@ -33,12 +33,23 @@ export type DisclosureWithEncoded = {
33
33
  encoded: string;
34
34
  };
35
35
 
36
+ const StatusAssertion = z.object({
37
+ credential_hash_alg: z.literal("sha-256"),
38
+ });
39
+
40
+ /**
41
+ * Type for a Verifiable Credential in SD-JWT format.
42
+ * It supports both the older and the new data model for backward compatibility.
43
+ */
36
44
  export type SdJwt4VC = z.infer<typeof SdJwt4VC>;
37
45
  export const SdJwt4VC = z.object({
38
46
  header: z.object({
39
- typ: z.literal("dc+sd-jwt"),
47
+ typ: z.enum(["vc+sd-jwt", "dc+sd-jwt"]),
40
48
  alg: z.string(),
41
- kid: z.string().optional(),
49
+ kid: z.string(),
50
+ trust_chain: z.array(z.string()).optional(),
51
+ x5c: z.array(z.string()).optional(),
52
+ vctm: z.array(z.string()).optional(),
42
53
  }),
43
54
  payload: z.intersection(
44
55
  z.object({
@@ -47,18 +58,21 @@ export const SdJwt4VC = z.object({
47
58
  iat: UnixTime.optional(),
48
59
  exp: UnixTime,
49
60
  _sd_alg: z.literal("sha-256"),
50
- status: z.object({
51
- status_assertion: z.object({
52
- credential_hash_alg: z.literal("sha-256"),
53
- }),
54
- }),
61
+ status: z
62
+ .union([
63
+ // Credentials v1.0
64
+ z.object({ status_assertion: StatusAssertion }),
65
+ // Credentials v0.7.1
66
+ z.object({ status_attestation: StatusAssertion }),
67
+ ])
68
+ .optional(),
55
69
  cnf: z.object({
56
70
  jwk: JWK,
57
71
  }),
58
72
  vct: z.string(),
59
- "vct#integrity": z.string(),
60
- issuing_authority: z.string(),
61
- issuing_country: z.string(),
73
+ "vct#integrity": z.string().optional(),
74
+ issuing_authority: z.string().optional(),
75
+ issuing_country: z.string().optional(),
62
76
  }),
63
77
  ObfuscatedDisclosures
64
78
  ),
@@ -0,0 +1,29 @@
1
+ import { decode } from "../sd-jwt";
2
+ import { thumbprint } from "@pagopa/io-react-native-jwt";
3
+ import type { Out } from "./misc";
4
+ import type { ObtainCredential } from "../credential/issuance";
5
+ import type { JWK } from "./jwk";
6
+ import { IoWalletError } from "./errors";
7
+
8
+ const SD_JWT = ["vc+sd-jwt", "dc+sd-jwt"];
9
+
10
+ /**
11
+ * Extracts a JWK from a credential.
12
+ * @param credential - The credential string, which can be in SD-JWT or CBOR format.
13
+ * @param format - The format of the credential
14
+ * @return A Promise that resolves to a JWK object if the credential is in SD-JWT format and contains a JWK, or undefined otherwise.
15
+ */
16
+ export const extractJwkFromCredential = async (
17
+ credential: Out<ObtainCredential>["credential"],
18
+ format: Out<ObtainCredential>["format"]
19
+ ): Promise<JWK> => {
20
+ if (SD_JWT.includes(format)) {
21
+ // 1. SD-JWT case
22
+ const decoded = decode(credential);
23
+ const jwk = decoded.sdJwt.payload.cnf.jwk;
24
+ if (jwk) {
25
+ return { ...jwk, kid: await thumbprint(jwk) };
26
+ }
27
+ }
28
+ throw new IoWalletError(`Credential format ${format} not supported`);
29
+ };
@@ -1,12 +1,11 @@
1
1
  import {
2
- getPublicKey,
3
- sign,
4
- generate,
5
2
  deleteKey,
3
+ generate,
4
+ getPublicKeyFixed,
5
+ sign,
6
6
  } from "@pagopa/io-react-native-crypto";
7
7
  import { v4 as uuidv4 } from "uuid";
8
- import { thumbprint, type CryptoContext } from "@pagopa/io-react-native-jwt";
9
- import { fixBase64EncodingOnKey } from "./jwk";
8
+ import { type CryptoContext, thumbprint } from "@pagopa/io-react-native-jwt";
10
9
 
11
10
  /**
12
11
  * Create a CryptoContext bound to a key pair.
@@ -17,22 +16,15 @@ import { fixBase64EncodingOnKey } from "./jwk";
17
16
  */
18
17
  export const createCryptoContextFor = (keytag: string): CryptoContext => {
19
18
  return {
20
- /**
21
- * Retrieve the public key of the pair.
22
- * If the key pair doesn't exist yet, an error is raised
23
- * @returns The public key.
24
- */
25
19
  async getPublicKey() {
26
- return getPublicKey(keytag)
27
- .then(fixBase64EncodingOnKey)
28
- .then(async (jwk) => ({
29
- ...jwk,
30
- // Keys in the TEE are not stored with their KID, which is supposed to be assigned when they are included in JWK sets.
31
- // (that is, KID is not a propoerty of the key itself, but it's property used to identify a key in a set).
32
- // We assume the convention we use the thumbprint of the public key as KID, thus for easy development we decided to evaluate KID here
33
- // However the values is an arbitrary string that might be anything
34
- kid: await thumbprint(jwk),
35
- }));
20
+ return getPublicKeyFixed(keytag).then(async (jwk) => ({
21
+ ...jwk,
22
+ // Keys in the TEE are not stored with their KID, which is supposed to be assigned when they are included in JWK sets.
23
+ // (that is, KID is not a propoerty of the key itself, but it's property used to identify a key in a set).
24
+ // We assume the convention we use the thumbprint of the public key as KID, thus for easy development we decided to evaluate KID here
25
+ // However the values is an arbitrary string that might be anything
26
+ kid: await thumbprint(jwk),
27
+ }));
36
28
  },
37
29
  /**
38
30
  * Get a signature for a provided value.
package/src/utils/jwk.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { decode, removePadding } from "@pagopa/io-react-native-jwt";
1
+ import { decode, removePadding, thumbprint } from "@pagopa/io-react-native-jwt";
2
2
  import { z } from "zod";
3
3
 
4
4
  export type JWK = z.infer<typeof JWK>;
@@ -65,3 +65,17 @@ export const JWKS = z.object({
65
65
  });
66
66
 
67
67
  export type JWTDecodeResult = ReturnType<typeof decode>;
68
+
69
+ /**
70
+ * Utility function that checks if two JWKs have the same thumbprint.
71
+ * @param jwkA The first JWK
72
+ * @param jwkB The second JWK
73
+ * @returns Whether the thumbprints match
74
+ */
75
+ export const isSameThumbprint = async (jwkA: JWK, jwkB: JWK) => {
76
+ const [thumbprintJwkA, thumbprintJwkB] = await Promise.all([
77
+ thumbprint(jwkA),
78
+ thumbprint(jwkB),
79
+ ]);
80
+ return thumbprintJwkA === thumbprintJwkB;
81
+ };
@@ -58,7 +58,7 @@ export const WalletInstanceAttestationJwt = z.object({
58
58
  Jwt.shape.header,
59
59
  z.object({
60
60
  typ: z.literal("oauth-client-attestation+jwt"),
61
- trust_chain: z.array(z.string()).optional(), // TODO: [SIW-2264] Make mandatory
61
+ trust_chain: z.array(z.string()).optional(),
62
62
  })
63
63
  ),
64
64
  payload: z.intersection(
@@ -1 +0,0 @@
1
- {"version":3,"names":["_misc","require","_ioReactNativeJwt","_uuid","_types","_errors","_logging","statusAttestation","issuerConf","credential","credentialCryptoContext","appFetch","arguments","length","undefined","fetch","jwk","getPublicKey","credentialHash","getCredentialHashWithouDiscloures","statusAttUrl","openid_credential_issuer","status_attestation_endpoint","credentialPop","SignJWT","setPayload","aud","jti","uuidv4","toString","credential_hash","credential_hash_alg","setProtectedHeader","alg","typ","kid","setIssuedAt","setExpirationTime","sign","body","credential_pop","Logger","log","LogLevel","DEBUG","result","method","headers","JSON","stringify","then","hasStatusOrThrow","raw","json","StatusAttestationResponse","parse","catch","handleStatusAttestationError","status_attestation","exports","e","UnexpectedStatusCodeError","ResponseErrorBuilder","IssuerResponseError","handle","code","IssuerResponseErrorCodes","CredentialInvalidStatus","message","StatusAttestationRequestFailed","buildFrom"],"sourceRoot":"../../../../src","sources":["credential/status/02-status-attestation.ts"],"mappings":";;;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AAMA,IAAAC,iBAAA,GAAAD,OAAA;AACA,IAAAE,KAAA,GAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AACA,IAAAI,OAAA,GAAAJ,OAAA;AAMA,IAAAK,QAAA,GAAAL,OAAA;AAWA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMM,iBAAoC,GAAG,eAAAA,CAClDC,UAAU,EACVC,UAAU,EACVC,uBAAuB,EAEpB;EAAA,IADHC,QAA8B,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAGG,KAAK;EAEtC,MAAMC,GAAG,GAAG,MAAMN,uBAAuB,CAACO,YAAY,CAAC,CAAC;EACxD,MAAMC,cAAc,GAAG,MAAM,IAAAC,uCAAiC,EAACV,UAAU,CAAC;EAC1E,MAAMW,YAAY,GAChBZ,UAAU,CAACa,wBAAwB,CAACC,2BAA2B;EACjE,MAAMC,aAAa,GAAG,MAAM,IAAIC,yBAAO,CAACd,uBAAuB,CAAC,CAC7De,UAAU,CAAC;IACVC,GAAG,EAAEN,YAAY;IACjBO,GAAG,EAAE,IAAAC,QAAM,EAAC,CAAC,CAACC,QAAQ,CAAC,CAAC;IACxBC,eAAe,EAAEZ,cAAc;IAC/Ba,mBAAmB,EAAE;EACvB,CAAC,CAAC,CACDC,kBAAkB,CAAC;IAClBC,GAAG,EAAE,OAAO;IACZC,GAAG,EAAE,gCAAgC;IACrCC,GAAG,EAAEnB,GAAG,CAACmB;EACX,CAAC,CAAC,CACDC,WAAW,CAAC,CAAC,CACbC,iBAAiB,CAAC,IAAI,CAAC,CACvBC,IAAI,CAAC,CAAC;EAET,MAAMC,IAAI,GAAG;IACXC,cAAc,EAAEjB;EAClB,CAAC;EAEDkB,eAAM,CAACC,GAAG,CAACC,iBAAQ,CAACC,KAAK,EAAG,mBAAkBrB,aAAc,EAAC,CAAC;EAE9D,MAAMsB,MAAM,GAAG,MAAMlC,QAAQ,CAACS,YAAY,EAAE;IAC1C0B,MAAM,EAAE,MAAM;IACdC,OAAO,EAAE;MACP,cAAc,EAAE;IAClB,CAAC;IACDR,IAAI,EAAES,IAAI,CAACC,SAAS,CAACV,IAAI;EAC3B,CAAC,CAAC,CACCW,IAAI,CAAC,IAAAC,sBAAgB,EAAC,GAAG,CAAC,CAAC,CAC3BD,IAAI,CAAEE,GAAG,IAAKA,GAAG,CAACC,IAAI,CAAC,CAAC,CAAC,CACzBH,IAAI,CAAEG,IAAI,IAAKC,gCAAyB,CAACC,KAAK,CAACF,IAAI,CAAC,CAAC,CACrDG,KAAK,CAACC,4BAA4B,CAAC;EAEtC,OAAO;IAAElD,iBAAiB,EAAEsC,MAAM,CAACa;EAAmB,CAAC;AACzD,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AALAC,OAAA,CAAApD,iBAAA,GAAAA,iBAAA;AAMA,MAAMkD,4BAA4B,GAAIG,CAAU,IAAK;EACnD,IAAI,EAAEA,CAAC,YAAYC,iCAAyB,CAAC,EAAE;IAC7C,MAAMD,CAAC;EACT;EAEA,MAAM,IAAIE,4BAAoB,CAACC,2BAAmB,CAAC,CAChDC,MAAM,CAAC,GAAG,EAAE;IACXC,IAAI,EAAEC,gCAAwB,CAACC,uBAAuB;IACtDC,OAAO,EAAE;EACX,CAAC,CAAC,CACDJ,MAAM,CAAC,GAAG,EAAE;IACXC,IAAI,EAAEC,gCAAwB,CAACG,8BAA8B;IAC7DD,OAAO,EAAG;EACZ,CAAC,CAAC,CACDE,SAAS,CAACV,CAAC,CAAC;AACjB,CAAC"}