@pagopa/io-react-native-wallet 0.27.1 → 0.28.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commonjs/client/generated/wallet-provider.js +27 -19
- package/lib/commonjs/client/generated/wallet-provider.js.map +1 -1
- package/lib/commonjs/credential/issuance/03-start-user-authorization.js +3 -0
- package/lib/commonjs/credential/issuance/03-start-user-authorization.js.map +1 -1
- package/lib/commonjs/credential/presentation/01-start-flow.js +14 -24
- package/lib/commonjs/credential/presentation/01-start-flow.js.map +1 -1
- package/lib/commonjs/credential/presentation/03-get-request-object.js +30 -42
- package/lib/commonjs/credential/presentation/03-get-request-object.js.map +1 -1
- package/lib/commonjs/credential/presentation/04-retrieve-rp-jwks.js +32 -0
- package/lib/commonjs/credential/presentation/04-retrieve-rp-jwks.js.map +1 -0
- package/lib/commonjs/credential/presentation/05-verify-request-object.js +53 -0
- package/lib/commonjs/credential/presentation/05-verify-request-object.js.map +1 -0
- package/lib/commonjs/credential/presentation/06-fetch-presentation-definition.js +39 -0
- package/lib/commonjs/credential/presentation/06-fetch-presentation-definition.js.map +1 -0
- package/lib/commonjs/credential/presentation/07-evaluate-dcql-query.js +125 -0
- package/lib/commonjs/credential/presentation/07-evaluate-dcql-query.js.map +1 -0
- package/lib/commonjs/credential/presentation/07-evaluate-input-descriptor.js +289 -0
- package/lib/commonjs/credential/presentation/07-evaluate-input-descriptor.js.map +1 -0
- package/lib/commonjs/credential/presentation/08-send-authorization-response.js +170 -0
- package/lib/commonjs/credential/presentation/08-send-authorization-response.js.map +1 -0
- package/lib/commonjs/credential/presentation/errors.js +69 -1
- package/lib/commonjs/credential/presentation/errors.js.map +1 -1
- package/lib/commonjs/credential/presentation/index.js +29 -1
- package/lib/commonjs/credential/presentation/index.js.map +1 -1
- package/lib/commonjs/credential/presentation/types.js +124 -3
- package/lib/commonjs/credential/presentation/types.js.map +1 -1
- package/lib/commonjs/sd-jwt/index.js +41 -1
- package/lib/commonjs/sd-jwt/index.js.map +1 -1
- package/lib/commonjs/trust/chain.js +35 -50
- package/lib/commonjs/trust/chain.js.map +1 -1
- package/lib/commonjs/trust/index.js +139 -16
- package/lib/commonjs/trust/index.js.map +1 -1
- package/lib/commonjs/trust/types.js +36 -12
- package/lib/commonjs/trust/types.js.map +1 -1
- package/lib/commonjs/trust/utils.js +41 -0
- package/lib/commonjs/trust/utils.js.map +1 -0
- package/lib/commonjs/utils/jwk.js +5 -1
- package/lib/commonjs/utils/jwk.js.map +1 -1
- package/lib/commonjs/wallet-instance/index.js +10 -0
- package/lib/commonjs/wallet-instance/index.js.map +1 -1
- package/lib/module/client/generated/wallet-provider.js +22 -15
- package/lib/module/client/generated/wallet-provider.js.map +1 -1
- package/lib/module/credential/issuance/03-start-user-authorization.js +3 -0
- package/lib/module/credential/issuance/03-start-user-authorization.js.map +1 -1
- package/lib/module/credential/presentation/01-start-flow.js +14 -24
- package/lib/module/credential/presentation/01-start-flow.js.map +1 -1
- package/lib/module/credential/presentation/03-get-request-object.js +31 -43
- package/lib/module/credential/presentation/03-get-request-object.js.map +1 -1
- package/lib/module/credential/presentation/04-retrieve-rp-jwks.js +25 -0
- package/lib/module/credential/presentation/04-retrieve-rp-jwks.js.map +1 -0
- package/lib/module/credential/presentation/05-verify-request-object.js +46 -0
- package/lib/module/credential/presentation/05-verify-request-object.js.map +1 -0
- package/lib/module/credential/presentation/06-fetch-presentation-definition.js +32 -0
- package/lib/module/credential/presentation/06-fetch-presentation-definition.js.map +1 -0
- package/lib/module/credential/presentation/07-evaluate-dcql-query.js +117 -0
- package/lib/module/credential/presentation/07-evaluate-dcql-query.js.map +1 -0
- package/lib/module/credential/presentation/07-evaluate-input-descriptor.js +278 -0
- package/lib/module/credential/presentation/07-evaluate-input-descriptor.js.map +1 -0
- package/lib/module/credential/presentation/08-send-authorization-response.js +158 -0
- package/lib/module/credential/presentation/08-send-authorization-response.js.map +1 -0
- package/lib/module/credential/presentation/errors.js +64 -0
- package/lib/module/credential/presentation/errors.js.map +1 -1
- package/lib/module/credential/presentation/index.js +6 -2
- package/lib/module/credential/presentation/index.js.map +1 -1
- package/lib/module/credential/presentation/types.js +121 -2
- package/lib/module/credential/presentation/types.js.map +1 -1
- package/lib/module/sd-jwt/index.js +40 -1
- package/lib/module/sd-jwt/index.js.map +1 -1
- package/lib/module/trust/chain.js +32 -46
- package/lib/module/trust/chain.js.map +1 -1
- package/lib/module/trust/index.js +139 -18
- package/lib/module/trust/index.js.map +1 -1
- package/lib/module/trust/types.js +34 -11
- package/lib/module/trust/types.js.map +1 -1
- package/lib/module/trust/utils.js +33 -0
- package/lib/module/trust/utils.js.map +1 -0
- package/lib/module/utils/jwk.js +3 -0
- package/lib/module/utils/jwk.js.map +1 -1
- package/lib/module/wallet-instance/index.js +9 -0
- package/lib/module/wallet-instance/index.js.map +1 -1
- package/lib/typescript/client/generated/wallet-provider.d.ts +91 -54
- package/lib/typescript/client/generated/wallet-provider.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/03-start-user-authorization.d.ts.map +1 -1
- package/lib/typescript/credential/presentation/01-start-flow.d.ts +26 -5
- package/lib/typescript/credential/presentation/01-start-flow.d.ts.map +1 -1
- package/lib/typescript/credential/presentation/03-get-request-object.d.ts +7 -10
- package/lib/typescript/credential/presentation/03-get-request-object.d.ts.map +1 -1
- package/lib/typescript/credential/presentation/04-retrieve-rp-jwks.d.ts +23 -0
- package/lib/typescript/credential/presentation/04-retrieve-rp-jwks.d.ts.map +1 -0
- package/lib/typescript/credential/presentation/05-verify-request-object.d.ts +18 -0
- package/lib/typescript/credential/presentation/05-verify-request-object.d.ts.map +1 -0
- package/lib/typescript/credential/presentation/06-fetch-presentation-definition.d.ts +21 -0
- package/lib/typescript/credential/presentation/06-fetch-presentation-definition.d.ts.map +1 -0
- package/lib/typescript/credential/presentation/07-evaluate-dcql-query.d.ts +20 -0
- package/lib/typescript/credential/presentation/07-evaluate-dcql-query.d.ts.map +1 -0
- package/lib/typescript/credential/presentation/07-evaluate-input-descriptor.d.ts +88 -0
- package/lib/typescript/credential/presentation/07-evaluate-input-descriptor.d.ts.map +1 -0
- package/lib/typescript/credential/presentation/08-send-authorization-response.d.ts +70 -0
- package/lib/typescript/credential/presentation/08-send-authorization-response.d.ts.map +1 -0
- package/lib/typescript/credential/presentation/errors.d.ts +44 -0
- package/lib/typescript/credential/presentation/errors.d.ts.map +1 -1
- package/lib/typescript/credential/presentation/index.d.ts +7 -3
- package/lib/typescript/credential/presentation/index.d.ts.map +1 -1
- package/lib/typescript/credential/presentation/types.d.ts +747 -10
- package/lib/typescript/credential/presentation/types.d.ts.map +1 -1
- package/lib/typescript/credential/status/types.d.ts +6 -6
- package/lib/typescript/sd-jwt/index.d.ts +31 -12
- package/lib/typescript/sd-jwt/index.d.ts.map +1 -1
- package/lib/typescript/sd-jwt/types.d.ts +6 -6
- package/lib/typescript/trust/chain.d.ts +4 -9
- package/lib/typescript/trust/chain.d.ts.map +1 -1
- package/lib/typescript/trust/index.d.ts +337 -61
- package/lib/typescript/trust/index.d.ts.map +1 -1
- package/lib/typescript/trust/types.d.ts +4074 -407
- package/lib/typescript/trust/types.d.ts.map +1 -1
- package/lib/typescript/trust/utils.d.ts +12 -0
- package/lib/typescript/trust/utils.d.ts.map +1 -0
- package/lib/typescript/utils/decoder.d.ts +1 -1
- package/lib/typescript/utils/decoder.d.ts.map +1 -1
- package/lib/typescript/utils/jwk.d.ts +137 -0
- package/lib/typescript/utils/jwk.d.ts.map +1 -1
- package/lib/typescript/wallet-instance/index.d.ts +8 -0
- package/lib/typescript/wallet-instance/index.d.ts.map +1 -1
- package/lib/typescript/wallet-instance-attestation/types.d.ts +36 -36
- package/package.json +5 -2
- package/src/client/generated/wallet-provider.ts +28 -19
- package/src/credential/issuance/03-start-user-authorization.ts +3 -0
- package/src/credential/presentation/01-start-flow.ts +19 -26
- package/src/credential/presentation/03-get-request-object.ts +35 -58
- package/src/credential/presentation/04-retrieve-rp-jwks.ts +34 -0
- package/src/credential/presentation/05-verify-request-object.ts +52 -0
- package/src/credential/presentation/06-fetch-presentation-definition.ts +48 -0
- package/src/credential/presentation/07-evaluate-dcql-query.ts +166 -0
- package/src/credential/presentation/07-evaluate-input-descriptor.ts +391 -0
- package/src/credential/presentation/08-send-authorization-response.ts +220 -0
- package/src/credential/presentation/errors.ts +64 -0
- package/src/credential/presentation/index.ts +22 -1
- package/src/credential/presentation/types.ts +133 -2
- package/src/sd-jwt/index.ts +49 -1
- package/src/trust/chain.ts +46 -66
- package/src/trust/index.ts +185 -20
- package/src/trust/types.ts +34 -10
- package/src/trust/utils.ts +35 -0
- package/src/utils/decoder.ts +1 -1
- package/src/utils/jwk.ts +8 -1
- package/src/wallet-instance/index.ts +13 -0
- package/lib/commonjs/credential/presentation/04-send-authorization-response.js +0 -138
- package/lib/commonjs/credential/presentation/04-send-authorization-response.js.map +0 -1
- package/lib/module/credential/presentation/04-send-authorization-response.js +0 -128
- package/lib/module/credential/presentation/04-send-authorization-response.js.map +0 -1
- package/lib/typescript/credential/presentation/04-send-authorization-response.d.ts +0 -34
- package/lib/typescript/credential/presentation/04-send-authorization-response.d.ts.map +0 -1
- package/src/credential/presentation/04-send-authorization-response.ts +0 -168
@@ -7,16 +7,33 @@ import {
|
|
7
7
|
getRequestObject,
|
8
8
|
type GetRequestObject,
|
9
9
|
} from "./03-get-request-object";
|
10
|
+
import { getJwksFromConfig, type FetchJwks } from "./04-retrieve-rp-jwks";
|
11
|
+
import {
|
12
|
+
verifyRequestObject,
|
13
|
+
type VerifyRequestObject,
|
14
|
+
} from "./05-verify-request-object";
|
15
|
+
import {
|
16
|
+
fetchPresentDefinition,
|
17
|
+
type FetchPresentationDefinition,
|
18
|
+
} from "./06-fetch-presentation-definition";
|
19
|
+
import {
|
20
|
+
evaluateInputDescriptorForSdJwt4VC,
|
21
|
+
type EvaluateInputDescriptorSdJwt4VC,
|
22
|
+
} from "./07-evaluate-input-descriptor";
|
10
23
|
import {
|
11
24
|
sendAuthorizationResponse,
|
12
25
|
type SendAuthorizationResponse,
|
13
|
-
} from "./
|
26
|
+
} from "./08-send-authorization-response";
|
14
27
|
import * as Errors from "./errors";
|
15
28
|
|
16
29
|
export {
|
17
30
|
startFlowFromQR,
|
18
31
|
evaluateRelyingPartyTrust,
|
19
32
|
getRequestObject,
|
33
|
+
getJwksFromConfig,
|
34
|
+
verifyRequestObject,
|
35
|
+
fetchPresentDefinition,
|
36
|
+
evaluateInputDescriptorForSdJwt4VC,
|
20
37
|
sendAuthorizationResponse,
|
21
38
|
Errors,
|
22
39
|
};
|
@@ -24,5 +41,9 @@ export type {
|
|
24
41
|
StartFlow,
|
25
42
|
EvaluateRelyingPartyTrust,
|
26
43
|
GetRequestObject,
|
44
|
+
FetchJwks,
|
45
|
+
VerifyRequestObject,
|
46
|
+
FetchPresentationDefinition,
|
47
|
+
EvaluateInputDescriptorSdJwt4VC,
|
27
48
|
SendAuthorizationResponse,
|
28
49
|
};
|
@@ -11,6 +11,84 @@ export type Presentation = [
|
|
11
11
|
/* the context for the key associated to the credential */ CryptoContext,
|
12
12
|
];
|
13
13
|
|
14
|
+
/**
|
15
|
+
* A object that associate the information needed to multiple remote presentation
|
16
|
+
* Used with `presentation_definition`
|
17
|
+
* @deprecated Use `RemotePresentation`
|
18
|
+
*/
|
19
|
+
export type LegacyRemotePresentation = {
|
20
|
+
requestedClaims: string[];
|
21
|
+
inputDescriptor: InputDescriptor;
|
22
|
+
format: string;
|
23
|
+
vpToken: string;
|
24
|
+
};
|
25
|
+
|
26
|
+
/**
|
27
|
+
* A object that associate the information needed to multiple remote presentation
|
28
|
+
* Used with DCQL queries
|
29
|
+
*/
|
30
|
+
export type RemotePresentation = {
|
31
|
+
requestedClaims: string[];
|
32
|
+
credentialId: string;
|
33
|
+
format: string;
|
34
|
+
vpToken: string;
|
35
|
+
};
|
36
|
+
|
37
|
+
const Fields = z.object({
|
38
|
+
path: z.array(z.string().min(1)), // Array of JSONPath string expressions
|
39
|
+
id: z.string().optional(), // Unique string ID
|
40
|
+
purpose: z.string().optional(), // Purpose of the field
|
41
|
+
name: z.string().optional(), // Human-friendly name
|
42
|
+
filter: z.any().optional(), // JSON Schema descriptor for filtering
|
43
|
+
optional: z.boolean().optional(), // Boolean indicating if the field is optional
|
44
|
+
intent_to_retain: z.boolean().optional(), // Boolean indicating that the Verifier intends to retain the Claim's data being requested
|
45
|
+
});
|
46
|
+
|
47
|
+
// Define the Constraints Object Schema
|
48
|
+
const Constraints = z.object({
|
49
|
+
fields: z.array(Fields).optional(), // Array of Field Objects
|
50
|
+
limit_disclosure: z.enum(["required", "preferred"]).optional(), // Limit disclosure property
|
51
|
+
});
|
52
|
+
|
53
|
+
// Define the Input Descriptor Object Schema
|
54
|
+
export type InputDescriptor = z.infer<typeof InputDescriptor>;
|
55
|
+
export const InputDescriptor = z.object({
|
56
|
+
id: z.string().min(1), // Mandatory unique string ID
|
57
|
+
name: z.string().optional(), // Human-friendly name
|
58
|
+
purpose: z.string().optional(), // Purpose of the schema
|
59
|
+
format: z.record(z.string(), z.any()).optional(), // Object with Claim Format Designations
|
60
|
+
constraints: Constraints, // Constraints Object (mandatory)
|
61
|
+
group: z.string().optional(), // Match one of the grouping strings listed in the "from" values of a Submission Requirement Rule
|
62
|
+
});
|
63
|
+
|
64
|
+
const SubmissionRequirement = z.object({
|
65
|
+
name: z.string().optional(),
|
66
|
+
purpose: z.string().optional(),
|
67
|
+
rule: z.string(), // "all": all group's rules must be present, or "pick": at least group's "count" rules must be present
|
68
|
+
from: z.string().optional(), // MUST contain either a "from" or "from_nested" property
|
69
|
+
from_nested: z
|
70
|
+
.array(
|
71
|
+
z.object({
|
72
|
+
name: z.string().optional(),
|
73
|
+
purpose: z.string().optional(),
|
74
|
+
rule: z.string(),
|
75
|
+
from: z.string(),
|
76
|
+
})
|
77
|
+
)
|
78
|
+
.optional(),
|
79
|
+
count: z.number().optional(),
|
80
|
+
//"count", "min", and "max" may be present with a "pick" rule
|
81
|
+
});
|
82
|
+
|
83
|
+
export type PresentationDefinition = z.infer<typeof PresentationDefinition>;
|
84
|
+
export const PresentationDefinition = z.object({
|
85
|
+
id: z.string(),
|
86
|
+
name: z.string().optional(),
|
87
|
+
purpose: z.string().optional(),
|
88
|
+
input_descriptors: z.array(InputDescriptor),
|
89
|
+
submission_requirements: z.array(SubmissionRequirement).optional(),
|
90
|
+
});
|
91
|
+
|
14
92
|
export type RequestObject = z.infer<typeof RequestObject>;
|
15
93
|
export const RequestObject = z.object({
|
16
94
|
iss: z.string(),
|
@@ -19,9 +97,62 @@ export const RequestObject = z.object({
|
|
19
97
|
state: z.string(),
|
20
98
|
nonce: z.string(),
|
21
99
|
response_uri: z.string(),
|
100
|
+
response_uri_method: z.string().optional(),
|
22
101
|
response_type: z.literal("vp_token"),
|
23
102
|
response_mode: z.literal("direct_post.jwt"),
|
24
103
|
client_id: z.string(),
|
25
|
-
|
26
|
-
scope: z.string(),
|
104
|
+
dcql_query: z.record(z.string(), z.any()).optional(), // Validation happens within the `dcql` library, no need to duplicate it here
|
105
|
+
scope: z.string().optional(),
|
106
|
+
presentation_definition: PresentationDefinition.optional(),
|
107
|
+
});
|
108
|
+
|
109
|
+
export type WalletMetadata = z.infer<typeof WalletMetadata>;
|
110
|
+
export const WalletMetadata = z.object({
|
111
|
+
presentation_definition_uri_supported: z.boolean().optional(),
|
112
|
+
client_id_schemes_supported: z.array(z.string()).optional(),
|
113
|
+
request_object_signing_alg_values_supported: z.array(z.string()).optional(),
|
114
|
+
vp_formats_supported: z.record(
|
115
|
+
z.string(), // TODO [SIW-2110]: use explicit credential format?
|
116
|
+
z.object({
|
117
|
+
"sd-jwt_alg_values": z.array(z.string()).optional(), // alg_values_supported?
|
118
|
+
})
|
119
|
+
),
|
120
|
+
// TODO [SIW-2110]: include other metadata?
|
121
|
+
});
|
122
|
+
|
123
|
+
/**
|
124
|
+
* Wallet capabilities that must be submitted to get the Request Object
|
125
|
+
* via POST request when the `request_uri_method` is `post`.
|
126
|
+
*/
|
127
|
+
export type RequestObjectWalletCapabilities = z.infer<
|
128
|
+
typeof RequestObjectWalletCapabilities
|
129
|
+
>;
|
130
|
+
export const RequestObjectWalletCapabilities = z.object({
|
131
|
+
wallet_metadata: WalletMetadata,
|
132
|
+
wallet_nonce: z.string().optional(),
|
133
|
+
});
|
134
|
+
|
135
|
+
/**
|
136
|
+
* Authorization Response payload when using `presentation_definition`.
|
137
|
+
* @deprecated Use `DirectAuthorizationBodyPayload`
|
138
|
+
*/
|
139
|
+
export type LegacyDirectAuthorizationBodyPayload = z.infer<
|
140
|
+
typeof LegacyDirectAuthorizationBodyPayload
|
141
|
+
>;
|
142
|
+
/**
|
143
|
+
* @deprecated Use `DirectAuthorizationBodyPayload`
|
144
|
+
*/
|
145
|
+
export const LegacyDirectAuthorizationBodyPayload = z.object({
|
146
|
+
vp_token: z.union([z.string(), z.array(z.string())]).optional(),
|
147
|
+
presentation_submission: z.record(z.string(), z.unknown()),
|
148
|
+
});
|
149
|
+
|
150
|
+
/**
|
151
|
+
* Authorization Response payload when using DCQL queries.
|
152
|
+
*/
|
153
|
+
export type DirectAuthorizationBodyPayload = z.infer<
|
154
|
+
typeof DirectAuthorizationBodyPayload
|
155
|
+
>;
|
156
|
+
export const DirectAuthorizationBodyPayload = z.object({
|
157
|
+
vp_token: z.record(z.string(), z.string()),
|
27
158
|
});
|
package/src/sd-jwt/index.ts
CHANGED
@@ -2,12 +2,13 @@ import { z } from "zod";
|
|
2
2
|
|
3
3
|
import { decode as decodeJwt } from "@pagopa/io-react-native-jwt";
|
4
4
|
import { verify as verifyJwt } from "@pagopa/io-react-native-jwt";
|
5
|
-
import { sha256ToBase64 } from "@pagopa/io-react-native-jwt";
|
5
|
+
import { SignJWT, sha256ToBase64 } from "@pagopa/io-react-native-jwt";
|
6
6
|
import { Disclosure, SdJwt4VC, type DisclosureWithEncoded } from "./types";
|
7
7
|
import { verifyDisclosure } from "./verifier";
|
8
8
|
import type { JWK } from "../utils/jwk";
|
9
9
|
import * as Errors from "./errors";
|
10
10
|
import { Base64 } from "js-base64";
|
11
|
+
import { type Presentation } from "../credential/presentation/types";
|
11
12
|
|
12
13
|
const decodeDisclosure = (encoded: string): DisclosureWithEncoded => {
|
13
14
|
const utf8String = Base64.decode(encoded); // Decode Base64 into UTF-8 string
|
@@ -163,4 +164,51 @@ export const verify = async <S extends z.ZodType<SdJwt4VC>>(
|
|
163
164
|
};
|
164
165
|
};
|
165
166
|
|
167
|
+
/**
|
168
|
+
* Prepares a Verified Presentation (VP) token to be sent as part of an
|
169
|
+
* authorization response in an OpenID 4 Verifiable Presentations flow.
|
170
|
+
*
|
171
|
+
* @param nonce - The nonce provided by the relying party.
|
172
|
+
* @param client_id - The client identifier of the relying party.
|
173
|
+
* @param presentation - An object containing the verifiable credential, the claims to disclose,
|
174
|
+
* and the cryptographic context for signing.
|
175
|
+
* @returns An object containing the signed VP token (`vp_token`).
|
176
|
+
*
|
177
|
+
* @remarks
|
178
|
+
* 1. The `disclose()` function is used to produce a token with only the requested claims.
|
179
|
+
* 2. A KB-JWT is then signed, including sd_hash and `nonce`.
|
180
|
+
* 3. The `vp_token` is composed of the disclosed VP and the KB-JWT.
|
181
|
+
*/
|
182
|
+
export const prepareVpToken = async (
|
183
|
+
nonce: string,
|
184
|
+
client_id: string,
|
185
|
+
[verifiableCredential, requestedClaims, cryptoContext]: Presentation
|
186
|
+
): Promise<{
|
187
|
+
vp_token: string;
|
188
|
+
}> => {
|
189
|
+
// Produce a VP token with only requested claims from the verifiable credential
|
190
|
+
const { token: vp } = await disclose(verifiableCredential, requestedClaims);
|
191
|
+
|
192
|
+
// <Issuer-signed JWT>~<Disclosure 1>~<Disclosure N>~
|
193
|
+
const sd_hash = await sha256ToBase64(`${vp}~`);
|
194
|
+
|
195
|
+
const kbJwt = await new SignJWT(cryptoContext)
|
196
|
+
.setProtectedHeader({
|
197
|
+
typ: "kb+jwt",
|
198
|
+
alg: "ES256",
|
199
|
+
})
|
200
|
+
.setPayload({
|
201
|
+
sd_hash,
|
202
|
+
nonce: nonce,
|
203
|
+
})
|
204
|
+
.setAudience(client_id)
|
205
|
+
.setIssuedAt()
|
206
|
+
.sign();
|
207
|
+
|
208
|
+
// <Issuer-signed JWT>~<Disclosure 1>~...~<Disclosure N>~<KB-JWT>
|
209
|
+
const vp_token = [vp, kbJwt].join("~");
|
210
|
+
|
211
|
+
return { vp_token };
|
212
|
+
};
|
213
|
+
|
166
214
|
export { SdJwt4VC, Errors };
|
package/src/trust/chain.ts
CHANGED
@@ -1,7 +1,3 @@
|
|
1
|
-
import {
|
2
|
-
decode as decodeJwt,
|
3
|
-
verify as verifyJwt,
|
4
|
-
} from "@pagopa/io-react-native-jwt";
|
5
1
|
import {
|
6
2
|
EntityConfiguration,
|
7
3
|
EntityStatement,
|
@@ -10,33 +6,8 @@ import {
|
|
10
6
|
import { JWK } from "../utils/jwk";
|
11
7
|
import { IoWalletError } from "../utils/errors";
|
12
8
|
import * as z from "zod";
|
13
|
-
import type { JWTDecodeResult } from "@pagopa/io-react-native-jwt/lib/typescript/types";
|
14
9
|
import { getSignedEntityConfiguration, getSignedEntityStatement } from ".";
|
15
|
-
|
16
|
-
type ParsedToken = {
|
17
|
-
header: JWTDecodeResult["protectedHeader"];
|
18
|
-
payload: JWTDecodeResult["payload"];
|
19
|
-
};
|
20
|
-
|
21
|
-
// Verify a token signature
|
22
|
-
// The kid is extracted from the token header
|
23
|
-
const verify = async (
|
24
|
-
token: string,
|
25
|
-
kid: string,
|
26
|
-
jwks: JWK[]
|
27
|
-
): Promise<ParsedToken> => {
|
28
|
-
const jwk = jwks.find((k) => k.kid === kid);
|
29
|
-
if (!jwk) {
|
30
|
-
throw new Error(`Invalid kid: ${kid}, token: ${token}`);
|
31
|
-
}
|
32
|
-
const { protectedHeader: header, payload } = await verifyJwt(token, jwk);
|
33
|
-
return { header, payload };
|
34
|
-
};
|
35
|
-
|
36
|
-
const decode = (token: string) => {
|
37
|
-
const { protectedHeader: header, payload } = decodeJwt(token);
|
38
|
-
return { header, payload };
|
39
|
-
};
|
10
|
+
import { decode, type ParsedToken, verify } from "./utils";
|
40
11
|
|
41
12
|
// The first element of the chain is supposed to be the Entity Configuration for the document issuer
|
42
13
|
const FirstElementShape = EntityConfiguration;
|
@@ -53,7 +24,7 @@ const LastElementShape = z.union([
|
|
53
24
|
* Validates a provided trust chain against a known trust
|
54
25
|
*
|
55
26
|
* @param trustAnchorEntity The entity configuration of the known trust anchor
|
56
|
-
* @param chain The chain of statements to be
|
27
|
+
* @param chain The chain of statements to be validated
|
57
28
|
* @returns The list of parsed token representing the chain
|
58
29
|
* @throws {IoWalletError} If the chain is not valid
|
59
30
|
*/
|
@@ -85,7 +56,7 @@ export async function validateTrustChain(
|
|
85
56
|
};
|
86
57
|
|
87
58
|
// select keys from the next token
|
88
|
-
// if the current token is the last, keys
|
59
|
+
// if the current token is the last, keys from trust anchor will be used
|
89
60
|
const selectKeys = (currentIndex: number): JWK[] => {
|
90
61
|
if (currentIndex === chain.length - 1) {
|
91
62
|
return trustAnchorEntity.payload.jwks.keys;
|
@@ -101,7 +72,7 @@ export async function validateTrustChain(
|
|
101
72
|
};
|
102
73
|
|
103
74
|
// Iterate the chain and validate each element's signature against the public keys of its next
|
104
|
-
// If there is no next, hence it's the end of the chain and it must be verified by the Trust Anchor
|
75
|
+
// If there is no next, hence it's the end of the chain, and it must be verified by the Trust Anchor
|
105
76
|
return Promise.all(
|
106
77
|
chain
|
107
78
|
.map((token, i) => [token, selectKid(i), selectKeys(i)] as const)
|
@@ -114,42 +85,51 @@ export async function validateTrustChain(
|
|
114
85
|
*
|
115
86
|
* @param chain The original chain
|
116
87
|
* @param appFetch (optional) fetch api implementation
|
117
|
-
* @returns A list of signed token that
|
118
|
-
* @throws When an element of the chain fails to parse
|
88
|
+
* @returns A list of signed token that represent the trust chain, in the same order of the provided chain
|
89
|
+
* @throws IoWalletError When an element of the chain fails to parse
|
119
90
|
*/
|
120
|
-
export function renewTrustChain(
|
91
|
+
export async function renewTrustChain(
|
121
92
|
chain: string[],
|
122
93
|
appFetch: GlobalFetch["fetch"] = fetch
|
123
|
-
) {
|
94
|
+
): Promise<string[]> {
|
124
95
|
return Promise.all(
|
125
|
-
chain
|
126
|
-
|
127
|
-
|
128
|
-
.
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
96
|
+
chain.map(async (token, index) => {
|
97
|
+
const decoded = decode(token);
|
98
|
+
|
99
|
+
const entityStatementResult = EntityStatement.safeParse(decoded);
|
100
|
+
const entityConfigurationResult = EntityConfiguration.safeParse(decoded);
|
101
|
+
|
102
|
+
if (entityConfigurationResult.success) {
|
103
|
+
return getSignedEntityConfiguration(
|
104
|
+
entityConfigurationResult.data.payload.iss,
|
105
|
+
{ appFetch }
|
106
|
+
);
|
107
|
+
}
|
108
|
+
if (entityStatementResult.success) {
|
109
|
+
const entityStatement = entityStatementResult.data;
|
110
|
+
|
111
|
+
const parentBaseUrl = entityStatement.payload.iss;
|
112
|
+
const parentECJwt = await getSignedEntityConfiguration(parentBaseUrl, {
|
113
|
+
appFetch,
|
114
|
+
});
|
115
|
+
const parentEC = EntityConfiguration.parse(decode(parentECJwt));
|
116
|
+
|
117
|
+
const federationFetchEndpoint =
|
118
|
+
parentEC.payload.metadata.federation_entity.federation_fetch_endpoint;
|
119
|
+
if (!federationFetchEndpoint) {
|
120
|
+
throw new IoWalletError(
|
121
|
+
`Parent EC at ${parentBaseUrl} is missing federation_fetch_endpoint`
|
122
|
+
);
|
123
|
+
}
|
124
|
+
return getSignedEntityStatement(
|
125
|
+
federationFetchEndpoint,
|
126
|
+
entityStatement.payload.sub,
|
127
|
+
{ appFetch }
|
128
|
+
);
|
129
|
+
}
|
130
|
+
throw new IoWalletError(
|
131
|
+
`Cannot renew trust chain because element #${index} failed to parse.`
|
132
|
+
);
|
133
|
+
})
|
154
134
|
);
|
155
135
|
}
|
package/src/trust/index.ts
CHANGED
@@ -1,14 +1,18 @@
|
|
1
|
+
import { decode, verify } from "./utils";
|
1
2
|
import { decode as decodeJwt } from "@pagopa/io-react-native-jwt";
|
2
3
|
import {
|
3
|
-
WalletProviderEntityConfiguration,
|
4
|
-
TrustAnchorEntityConfiguration,
|
5
4
|
CredentialIssuerEntityConfiguration,
|
6
|
-
RelyingPartyEntityConfiguration,
|
7
5
|
EntityConfiguration,
|
8
6
|
EntityStatement,
|
7
|
+
FederationListResponse,
|
8
|
+
RelyingPartyEntityConfiguration,
|
9
|
+
TrustAnchorEntityConfiguration,
|
10
|
+
WalletProviderEntityConfiguration,
|
9
11
|
} from "./types";
|
10
|
-
import {
|
12
|
+
import { renewTrustChain, validateTrustChain } from "./chain";
|
11
13
|
import { hasStatusOrThrow } from "../utils/misc";
|
14
|
+
import { IoWalletError } from "../utils/errors";
|
15
|
+
import type { JWK } from "../utils/jwk";
|
12
16
|
|
13
17
|
export type {
|
14
18
|
WalletProviderEntityConfiguration,
|
@@ -24,9 +28,9 @@ export type {
|
|
24
28
|
* It can handle fast chain renewal, which means we try to fetch a fresh version of each statement.
|
25
29
|
*
|
26
30
|
* @param trustAnchorEntity The entity configuration of the known trust anchor
|
27
|
-
* @param chain The chain of statements to be
|
28
|
-
* @param
|
29
|
-
* @param
|
31
|
+
* @param chain The chain of statements to be validated
|
32
|
+
* @param renewOnFail Whether to renew the provided chain if the validation fails at first. Default: true
|
33
|
+
* @param appFetch Fetch api implementation. Default: the built-in implementation
|
30
34
|
* @returns The result of the chain validation
|
31
35
|
* @throws {IoWalletError} When either validation or renewal fail
|
32
36
|
*/
|
@@ -54,7 +58,7 @@ export async function verifyTrustChain(
|
|
54
58
|
* Fetch the signed entity configuration token for an entity
|
55
59
|
*
|
56
60
|
* @param entityBaseUrl The url of the entity to fetch
|
57
|
-
* @param
|
61
|
+
* @param appFetch (optional) fetch api implementation
|
58
62
|
* @returns The signed Entity Configuration token
|
59
63
|
*/
|
60
64
|
export async function getSignedEntityConfiguration(
|
@@ -86,6 +90,7 @@ export async function getSignedEntityConfiguration(
|
|
86
90
|
*
|
87
91
|
* @param entityBaseUrl The base url of the entity.
|
88
92
|
* @param schema The expected schema of the entity configuration, according to the kind of entity we are fetching from.
|
93
|
+
* @param options An optional object with additional options.
|
89
94
|
* @param options.appFetch An optional instance of the http client to be used.
|
90
95
|
* @returns The parsed entity configuration object
|
91
96
|
* @throws {IoWalletError} If the http request fails
|
@@ -200,9 +205,9 @@ export const getEntityConfiguration = (
|
|
200
205
|
/**
|
201
206
|
* Fetch and parse the entity statement document for a given federation entity.
|
202
207
|
*
|
203
|
-
* @param accreditationBodyBaseUrl The base url of the
|
208
|
+
* @param accreditationBodyBaseUrl The base url of the accreditation body which holds and signs the required entity statement
|
204
209
|
* @param subordinatedEntityBaseUrl The url that identifies the subordinate entity
|
205
|
-
* @param
|
210
|
+
* @param appFetch An optional instance of the http client to be used.
|
206
211
|
* @returns The parsed entity configuration object
|
207
212
|
* @throws {IoWalletError} If the http request fails
|
208
213
|
* @throws Parse error if the document is not in the expected shape.
|
@@ -234,14 +239,14 @@ export async function getEntityStatement(
|
|
234
239
|
/**
|
235
240
|
* Fetch the entity statement document for a given federation entity.
|
236
241
|
*
|
237
|
-
* @param
|
238
|
-
* @param subordinatedEntityBaseUrl The url that identifies the subordinate entity
|
239
|
-
* @param
|
240
|
-
* @returns The signed entity statement token
|
241
|
-
* @throws {IoWalletError} If the http request fails
|
242
|
+
* @param federationFetchEndpoint The exact endpoint provided by the parent EC's metadata.
|
243
|
+
* @param subordinatedEntityBaseUrl The url that identifies the subordinate entity.
|
244
|
+
* @param appFetch An optional instance of the http client to be used.
|
245
|
+
* @returns The signed entity statement token.
|
246
|
+
* @throws {IoWalletError} If the http request fails.
|
242
247
|
*/
|
243
248
|
export async function getSignedEntityStatement(
|
244
|
-
|
249
|
+
federationFetchEndpoint: string,
|
245
250
|
subordinatedEntityBaseUrl: string,
|
246
251
|
{
|
247
252
|
appFetch = fetch,
|
@@ -249,13 +254,173 @@ export async function getSignedEntityStatement(
|
|
249
254
|
appFetch?: GlobalFetch["fetch"];
|
250
255
|
} = {}
|
251
256
|
) {
|
252
|
-
const url =
|
253
|
-
|
254
|
-
})}`;
|
257
|
+
const url = new URL(federationFetchEndpoint);
|
258
|
+
url.searchParams.set("sub", subordinatedEntityBaseUrl);
|
255
259
|
|
256
|
-
return await appFetch(url, {
|
260
|
+
return await appFetch(url.toString(), {
|
257
261
|
method: "GET",
|
258
262
|
})
|
259
263
|
.then(hasStatusOrThrow(200))
|
260
264
|
.then((res) => res.text());
|
261
265
|
}
|
266
|
+
|
267
|
+
/**
|
268
|
+
* Fetch the federation list document from a given endpoint.
|
269
|
+
*
|
270
|
+
* @param federationListEndpoint The URL of the federation list endpoint.
|
271
|
+
* @param appFetch An optional instance of the http client to be used.
|
272
|
+
* @returns The federation list as an array of strings.
|
273
|
+
* @throws {IoWalletError} If the HTTP request fails or the response cannot be parsed.
|
274
|
+
*/
|
275
|
+
export async function getFederationList(
|
276
|
+
federationListEndpoint: string,
|
277
|
+
{
|
278
|
+
appFetch = fetch,
|
279
|
+
}: {
|
280
|
+
appFetch?: GlobalFetch["fetch"];
|
281
|
+
} = {}
|
282
|
+
): Promise<string[]> {
|
283
|
+
return await appFetch(federationListEndpoint, {
|
284
|
+
method: "GET",
|
285
|
+
})
|
286
|
+
.then(hasStatusOrThrow(200))
|
287
|
+
.then((res) => res.json())
|
288
|
+
.then((json) => {
|
289
|
+
const result = FederationListResponse.safeParse(json);
|
290
|
+
if (!result.success) {
|
291
|
+
throw new IoWalletError(
|
292
|
+
`Invalid federation list format received from Trust Anchor: ${result.error.message}`
|
293
|
+
);
|
294
|
+
}
|
295
|
+
return result.data;
|
296
|
+
});
|
297
|
+
}
|
298
|
+
|
299
|
+
/**
|
300
|
+
* Build a not-verified trust chain for a given Relying Party (RP) entity.
|
301
|
+
*
|
302
|
+
* @param relyingPartyEntityBaseUrl The base URL of the RP entity
|
303
|
+
* @param trustAnchorKey The public key of the Trust Anchor (TA) entity
|
304
|
+
* @param appFetch An optional instance of the http client to be used.
|
305
|
+
* @returns A list of signed tokens that represent the trust chain, in the order of the chain (from the RP to the Trust Anchor)
|
306
|
+
* @throws {IoWalletError} When an element of the chain fails to parse
|
307
|
+
* The result of this function can be used to validate the trust chain with {@link verifyTrustChain}
|
308
|
+
*/
|
309
|
+
export async function buildTrustChain(
|
310
|
+
relyingPartyEntityBaseUrl: string,
|
311
|
+
trustAnchorKey: JWK,
|
312
|
+
appFetch: GlobalFetch["fetch"] = fetch
|
313
|
+
): Promise<string[]> {
|
314
|
+
// 1: Recursively gather the trust chain from the RP up to the Trust Anchor
|
315
|
+
const trustChain = await gatherTrustChain(
|
316
|
+
relyingPartyEntityBaseUrl,
|
317
|
+
appFetch
|
318
|
+
);
|
319
|
+
|
320
|
+
// 2: Trust Anchor signature verification
|
321
|
+
const trustAnchorJwt = trustChain[trustChain.length - 1];
|
322
|
+
if (!trustAnchorJwt) {
|
323
|
+
throw new IoWalletError(
|
324
|
+
"Cannot verify trust anchor: missing entity configuration."
|
325
|
+
);
|
326
|
+
}
|
327
|
+
|
328
|
+
if (!trustAnchorKey.kid) {
|
329
|
+
throw new IoWalletError("Missing 'kid' in provided Trust Anchor key.");
|
330
|
+
}
|
331
|
+
|
332
|
+
await verify(trustAnchorJwt, trustAnchorKey.kid, [trustAnchorKey]);
|
333
|
+
|
334
|
+
// 3: Check the federation list
|
335
|
+
const trustAnchorConfig = EntityConfiguration.parse(decode(trustAnchorJwt));
|
336
|
+
const federationListEndpoint =
|
337
|
+
trustAnchorConfig.payload.metadata.federation_entity
|
338
|
+
.federation_list_endpoint;
|
339
|
+
|
340
|
+
if (federationListEndpoint) {
|
341
|
+
const federationList = await getFederationList(federationListEndpoint, {
|
342
|
+
appFetch,
|
343
|
+
});
|
344
|
+
|
345
|
+
if (!federationList.includes(relyingPartyEntityBaseUrl)) {
|
346
|
+
throw new IoWalletError(
|
347
|
+
"Relying Party entity base URL is not authorized by the Trust Anchor's federation list."
|
348
|
+
);
|
349
|
+
}
|
350
|
+
}
|
351
|
+
|
352
|
+
return trustChain;
|
353
|
+
}
|
354
|
+
|
355
|
+
/**
|
356
|
+
* Recursively gather the trust chain for an entity and all its superiors.
|
357
|
+
* @param entityBaseUrl The base URL of the entity for which to gather the chain.
|
358
|
+
* @param appFetch An optional instance of the http client to be used.
|
359
|
+
* @param isLeaf Whether the current entity is the leaf of the chain.
|
360
|
+
* @returns A full ordered list of JWTs (ECs and ESs) forming the trust chain.
|
361
|
+
*/
|
362
|
+
async function gatherTrustChain(
|
363
|
+
entityBaseUrl: string,
|
364
|
+
appFetch: GlobalFetch["fetch"],
|
365
|
+
isLeaf: boolean = true
|
366
|
+
): Promise<string[]> {
|
367
|
+
const chain: string[] = [];
|
368
|
+
|
369
|
+
// Fetch self-signed EC (only needed for the leaf)
|
370
|
+
const entityECJwt = await getSignedEntityConfiguration(entityBaseUrl, {
|
371
|
+
appFetch,
|
372
|
+
});
|
373
|
+
const entityEC = EntityConfiguration.parse(decode(entityECJwt));
|
374
|
+
|
375
|
+
if (isLeaf) {
|
376
|
+
// Only push EC for the leaf
|
377
|
+
chain.push(entityECJwt);
|
378
|
+
}
|
379
|
+
|
380
|
+
// Find authority_hints (parent, if any)
|
381
|
+
const authorityHints = entityEC.payload.authority_hints ?? [];
|
382
|
+
if (authorityHints.length === 0) {
|
383
|
+
// This is the Trust Anchor (no parent)
|
384
|
+
if (!isLeaf) {
|
385
|
+
chain.push(entityECJwt);
|
386
|
+
}
|
387
|
+
return chain;
|
388
|
+
}
|
389
|
+
|
390
|
+
const parentEntityBaseUrl = authorityHints[0]!;
|
391
|
+
|
392
|
+
// Fetch parent EC
|
393
|
+
const parentECJwt = await getSignedEntityConfiguration(parentEntityBaseUrl, {
|
394
|
+
appFetch,
|
395
|
+
});
|
396
|
+
const parentEC = EntityConfiguration.parse(decode(parentECJwt));
|
397
|
+
|
398
|
+
// Fetch ES
|
399
|
+
const federationFetchEndpoint =
|
400
|
+
parentEC.payload.metadata.federation_entity.federation_fetch_endpoint;
|
401
|
+
if (!federationFetchEndpoint) {
|
402
|
+
throw new IoWalletError(
|
403
|
+
"Missing federation_fetch_endpoint in parent's configuration."
|
404
|
+
);
|
405
|
+
}
|
406
|
+
|
407
|
+
const entityStatementJwt = await getSignedEntityStatement(
|
408
|
+
federationFetchEndpoint,
|
409
|
+
entityBaseUrl,
|
410
|
+
{ appFetch }
|
411
|
+
);
|
412
|
+
// Validate the ES
|
413
|
+
EntityStatement.parse(decode(entityStatementJwt));
|
414
|
+
|
415
|
+
// Push this ES into the chain
|
416
|
+
chain.push(entityStatementJwt);
|
417
|
+
|
418
|
+
// Recurse into the parent
|
419
|
+
const parentChain = await gatherTrustChain(
|
420
|
+
parentEntityBaseUrl,
|
421
|
+
appFetch,
|
422
|
+
false
|
423
|
+
);
|
424
|
+
|
425
|
+
return chain.concat(parentChain);
|
426
|
+
}
|