@pagopa/io-react-native-wallet 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. package/README.md +98 -22
  2. package/lib/commonjs/index.js +12 -8
  3. package/lib/commonjs/index.js.map +1 -1
  4. package/lib/commonjs/pid/index.js +3 -8
  5. package/lib/commonjs/pid/index.js.map +1 -1
  6. package/lib/commonjs/pid/issuing.js +152 -168
  7. package/lib/commonjs/pid/issuing.js.map +1 -1
  8. package/lib/commonjs/pid/metadata.js +28 -25
  9. package/lib/commonjs/pid/metadata.js.map +1 -1
  10. package/lib/commonjs/rp/__test__/index.test.js +5 -3
  11. package/lib/commonjs/rp/__test__/index.test.js.map +1 -1
  12. package/lib/commonjs/rp/index.js +158 -154
  13. package/lib/commonjs/rp/index.js.map +1 -1
  14. package/lib/commonjs/trust/types.js +9 -7
  15. package/lib/commonjs/trust/types.js.map +1 -1
  16. package/lib/commonjs/utils/crypto.js +46 -0
  17. package/lib/commonjs/utils/crypto.js.map +1 -0
  18. package/lib/commonjs/utils/dpop.js +14 -7
  19. package/lib/commonjs/utils/dpop.js.map +1 -1
  20. package/lib/commonjs/wallet-instance-attestation/index.js +3 -3
  21. package/lib/commonjs/wallet-instance-attestation/issuing.js +50 -60
  22. package/lib/commonjs/wallet-instance-attestation/issuing.js.map +1 -1
  23. package/lib/module/index.js +4 -3
  24. package/lib/module/index.js.map +1 -1
  25. package/lib/module/pid/index.js +1 -1
  26. package/lib/module/pid/index.js.map +1 -1
  27. package/lib/module/pid/issuing.js +151 -171
  28. package/lib/module/pid/issuing.js.map +1 -1
  29. package/lib/module/pid/metadata.js +28 -25
  30. package/lib/module/pid/metadata.js.map +1 -1
  31. package/lib/module/rp/__test__/index.test.js +1 -1
  32. package/lib/module/rp/__test__/index.test.js.map +1 -1
  33. package/lib/module/rp/index.js +155 -153
  34. package/lib/module/rp/index.js.map +1 -1
  35. package/lib/module/trust/types.js +7 -6
  36. package/lib/module/trust/types.js.map +1 -1
  37. package/lib/module/utils/crypto.js +40 -0
  38. package/lib/module/utils/crypto.js.map +1 -0
  39. package/lib/module/utils/dpop.js +13 -5
  40. package/lib/module/utils/dpop.js.map +1 -1
  41. package/lib/module/wallet-instance-attestation/index.js +2 -2
  42. package/lib/module/wallet-instance-attestation/index.js.map +1 -1
  43. package/lib/module/wallet-instance-attestation/issuing.js +48 -58
  44. package/lib/module/wallet-instance-attestation/issuing.js.map +1 -1
  45. package/lib/typescript/index.d.ts +4 -3
  46. package/lib/typescript/index.d.ts.map +1 -1
  47. package/lib/typescript/pid/index.d.ts +1 -1
  48. package/lib/typescript/pid/index.d.ts.map +1 -1
  49. package/lib/typescript/pid/issuing.d.ts +51 -87
  50. package/lib/typescript/pid/issuing.d.ts.map +1 -1
  51. package/lib/typescript/pid/metadata.d.ts +1338 -408
  52. package/lib/typescript/pid/metadata.d.ts.map +1 -1
  53. package/lib/typescript/rp/index.d.ts +48 -86
  54. package/lib/typescript/rp/index.d.ts.map +1 -1
  55. package/lib/typescript/rp/types.d.ts +413 -57
  56. package/lib/typescript/rp/types.d.ts.map +1 -1
  57. package/lib/typescript/sd-jwt/index.d.ts +1 -1
  58. package/lib/typescript/sd-jwt/index.d.ts.map +1 -1
  59. package/lib/typescript/trust/types.d.ts +1000 -274
  60. package/lib/typescript/trust/types.d.ts.map +1 -1
  61. package/lib/typescript/utils/crypto.d.ts +10 -0
  62. package/lib/typescript/utils/crypto.d.ts.map +1 -0
  63. package/lib/typescript/utils/dpop.d.ts +10 -2
  64. package/lib/typescript/utils/dpop.d.ts.map +1 -1
  65. package/lib/typescript/wallet-instance-attestation/index.d.ts +2 -2
  66. package/lib/typescript/wallet-instance-attestation/index.d.ts.map +1 -1
  67. package/lib/typescript/wallet-instance-attestation/issuing.d.ts +17 -31
  68. package/lib/typescript/wallet-instance-attestation/issuing.d.ts.map +1 -1
  69. package/package.json +2 -2
  70. package/src/index.ts +5 -3
  71. package/src/pid/index.ts +1 -1
  72. package/src/pid/issuing.ts +233 -225
  73. package/src/pid/metadata.ts +32 -27
  74. package/src/rp/__test__/index.test.ts +1 -1
  75. package/src/rp/index.ts +180 -188
  76. package/src/sd-jwt/index.ts +1 -1
  77. package/src/trust/types.ts +39 -32
  78. package/src/utils/crypto.ts +41 -0
  79. package/src/utils/dpop.ts +17 -7
  80. package/src/wallet-instance-attestation/index.ts +2 -2
  81. package/src/wallet-instance-attestation/issuing.ts +55 -62
