@pagopa/io-react-native-wallet 0.7.4 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. package/README.md +49 -31
  2. package/lib/commonjs/credential/index.js +13 -0
  3. package/lib/commonjs/credential/index.js.map +1 -0
  4. package/lib/commonjs/credential/issuance/01-start-flow.js +2 -0
  5. package/lib/commonjs/credential/issuance/01-start-flow.js.map +1 -0
  6. package/lib/commonjs/credential/issuance/02-evaluate-issuer-trust.js +26 -0
  7. package/lib/commonjs/credential/issuance/02-evaluate-issuer-trust.js.map +1 -0
  8. package/lib/commonjs/credential/issuance/03-start-user-authorization.js +119 -0
  9. package/lib/commonjs/credential/issuance/03-start-user-authorization.js.map +1 -0
  10. package/lib/commonjs/credential/issuance/04-complete-user-authorization.js +6 -0
  11. package/lib/commonjs/credential/issuance/04-complete-user-authorization.js.map +1 -0
  12. package/lib/commonjs/credential/issuance/05-authorize-access.js +63 -0
  13. package/lib/commonjs/credential/issuance/05-authorize-access.js.map +1 -0
  14. package/lib/commonjs/credential/issuance/06-obtain-credential.js +128 -0
  15. package/lib/commonjs/credential/issuance/06-obtain-credential.js.map +1 -0
  16. package/lib/commonjs/credential/issuance/07-confirm-credential.js +6 -0
  17. package/lib/commonjs/credential/issuance/07-confirm-credential.js.map +1 -0
  18. package/lib/commonjs/credential/issuance/const.js +9 -0
  19. package/lib/commonjs/credential/issuance/const.js.map +1 -0
  20. package/lib/commonjs/credential/issuance/index.js +34 -0
  21. package/lib/commonjs/credential/issuance/index.js.map +1 -0
  22. package/lib/commonjs/credential/presentation/01-start-flow.js +55 -0
  23. package/lib/commonjs/credential/presentation/01-start-flow.js.map +1 -0
  24. package/lib/commonjs/credential/presentation/02-evaluate-rp-trust.js +32 -0
  25. package/lib/commonjs/credential/presentation/02-evaluate-rp-trust.js.map +1 -0
  26. package/lib/commonjs/credential/presentation/03-get-request-object.js +68 -0
  27. package/lib/commonjs/credential/presentation/03-get-request-object.js.map +1 -0
  28. package/lib/commonjs/credential/presentation/04-send-authorization-response.js +139 -0
  29. package/lib/commonjs/credential/presentation/04-send-authorization-response.js.map +1 -0
  30. package/lib/commonjs/credential/presentation/index.js +34 -0
  31. package/lib/commonjs/credential/presentation/index.js.map +1 -0
  32. package/lib/commonjs/{rp → credential/presentation}/types.js +17 -34
  33. package/lib/commonjs/credential/presentation/types.js.map +1 -0
  34. package/lib/commonjs/index.js +10 -61
  35. package/lib/commonjs/index.js.map +1 -1
  36. package/lib/commonjs/pid/index.js +1 -3
  37. package/lib/commonjs/pid/index.js.map +1 -1
  38. package/lib/commonjs/sd-jwt/index.js +1 -1
  39. package/lib/commonjs/sd-jwt/index.js.map +1 -1
  40. package/lib/commonjs/sd-jwt/types.js +1 -1
  41. package/lib/commonjs/sd-jwt/types.js.map +1 -1
  42. package/lib/commonjs/trust/chain.js +32 -4
  43. package/lib/commonjs/trust/chain.js.map +1 -1
  44. package/lib/commonjs/trust/index.js +105 -20
  45. package/lib/commonjs/trust/index.js.map +1 -1
  46. package/lib/commonjs/trust/types.js +54 -35
  47. package/lib/commonjs/trust/types.js.map +1 -1
  48. package/lib/commonjs/utils/crypto.js +5 -18
  49. package/lib/commonjs/utils/crypto.js.map +1 -1
  50. package/lib/commonjs/utils/errors.js +35 -4
  51. package/lib/commonjs/utils/errors.js.map +1 -1
  52. package/lib/commonjs/utils/misc.js +23 -0
  53. package/lib/commonjs/utils/misc.js.map +1 -0
  54. package/lib/commonjs/utils/par.js +86 -0
  55. package/lib/commonjs/utils/par.js.map +1 -0
  56. package/lib/module/credential/index.js +4 -0
  57. package/lib/module/credential/index.js.map +1 -0
  58. package/lib/module/credential/issuance/01-start-flow.js +2 -0
  59. package/lib/module/credential/issuance/01-start-flow.js.map +1 -0
  60. package/lib/module/credential/issuance/02-evaluate-issuer-trust.js +19 -0
  61. package/lib/module/credential/issuance/02-evaluate-issuer-trust.js.map +1 -0
  62. package/lib/module/credential/issuance/03-start-user-authorization.js +109 -0
  63. package/lib/module/credential/issuance/03-start-user-authorization.js.map +1 -0
  64. package/lib/module/credential/issuance/04-complete-user-authorization.js +2 -0
  65. package/lib/module/credential/issuance/04-complete-user-authorization.js.map +1 -0
  66. package/lib/module/credential/issuance/05-authorize-access.js +55 -0
  67. package/lib/module/credential/issuance/05-authorize-access.js.map +1 -0
  68. package/lib/module/credential/issuance/06-obtain-credential.js +117 -0
  69. package/lib/module/credential/issuance/06-obtain-credential.js.map +1 -0
  70. package/lib/module/credential/issuance/07-confirm-credential.js +2 -0
  71. package/lib/module/credential/issuance/07-confirm-credential.js.map +1 -0
  72. package/lib/module/credential/issuance/const.js +2 -0
  73. package/lib/module/credential/issuance/const.js.map +1 -0
  74. package/lib/module/credential/issuance/index.js +6 -0
  75. package/lib/module/credential/issuance/index.js.map +1 -0
  76. package/lib/module/credential/presentation/01-start-flow.js +46 -0
  77. package/lib/module/credential/presentation/01-start-flow.js.map +1 -0
  78. package/lib/module/credential/presentation/02-evaluate-rp-trust.js +25 -0
  79. package/lib/module/credential/presentation/02-evaluate-rp-trust.js.map +1 -0
  80. package/lib/module/credential/presentation/03-get-request-object.js +60 -0
  81. package/lib/module/credential/presentation/03-get-request-object.js.map +1 -0
  82. package/lib/module/credential/presentation/04-send-authorization-response.js +128 -0
  83. package/lib/module/credential/presentation/04-send-authorization-response.js.map +1 -0
  84. package/lib/module/credential/presentation/index.js +6 -0
  85. package/lib/module/credential/presentation/index.js.map +1 -0
  86. package/lib/module/credential/presentation/types.js +21 -0
  87. package/lib/module/credential/presentation/types.js.map +1 -0
  88. package/lib/module/index.js +4 -5
  89. package/lib/module/index.js.map +1 -1
  90. package/lib/module/pid/index.js +1 -2
  91. package/lib/module/pid/index.js.map +1 -1
  92. package/lib/module/sd-jwt/index.js +1 -1
  93. package/lib/module/sd-jwt/index.js.map +1 -1
  94. package/lib/module/sd-jwt/types.js +1 -1
  95. package/lib/module/sd-jwt/types.js.map +1 -1
  96. package/lib/module/trust/chain.js +30 -3
  97. package/lib/module/trust/chain.js.map +1 -1
  98. package/lib/module/trust/index.js +99 -16
  99. package/lib/module/trust/index.js.map +1 -1
  100. package/lib/module/trust/types.js +50 -31
  101. package/lib/module/trust/types.js.map +1 -1
  102. package/lib/module/utils/crypto.js +2 -15
  103. package/lib/module/utils/crypto.js.map +1 -1
  104. package/lib/module/utils/errors.js +35 -4
  105. package/lib/module/utils/errors.js.map +1 -1
  106. package/lib/module/utils/misc.js +17 -0
  107. package/lib/module/utils/misc.js.map +1 -0
  108. package/lib/module/utils/par.js +74 -0
  109. package/lib/module/utils/par.js.map +1 -0
  110. package/lib/typescript/credential/index.d.ts +4 -0
  111. package/lib/typescript/credential/index.d.ts.map +1 -0
  112. package/lib/typescript/credential/issuance/01-start-flow.d.ts +11 -0
  113. package/lib/typescript/credential/issuance/01-start-flow.d.ts.map +1 -0
  114. package/lib/typescript/credential/issuance/02-evaluate-issuer-trust.d.ts +18 -0
  115. package/lib/typescript/credential/issuance/02-evaluate-issuer-trust.d.ts.map +1 -0
  116. package/lib/typescript/credential/issuance/03-start-user-authorization.d.ts +31 -0
  117. package/lib/typescript/credential/issuance/03-start-user-authorization.d.ts.map +1 -0
  118. package/lib/typescript/credential/issuance/04-complete-user-authorization.d.ts +16 -0
  119. package/lib/typescript/credential/issuance/04-complete-user-authorization.d.ts.map +1 -0
  120. package/lib/typescript/credential/issuance/05-authorize-access.d.ts +26 -0
  121. package/lib/typescript/credential/issuance/05-authorize-access.d.ts.map +1 -0
  122. package/lib/typescript/credential/issuance/06-obtain-credential.d.ts +32 -0
  123. package/lib/typescript/credential/issuance/06-obtain-credential.d.ts.map +1 -0
  124. package/lib/typescript/credential/issuance/07-confirm-credential.d.ts +11 -0
  125. package/lib/typescript/credential/issuance/07-confirm-credential.d.ts.map +1 -0
  126. package/lib/typescript/credential/issuance/const.d.ts +2 -0
  127. package/lib/typescript/credential/issuance/const.d.ts.map +1 -0
  128. package/lib/typescript/credential/issuance/index.d.ts +10 -0
  129. package/lib/typescript/credential/issuance/index.d.ts.map +1 -0
  130. package/lib/typescript/credential/presentation/01-start-flow.d.ts +20 -0
  131. package/lib/typescript/credential/presentation/01-start-flow.d.ts.map +1 -0
  132. package/lib/typescript/credential/presentation/02-evaluate-rp-trust.d.ts +18 -0
  133. package/lib/typescript/credential/presentation/02-evaluate-rp-trust.d.ts.map +1 -0
  134. package/lib/typescript/credential/presentation/03-get-request-object.d.ts +25 -0
  135. package/lib/typescript/credential/presentation/03-get-request-object.d.ts.map +1 -0
  136. package/lib/typescript/credential/presentation/04-send-authorization-response.d.ts +34 -0
  137. package/lib/typescript/credential/presentation/04-send-authorization-response.d.ts.map +1 -0
  138. package/lib/typescript/credential/presentation/index.d.ts +7 -0
  139. package/lib/typescript/credential/presentation/index.d.ts.map +1 -0
  140. package/lib/typescript/credential/presentation/types.d.ts +49 -0
  141. package/lib/typescript/credential/presentation/types.d.ts.map +1 -0
  142. package/lib/typescript/index.d.ts +4 -5
  143. package/lib/typescript/index.d.ts.map +1 -1
  144. package/lib/typescript/pid/index.d.ts +1 -2
  145. package/lib/typescript/pid/index.d.ts.map +1 -1
  146. package/lib/typescript/sd-jwt/index.d.ts +2 -2
  147. package/lib/typescript/sd-jwt/index.d.ts.map +1 -1
  148. package/lib/typescript/sd-jwt/types.d.ts +5 -5
  149. package/lib/typescript/trust/chain.d.ts +12 -3
  150. package/lib/typescript/trust/chain.d.ts.map +1 -1
  151. package/lib/typescript/trust/index.d.ts +198 -24
  152. package/lib/typescript/trust/index.d.ts.map +1 -1
  153. package/lib/typescript/trust/types.d.ts +1299 -623
  154. package/lib/typescript/trust/types.d.ts.map +1 -1
  155. package/lib/typescript/utils/crypto.d.ts +1 -1
  156. package/lib/typescript/utils/crypto.d.ts.map +1 -1
  157. package/lib/typescript/utils/dpop.d.ts +2 -2
  158. package/lib/typescript/utils/errors.d.ts.map +1 -1
  159. package/lib/typescript/utils/misc.d.ts +8 -0
  160. package/lib/typescript/utils/misc.d.ts.map +1 -0
  161. package/lib/typescript/utils/par.d.ts +68 -0
  162. package/lib/typescript/utils/par.d.ts.map +1 -0
  163. package/package.json +2 -2
  164. package/src/credential/index.ts +4 -0
  165. package/src/credential/issuance/01-start-flow.ts +10 -0
  166. package/src/credential/issuance/02-evaluate-issuer-trust.ts +31 -0
  167. package/src/credential/issuance/03-start-user-authorization.ts +138 -0
  168. package/src/credential/issuance/04-complete-user-authorization.ts +17 -0
  169. package/src/credential/issuance/05-authorize-access.ts +92 -0
  170. package/src/credential/issuance/06-obtain-credential.ts +179 -0
  171. package/src/credential/issuance/07-confirm-credential.ts +14 -0
  172. package/src/credential/issuance/const.ts +2 -0
  173. package/src/credential/issuance/index.ts +32 -0
  174. package/src/credential/presentation/01-start-flow.ts +51 -0
  175. package/src/credential/presentation/02-evaluate-rp-trust.ts +33 -0
  176. package/src/credential/presentation/03-get-request-object.ts +85 -0
  177. package/src/credential/presentation/04-send-authorization-response.ts +168 -0
  178. package/src/credential/presentation/index.ts +26 -0
  179. package/src/credential/presentation/types.ts +27 -0
  180. package/src/index.ts +7 -28
  181. package/src/pid/index.ts +1 -2
  182. package/src/sd-jwt/index.ts +2 -2
  183. package/src/sd-jwt/types.ts +1 -1
  184. package/src/trust/chain.ts +45 -3
  185. package/src/trust/index.ts +136 -19
  186. package/src/trust/types.ts +57 -35
  187. package/src/utils/crypto.ts +2 -20
  188. package/src/utils/errors.ts +40 -8
  189. package/src/utils/misc.ts +23 -0
  190. package/src/utils/par.ts +103 -0
  191. package/lib/commonjs/pid/issuing.js +0 -276
  192. package/lib/commonjs/pid/issuing.js.map +0 -1
  193. package/lib/commonjs/rp/__test__/index.test.js +0 -172
  194. package/lib/commonjs/rp/__test__/index.test.js.map +0 -1
  195. package/lib/commonjs/rp/index.js +0 -239
  196. package/lib/commonjs/rp/index.js.map +0 -1
  197. package/lib/commonjs/rp/types.js.map +0 -1
  198. package/lib/module/pid/issuing.js +0 -266
  199. package/lib/module/pid/issuing.js.map +0 -1
  200. package/lib/module/rp/__test__/index.test.js +0 -168
  201. package/lib/module/rp/__test__/index.test.js.map +0 -1
  202. package/lib/module/rp/index.js +0 -228
  203. package/lib/module/rp/index.js.map +0 -1
  204. package/lib/module/rp/types.js +0 -36
  205. package/lib/module/rp/types.js.map +0 -1
  206. package/lib/typescript/pid/issuing.d.ts +0 -57
  207. package/lib/typescript/pid/issuing.d.ts.map +0 -1
  208. package/lib/typescript/rp/__test__/index.test.d.ts +0 -2
  209. package/lib/typescript/rp/__test__/index.test.d.ts.map +0 -1
  210. package/lib/typescript/rp/index.d.ts +0 -43
  211. package/lib/typescript/rp/index.d.ts.map +0 -1
  212. package/lib/typescript/rp/types.d.ts +0 -122
  213. package/lib/typescript/rp/types.d.ts.map +0 -1
  214. package/src/pid/issuing.ts +0 -405
  215. package/src/rp/__test__/index.test.ts +0 -250
  216. package/src/rp/index.ts +0 -287
  217. package/src/rp/types.ts +0 -42
