@pagopa/io-react-native-wallet 2.2.0 → 2.4.0
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/README.md +4 -3
- package/lib/commonjs/credential/index.js +3 -1
- package/lib/commonjs/credential/index.js.map +1 -1
- package/lib/commonjs/credential/issuance/03-start-user-authorization.js +24 -6
- package/lib/commonjs/credential/issuance/03-start-user-authorization.js.map +1 -1
- package/lib/commonjs/credential/issuance/04-complete-user-authorization.js +26 -2
- package/lib/commonjs/credential/issuance/04-complete-user-authorization.js.map +1 -1
- package/lib/commonjs/credential/issuance/README.md +155 -18
- package/lib/commonjs/credential/issuance/index.js +9 -1
- package/lib/commonjs/credential/issuance/index.js.map +1 -1
- package/lib/commonjs/credential/issuance/mrtd-pop/01-verify-and-parse-challenge-info.js +57 -0
- package/lib/commonjs/credential/issuance/mrtd-pop/01-verify-and-parse-challenge-info.js.map +1 -0
- package/lib/commonjs/credential/issuance/mrtd-pop/02-init-challenge.js +61 -0
- package/lib/commonjs/credential/issuance/mrtd-pop/02-init-challenge.js.map +1 -0
- package/lib/commonjs/credential/issuance/mrtd-pop/03-validate-challenge.js +95 -0
- package/lib/commonjs/credential/issuance/mrtd-pop/03-validate-challenge.js.map +1 -0
- package/lib/commonjs/credential/issuance/mrtd-pop/README.md +92 -0
- package/lib/commonjs/credential/issuance/mrtd-pop/index.js +33 -0
- package/lib/commonjs/credential/issuance/mrtd-pop/index.js.map +1 -0
- package/lib/commonjs/credential/issuance/mrtd-pop/types.js +57 -0
- package/lib/commonjs/credential/issuance/mrtd-pop/types.js.map +1 -0
- package/lib/commonjs/credential/offer/01-start-flow.js +75 -0
- package/lib/commonjs/credential/offer/01-start-flow.js.map +1 -0
- package/lib/commonjs/credential/offer/02-fetch-credential-offer.js +45 -0
- package/lib/commonjs/credential/offer/02-fetch-credential-offer.js.map +1 -0
- package/lib/commonjs/credential/offer/README.md +174 -0
- package/lib/commonjs/credential/offer/errors.js +22 -0
- package/lib/commonjs/credential/offer/errors.js.map +1 -0
- package/lib/commonjs/credential/offer/index.js +25 -0
- package/lib/commonjs/credential/offer/index.js.map +1 -0
- package/lib/commonjs/credential/offer/types.js +51 -0
- package/lib/commonjs/credential/offer/types.js.map +1 -0
- package/lib/commonjs/credential/presentation/01-start-flow.js +1 -1
- package/lib/commonjs/credentials-catalogue/README.md +15 -0
- package/lib/commonjs/credentials-catalogue/fetch-and-parse-catalogue.js +42 -0
- package/lib/commonjs/credentials-catalogue/fetch-and-parse-catalogue.js.map +1 -0
- package/lib/commonjs/credentials-catalogue/index.js +13 -0
- package/lib/commonjs/credentials-catalogue/index.js.map +1 -0
- package/lib/commonjs/credentials-catalogue/types.js +99 -0
- package/lib/commonjs/credentials-catalogue/types.js.map +1 -0
- package/lib/commonjs/index.js +3 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/auth.js +9 -1
- package/lib/commonjs/utils/auth.js.map +1 -1
- package/lib/commonjs/utils/par.js +7 -2
- package/lib/commonjs/utils/par.js.map +1 -1
- package/lib/commonjs/utils/zod.js +28 -0
- package/lib/commonjs/utils/zod.js.map +1 -0
- package/lib/module/credential/index.js +2 -1
- package/lib/module/credential/index.js.map +1 -1
- package/lib/module/credential/issuance/03-start-user-authorization.js +24 -6
- package/lib/module/credential/issuance/03-start-user-authorization.js.map +1 -1
- package/lib/module/credential/issuance/04-complete-user-authorization.js +25 -2
- package/lib/module/credential/issuance/04-complete-user-authorization.js.map +1 -1
- package/lib/module/credential/issuance/README.md +155 -18
- package/lib/module/credential/issuance/index.js +3 -2
- package/lib/module/credential/issuance/index.js.map +1 -1
- package/lib/module/credential/issuance/mrtd-pop/01-verify-and-parse-challenge-info.js +50 -0
- package/lib/module/credential/issuance/mrtd-pop/01-verify-and-parse-challenge-info.js.map +1 -0
- package/lib/module/credential/issuance/mrtd-pop/02-init-challenge.js +52 -0
- package/lib/module/credential/issuance/mrtd-pop/02-init-challenge.js.map +1 -0
- package/lib/module/credential/issuance/mrtd-pop/03-validate-challenge.js +85 -0
- package/lib/module/credential/issuance/mrtd-pop/03-validate-challenge.js.map +1 -0
- package/lib/module/credential/issuance/mrtd-pop/README.md +92 -0
- package/lib/module/credential/issuance/mrtd-pop/index.js +5 -0
- package/lib/module/credential/issuance/mrtd-pop/index.js.map +1 -0
- package/lib/module/credential/issuance/mrtd-pop/types.js +46 -0
- package/lib/module/credential/issuance/mrtd-pop/types.js.map +1 -0
- package/lib/module/credential/offer/01-start-flow.js +66 -0
- package/lib/module/credential/offer/01-start-flow.js.map +1 -0
- package/lib/module/credential/offer/02-fetch-credential-offer.js +38 -0
- package/lib/module/credential/offer/02-fetch-credential-offer.js.map +1 -0
- package/lib/module/credential/offer/README.md +174 -0
- package/lib/module/credential/offer/errors.js +14 -0
- package/lib/module/credential/offer/errors.js.map +1 -0
- package/lib/module/credential/offer/index.js +5 -0
- package/lib/module/credential/offer/index.js.map +1 -0
- package/lib/module/credential/offer/types.js +41 -0
- package/lib/module/credential/offer/types.js.map +1 -0
- package/lib/module/credential/presentation/01-start-flow.js +1 -1
- package/lib/module/credentials-catalogue/README.md +15 -0
- package/lib/module/credentials-catalogue/fetch-and-parse-catalogue.js +35 -0
- package/lib/module/credentials-catalogue/fetch-and-parse-catalogue.js.map +1 -0
- package/lib/module/credentials-catalogue/index.js +2 -0
- package/lib/module/credentials-catalogue/index.js.map +1 -0
- package/lib/module/credentials-catalogue/types.js +89 -0
- package/lib/module/credentials-catalogue/types.js.map +1 -0
- package/lib/module/index.js +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/auth.js +8 -0
- package/lib/module/utils/auth.js.map +1 -1
- package/lib/module/utils/par.js +7 -2
- package/lib/module/utils/par.js.map +1 -1
- package/lib/module/utils/zod.js +20 -0
- package/lib/module/utils/zod.js.map +1 -0
- package/lib/typescript/credential/index.d.ts +2 -1
- package/lib/typescript/credential/index.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/01-start-flow.d.ts +1 -1
- package/lib/typescript/credential/issuance/03-start-user-authorization.d.ts +14 -4
- package/lib/typescript/credential/issuance/03-start-user-authorization.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/04-complete-user-authorization.d.ts +9 -1
- package/lib/typescript/credential/issuance/04-complete-user-authorization.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/index.d.ts +4 -3
- package/lib/typescript/credential/issuance/index.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/mrtd-pop/01-verify-and-parse-challenge-info.d.ts +25 -0
- package/lib/typescript/credential/issuance/mrtd-pop/01-verify-and-parse-challenge-info.d.ts.map +1 -0
- package/lib/typescript/credential/issuance/mrtd-pop/02-init-challenge.d.ts +23 -0
- package/lib/typescript/credential/issuance/mrtd-pop/02-init-challenge.d.ts.map +1 -0
- package/lib/typescript/credential/issuance/mrtd-pop/03-validate-challenge.d.ts +39 -0
- package/lib/typescript/credential/issuance/mrtd-pop/03-validate-challenge.d.ts.map +1 -0
- package/lib/typescript/credential/issuance/mrtd-pop/index.d.ts +7 -0
- package/lib/typescript/credential/issuance/mrtd-pop/index.d.ts.map +1 -0
- package/lib/typescript/credential/issuance/mrtd-pop/types.d.ts +201 -0
- package/lib/typescript/credential/issuance/mrtd-pop/types.d.ts.map +1 -0
- package/lib/typescript/credential/offer/01-start-flow.d.ts +172 -0
- package/lib/typescript/credential/offer/01-start-flow.d.ts.map +1 -0
- package/lib/typescript/credential/offer/02-fetch-credential-offer.d.ts +20 -0
- package/lib/typescript/credential/offer/02-fetch-credential-offer.d.ts.map +1 -0
- package/lib/typescript/credential/offer/errors.d.ts +10 -0
- package/lib/typescript/credential/offer/errors.d.ts.map +1 -0
- package/lib/typescript/credential/offer/index.d.ts +7 -0
- package/lib/typescript/credential/offer/index.d.ts.map +1 -0
- package/lib/typescript/credential/offer/types.d.ts +264 -0
- package/lib/typescript/credential/offer/types.d.ts.map +1 -0
- package/lib/typescript/credential/presentation/01-start-flow.d.ts +1 -1
- package/lib/typescript/credentials-catalogue/fetch-and-parse-catalogue.d.ts +15 -0
- package/lib/typescript/credentials-catalogue/fetch-and-parse-catalogue.d.ts.map +1 -0
- package/lib/typescript/credentials-catalogue/index.d.ts +3 -0
- package/lib/typescript/credentials-catalogue/index.d.ts.map +1 -0
- package/lib/typescript/credentials-catalogue/types.d.ts +844 -0
- package/lib/typescript/credentials-catalogue/types.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +2 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/auth.d.ts +11 -0
- package/lib/typescript/utils/auth.d.ts.map +1 -1
- package/lib/typescript/utils/par.d.ts +34 -4
- package/lib/typescript/utils/par.d.ts.map +1 -1
- package/lib/typescript/utils/zod.d.ts +15 -0
- package/lib/typescript/utils/zod.d.ts.map +1 -0
- package/package.json +21 -2
- package/src/credential/index.ts +2 -1
- package/src/credential/issuance/01-start-flow.ts +1 -1
- package/src/credential/issuance/03-start-user-authorization.ts +30 -7
- package/src/credential/issuance/04-complete-user-authorization.ts +43 -1
- package/src/credential/issuance/README.md +155 -18
- package/src/credential/issuance/index.ts +7 -1
- package/src/credential/issuance/mrtd-pop/01-verify-and-parse-challenge-info.ts +70 -0
- package/src/credential/issuance/mrtd-pop/02-init-challenge.ts +82 -0
- package/src/credential/issuance/mrtd-pop/03-validate-challenge.ts +140 -0
- package/src/credential/issuance/mrtd-pop/README.md +92 -0
- package/src/credential/issuance/mrtd-pop/index.ts +27 -0
- package/src/credential/issuance/mrtd-pop/types.ts +65 -0
- package/src/credential/offer/01-start-flow.ts +89 -0
- package/src/credential/offer/02-fetch-credential-offer.ts +54 -0
- package/src/credential/offer/README.md +174 -0
- package/src/credential/offer/errors.ts +17 -0
- package/src/credential/offer/index.ts +16 -0
- package/src/credential/offer/types.ts +59 -0
- package/src/credential/presentation/01-start-flow.ts +1 -1
- package/src/credentials-catalogue/README.md +15 -0
- package/src/credentials-catalogue/fetch-and-parse-catalogue.ts +54 -0
- package/src/credentials-catalogue/index.ts +2 -0
- package/src/credentials-catalogue/types.ts +97 -0
- package/src/index.ts +2 -0
- package/src/utils/auth.ts +12 -0
- package/src/utils/par.ts +12 -4
- package/src/utils/zod.ts +28 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decode as decodeJwt,
|
|
3
|
+
verify as verifyJwt,
|
|
4
|
+
type CryptoContext,
|
|
5
|
+
} from "@pagopa/io-react-native-jwt";
|
|
6
|
+
import { MrtdProofChallengeInfo } from "./types";
|
|
7
|
+
import type { EvaluateIssuerTrust } from "../../issuance";
|
|
8
|
+
import type { Out } from "../../../utils/misc";
|
|
9
|
+
import { IoWalletError } from "../../../utils/errors";
|
|
10
|
+
|
|
11
|
+
export type VerifyAndParseChallengeInfo = (
|
|
12
|
+
issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
|
|
13
|
+
challengeInfoJwt: string,
|
|
14
|
+
context: {
|
|
15
|
+
wiaCryptoContext: CryptoContext;
|
|
16
|
+
}
|
|
17
|
+
) => Promise<MrtdProofChallengeInfo["payload"]>;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Verifies and parses the payload of a MRTD Proof Challenge Info JWT obtained after the primary authentication.
|
|
21
|
+
*
|
|
22
|
+
* This function performs the following steps:
|
|
23
|
+
* 1. Validates the JWT signature using the issuer's JWKS.
|
|
24
|
+
* 2. Decodes the JWT and parses its structure according to the {@link MrtdProofChallengeInfo} schema.
|
|
25
|
+
* 3. Verifies that the `aud` claim matches the client's public key ID.
|
|
26
|
+
* 4. Checks that the JWT is not expired and was not issued in the future.
|
|
27
|
+
*
|
|
28
|
+
* @param issuerConf - The issuer configuration containing the JWKS for signature verification.
|
|
29
|
+
* @param challengeInfoJwt - The JWT string representing the MRTD Proof Challenge Info.
|
|
30
|
+
* @param context - The context containing the WIA crypto context used to retrieve the client public key.
|
|
31
|
+
* @returns The parsed payload of the MRTD Proof Challenge Info JWT.
|
|
32
|
+
* @throws {Error} If the JWT signature is invalid, the structure is malformed, the `aud` claim does not match,
|
|
33
|
+
* or the JWT is expired/not yet valid.
|
|
34
|
+
*/
|
|
35
|
+
export const verifyAndParseChallengeInfo: VerifyAndParseChallengeInfo = async (
|
|
36
|
+
issuerConf,
|
|
37
|
+
challengeInfoJwt: string,
|
|
38
|
+
{ wiaCryptoContext }
|
|
39
|
+
) => {
|
|
40
|
+
// Verify JWT signature
|
|
41
|
+
await verifyJwt(
|
|
42
|
+
challengeInfoJwt,
|
|
43
|
+
issuerConf.oauth_authorization_server.jwks.keys
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
// Decode JWT
|
|
47
|
+
const challengeInfoDecoded = decodeJwt(challengeInfoJwt);
|
|
48
|
+
|
|
49
|
+
// Parse and validate structure
|
|
50
|
+
const challengeInfoParsed =
|
|
51
|
+
MrtdProofChallengeInfo.safeParse(challengeInfoDecoded);
|
|
52
|
+
if (!challengeInfoParsed.success) {
|
|
53
|
+
throw new IoWalletError("Malformed challenge info.");
|
|
54
|
+
}
|
|
55
|
+
const payload = challengeInfoParsed.data.payload;
|
|
56
|
+
|
|
57
|
+
// Verify aud claim
|
|
58
|
+
const clientId = await wiaCryptoContext.getPublicKey().then((_) => _.kid);
|
|
59
|
+
if (payload.aud !== clientId) {
|
|
60
|
+
throw new IoWalletError("aud claim does not match client_id.");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Verify iat and exp
|
|
64
|
+
const now = Math.floor(Date.now() / 1000);
|
|
65
|
+
if (payload.iat > now || payload.exp < now) {
|
|
66
|
+
throw new IoWalletError("JWT is not valid (issued in future or expired).");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return payload;
|
|
70
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { hasStatusOrThrow, type Out } from "../../../utils/misc";
|
|
2
|
+
import type { CryptoContext } from "@pagopa/io-react-native-jwt";
|
|
3
|
+
import { v4 as uuidv4 } from "uuid";
|
|
4
|
+
import { createPopToken } from "../../../utils/pop";
|
|
5
|
+
import * as WalletInstanceAttestation from "../../../wallet-instance-attestation";
|
|
6
|
+
import type { EvaluateIssuerTrust } from "../../issuance";
|
|
7
|
+
import { IssuerResponseError } from "../../../utils/errors";
|
|
8
|
+
import { decode as decodeJwt } from "@pagopa/io-react-native-jwt";
|
|
9
|
+
import { MrtdPoPChallenge } from "./types";
|
|
10
|
+
|
|
11
|
+
export type InitChallenge = (
|
|
12
|
+
issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
|
|
13
|
+
initUrl: string,
|
|
14
|
+
mrtd_auth_session: string,
|
|
15
|
+
mrtd_pop_jwt_nonce: string,
|
|
16
|
+
context: {
|
|
17
|
+
wiaCryptoContext: CryptoContext;
|
|
18
|
+
walletInstanceAttestation: string;
|
|
19
|
+
appFetch?: GlobalFetch["fetch"];
|
|
20
|
+
}
|
|
21
|
+
) => Promise<MrtdPoPChallenge["payload"]>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Initialaizes the MRTD challenge with the data received from the issuer after the primary authentication.
|
|
25
|
+
* This function must be called after {@link verifyAndParseChallengeInfo}.
|
|
26
|
+
*
|
|
27
|
+
* @param issuerConf - The issuer configuration containing the JWKS for signature verification.
|
|
28
|
+
* @param initUrl - The endpoint to call to initialize the challenge.
|
|
29
|
+
* @param mrtd_auth_session - Session identifier for session binding obtained from the MRTD Proof JWT.
|
|
30
|
+
* @param mrtd_pop_jwt_nonce - Nonce value obtained from the MRTD Proof JWT.
|
|
31
|
+
* @param context - The context containing the WIA crypto context used to retrieve the client public key,
|
|
32
|
+
* the wallet instance attestation and an optional fetch implementation.
|
|
33
|
+
* @returns The payload of the MRTD PoP Challenge JWT.
|
|
34
|
+
*/
|
|
35
|
+
export const initChallenge: InitChallenge = async (
|
|
36
|
+
issuerConf,
|
|
37
|
+
initUrl,
|
|
38
|
+
mrtd_auth_session,
|
|
39
|
+
mrtd_pop_jwt_nonce,
|
|
40
|
+
context
|
|
41
|
+
) => {
|
|
42
|
+
const {
|
|
43
|
+
appFetch = fetch,
|
|
44
|
+
walletInstanceAttestation,
|
|
45
|
+
wiaCryptoContext,
|
|
46
|
+
} = context;
|
|
47
|
+
|
|
48
|
+
const aud = issuerConf.openid_credential_issuer.credential_issuer;
|
|
49
|
+
const iss = WalletInstanceAttestation.decode(walletInstanceAttestation)
|
|
50
|
+
.payload.cnf.jwk.kid;
|
|
51
|
+
|
|
52
|
+
const signedWiaPoP = await createPopToken(
|
|
53
|
+
{
|
|
54
|
+
jti: `${uuidv4()}`,
|
|
55
|
+
aud,
|
|
56
|
+
iss,
|
|
57
|
+
},
|
|
58
|
+
wiaCryptoContext
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const requestBody = {
|
|
62
|
+
mrtd_auth_session,
|
|
63
|
+
mrtd_pop_jwt_nonce,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const mrtdPoPChallengeJwt = await appFetch(initUrl, {
|
|
67
|
+
method: "POST",
|
|
68
|
+
headers: {
|
|
69
|
+
"Content-Type": "application/json",
|
|
70
|
+
"OAuth-Client-Attestation": walletInstanceAttestation,
|
|
71
|
+
"OAuth-Client-Attestation-PoP": signedWiaPoP,
|
|
72
|
+
},
|
|
73
|
+
body: JSON.stringify(requestBody),
|
|
74
|
+
})
|
|
75
|
+
.then(hasStatusOrThrow(202, IssuerResponseError))
|
|
76
|
+
.then((res) => res.text());
|
|
77
|
+
|
|
78
|
+
const mrtdPoPChallengeDecoded = decodeJwt(mrtdPoPChallengeJwt);
|
|
79
|
+
const { payload } = MrtdPoPChallenge.parse(mrtdPoPChallengeDecoded);
|
|
80
|
+
|
|
81
|
+
return payload;
|
|
82
|
+
};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { SignJWT, type CryptoContext } from "@pagopa/io-react-native-jwt";
|
|
2
|
+
import { v4 as uuidv4 } from "uuid";
|
|
3
|
+
import { IssuerResponseError } from "../../../utils/errors";
|
|
4
|
+
import { hasStatusOrThrow, type Out } from "../../../utils/misc";
|
|
5
|
+
import { createPopToken } from "../../../utils/pop";
|
|
6
|
+
import * as WalletInstanceAttestation from "../../../wallet-instance-attestation";
|
|
7
|
+
import type { EvaluateIssuerTrust } from "../../issuance";
|
|
8
|
+
import {
|
|
9
|
+
MrtdPopVerificationResult,
|
|
10
|
+
type IasPayload,
|
|
11
|
+
type MrtdPayload,
|
|
12
|
+
} from "./types";
|
|
13
|
+
import type { VerifyAndParseChallengeInfo } from "./01-verify-and-parse-challenge-info";
|
|
14
|
+
|
|
15
|
+
export type ValidateChallenge = (
|
|
16
|
+
issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
|
|
17
|
+
verifyUrl: string,
|
|
18
|
+
mrtd_auth_session: string,
|
|
19
|
+
mrtd_pop_nonce: string,
|
|
20
|
+
mrtd: MrtdPayload,
|
|
21
|
+
ias: IasPayload,
|
|
22
|
+
context: {
|
|
23
|
+
wiaCryptoContext: CryptoContext;
|
|
24
|
+
walletInstanceAttestation: string;
|
|
25
|
+
appFetch?: GlobalFetch["fetch"];
|
|
26
|
+
}
|
|
27
|
+
) => Promise<MrtdPopVerificationResult>;
|
|
28
|
+
|
|
29
|
+
export type BuildChallengeCallbackUrl = (
|
|
30
|
+
redirectUri: Out<ValidateChallenge>["redirect_uri"],
|
|
31
|
+
valPopNonce: Out<ValidateChallenge>["mrtd_val_pop_nonce"],
|
|
32
|
+
authSession: Out<VerifyAndParseChallengeInfo>["mrtd_auth_session"]
|
|
33
|
+
) => Promise<{
|
|
34
|
+
callbackUrl: string;
|
|
35
|
+
}>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Validates the MRTD signed challenge by sending the MRTD and IAS payloads to the issuer.
|
|
39
|
+
* This function must be called after {@link initChallenge} and after obtaining the MRTD and IAS payloads
|
|
40
|
+
* through the CIE PACE process.
|
|
41
|
+
*
|
|
42
|
+
* @param issuerConf - The issuer configuration containing the JWKS for signature verification.
|
|
43
|
+
* @param verifyUrl - The endpoint to call to validate the challenge.
|
|
44
|
+
* @param mrtd_auth_session - Session identifier for session binding obtained from the MRTD Proof JWT.
|
|
45
|
+
* @param mrtd_pop_nonce - Nonce value obtained from the MRTD Proof JWT.
|
|
46
|
+
* @param mrtd - MRTD validation data containing Data Groups and SOD.
|
|
47
|
+
* @param ias - IAS validation data containing Anti-Cloning Public Key, and SOD.
|
|
48
|
+
* @param context - The context containing the WIA crypto context used to retrieve the client public key,
|
|
49
|
+
* the wallet instance attestation and an optional fetch implementation.
|
|
50
|
+
* @returns The MRTD PoP Verification Result containing the validation nonce and redirect URI to complete the flow.
|
|
51
|
+
*/
|
|
52
|
+
export const validateChallenge: ValidateChallenge = async (
|
|
53
|
+
issuerConf,
|
|
54
|
+
verifyUrl,
|
|
55
|
+
mrtd_auth_session,
|
|
56
|
+
mrtd_pop_nonce,
|
|
57
|
+
mrtd,
|
|
58
|
+
ias,
|
|
59
|
+
context
|
|
60
|
+
) => {
|
|
61
|
+
const {
|
|
62
|
+
appFetch = fetch,
|
|
63
|
+
walletInstanceAttestation,
|
|
64
|
+
wiaCryptoContext,
|
|
65
|
+
} = context;
|
|
66
|
+
|
|
67
|
+
const aud = issuerConf.openid_credential_issuer.credential_issuer;
|
|
68
|
+
const iss = WalletInstanceAttestation.decode(walletInstanceAttestation)
|
|
69
|
+
.payload.cnf.jwk.kid;
|
|
70
|
+
|
|
71
|
+
const signedWiaPoP = await createPopToken(
|
|
72
|
+
{
|
|
73
|
+
jti: `${uuidv4()}`,
|
|
74
|
+
aud,
|
|
75
|
+
iss,
|
|
76
|
+
},
|
|
77
|
+
wiaCryptoContext
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const { kid } = await wiaCryptoContext.getPublicKey();
|
|
81
|
+
|
|
82
|
+
const mrtd_validation_jwt = await new SignJWT(wiaCryptoContext)
|
|
83
|
+
.setProtectedHeader({
|
|
84
|
+
typ: "mrtd-ias+jwt",
|
|
85
|
+
kid,
|
|
86
|
+
})
|
|
87
|
+
.setPayload({
|
|
88
|
+
iss,
|
|
89
|
+
aud,
|
|
90
|
+
document_type: "cie",
|
|
91
|
+
mrtd,
|
|
92
|
+
ias,
|
|
93
|
+
})
|
|
94
|
+
.setIssuedAt()
|
|
95
|
+
.setExpirationTime("5m")
|
|
96
|
+
.sign();
|
|
97
|
+
|
|
98
|
+
const requestBody = {
|
|
99
|
+
mrtd_validation_jwt,
|
|
100
|
+
mrtd_auth_session,
|
|
101
|
+
mrtd_pop_nonce,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const verifyResult = await appFetch(verifyUrl, {
|
|
105
|
+
method: "POST",
|
|
106
|
+
headers: {
|
|
107
|
+
"Content-Type": "application/json",
|
|
108
|
+
"OAuth-Client-Attestation": walletInstanceAttestation,
|
|
109
|
+
"OAuth-Client-Attestation-PoP": signedWiaPoP,
|
|
110
|
+
},
|
|
111
|
+
body: JSON.stringify(requestBody),
|
|
112
|
+
})
|
|
113
|
+
.then(hasStatusOrThrow(202, IssuerResponseError))
|
|
114
|
+
.then((res) => res.json());
|
|
115
|
+
|
|
116
|
+
const verifyResultParsed = MrtdPopVerificationResult.parse(verifyResult);
|
|
117
|
+
return verifyResultParsed;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* WARNING: This function must be called after {@link validateChallenge}. The generated authUrl must be used to open a browser or webview capable of catching the redirectSchema to perform a get request to the authorization endpoint.
|
|
122
|
+
* Builds the callback URL to which the end user should be redirected to continue the authentication flow after the MRTD challenge validation.
|
|
123
|
+
* @param redirectUri - The redirect URI provided by the issuer after the challenge validation to continue the authentication flow.
|
|
124
|
+
* @param valPopNonce - The MRTD validation PoP nonce obtained from the challenge validation response.
|
|
125
|
+
* @param authSession - The MRTD authentication session identifier used for session binding.
|
|
126
|
+
* @returns An object containing the callback URL
|
|
127
|
+
*/
|
|
128
|
+
export const buildChallengeCallbackUrl: BuildChallengeCallbackUrl = async (
|
|
129
|
+
redirectUri,
|
|
130
|
+
valPopNonce,
|
|
131
|
+
authSession
|
|
132
|
+
) => {
|
|
133
|
+
const params = new URLSearchParams({
|
|
134
|
+
mrtd_val_pop_nonce: valPopNonce,
|
|
135
|
+
mrtd_auth_session: authSession,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const callbackUrl = `${redirectUri}?${params}`;
|
|
139
|
+
return { callbackUrl };
|
|
140
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# MRTD PoP flow
|
|
2
|
+
|
|
3
|
+
**MRTD-PoP (Machine Readable Travel Document - Proof of Possession)** flow for the IO Wallet, following the [eID Wallet L2+ Credential Issuance specification](https://italia.github.io/eid-wallet-it-docs/versione-corrente/en/credential-issuance-l2plus.html).
|
|
4
|
+
|
|
5
|
+
The MRTD-PoP flow is used to prove possession of an MRTD (such as a CIE) during the issuance of high-assurance credentials. The process involves a challenge-response protocol between the wallet and the issuer, leveraging JWTs and cryptographic attestation.
|
|
6
|
+
|
|
7
|
+
This flow is part of the [PID issuance flow](../README.md) and must be started after the `continueUserAuthorizationWithMRTDPoPChallenge` function. Once MRTD PoP is completed, the PID issuance flow must continue with the `completeUserAuthorizationWithQueryMode` function with the authorization url obtained from the validation.
|
|
8
|
+
|
|
9
|
+
> **⚠️ Important**: The entire flow must be initiated and concluded within the same web context (e.g., the same WebView instance) to maintain session continuity. Using different contexts (such as switching between an external browser and a WebView) will result in session loss and authentication failures due to cookie/session mismatch (JSESSIONID).
|
|
10
|
+
|
|
11
|
+
## Sequence Diagram
|
|
12
|
+
|
|
13
|
+
```mermaid
|
|
14
|
+
graph TD;
|
|
15
|
+
A@{ shape: subproc, label: "continueUserAuthorizationWithMRTDPoPChallenge" }
|
|
16
|
+
subgraph MRTD PoP
|
|
17
|
+
B[verifyAndParseChallengeInfo]
|
|
18
|
+
C[initChallenge]
|
|
19
|
+
E[validateChallenge]
|
|
20
|
+
end
|
|
21
|
+
F@{ shape: subproc, label: "completeUserAuthorizationWithQueryModeChallenge" }
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
A -.-> B
|
|
25
|
+
B --> C
|
|
26
|
+
C -->E
|
|
27
|
+
E -.-> F
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Example
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
// Verify and parse challenge info and extract challenge data: initialization url, session and nonce
|
|
35
|
+
const {
|
|
36
|
+
htu: initUrl,
|
|
37
|
+
mrtd_auth_session,
|
|
38
|
+
mrtd_pop_jwt_nonce,
|
|
39
|
+
} = await Credential.Issuance.MRTDPoP.verifyAndParseChallengeInfo(
|
|
40
|
+
issuerConf,
|
|
41
|
+
challenge_info,
|
|
42
|
+
{ wiaCryptoContext }
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// Initialize challenge and obtain the challenge text to sign the CIE PACE protocol and validation url
|
|
46
|
+
const {
|
|
47
|
+
htu: validationUrl,
|
|
48
|
+
challenge,
|
|
49
|
+
mrtd_pop_nonce,
|
|
50
|
+
} = await Credential.Issuance.MRTDPoP.initChallenge(
|
|
51
|
+
issuerConf,
|
|
52
|
+
initUrl,
|
|
53
|
+
mrtd_auth_session,
|
|
54
|
+
mrtd_pop_jwt_nonce,
|
|
55
|
+
{
|
|
56
|
+
walletInstanceAttestation,
|
|
57
|
+
wiaCryptoContext,
|
|
58
|
+
appFetch,
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
// CIE cryptographic interaction: you need to sign the challenge with the CIE through NFC interaction
|
|
63
|
+
const { nis, mrtds } = /* NFC interactions functions */
|
|
64
|
+
|
|
65
|
+
// Validate challenge
|
|
66
|
+
const { mrtd_val_pop_nonce, redirect_uri } =
|
|
67
|
+
await Credential.Issuance.MRTDPoP.validateChallenge(
|
|
68
|
+
issuerConf,
|
|
69
|
+
validationUrl,
|
|
70
|
+
mrtd_auth_session,
|
|
71
|
+
mrtd_pop_nonce,
|
|
72
|
+
mrtd,
|
|
73
|
+
ias,
|
|
74
|
+
{
|
|
75
|
+
walletInstanceAttestation,
|
|
76
|
+
wiaCryptoContext,
|
|
77
|
+
appFetch,
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
// Build the callback url
|
|
82
|
+
const { callbackUrl } = await Credential.Issuance.buildChallengeCallbackUrl(
|
|
83
|
+
redirect_uri,
|
|
84
|
+
mrtd_val_pop_nonce,
|
|
85
|
+
mrtd_auth_session
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
// The generated authUrl must be used to open a browser or webview capable of catching the redirectSchema to perform a get request to the authorization endpoint.
|
|
89
|
+
const authRedirectUrl = /* From a browser or webview redirect */
|
|
90
|
+
|
|
91
|
+
// Use the authRedirectUrl to continue the PID issuance flow
|
|
92
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {
|
|
2
|
+
verifyAndParseChallengeInfo,
|
|
3
|
+
type VerifyAndParseChallengeInfo,
|
|
4
|
+
} from "./01-verify-and-parse-challenge-info";
|
|
5
|
+
import { initChallenge, type InitChallenge } from "./02-init-challenge";
|
|
6
|
+
import {
|
|
7
|
+
validateChallenge,
|
|
8
|
+
buildChallengeCallbackUrl,
|
|
9
|
+
type ValidateChallenge,
|
|
10
|
+
type BuildChallengeCallbackUrl,
|
|
11
|
+
} from "./03-validate-challenge";
|
|
12
|
+
import type { MrtdPayload, IasPayload } from "./types";
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
verifyAndParseChallengeInfo,
|
|
16
|
+
initChallenge,
|
|
17
|
+
validateChallenge,
|
|
18
|
+
buildChallengeCallbackUrl,
|
|
19
|
+
};
|
|
20
|
+
export type {
|
|
21
|
+
VerifyAndParseChallengeInfo,
|
|
22
|
+
InitChallenge,
|
|
23
|
+
ValidateChallenge,
|
|
24
|
+
BuildChallengeCallbackUrl,
|
|
25
|
+
MrtdPayload,
|
|
26
|
+
IasPayload,
|
|
27
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
|
|
3
|
+
export type MrtdProofChallengeInfo = z.infer<typeof MrtdProofChallengeInfo>;
|
|
4
|
+
export const MrtdProofChallengeInfo = z.object({
|
|
5
|
+
protectedHeader: z.object({
|
|
6
|
+
typ: z.literal("mrtd-ias+jwt"),
|
|
7
|
+
alg: z.string(),
|
|
8
|
+
kid: z.string(),
|
|
9
|
+
}),
|
|
10
|
+
payload: z.object({
|
|
11
|
+
iss: z.string(),
|
|
12
|
+
aud: z.string(),
|
|
13
|
+
iat: z.number(),
|
|
14
|
+
exp: z.number(),
|
|
15
|
+
status: z.literal("require_interaction"),
|
|
16
|
+
type: z.literal("mrtd+ias"),
|
|
17
|
+
mrtd_auth_session: z.string(),
|
|
18
|
+
state: z.string(),
|
|
19
|
+
mrtd_pop_jwt_nonce: z.string(),
|
|
20
|
+
htu: z.string(),
|
|
21
|
+
htm: z.literal("POST"),
|
|
22
|
+
}),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export type MrtdPoPChallenge = z.infer<typeof MrtdPoPChallenge>;
|
|
26
|
+
export const MrtdPoPChallenge = z.object({
|
|
27
|
+
protectedHeader: z.object({
|
|
28
|
+
typ: z.literal("mrtd-ias-pop+jwt"),
|
|
29
|
+
alg: z.string(),
|
|
30
|
+
kid: z.string(),
|
|
31
|
+
}),
|
|
32
|
+
payload: z.object({
|
|
33
|
+
iss: z.string(),
|
|
34
|
+
aud: z.string(),
|
|
35
|
+
iat: z.number(),
|
|
36
|
+
exp: z.number(),
|
|
37
|
+
challenge: z.string(),
|
|
38
|
+
mrtd_pop_nonce: z.string(),
|
|
39
|
+
mrz: z.string().optional(),
|
|
40
|
+
htu: z.string(),
|
|
41
|
+
htm: z.literal("POST"),
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export type MrtdPayload = {
|
|
46
|
+
dg1: string;
|
|
47
|
+
dg11: string;
|
|
48
|
+
sod_mrtd: string;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type IasPayload = {
|
|
52
|
+
ias_pk: string;
|
|
53
|
+
sod_ias: string;
|
|
54
|
+
challenge_signed: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type MrtdPopVerificationResult = z.infer<
|
|
58
|
+
typeof MrtdPopVerificationResult
|
|
59
|
+
>;
|
|
60
|
+
export const MrtdPopVerificationResult = z.object({
|
|
61
|
+
status: z.literal("require_interaction"),
|
|
62
|
+
type: z.literal("redirect_to_web"),
|
|
63
|
+
mrtd_val_pop_nonce: z.string(),
|
|
64
|
+
redirect_uri: z.string(),
|
|
65
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { Logger, LogLevel } from "../../utils/logging";
|
|
3
|
+
import { stringToJSONSchema } from "../../utils/zod";
|
|
4
|
+
import { InvalidQRCodeError } from "./errors";
|
|
5
|
+
import { CredentialOfferSchema } from "./types";
|
|
6
|
+
|
|
7
|
+
const CREDENTIAL_OFFER_SCHEMES = ["openid-credential-offer://", "haip://"];
|
|
8
|
+
const CREDENTIAL_OFFER_PARAM = "credential_offer";
|
|
9
|
+
const CREDENTIAL_OFFER_URI_PARAM = "credential_offer_uri";
|
|
10
|
+
|
|
11
|
+
const CredentialOfferParams = z.union([
|
|
12
|
+
z.object({
|
|
13
|
+
credential_offer: stringToJSONSchema.pipe(CredentialOfferSchema),
|
|
14
|
+
credential_offer_uri: z.undefined(),
|
|
15
|
+
}),
|
|
16
|
+
z.object({
|
|
17
|
+
credential_offer: z.undefined(),
|
|
18
|
+
credential_offer_uri: z.string().url(),
|
|
19
|
+
}),
|
|
20
|
+
]);
|
|
21
|
+
type CredentialOfferParams = z.infer<typeof CredentialOfferParams>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The beginning of the credential offer flow.
|
|
25
|
+
* To be implemented according to the user touchpoint
|
|
26
|
+
*
|
|
27
|
+
* @param params Credential offer encoded url
|
|
28
|
+
* @returns Object containing the credential offer by reference or by value
|
|
29
|
+
*/
|
|
30
|
+
export type StartFlow = (encodedUrl: string) => CredentialOfferParams;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Start a credential offer flow by validating and parse an encoded url
|
|
34
|
+
* extracted from a QR code or a deep link.
|
|
35
|
+
*
|
|
36
|
+
* @param params The encoded url to be validated and parsed
|
|
37
|
+
* @returns Object containing the credential offer by reference or by value
|
|
38
|
+
* @throws If the provided encoded url is not valid
|
|
39
|
+
*/
|
|
40
|
+
export const startFlowFromQR: StartFlow = (encodedUrl) => {
|
|
41
|
+
const hasValidScheme = CREDENTIAL_OFFER_SCHEMES.some((prefix) =>
|
|
42
|
+
encodedUrl.startsWith(prefix)
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
if (!hasValidScheme) {
|
|
46
|
+
throw new InvalidQRCodeError("Url must have one of the supported schemes");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const url = new URL(encodedUrl);
|
|
50
|
+
const offerParam = url.searchParams.get(CREDENTIAL_OFFER_PARAM);
|
|
51
|
+
const offerUriParam = url.searchParams.get(CREDENTIAL_OFFER_URI_PARAM);
|
|
52
|
+
|
|
53
|
+
if (offerParam) {
|
|
54
|
+
const decoded = decodeURIComponent(offerParam);
|
|
55
|
+
const result = CredentialOfferParams.safeParse({
|
|
56
|
+
credential_offer: decoded,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (result.success) {
|
|
60
|
+
return result.data;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
Logger.log(
|
|
64
|
+
LogLevel.ERROR,
|
|
65
|
+
`Invalid credential offer object found in QR Code: ${result.error.message}`
|
|
66
|
+
);
|
|
67
|
+
throw new InvalidQRCodeError(result.error.message);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (offerUriParam) {
|
|
71
|
+
const decoded = decodeURIComponent(offerUriParam);
|
|
72
|
+
const result = CredentialOfferParams.safeParse({
|
|
73
|
+
credential_offer_uri: decoded,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
if (result.success) {
|
|
77
|
+
return result.data;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
Logger.log(
|
|
81
|
+
LogLevel.ERROR,
|
|
82
|
+
`Invalid credential offer URI found in QR Code: ${result.error.message}`
|
|
83
|
+
);
|
|
84
|
+
throw new InvalidQRCodeError(result.error.message);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
Logger.log(LogLevel.ERROR, `Invalid credential offer QR Code:`);
|
|
88
|
+
throw new InvalidQRCodeError("QR Code does not contain valid params");
|
|
89
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { IssuerResponseError } from "../../utils/errors";
|
|
2
|
+
import { Logger, LogLevel } from "../../utils/logging";
|
|
3
|
+
import { hasStatusOrThrow } from "../../utils/misc";
|
|
4
|
+
import { InvalidCredentialOfferError } from "./errors";
|
|
5
|
+
import type { CredentialOffer } from "./types";
|
|
6
|
+
import { CredentialOfferSchema } from "./types";
|
|
7
|
+
|
|
8
|
+
export type GetCredentialOffer = (
|
|
9
|
+
credentialOfferUri: string,
|
|
10
|
+
context: {
|
|
11
|
+
appFetch?: GlobalFetch["fetch"];
|
|
12
|
+
}
|
|
13
|
+
) => Promise<CredentialOffer>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Fetches and validates a credential offer from a given URI.
|
|
17
|
+
*
|
|
18
|
+
* This function performs an HTTP GET request to the specified `credentialOfferUri`,
|
|
19
|
+
* expecting a JSON response that matches the `CredentialOfferSchema`. If the response
|
|
20
|
+
* is invalid or does not conform to the schema, an error is logged and an
|
|
21
|
+
* `InvalidCredentialOfferError` is thrown.
|
|
22
|
+
*
|
|
23
|
+
* @param credentialOfferUri - The URI from which to fetch the credential offer.
|
|
24
|
+
* @param context - Optional context object that may provide a custom `appFetch` implementation.
|
|
25
|
+
* @returns The validated credential offer data.
|
|
26
|
+
* @throws {IssuerResponseError} If the HTTP response status is not 200.
|
|
27
|
+
* @throws {InvalidCredentialOfferError} If the response does not match the expected schema.
|
|
28
|
+
*/
|
|
29
|
+
export const fetchCredentialOffer: GetCredentialOffer = async (
|
|
30
|
+
uri: string,
|
|
31
|
+
context = {}
|
|
32
|
+
) => {
|
|
33
|
+
const { appFetch = fetch } = context;
|
|
34
|
+
|
|
35
|
+
const response = await appFetch(uri, {
|
|
36
|
+
method: "GET",
|
|
37
|
+
headers: { Accept: "application/json" },
|
|
38
|
+
})
|
|
39
|
+
.then(hasStatusOrThrow(200, IssuerResponseError))
|
|
40
|
+
.then((reqUri) => reqUri.json());
|
|
41
|
+
|
|
42
|
+
const credentialOffer = CredentialOfferSchema.safeParse(response);
|
|
43
|
+
if (!credentialOffer.success) {
|
|
44
|
+
Logger.log(
|
|
45
|
+
LogLevel.ERROR,
|
|
46
|
+
`Invalid credential offer fetched from URI: ${uri} - ${credentialOffer.error.message}`
|
|
47
|
+
);
|
|
48
|
+
throw new InvalidCredentialOfferError(
|
|
49
|
+
`Invalid credential offer fetched from URI: ${uri} - ${credentialOffer.error.message}`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return credentialOffer.data;
|
|
54
|
+
};
|