@@ -1,4 +1,7 @@
1
- import { decode as decodeJwt } from "@pagopa/io-react-native-jwt";
1
+ import {
2
+ type CryptoContext,
3
+ decode as decodeJwt,
4
+ } from "@pagopa/io-react-native-jwt";
2
5
  import { verify as verifyJwt } from "@pagopa/io-react-native-jwt";
3
6
  import { SignJWT, thumbprint } from "@pagopa/io-react-native-jwt";
4
7
  import { JWK, fixBase64EncodingOnKey } from "../utils/jwk";
@@ -6,73 +9,64 @@ import { WalletInstanceAttestationRequestJwt } from "./types";
6
9
  import uuid from "react-native-uuid";
7
10
  import { WalletInstanceAttestationIssuingError } from "../utils/errors";
8
11
 
9
- export class Issuing {
10
- walletProviderBaseUrl: string;
11
- appFetch: GlobalFetch["fetch"];
12
- constructor(
13
- walletProviderBaseUrl: string,
14
- appFetch: GlobalFetch["fetch"] = fetch
15
- ) {
16
- this.walletProviderBaseUrl = walletProviderBaseUrl;
17
- this.appFetch = appFetch;
18
- }
12
+ async function getAttestationRequest(
13
+ wiaCryptoContext: CryptoContext,
14
+ walletProviderBaseUrl: string
15
+ ): Promise<string> {
16
+ const jwk = await wiaCryptoContext.getPublicKey();
17
+ const parsedJwk = JWK.parse(jwk);
18
+ const keyThumbprint = await thumbprint(parsedJwk);
19
+ const publicKey = { ...parsedJwk, kid: keyThumbprint };
19
20
 
20
- /**
21
- * Get the Wallet Instance Attestation Request to sign
22
- *
23
- * @async @function
24
- *
25
- * @param jwk Public key of the wallet instance
26
- *
27
- * @returns {string} Wallet Instance Attestation Request to sign
28
- *
29
- */
30
- async getAttestationRequestToSign(jwk: JWK): Promise<string> {
31
- const parsedJwk = JWK.parse(jwk);
32
- const keyThumbprint = await thumbprint(parsedJwk);
33
- const publicKey = { ...parsedJwk, kid: keyThumbprint };
34
-
35
- const walletInstanceAttestationRequest = new SignJWT({
21
+ return new SignJWT(wiaCryptoContext)
22
+ .setPayload({
36
23
  iss: keyThumbprint,
37
- aud: this.walletProviderBaseUrl,
24
+ aud: walletProviderBaseUrl,
38
25
  jti: `${uuid.v4()}`,
39
26
  nonce: `${uuid.v4()}`,
40
27
  cnf: {
41
28
  jwk: fixBase64EncodingOnKey(publicKey),
42
29
  },
43
30
  })
44
- .setProtectedHeader({
45
- alg: "ES256",
46
- kid: publicKey.kid,
47
- typ: "wiar+jwt",
48
- })
49
- .setIssuedAt()
50
- .setExpirationTime("1h")
51
- .toSign();
31
+ .setProtectedHeader({
32
+ kid: publicKey.kid,
33
+ typ: "wiar+jwt",
34
+ })
35
+ .setPayload({
36
+ iss: keyThumbprint,
37
+ sub: walletProviderBaseUrl,
38
+ jti: `${uuid.v4()}`,
39
+ type: "WalletInstanceAttestationRequest",
40
+ cnf: {
41
+ jwk: fixBase64EncodingOnKey(publicKey),
42
+ },
43
+ })
52
44
 
53
- return walletInstanceAttestationRequest;
54
- }
45
+ .setIssuedAt()
46
+ .setExpirationTime("1h")
47
+ .sign();
48
+ }
55
49
 
56
- /**
57
- * Get the Wallet Instance Attestation given a
58
- * Wallet Instance Attestation Request and signature
59
- *
60
- * @async @function
61
- *
62
- * @param attestationRequest Wallet Instance Attestaion Request
63
- * obtained with {@link getAttestationRequestToSign}
64
- * @param signature Signature of the Wallet Instance Attestaion Request
65
- *
66
- * @returns {string} Wallet Instance Attestation
67
- *
68
- */
69
- async getAttestation(
70
- attestationRequest: string,
71
- signature: string
72
- ): Promise<string> {
73
- const signedAttestationRequest = await SignJWT.appendSignature(
74
- attestationRequest,
75
- signature
50
+ /**
51
+ * Request a Wallet Instance Attestation (WIA) to the Wallet provider
52
+ *
53
+ * @param params.wiaCryptoContext The key pair associated with the WIA. Will be use to prove the ownership of the attestation.
54
+ * @param params.appFetch (optional) Http client
55
+ * @param walletProviderBaseUrl Base url for the Wallet Provider
56
+ * @returns The retrieved Wallet Instance Attestation token
57
+ */
58
+ export const getAttestation =
59
+ ({
60
+ wiaCryptoContext,
61
+ appFetch = fetch,
62
+ }: {
63
+ wiaCryptoContext: CryptoContext;
64
+ appFetch?: GlobalFetch["fetch"];
65
+ }) =>
66
+ async (walletProviderBaseUrl: string): Promise<string> => {
67
+ const signedAttestationRequest = await getAttestationRequest(
68
+ wiaCryptoContext,
69
+ walletProviderBaseUrl
76
70
  );
77
71
 
78
72
  const decodedRequest = decodeJwt(signedAttestationRequest);
@@ -84,13 +78,13 @@ export class Issuing {
84
78
 
85
79
  await verifyJwt(signedAttestationRequest, publicKey);
86
80
 
87
- const tokenUrl = new URL("token", this.walletProviderBaseUrl).href;
81
+ const tokenUrl = new URL("token", walletProviderBaseUrl).href;
88
82
  const requestBody = {
89
83
  grant_type:
90
84
  "urn:ietf:params:oauth:client-assertion-type:jwt-client-attestation",
91
85
  assertion: signedAttestationRequest,
92
86
  };
93
- const response = await this.appFetch(tokenUrl, {
87
+ const response = await appFetch(tokenUrl, {
94
88
  method: "POST",
95
89
  headers: {
96
90
  "Content-Type": "application/json",
@@ -106,5 +100,4 @@ export class Issuing {
106
100
  "Unable to obtain wallet instance attestation from wallet provider",
107
101
  `Response code: ${response.status}`
108
102
  );
109
- }
110
- }
103
+ };