@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
@@ -1,85 +1,62 @@
|
|
1
|
-
import { v4 as uuidv4 } from "uuid";
|
2
|
-
import {
|
3
|
-
decode as decodeJwt,
|
4
|
-
sha256ToBase64,
|
5
|
-
verify,
|
6
|
-
type CryptoContext,
|
7
|
-
} from "@pagopa/io-react-native-jwt";
|
8
|
-
|
9
|
-
import { createDPopToken } from "../../utils/dpop";
|
10
|
-
import { NoSuitableKeysFoundInEntityConfiguration } from "./errors";
|
11
|
-
import type { EvaluateRelyingPartyTrust } from "./02-evaluate-rp-trust";
|
12
1
|
import { hasStatusOrThrow, type Out } from "../../utils/misc";
|
13
2
|
import type { StartFlow } from "./01-start-flow";
|
14
|
-
import {
|
3
|
+
import { RequestObjectWalletCapabilities } from "./types";
|
15
4
|
|
16
5
|
export type GetRequestObject = (
|
17
|
-
requestUri: Out<StartFlow>["
|
18
|
-
rpConf: Out<EvaluateRelyingPartyTrust>["rpConf"],
|
6
|
+
requestUri: Out<StartFlow>["requestUri"],
|
19
7
|
context: {
|
20
|
-
wiaCryptoContext: CryptoContext;
|
21
8
|
appFetch?: GlobalFetch["fetch"];
|
22
9
|
walletInstanceAttestation: string;
|
10
|
+
walletCapabilities?: RequestObjectWalletCapabilities;
|
23
11
|
}
|
24
|
-
) => Promise<{
|
12
|
+
) => Promise<{ requestObjectEncodedJwt: string }>;
|
25
13
|
|
26
14
|
/**
|
27
|
-
* Obtain the Request Object for RP authentication
|
15
|
+
* Obtain the Request Object for RP authentication. Both the GET and POST `request_uri_method` are supported.
|
28
16
|
* @see https://italia.github.io/eudi-wallet-it-docs/versione-corrente/en/relying-party-solution.html
|
29
17
|
*
|
30
18
|
* @param requestUri The url for the Relying Party to connect with
|
31
|
-
* @param rpConf The Relying Party's configuration
|
32
|
-
* @param context.
|
33
|
-
* @param context.walletInstanceAttestation The Wallet Instance Attestation token
|
19
|
+
* @param rpConf The Relying Party's configuration * @param context.walletInstanceAttestation The Wallet Instance Attestation token
|
20
|
+
* @param context.walletCapabilities (optional) An object containing the wallet technical capabilities that will be sent with a POST request
|
34
21
|
* @param context.appFetch (optional) fetch api implementation. Default: built-in fetch
|
35
22
|
* @returns The Request Object that describes the presentation
|
36
23
|
*/
|
37
24
|
export const getRequestObject: GetRequestObject = async (
|
38
25
|
requestUri,
|
39
|
-
|
40
|
-
{ wiaCryptoContext, appFetch = fetch, walletInstanceAttestation }
|
26
|
+
{ appFetch = fetch, walletCapabilities }
|
41
27
|
) => {
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
28
|
+
if (walletCapabilities) {
|
29
|
+
// Validate external input
|
30
|
+
const { wallet_metadata, wallet_nonce } =
|
31
|
+
RequestObjectWalletCapabilities.parse(walletCapabilities);
|
32
|
+
|
33
|
+
const formUrlEncodedBody = new URLSearchParams({
|
34
|
+
wallet_metadata: JSON.stringify(wallet_metadata),
|
35
|
+
...(wallet_nonce && { wallet_nonce }),
|
36
|
+
});
|
37
|
+
|
38
|
+
const requestObjectEncodedJwt = await appFetch(requestUri, {
|
39
|
+
method: "POST",
|
40
|
+
headers: {
|
41
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
42
|
+
},
|
43
|
+
body: formUrlEncodedBody.toString(),
|
44
|
+
})
|
45
|
+
.then(hasStatusOrThrow(200))
|
46
|
+
.then((res) => res.text());
|
47
|
+
|
48
|
+
return {
|
49
|
+
requestObjectEncodedJwt,
|
50
|
+
};
|
51
|
+
}
|
51
52
|
|
52
|
-
const
|
53
|
+
const requestObjectEncodedJwt = await appFetch(requestUri, {
|
53
54
|
method: "GET",
|
54
|
-
headers: {
|
55
|
-
Authorization: `DPoP ${walletInstanceAttestation}`,
|
56
|
-
DPoP: signedWalletInstanceDPoP,
|
57
|
-
},
|
58
55
|
})
|
59
56
|
.then(hasStatusOrThrow(200))
|
60
|
-
.then((res) => res.
|
61
|
-
.then((responseJson) => responseJson.response);
|
62
|
-
|
63
|
-
const responseJwt = decodeJwt(responseEncodedJwt);
|
64
|
-
|
65
|
-
// verify token signature according to RP's entity configuration
|
66
|
-
// to ensure the request object is authentic
|
67
|
-
{
|
68
|
-
const pubKey = rpConf.wallet_relying_party.jwks.keys.find(
|
69
|
-
({ kid }) => kid === responseJwt.protectedHeader.kid
|
70
|
-
);
|
71
|
-
if (!pubKey) {
|
72
|
-
throw new NoSuitableKeysFoundInEntityConfiguration(
|
73
|
-
"Request Object signature verification"
|
74
|
-
);
|
75
|
-
}
|
76
|
-
await verify(responseEncodedJwt, pubKey);
|
77
|
-
}
|
78
|
-
|
79
|
-
// Ensure that the request object conforms to the expected specification.
|
80
|
-
const requestObject = RequestObject.parse(responseJwt.payload);
|
57
|
+
.then((res) => res.text());
|
81
58
|
|
82
59
|
return {
|
83
|
-
|
60
|
+
requestObjectEncodedJwt,
|
84
61
|
};
|
85
62
|
};
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { JWK } from "../../utils/jwk";
|
2
|
+
import { RelyingPartyEntityConfiguration } from "../../trust/types";
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Defines the signature for a function that retrieves JSON Web Key Sets (JWKS) from a client.
|
6
|
+
*
|
7
|
+
* @template T - The tuple type representing the function arguments.
|
8
|
+
* @param args - The arguments passed to the function.
|
9
|
+
* @returns A promise resolving to an object containing an array of JWKs.
|
10
|
+
*/
|
11
|
+
export type FetchJwks<T extends Array<unknown> = []> = (...args: T) => {
|
12
|
+
keys: JWK[];
|
13
|
+
};
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Retrieves the JSON Web Key Set (JWKS) from a Relying Party's entity configuration.
|
17
|
+
*
|
18
|
+
* @param rpConfig - The configuration object of the Relying Party entity.
|
19
|
+
* @returns An object containing an array of JWKs.
|
20
|
+
* @throws Will throw an error if the configuration is invalid or if JWKS is not found.
|
21
|
+
*/
|
22
|
+
export const getJwksFromConfig: FetchJwks<
|
23
|
+
[RelyingPartyEntityConfiguration["payload"]["metadata"]]
|
24
|
+
> = (rpConfig) => {
|
25
|
+
const jwks = rpConfig.openid_credential_verifier.jwks;
|
26
|
+
|
27
|
+
if (!jwks || !Array.isArray(jwks.keys)) {
|
28
|
+
throw new Error("JWKS not found in Relying Party configuration.");
|
29
|
+
}
|
30
|
+
|
31
|
+
return {
|
32
|
+
keys: jwks.keys,
|
33
|
+
};
|
34
|
+
};
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import { decode as decodeJwt, verify } from "@pagopa/io-react-native-jwt";
|
2
|
+
import type { RelyingPartyEntityConfiguration } from "../../trust";
|
3
|
+
import { UnverifiedEntityError } from "./errors";
|
4
|
+
import { RequestObject } from "./types";
|
5
|
+
import { getJwksFromConfig } from "./04-retrieve-rp-jwks";
|
6
|
+
|
7
|
+
export type VerifyRequestObject = (
|
8
|
+
requestObjectEncodedJwt: string,
|
9
|
+
context: {
|
10
|
+
clientId: string;
|
11
|
+
// jwkKeys: Out<FetchJwks>["keys"];
|
12
|
+
rpConf: RelyingPartyEntityConfiguration["payload"];
|
13
|
+
}
|
14
|
+
) => Promise<{ requestObject: RequestObject }>;
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Function to verify the Request Object's signature and the client ID.
|
18
|
+
* @param requestObjectEncodedJwt The Request Object in JWT format
|
19
|
+
* @param context.clientId The client ID to verify
|
20
|
+
* @param context.jwkKeys The set of keys to verify the signature
|
21
|
+
* @param context.rpConf The Entity Configuration of the Relying Party
|
22
|
+
* @returns The verified Request Object
|
23
|
+
*/
|
24
|
+
export const verifyRequestObject: VerifyRequestObject = async (
|
25
|
+
requestObjectEncodedJwt,
|
26
|
+
{ clientId, rpConf }
|
27
|
+
) => {
|
28
|
+
const requestObjectJwt = decodeJwt(requestObjectEncodedJwt);
|
29
|
+
const { keys } = getJwksFromConfig(rpConf.metadata);
|
30
|
+
|
31
|
+
// Verify token signature to ensure the request object is authentic
|
32
|
+
const pubKey = keys?.find(
|
33
|
+
({ kid }) => kid === requestObjectJwt.protectedHeader.kid
|
34
|
+
);
|
35
|
+
|
36
|
+
if (!pubKey) {
|
37
|
+
throw new UnverifiedEntityError("Request Object signature verification!");
|
38
|
+
}
|
39
|
+
|
40
|
+
// Standard claims are verified within `verify`
|
41
|
+
await verify(requestObjectEncodedJwt, pubKey, { issuer: clientId });
|
42
|
+
|
43
|
+
const requestObject = RequestObject.parse(requestObjectJwt.payload);
|
44
|
+
|
45
|
+
if (!(clientId === requestObject.client_id && clientId === rpConf.sub)) {
|
46
|
+
throw new UnverifiedEntityError(
|
47
|
+
"Client ID does not match Request Object or Entity Configuration"
|
48
|
+
);
|
49
|
+
}
|
50
|
+
|
51
|
+
return { requestObject };
|
52
|
+
};
|
@@ -0,0 +1,48 @@
|
|
1
|
+
import { PresentationDefinition, RequestObject } from "./types";
|
2
|
+
import { RelyingPartyEntityConfiguration } from "../../trust/types";
|
3
|
+
|
4
|
+
export type FetchPresentationDefinition = (
|
5
|
+
requestObject: RequestObject,
|
6
|
+
rpConf?: RelyingPartyEntityConfiguration["payload"]["metadata"]
|
7
|
+
) => Promise<{
|
8
|
+
presentationDefinition: PresentationDefinition;
|
9
|
+
}>;
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Retrieves a PresentationDefinition based on the given parameters.
|
13
|
+
*
|
14
|
+
* The method attempts the following strategies in order:
|
15
|
+
* 1. Checks if `presentation_definition` is directly available in the request object.
|
16
|
+
* 2. Uses a pre-configured `presentation_definition` from the relying party configuration if the `scope` is present in the request object.
|
17
|
+
*
|
18
|
+
* If none of the above conditions are met, the function throws an error indicating the definition could not be found. Note that `presentation_definition_uri` is not supported in 0.9.x.
|
19
|
+
*
|
20
|
+
* @param {RequestObject} requestObject - The request object containing the presentation definition or references to it.
|
21
|
+
* @param {RelyingPartyEntityConfiguration["payload"]["metadata"]} [rpConf] - Optional relying party configuration.
|
22
|
+
* @returns {Promise<{ presentationDefinition: PresentationDefinition }>} - Resolves with the presentation definition.
|
23
|
+
* @throws {Error} - Throws if the presentation definition cannot be found or fetched.
|
24
|
+
*/
|
25
|
+
export const fetchPresentDefinition: FetchPresentationDefinition = async (
|
26
|
+
requestObject,
|
27
|
+
rpConf
|
28
|
+
) => {
|
29
|
+
// Check if `presentation_definition` is directly available in the request object
|
30
|
+
if (requestObject.presentation_definition) {
|
31
|
+
return {
|
32
|
+
presentationDefinition: requestObject.presentation_definition,
|
33
|
+
};
|
34
|
+
}
|
35
|
+
|
36
|
+
// Check if `scope` is present in the request object and a pre-configured presentation definition exists
|
37
|
+
if (
|
38
|
+
requestObject.scope &&
|
39
|
+
rpConf?.openid_credential_verifier?.presentation_definition
|
40
|
+
) {
|
41
|
+
return {
|
42
|
+
presentationDefinition:
|
43
|
+
rpConf.openid_credential_verifier.presentation_definition,
|
44
|
+
};
|
45
|
+
}
|
46
|
+
|
47
|
+
throw new Error("Presentation definition not found");
|
48
|
+
};
|
@@ -0,0 +1,166 @@
|
|
1
|
+
import {
|
2
|
+
DcqlQuery,
|
3
|
+
DcqlError,
|
4
|
+
DcqlCredentialSetError,
|
5
|
+
DcqlQueryResult,
|
6
|
+
} from "dcql";
|
7
|
+
import { isValiError } from "valibot";
|
8
|
+
import { decode, prepareVpToken } from "../../sd-jwt";
|
9
|
+
import type { Disclosure, DisclosureWithEncoded } from "../../sd-jwt/types";
|
10
|
+
import { ValidationFailed } from "../../utils/errors";
|
11
|
+
import { createCryptoContextFor } from "../../utils/crypto";
|
12
|
+
import type { RemotePresentation } from "./types";
|
13
|
+
|
14
|
+
type EvaluateDcqlQuery = (
|
15
|
+
credentialsSdJwt: [string /* keyTag */, string /* credential */][],
|
16
|
+
query: DcqlQuery.Input
|
17
|
+
) => {
|
18
|
+
id: string;
|
19
|
+
credential: string;
|
20
|
+
keyTag: string;
|
21
|
+
requiredDisclosures: DisclosureWithEncoded[];
|
22
|
+
isOptional?: boolean;
|
23
|
+
}[];
|
24
|
+
|
25
|
+
type PrepareRemotePresentations = (
|
26
|
+
credentials: {
|
27
|
+
id: string;
|
28
|
+
credential: string;
|
29
|
+
keyTag: string;
|
30
|
+
requestedClaims: string[];
|
31
|
+
}[],
|
32
|
+
nonce: string,
|
33
|
+
clientId: string
|
34
|
+
) => Promise<RemotePresentation[]>;
|
35
|
+
|
36
|
+
type DcqlMatchSuccess = Extract<
|
37
|
+
DcqlQueryResult.CredentialMatch,
|
38
|
+
{ success: true }
|
39
|
+
>;
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Convert a credential in JWT format to an object with claims
|
43
|
+
* for correct parsing by the `dcql` library.
|
44
|
+
*/
|
45
|
+
const mapCredentialToObject = (jwt: string) => {
|
46
|
+
const { sdJwt, disclosures } = decode(jwt);
|
47
|
+
const credentialFormat = sdJwt.header.typ;
|
48
|
+
|
49
|
+
// TODO [SIW-2082]: support MDOC credentials
|
50
|
+
if (credentialFormat !== "vc+sd-jwt") {
|
51
|
+
throw new Error(`Unsupported credential format: ${credentialFormat}`);
|
52
|
+
}
|
53
|
+
|
54
|
+
return {
|
55
|
+
vct: sdJwt.payload.vct,
|
56
|
+
credential_format: credentialFormat,
|
57
|
+
claims: disclosures.reduce(
|
58
|
+
(acc, disclosure) => ({
|
59
|
+
...acc,
|
60
|
+
[disclosure.decoded[1]]: disclosure.decoded,
|
61
|
+
}),
|
62
|
+
{} as Record<string, Disclosure>
|
63
|
+
),
|
64
|
+
};
|
65
|
+
};
|
66
|
+
|
67
|
+
/**
|
68
|
+
* Extract only successful matches from the DCQL query result.
|
69
|
+
*/
|
70
|
+
const getDcqlQueryMatches = (result: DcqlQueryResult) =>
|
71
|
+
Object.entries(result.credential_matches).filter(
|
72
|
+
([, match]) => match.success === true
|
73
|
+
) as [string, DcqlMatchSuccess][];
|
74
|
+
|
75
|
+
export const evaluateDcqlQuery: EvaluateDcqlQuery = (
|
76
|
+
credentialsSdJwt,
|
77
|
+
query
|
78
|
+
) => {
|
79
|
+
const credentials = credentialsSdJwt.map(([, credential]) =>
|
80
|
+
mapCredentialToObject(credential)
|
81
|
+
);
|
82
|
+
|
83
|
+
try {
|
84
|
+
// Validate the query
|
85
|
+
const parsedQuery = DcqlQuery.parse(query);
|
86
|
+
DcqlQuery.validate(parsedQuery);
|
87
|
+
|
88
|
+
const queryResult = DcqlQuery.query(parsedQuery, credentials);
|
89
|
+
|
90
|
+
if (!queryResult.canBeSatisfied) {
|
91
|
+
throw new Error("No credential can satisfy the provided DCQL query");
|
92
|
+
}
|
93
|
+
|
94
|
+
// Build an object vct:credentialJwt to map matched credentials to their JWT
|
95
|
+
const credentialsSdJwtByVct = credentials.reduce(
|
96
|
+
(acc, c, i) => ({ ...acc, [c.vct]: credentialsSdJwt[i]! }),
|
97
|
+
{} as Record<string, [string /* keyTag */, string /* credential */]>
|
98
|
+
);
|
99
|
+
|
100
|
+
return getDcqlQueryMatches(queryResult).map(([id, match]) => {
|
101
|
+
if (match.output.credential_format !== "vc+sd-jwt") {
|
102
|
+
throw new Error("Unsupported format"); // TODO [SIW-2082]: support MDOC credentials
|
103
|
+
}
|
104
|
+
const { vct, claims } = match.output;
|
105
|
+
|
106
|
+
// Find a matching credential set to see whether the credential is optional
|
107
|
+
// If no credential set is found, then the credential is required by default
|
108
|
+
// NOTE: This is an extra, it might not be necessary
|
109
|
+
const credentialSet = queryResult.credential_sets?.find((set) =>
|
110
|
+
set.matching_options?.flat().includes(vct)
|
111
|
+
);
|
112
|
+
const isOptional = credentialSet ? !credentialSet.required : false;
|
113
|
+
|
114
|
+
const [keyTag, credential] = credentialsSdJwtByVct[vct]!;
|
115
|
+
const requiredDisclosures = Object.values(
|
116
|
+
claims
|
117
|
+
) as DisclosureWithEncoded[];
|
118
|
+
return {
|
119
|
+
id,
|
120
|
+
keyTag,
|
121
|
+
credential,
|
122
|
+
isOptional,
|
123
|
+
requiredDisclosures,
|
124
|
+
};
|
125
|
+
});
|
126
|
+
} catch (error) {
|
127
|
+
// Invalid DCQL query structure
|
128
|
+
if (isValiError(error)) {
|
129
|
+
throw new ValidationFailed({
|
130
|
+
message: "Invalid DCQL query",
|
131
|
+
reason: error.issues.map((issue) => issue.message).join(", "),
|
132
|
+
});
|
133
|
+
}
|
134
|
+
|
135
|
+
if (error instanceof DcqlError) {
|
136
|
+
// TODO [SIW-2110]: handle invalid DQCL query or let the error propagate
|
137
|
+
}
|
138
|
+
if (error instanceof DcqlCredentialSetError) {
|
139
|
+
// TODO [SIW-2110]: handle missing credentials or let the error propagate
|
140
|
+
}
|
141
|
+
throw error;
|
142
|
+
}
|
143
|
+
};
|
144
|
+
|
145
|
+
export const prepareRemotePresentations: PrepareRemotePresentations = async (
|
146
|
+
credentials,
|
147
|
+
nonce,
|
148
|
+
clientId
|
149
|
+
) => {
|
150
|
+
return Promise.all(
|
151
|
+
credentials.map(async (item) => {
|
152
|
+
const { vp_token } = await prepareVpToken(nonce, clientId, [
|
153
|
+
item.credential,
|
154
|
+
item.requestedClaims,
|
155
|
+
createCryptoContextFor(item.keyTag),
|
156
|
+
]);
|
157
|
+
|
158
|
+
return {
|
159
|
+
credentialId: item.id,
|
160
|
+
requestedClaims: item.requestedClaims,
|
161
|
+
vpToken: vp_token,
|
162
|
+
format: "vc+sd-jwt",
|
163
|
+
};
|
164
|
+
})
|
165
|
+
);
|
166
|
+
};
|