@@ -1,122 +0,0 @@
1
- import * as z from "zod";
2
- export type RequestObject = z.infer<typeof RequestObject>;
3
- export declare const RequestObject: z.ZodObject<{
4
- header: z.ZodObject<{
5
- typ: z.ZodOptional<z.ZodLiteral<"JWT">>;
6
- alg: z.ZodString;
7
- kid: z.ZodString;
8
- trust_chain: z.ZodArray<z.ZodString, "many">;
9
- }, "strip", z.ZodTypeAny, {
10
- alg: string;
11
- kid: string;
12
- trust_chain: string[];
13
- typ?: "JWT" | undefined;
14
- }, {
15
- alg: string;
16
- kid: string;
17
- trust_chain: string[];
18
- typ?: "JWT" | undefined;
19
- }>;
20
- payload: z.ZodObject<{
21
- iss: z.ZodString;
22
- iat: z.ZodNumber;
23
- exp: z.ZodNumber;
24
- state: z.ZodString;
25
- nonce: z.ZodString;
26
- response_uri: z.ZodString;
27
- response_type: z.ZodLiteral<"vp_token">;
28
- response_mode: z.ZodLiteral<"direct_post.jwt">;
29
- client_id: z.ZodString;
30
- client_id_scheme: z.ZodLiteral<"entity_id">;
31
- scope: z.ZodString;
32
- }, "strip", z.ZodTypeAny, {
33
- iss: string;
34
- iat: number;
35
- exp: number;
36
- client_id: string;
37
- nonce: string;
38
- state: string;
39
- response_type: "vp_token";
40
- response_uri: string;
41
- response_mode: "direct_post.jwt";
42
- client_id_scheme: "entity_id";
43
- scope: string;
44
- }, {
45
- iss: string;
46
- iat: number;
47
- exp: number;
48
- client_id: string;
49
- nonce: string;
50
- state: string;
51
- response_type: "vp_token";
52
- response_uri: string;
53
- response_mode: "direct_post.jwt";
54
- client_id_scheme: "entity_id";
55
- scope: string;
56
- }>;
57
- }, "strip", z.ZodTypeAny, {
58
- header: {
59
- alg: string;
60
- kid: string;
61
- trust_chain: string[];
62
- typ?: "JWT" | undefined;
63
- };
64
- payload: {
65
- iss: string;
66
- iat: number;
67
- exp: number;
68
- client_id: string;
69
- nonce: string;
70
- state: string;
71
- response_type: "vp_token";
72
- response_uri: string;
73
- response_mode: "direct_post.jwt";
74
- client_id_scheme: "entity_id";
75
- scope: string;
76
- };
77
- }, {
78
- header: {
79
- alg: string;
80
- kid: string;
81
- trust_chain: string[];
82
- typ?: "JWT" | undefined;
83
- };
84
- payload: {
85
- iss: string;
86
- iat: number;
87
- exp: number;
88
- client_id: string;
89
- nonce: string;
90
- state: string;
91
- response_type: "vp_token";
92
- response_uri: string;
93
- response_mode: "direct_post.jwt";
94
- client_id_scheme: "entity_id";
95
- scope: string;
96
- };
97
- }>;
98
- export type QRCodePayload = z.infer<typeof QRCodePayload>;
99
- export declare const QRCodePayload: z.ZodObject<{
100
- protocol: z.ZodString;
101
- resource: z.ZodString;
102
- clientId: z.ZodString;
103
- requestURI: z.ZodString;
104
- }, "strip", z.ZodTypeAny, {
105
- clientId: string;
106
- protocol: string;
107
- resource: string;
108
- requestURI: string;
109
- }, {
110
- clientId: string;
111
- protocol: string;
112
- resource: string;
113
- requestURI: string;
114
- }>;
115
- /**
116
- * A pair that associate a tokenized Verified Credential with the claims presented or requested to present.
117
- */
118
- export type Presentation = [
119
- string,
120
- string[]
121
- ];
122
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/rp/types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAC1D,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAqBxB,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAC1D,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;EAKxB,CAAC;AAEH;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG;IACO,MAAM;IACzB,MAAM,EAAE;CACtB,CAAC"}
@@ -1,405 +0,0 @@
1
- import {
2
- sha256ToBase64,
3
- type CryptoContext,
4
- SignJWT,
5
- thumbprint,
6
- } from "@pagopa/io-react-native-jwt";
7
-
8
- import { JWK } from "../utils/jwk";
9
- import uuid from "react-native-uuid";
10
- import { PidIssuingError } from "../utils/errors";
11
- import { createDPopToken } from "../utils/dpop";
12
- import { CredentialIssuerEntityConfiguration } from "../trust/types";
13
- import * as WalletInstanceAttestation from "../wallet-instance-attestation";
14
- import { SdJwt } from ".";
15
- import { useEphemeralKey } from "../utils/crypto";
16
-
17
- import * as z from "zod";
18
- import { getJwtFromFormPost } from "../utils/decoder";
19
-
20
- // This is a temporary type that will be used for demo purposes only
21
- export type CieData = {
22
- birthDate: string;
23
- fiscalCode: string;
24
- name: string;
25
- surname: string;
26
- };
27
-
28
- export type AuthorizationConf = {
29
- accessToken: string;
30
- nonce: string;
31
- clientId: string;
32
- authorizationCode: string;
33
- codeVerifier: string;
34
- walletProviderBaseUrl: string;
35
- };
36
-
37
- export type PidResponse = {
38
- credential: string;
39
- c_nonce: string;
40
- c_nonce_expires_in: number;
41
- format: string;
42
- };
43
-
44
- type AuthenticationRequestResponse = z.infer<
45
- typeof AuthenticationRequestResponse
46
- >;
47
- const AuthenticationRequestResponse = z.object({
48
- code: z.string(),
49
- state: z.string(), // TODO: refine to known paths using literals
50
- iss: z.string(),
51
- });
52
-
53
- const assertionType =
54
- "urn:ietf:params:oauth:client-assertion-type:jwt-client-attestation";
55
-
56
- /**
57
- * Make a PAR request to the PID issuer and return the response url
58
- */
59
- const getPar =
60
- ({
61
- wiaCryptoContext,
62
- appFetch = fetch,
63
- }: {
64
- wiaCryptoContext: CryptoContext;
65
- appFetch?: GlobalFetch["fetch"];
66
- }) =>
67
- async (
68
- clientId: string,
69
- codeVerifier: string,
70
- walletProviderBaseUrl: string,
71
- pidProviderEntityConfiguration: CredentialIssuerEntityConfiguration,
72
- walletInstanceAttestation: string
73
- ): Promise<string> => {
74
- // Calculate the thumbprint of the public key of the Wallet Instance Attestation.
75
- // The PAR request token is signed used the Wallet Instance Attestation key.
76
- // The signature can be verified by reading the public key from the key set shippet with the it will ship the Wallet Instance Attestation;
77
- // key is matched by its kid, which is supposed to be the thumbprint of its public key.
78
- const keyThumbprint = await wiaCryptoContext
79
- .getPublicKey()
80
- .then(JWK.parse)
81
- .then(thumbprint);
82
-
83
- const iss = WalletInstanceAttestation.decode(walletInstanceAttestation)
84
- .payload.cnf.jwk.kid;
85
-
86
- const codeChallenge = await sha256ToBase64(codeVerifier);
87
-
88
- const signedJwtForPar = await new SignJWT(wiaCryptoContext)
89
- .setProtectedHeader({
90
- kid: keyThumbprint,
91
- })
92
- .setPayload({
93
- iss,
94
- aud: pidProviderEntityConfiguration.payload.iss,
95
- jti: `${uuid.v4()}`,
96
- client_assertion_type: assertionType,
97
- authorization_details: [
98
- {
99
- credential_definition: {
100
- type: "PersonIdentificationData",
101
- },
102
- format: "vc+sd-jwt",
103
- type: "openid_credential",
104
- },
105
- ],
106
- response_type: "code",
107
- code_challenge_method: "s256",
108
- redirect_uri: walletProviderBaseUrl,
109
- state: `${uuid.v4()}`,
110
- client_id: clientId,
111
- code_challenge: codeChallenge,
112
- })
113
- .setIssuedAt()
114
- .setExpirationTime("1h")
115
- .sign();
116
-
117
- const parUrl =
118
- pidProviderEntityConfiguration.payload.metadata.openid_credential_issuer
119
- .pushed_authorization_request_endpoint;
120
-
121
- const requestBody = {
122
- response_type: "code",
123
- client_id: clientId,
124
- code_challenge: codeChallenge,
125
- code_challenge_method: "S256",
126
- client_assertion_type: assertionType,
127
- client_assertion: walletInstanceAttestation,
128
- request: signedJwtForPar,
129
- };
130
-
131
- var formBody = new URLSearchParams(requestBody);
132
-
133
- const response = await appFetch(parUrl, {
134
- method: "POST",
135
- headers: {
136
- "Content-Type": "application/x-www-form-urlencoded",
137
- },
138
- body: formBody.toString(),
139
- });
140
-
141
- if (response.status === 201) {
142
- const result = await response.json();
143
- return result.request_uri;
144
- }
145
-
146
- throw new PidIssuingError(
147
- `Unable to obtain PAR. Response code: ${await response.text()}`
148
- );
149
- };
150
-
151
- /**
152
- * Make an authorization request
153
- */
154
- const getAuthenticationRequest =
155
- ({ appFetch = fetch }: { appFetch?: GlobalFetch["fetch"] }) =>
156
- async (
157
- clientId: string,
158
- requestUri: string,
159
- pidProviderEntityConfiguration: CredentialIssuerEntityConfiguration,
160
- cieData: CieData
161
- ): Promise<AuthenticationRequestResponse> => {
162
- const authzRequestEndpoint =
163
- pidProviderEntityConfiguration.payload.metadata.openid_credential_issuer
164
- .authorization_endpoint;
165
-
166
- /* User's personal data is not supposed to transit in this flow,
167
- * but to be provided to the PID issuer directly by its chosen authentication method (CIE).
168
- * Being the project in an initial phase, and being we were still unable to fully comply with authentication,
169
- * we temporarily provide data from the App's logged user.
170
- * */
171
- const params = new URLSearchParams({
172
- client_id: clientId,
173
- request_uri: requestUri,
174
- name: cieData.name,
175
- surname: cieData.surname,
176
- birth_date: cieData.birthDate,
177
- fiscal_code: cieData.fiscalCode,
178
- });
179
-
180
- const response = await appFetch(authzRequestEndpoint + "?" + params, {
181
- method: "GET",
182
- });
183
-
184
- if (response.status === 200) {
185
- const formData = await response.text();
186
- const { decodedJwt } = await getJwtFromFormPost(formData);
187
- const parsed = AuthenticationRequestResponse.parse(decodedJwt.payload);
188
- return parsed;
189
- }
190
-
191
- throw new PidIssuingError(
192
- `Unable to obtain Authorization Request. Response code: ${await response.text()}`
193
- );
194
- };
195
-
196
- /**
197
- * Start the issuing flow by generating an authorization request to the PID Provider. Obtain from the PID Provider an access token to be used to complete the issuing flow.
198
- *
199
- * @param params.wiaCryptoContext The key pair associated with the WIA. Will be use to prove the ownership of the attestation.
200
- * @param params.appFetch (optional) Http client
201
- * @param walletInstanceAttestation Wallet Instance Attestation token.
202
- * @param walletProviderBaseUrl Base url for the Wallet Provider.
203
- * @param pidProviderEntityConfiguration The Entity Configuration of the PID Provider, from which discover public endooints.
204
- * @param cieData Data red from the CIE login process
205
- * @returns The access token along with the values that identify the issuing session.
206
- */
207
- export const authorizeIssuing =
208
- ({
209
- wiaCryptoContext,
210
- appFetch = fetch,
211
- }: {
212
- wiaCryptoContext: CryptoContext;
213
- appFetch?: GlobalFetch["fetch"];
214
- }) =>
215
- async (
216
- walletInstanceAttestation: string,
217
- walletProviderBaseUrl: string,
218
- pidProviderEntityConfiguration: CredentialIssuerEntityConfiguration,
219
- cieData: CieData
220
- ): Promise<AuthorizationConf> => {
221
- // FIXME: do better
222
- const clientId = await wiaCryptoContext.getPublicKey().then((_) => _.kid);
223
- const codeVerifier = `${uuid.v4()}`;
224
-
225
- const tokenUrl =
226
- pidProviderEntityConfiguration.payload.metadata.openid_credential_issuer
227
- .token_endpoint;
228
-
229
- const requestUri = await getPar({ wiaCryptoContext, appFetch })(
230
- clientId,
231
- codeVerifier,
232
- walletProviderBaseUrl,
233
- pidProviderEntityConfiguration,
234
- walletInstanceAttestation
235
- );
236
-
237
- const authenticationRequest = await getAuthenticationRequest({})(
238
- clientId,
239
- requestUri,
240
- pidProviderEntityConfiguration,
241
- cieData
242
- );
243
-
244
- const authorizationCode = authenticationRequest.code;
245
-
246
- const signedDPop = await useEphemeralKey((ctx) =>
247
- createDPopToken(
248
- {
249
- htm: "POST",
250
- htu: tokenUrl,
251
- jti: `${uuid.v4()}`,
252
- },
253
- ctx
254
- )
255
- );
256
-
257
- const requestBody = {
258
- grant_type: "authorization code",
259
- client_id: clientId,
260
- code: authorizationCode,
261
- code_verifier: codeVerifier,
262
- client_assertion_type: assertionType,
263
- client_assertion: walletInstanceAttestation,
264
- redirect_uri: walletProviderBaseUrl,
265
- };
266
- var formBody = new URLSearchParams(requestBody);
267
-
268
- const response = await appFetch(tokenUrl, {
269
- method: "POST",
270
- headers: {
271
- "Content-Type": "application/x-www-form-urlencoded",
272
- DPoP: signedDPop,
273
- },
274
- body: formBody.toString(),
275
- });
276
-
277
- if (response.status === 200) {
278
- const { c_nonce, access_token } = await response.json();
279
- return {
280
- accessToken: access_token,
281
- nonce: c_nonce,
282
- clientId,
283
- codeVerifier,
284
- authorizationCode,
285
- walletProviderBaseUrl,
286
- };
287
- }
288
-
289
- throw new PidIssuingError(
290
- `Unable to obtain token. Response code: ${await response.text()}`
291
- );
292
- };
293
-
294
- /**
295
- * Return the signed jwt for nonce proof of possession
296
- */
297
- const createNonceProof = async (
298
- nonce: string,
299
- issuer: string,
300
- audience: string,
301
- ctx: CryptoContext
302
- ): Promise<string> => {
303
- return new SignJWT(ctx)
304
- .setPayload({
305
- nonce,
306
- jwk: await ctx.getPublicKey(),
307
- })
308
- .setProtectedHeader({
309
- type: "openid4vci-proof+jwt",
310
- })
311
- .setAudience(audience)
312
- .setIssuer(issuer)
313
- .setIssuedAt()
314
- .setExpirationTime("1h")
315
- .sign();
316
- };
317
-
318
- /**
319
- * Complete the issuing flow and get the PID credential.
320
- *
321
- * @param params.pidCryptoContext The key pair associated with the PID. Will be use to prove the ownership of the credential.
322
- * @param params.appFetch (optional) Http client
323
- * @param authConf The authorization configuration retrieved with the access token
324
- * @returns The PID credential token
325
- */
326
- export const getCredential =
327
- ({
328
- pidCryptoContext,
329
- appFetch = fetch,
330
- }: {
331
- pidCryptoContext: CryptoContext;
332
- appFetch?: GlobalFetch["fetch"];
333
- }) =>
334
- async (
335
- { nonce, accessToken, clientId, walletProviderBaseUrl }: AuthorizationConf,
336
- pidProviderEntityConfiguration: CredentialIssuerEntityConfiguration
337
- ): Promise<PidResponse> => {
338
- const credentialUrl =
339
- pidProviderEntityConfiguration.payload.metadata.openid_credential_issuer
340
- .credential_endpoint;
341
-
342
- const signedDPopForPid = await createDPopToken(
343
- {
344
- htm: "POST",
345
- htu: credentialUrl,
346
- jti: `${uuid.v4()}`,
347
- },
348
- pidCryptoContext
349
- );
350
-
351
- const signedNonceProof = await createNonceProof(
352
- nonce,
353
- clientId,
354
- walletProviderBaseUrl,
355
- pidCryptoContext
356
- );
357
-
358
- const requestBody = {
359
- credential_definition: JSON.stringify({
360
- type: ["PersonIdentificationData"],
361
- }),
362
- format: "vc+sd-jwt",
363
- proof: JSON.stringify({
364
- jwt: signedNonceProof,
365
- proof_type: "jwt",
366
- }),
367
- };
368
- const formBody = new URLSearchParams(requestBody);
369
-
370
- const response = await appFetch(credentialUrl, {
371
- method: "POST",
372
- headers: {
373
- "Content-Type": "application/x-www-form-urlencoded",
374
- DPoP: signedDPopForPid,
375
- Authorization: accessToken,
376
- },
377
- body: formBody.toString(),
378
- });
379
-
380
- if (response.status === 200) {
381
- const pidResponse = (await response.json()) as PidResponse;
382
- await validatePid(pidResponse.credential, pidCryptoContext);
383
- return pidResponse;
384
- }
385
-
386
- throw new PidIssuingError(
387
- `Unable to obtain credential! url=${credentialUrl} status=${
388
- response.status
389
- } body=${await response.text()}`
390
- );
391
- };
392
-
393
- const validatePid = async (pidJwt: string, pidCryptoContext: CryptoContext) => {
394
- const decoded = SdJwt.decode(pidJwt);
395
- const pidKey = await pidCryptoContext.getPublicKey();
396
- const holderBindedKey = decoded.sdJwt.payload.cnf.jwk;
397
-
398
- if ((await thumbprint(pidKey)) !== (await thumbprint(holderBindedKey))) {
399
- throw new PidIssuingError(
400
- `The obtained pid does not seem to be valid according to your configuration. Your PID public key is: ${JSON.stringify(
401
- pidKey
402
- )} but PID holder binded key is: ${JSON.stringify(holderBindedKey)}`
403
- );
404
- }
405
- };