@pagopa/io-react-native-wallet 2.0.0-next.4 → 2.0.0-next.5
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/credential/issuance/07-verify-and-parse-credential.js +3 -3
- package/lib/commonjs/credential/issuance/07-verify-and-parse-credential.js.map +1 -1
- package/lib/commonjs/credential/status/{02-status-attestation.js → 02-status-assertion.js} +28 -22
- package/lib/commonjs/credential/status/02-status-assertion.js.map +1 -0
- package/lib/commonjs/credential/status/03-verify-and-parse-status-assertion.js +85 -0
- package/lib/commonjs/credential/status/03-verify-and-parse-status-assertion.js.map +1 -0
- package/lib/commonjs/credential/status/README.md +22 -20
- package/lib/commonjs/credential/status/index.js +6 -6
- package/lib/commonjs/credential/status/index.js.map +1 -1
- package/lib/commonjs/credential/status/types.js +48 -15
- package/lib/commonjs/credential/status/types.js.map +1 -1
- package/lib/commonjs/utils/credentials.js +33 -0
- package/lib/commonjs/utils/credentials.js.map +1 -0
- package/lib/commonjs/utils/crypto.js +1 -7
- package/lib/commonjs/utils/crypto.js.map +1 -1
- package/lib/commonjs/utils/jwk.js +12 -0
- package/lib/commonjs/utils/jwk.js.map +1 -1
- package/lib/module/credential/issuance/07-verify-and-parse-credential.js +4 -4
- package/lib/module/credential/issuance/07-verify-and-parse-credential.js.map +1 -1
- package/lib/module/credential/status/{02-status-attestation.js → 02-status-assertion.js} +28 -22
- package/lib/module/credential/status/02-status-assertion.js.map +1 -0
- package/lib/module/credential/status/03-verify-and-parse-status-assertion.js +78 -0
- package/lib/module/credential/status/03-verify-and-parse-status-assertion.js.map +1 -0
- package/lib/module/credential/status/README.md +22 -20
- package/lib/module/credential/status/index.js +3 -3
- package/lib/module/credential/status/index.js.map +1 -1
- package/lib/module/credential/status/types.js +43 -12
- package/lib/module/credential/status/types.js.map +1 -1
- package/lib/module/utils/credentials.js +26 -0
- package/lib/module/utils/credentials.js.map +1 -0
- package/lib/module/utils/crypto.js +2 -8
- package/lib/module/utils/crypto.js.map +1 -1
- package/lib/module/utils/jwk.js +11 -1
- package/lib/module/utils/jwk.js.map +1 -1
- package/lib/typescript/credential/issuance/07-verify-and-parse-credential.d.ts.map +1 -1
- package/lib/typescript/credential/status/02-status-assertion.d.ts +23 -0
- package/lib/typescript/credential/status/02-status-assertion.d.ts.map +1 -0
- package/lib/typescript/credential/status/03-verify-and-parse-status-assertion.d.ts +21 -0
- package/lib/typescript/credential/status/03-verify-and-parse-status-assertion.d.ts.map +1 -0
- package/lib/typescript/credential/status/index.d.ts +4 -4
- package/lib/typescript/credential/status/index.d.ts.map +1 -1
- package/lib/typescript/credential/status/types.d.ts +495 -18
- package/lib/typescript/credential/status/types.d.ts.map +1 -1
- package/lib/typescript/utils/credentials.d.ts +11 -0
- package/lib/typescript/utils/credentials.d.ts.map +1 -0
- package/lib/typescript/utils/crypto.d.ts.map +1 -1
- package/lib/typescript/utils/jwk.d.ts +7 -0
- package/lib/typescript/utils/jwk.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/credential/issuance/07-verify-and-parse-credential.ts +4 -6
- package/src/credential/status/{02-status-attestation.ts → 02-status-assertion.ts} +37 -28
- package/src/credential/status/03-verify-and-parse-status-assertion.ts +109 -0
- package/src/credential/status/README.md +22 -20
- package/src/credential/status/index.ts +7 -14
- package/src/credential/status/types.ts +62 -15
- package/src/utils/credentials.ts +29 -0
- package/src/utils/crypto.ts +12 -20
- package/src/utils/jwk.ts +15 -1
- package/lib/commonjs/credential/status/02-status-attestation.js.map +0 -1
- package/lib/commonjs/credential/status/03-verify-and-parse-status-attestation.js +0 -55
- package/lib/commonjs/credential/status/03-verify-and-parse-status-attestation.js.map +0 -1
- package/lib/module/credential/status/02-status-attestation.js.map +0 -1
- package/lib/module/credential/status/03-verify-and-parse-status-attestation.js +0 -49
- package/lib/module/credential/status/03-verify-and-parse-status-attestation.js.map +0 -1
- package/lib/typescript/credential/status/02-status-attestation.d.ts +0 -19
- package/lib/typescript/credential/status/02-status-attestation.d.ts.map +0 -1
- package/lib/typescript/credential/status/03-verify-and-parse-status-attestation.d.ts +0 -24
- package/lib/typescript/credential/status/03-verify-and-parse-status-attestation.d.ts.map +0 -1
- package/src/credential/status/03-verify-and-parse-status-attestation.ts +0 -70
@@ -2,12 +2,11 @@ import type { CryptoContext } from "@pagopa/io-react-native-jwt";
|
|
2
2
|
import type { Out } from "../../utils/misc";
|
3
3
|
import type { EvaluateIssuerTrust } from "./02-evaluate-issuer-trust";
|
4
4
|
import { IoWalletError } from "../../utils/errors";
|
5
|
-
import { SdJwt4VC } from "../../sd-jwt
|
6
|
-
import { verify as verifySdJwt } from "../../sd-jwt";
|
5
|
+
import { SdJwt4VC, verify as verifySdJwt } from "../../sd-jwt";
|
7
6
|
import { getValueFromDisclosures } from "../../sd-jwt/converters";
|
8
|
-
import type
|
7
|
+
import { isSameThumbprint, type JWK } from "../../utils/jwk";
|
9
8
|
import type { ObtainCredential } from "./06-obtain-credential";
|
10
|
-
import {
|
9
|
+
import { Logger, LogLevel } from "../../utils/logging";
|
11
10
|
|
12
11
|
type IssuerConf = Out<EvaluateIssuerTrust>["issuerConf"];
|
13
12
|
type CredentialConf =
|
@@ -169,8 +168,7 @@ async function verifyCredentialSdJwt(
|
|
169
168
|
]);
|
170
169
|
|
171
170
|
const { cnf } = decodedCredential.sdJwt.payload;
|
172
|
-
|
173
|
-
if (!cnf.jwk.kid || cnf.jwk.kid !== holderBindingKey.kid) {
|
171
|
+
if (!(await isSameThumbprint(cnf.jwk, holderBindingKey as JWK))) {
|
174
172
|
const message = `Failed to verify holder binding, expected kid: ${holderBindingKey.kid}, got: ${decodedCredential.sdJwt.payload.cnf.jwk.kid}`;
|
175
173
|
Logger.log(LogLevel.ERROR, message);
|
176
174
|
throw new IoWalletError(message);
|
@@ -6,54 +6,65 @@ import {
|
|
6
6
|
import type { EvaluateIssuerTrust, ObtainCredential } from "../issuance";
|
7
7
|
import { type CryptoContext, SignJWT } from "@pagopa/io-react-native-jwt";
|
8
8
|
import { v4 as uuidv4 } from "uuid";
|
9
|
-
import {
|
9
|
+
import { StatusAssertionResponse } from "./types";
|
10
10
|
import {
|
11
11
|
IssuerResponseError,
|
12
12
|
IssuerResponseErrorCodes,
|
13
13
|
ResponseErrorBuilder,
|
14
14
|
UnexpectedStatusCodeError,
|
15
15
|
} from "../../utils/errors";
|
16
|
-
import {
|
16
|
+
import { Logger, LogLevel } from "../../utils/logging";
|
17
|
+
import { extractJwkFromCredential } from "../../utils/credentials";
|
17
18
|
|
18
|
-
export type
|
19
|
+
export type StatusAssertion = (
|
19
20
|
issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
|
20
21
|
credential: Out<ObtainCredential>["credential"],
|
21
|
-
|
22
|
-
|
22
|
+
format: Out<ObtainCredential>["format"],
|
23
|
+
context: {
|
24
|
+
credentialCryptoContext: CryptoContext;
|
25
|
+
wiaCryptoContext: CryptoContext;
|
26
|
+
appFetch?: GlobalFetch["fetch"];
|
27
|
+
}
|
23
28
|
) => Promise<{
|
24
|
-
|
29
|
+
statusAssertion: string;
|
25
30
|
}>;
|
26
31
|
|
27
32
|
/**
|
28
|
-
*
|
29
|
-
* Verify the status of the credential attestation.
|
33
|
+
* Get the status assertion of a digital credential.
|
30
34
|
* @param issuerConf - The issuer's configuration
|
31
35
|
* @param credential - The credential to be verified
|
32
|
-
* @param
|
36
|
+
* @param format - The format of the credential, e.g. "sd-jwt"
|
37
|
+
* @param context.credentialCryptoContext - The credential's crypto context
|
38
|
+
* @param context.wiaCryptoContext - The Wallet Attestation's crypto context
|
33
39
|
* @param context.appFetch (optional) fetch api implementation. Default: built-in fetch
|
34
40
|
* @throws {IssuerResponseError} with a specific code for more context
|
35
|
-
* @returns The credential status
|
41
|
+
* @returns The credential status assertion
|
36
42
|
*/
|
37
|
-
export const
|
43
|
+
export const statusAssertion: StatusAssertion = async (
|
38
44
|
issuerConf,
|
39
45
|
credential,
|
40
|
-
|
41
|
-
|
46
|
+
format,
|
47
|
+
ctx
|
42
48
|
) => {
|
43
|
-
const
|
49
|
+
const { credentialCryptoContext, wiaCryptoContext, appFetch = fetch } = ctx;
|
50
|
+
|
51
|
+
const jwk = await extractJwkFromCredential(credential, format);
|
52
|
+
const issuerJwk = await wiaCryptoContext.getPublicKey();
|
44
53
|
const credentialHash = await getCredentialHashWithouDiscloures(credential);
|
45
54
|
const statusAttUrl =
|
46
55
|
issuerConf.openid_credential_issuer.status_attestation_endpoint;
|
56
|
+
|
47
57
|
const credentialPop = await new SignJWT(credentialCryptoContext)
|
48
58
|
.setPayload({
|
59
|
+
iss: issuerJwk.kid,
|
49
60
|
aud: statusAttUrl,
|
50
61
|
jti: uuidv4().toString(),
|
51
62
|
credential_hash: credentialHash,
|
52
|
-
credential_hash_alg: "
|
63
|
+
credential_hash_alg: "sha-256",
|
53
64
|
})
|
54
65
|
.setProtectedHeader({
|
55
66
|
alg: "ES256",
|
56
|
-
typ: "status-
|
67
|
+
typ: "status-assertion-request+jwt",
|
57
68
|
kid: jwk.kid,
|
58
69
|
})
|
59
70
|
.setIssuedAt()
|
@@ -61,7 +72,7 @@ export const statusAttestation: StatusAttestation = async (
|
|
61
72
|
.sign();
|
62
73
|
|
63
74
|
const body = {
|
64
|
-
|
75
|
+
status_assertion_requests: [credentialPop],
|
65
76
|
};
|
66
77
|
|
67
78
|
Logger.log(LogLevel.DEBUG, `Credential pop: ${credentialPop}`);
|
@@ -73,33 +84,31 @@ export const statusAttestation: StatusAttestation = async (
|
|
73
84
|
},
|
74
85
|
body: JSON.stringify(body),
|
75
86
|
})
|
76
|
-
.then(hasStatusOrThrow(
|
87
|
+
.then(hasStatusOrThrow(200))
|
77
88
|
.then((raw) => raw.json())
|
78
|
-
.then((json) =>
|
79
|
-
.catch(
|
89
|
+
.then((json) => StatusAssertionResponse.parse(json))
|
90
|
+
.catch(handleStatusAssertionError);
|
80
91
|
|
81
|
-
|
92
|
+
const [statusAttestationJwt] = result.status_assertion_responses;
|
93
|
+
|
94
|
+
return { statusAssertion: statusAttestationJwt! };
|
82
95
|
};
|
83
96
|
|
84
97
|
/**
|
85
|
-
* Handle the status
|
98
|
+
* Handle the status assertion error by mapping it to a custom exception.
|
86
99
|
* If the error is not an instance of {@link UnexpectedStatusCodeError}, it is thrown as is.
|
87
100
|
* @param e - The error to be handled
|
88
101
|
* @throws {IssuerResponseError} with a specific code for more context
|
89
102
|
*/
|
90
|
-
const
|
103
|
+
const handleStatusAssertionError = (e: unknown) => {
|
91
104
|
if (!(e instanceof UnexpectedStatusCodeError)) {
|
92
105
|
throw e;
|
93
106
|
}
|
94
107
|
|
95
108
|
throw new ResponseErrorBuilder(IssuerResponseError)
|
96
|
-
.handle(404, {
|
97
|
-
code: IssuerResponseErrorCodes.CredentialInvalidStatus,
|
98
|
-
message: "Invalid status found for the given credential",
|
99
|
-
})
|
100
109
|
.handle("*", {
|
101
110
|
code: IssuerResponseErrorCodes.StatusAttestationRequestFailed,
|
102
|
-
message: `Unable to obtain the status
|
111
|
+
message: `Unable to obtain the status assertion for the given credential`,
|
103
112
|
})
|
104
113
|
.buildFrom(e);
|
105
114
|
};
|
@@ -0,0 +1,109 @@
|
|
1
|
+
import type { Out } from "../../utils/misc";
|
2
|
+
import {
|
3
|
+
IoWalletError,
|
4
|
+
IssuerResponseError,
|
5
|
+
IssuerResponseErrorCodes,
|
6
|
+
} from "../../utils/errors";
|
7
|
+
import { decode as decodeJwt, verify } from "@pagopa/io-react-native-jwt";
|
8
|
+
import type { EvaluateIssuerTrust, StatusAssertion } from ".";
|
9
|
+
import {
|
10
|
+
type InvalidStatusErrorReason,
|
11
|
+
ParsedStatusAssertion,
|
12
|
+
ParsedStatusAssertionError,
|
13
|
+
ParsedStatusAssertionResponse,
|
14
|
+
StatusType,
|
15
|
+
} from "./types";
|
16
|
+
import { Logger, LogLevel } from "../../utils/logging";
|
17
|
+
import type { ObtainCredential } from "../issuance";
|
18
|
+
import { extractJwkFromCredential } from "../../utils/credentials";
|
19
|
+
import { isSameThumbprint } from "../../utils/jwk";
|
20
|
+
|
21
|
+
export type VerifyAndParseStatusAssertion = (
|
22
|
+
issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
|
23
|
+
statusAssertion: Out<StatusAssertion>,
|
24
|
+
credential: Out<ObtainCredential>["credential"],
|
25
|
+
format: Out<ObtainCredential>["format"]
|
26
|
+
) => Promise<{ parsedStatusAssertion: ParsedStatusAssertion }>;
|
27
|
+
|
28
|
+
/**
|
29
|
+
* Given a status assertion, verifies that:
|
30
|
+
* - It's in the supported format;
|
31
|
+
* - The assertion is correctly signed;
|
32
|
+
* - It's bound to the given key.
|
33
|
+
* @param issuerConf The Issuer configuration returned by {@link evaluateIssuerTrust}
|
34
|
+
* @param statusAssertion The encoded status assertion returned by {@link statusAssertion}
|
35
|
+
* @param context.credentialCryptoContext The crypto context used to obtain the credential in {@link obtainCredential}
|
36
|
+
* @returns A parsed status assertion
|
37
|
+
* @throws {IoWalletError} If the credential signature is not verified with the Issuer key set
|
38
|
+
* @throws {IssuerResponseError} If the status assertion contains an error or the credential status is invalid
|
39
|
+
*/
|
40
|
+
export const verifyAndParseStatusAssertion: VerifyAndParseStatusAssertion =
|
41
|
+
async (issuerConf, rawStatusAssertion, credential, format) => {
|
42
|
+
const { statusAssertion } = rawStatusAssertion;
|
43
|
+
|
44
|
+
await verify(
|
45
|
+
statusAssertion,
|
46
|
+
issuerConf.openid_credential_issuer.jwks.keys
|
47
|
+
);
|
48
|
+
|
49
|
+
const decodedJwt = decodeJwt(statusAssertion);
|
50
|
+
const parsedStatusAssertion = ParsedStatusAssertionResponse.parse({
|
51
|
+
header: decodedJwt.protectedHeader,
|
52
|
+
payload: decodedJwt.payload,
|
53
|
+
});
|
54
|
+
|
55
|
+
Logger.log(
|
56
|
+
LogLevel.DEBUG,
|
57
|
+
`Parsed status assertion: ${JSON.stringify(parsedStatusAssertion)}`
|
58
|
+
);
|
59
|
+
|
60
|
+
// Errors are transmitted in the JWT and use a 200 HTTP status code
|
61
|
+
if (isStatusAssertionError(parsedStatusAssertion)) {
|
62
|
+
throw new IssuerResponseError({
|
63
|
+
code: IssuerResponseErrorCodes.CredentialInvalidStatus,
|
64
|
+
message: "The status assertion contains an error",
|
65
|
+
statusCode: 200,
|
66
|
+
reason: buildErrorReason(parsedStatusAssertion),
|
67
|
+
});
|
68
|
+
}
|
69
|
+
|
70
|
+
const { cnf, credential_status_type } = parsedStatusAssertion.payload;
|
71
|
+
const holderBindingKey = await extractJwkFromCredential(credential, format);
|
72
|
+
|
73
|
+
if (!(await isSameThumbprint(cnf.jwk, holderBindingKey))) {
|
74
|
+
const errorMessage = `Failed to verify holder binding for status assertion: the thumbprints of keys ${cnf.jwk.kid} and ${holderBindingKey.kid} do not match`;
|
75
|
+
Logger.log(LogLevel.ERROR, errorMessage);
|
76
|
+
throw new IoWalletError(errorMessage);
|
77
|
+
}
|
78
|
+
|
79
|
+
if (credential_status_type !== StatusType.VALID) {
|
80
|
+
throw new IssuerResponseError({
|
81
|
+
code: IssuerResponseErrorCodes.CredentialInvalidStatus,
|
82
|
+
message: "Invalid status found for the given credential",
|
83
|
+
statusCode: 200,
|
84
|
+
reason: buildErrorReason(parsedStatusAssertion),
|
85
|
+
});
|
86
|
+
}
|
87
|
+
|
88
|
+
return { parsedStatusAssertion };
|
89
|
+
};
|
90
|
+
|
91
|
+
const isStatusAssertionError = (
|
92
|
+
assertion: ParsedStatusAssertionResponse
|
93
|
+
): assertion is ParsedStatusAssertionError =>
|
94
|
+
assertion.header.typ === "status-assertion-error+jwt";
|
95
|
+
|
96
|
+
/**
|
97
|
+
* Build an object containing the details on the error to use as the IssuerResponseError's reason
|
98
|
+
* @param assertion The status assertion response, both success or failure
|
99
|
+
* @returns The error's reason object
|
100
|
+
*/
|
101
|
+
const buildErrorReason = ({
|
102
|
+
payload,
|
103
|
+
}: ParsedStatusAssertionResponse): InvalidStatusErrorReason =>
|
104
|
+
"error" in payload
|
105
|
+
? payload
|
106
|
+
: {
|
107
|
+
error: payload.credential_status_detail!.state,
|
108
|
+
error_description: payload.credential_status_detail!.description,
|
109
|
+
};
|
@@ -1,16 +1,16 @@
|
|
1
|
-
# Credential Status
|
1
|
+
# Credential Status Assertion
|
2
2
|
|
3
|
-
This flow is used to obtain a credential status
|
4
|
-
The credential status
|
5
|
-
The status
|
3
|
+
This flow is used to obtain a credential status assertion from its credential issuer. Each step in the flow is imported from the related file which is named with a sequential number.
|
4
|
+
The credential status assertion is a JWT which contains the credential status which indicates if the credential is valid or not (see [OAuth Status Assertions](https://italia.github.io/eid-wallet-it-docs/versione-corrente/en/credential-revocation.html#oauth-status-assertions)).
|
5
|
+
The status assertion is supposed to be stored securely along with the credential. It has a limited lifetime and should be refreshed periodically according to the `exp` field in the JWT payload.
|
6
6
|
|
7
7
|
## Sequence Diagram
|
8
8
|
|
9
9
|
```mermaid
|
10
10
|
graph TD;
|
11
11
|
0[startFlow]
|
12
|
-
1[
|
13
|
-
2[
|
12
|
+
1[statusAssertion]
|
13
|
+
2[verifyAndParseStatusAssertion]
|
14
14
|
|
15
15
|
0 --> 1
|
16
16
|
1 --> 2
|
@@ -21,14 +21,14 @@ graph TD;
|
|
21
21
|
|
22
22
|
The following errors are mapped to a `IssuerResponseError` with specific codes.
|
23
23
|
|
24
|
-
|
|
25
|
-
|
26
|
-
|`
|
24
|
+
|Error Code|Description|
|
25
|
+
|----------|-----------|
|
26
|
+
|`ERR_CREDENTIAL_INVALID_STATUS`|This error is thrown when the status assertion for a given credential is invalid. It might contain more details in the `reason` property.|
|
27
27
|
|
28
28
|
## Example
|
29
29
|
|
30
30
|
<details>
|
31
|
-
<summary>Credential status
|
31
|
+
<summary>Credential status assertion flow</summary>
|
32
32
|
|
33
33
|
```ts
|
34
34
|
// Start the issuance flow
|
@@ -42,24 +42,26 @@ const { issuerUrl } = startFlow();
|
|
42
42
|
// Evaluate issuer trust
|
43
43
|
const { issuerConf } = await Credential.Status.evaluateIssuerTrust(issuerUrl);
|
44
44
|
|
45
|
-
// Get the credential
|
46
|
-
const res = await Credential.Status.
|
45
|
+
// Get the credential assertion
|
46
|
+
const res = await Credential.Status.statusAssertion(
|
47
47
|
issuerConf,
|
48
48
|
credential,
|
49
|
-
|
49
|
+
format,
|
50
|
+
{ credentialCryptoContext, wiaCryptoContext }
|
50
51
|
);
|
51
52
|
|
52
|
-
// Verify and parse the status
|
53
|
-
const {
|
54
|
-
await Credential.Status.
|
53
|
+
// Verify and parse the status assertion
|
54
|
+
const { parsedStatusAssertion } =
|
55
|
+
await Credential.Status.verifyAndParseStatusAssertion(
|
55
56
|
issuerConf,
|
56
|
-
res.
|
57
|
-
|
57
|
+
res.statusAssertion,
|
58
|
+
credential,
|
59
|
+
format
|
58
60
|
);
|
59
61
|
|
60
62
|
return {
|
61
|
-
|
62
|
-
|
63
|
+
statusAssertion: res.statusAssertion,
|
64
|
+
parsedStatusAssertion,
|
63
65
|
};
|
64
66
|
```
|
65
67
|
|
@@ -1,22 +1,15 @@
|
|
1
1
|
import { type StartFlow } from "./01-start-flow";
|
2
|
-
import {
|
3
|
-
statusAttestation,
|
4
|
-
type StatusAttestation,
|
5
|
-
} from "./02-status-attestation";
|
2
|
+
import { statusAssertion, type StatusAssertion } from "./02-status-assertion";
|
6
3
|
import { evaluateIssuerTrust, type EvaluateIssuerTrust } from "../issuance";
|
7
4
|
import {
|
8
|
-
|
9
|
-
type
|
10
|
-
} from "./03-verify-and-parse-status-
|
5
|
+
verifyAndParseStatusAssertion,
|
6
|
+
type VerifyAndParseStatusAssertion,
|
7
|
+
} from "./03-verify-and-parse-status-assertion";
|
11
8
|
|
12
|
-
export {
|
13
|
-
evaluateIssuerTrust,
|
14
|
-
statusAttestation,
|
15
|
-
verifyAndParseStatusAttestation,
|
16
|
-
};
|
9
|
+
export { evaluateIssuerTrust, statusAssertion, verifyAndParseStatusAssertion };
|
17
10
|
export type {
|
18
11
|
StartFlow,
|
19
12
|
EvaluateIssuerTrust,
|
20
|
-
|
21
|
-
|
13
|
+
StatusAssertion,
|
14
|
+
VerifyAndParseStatusAssertion,
|
22
15
|
};
|
@@ -3,35 +3,38 @@ import { JWK } from "../../utils/jwk";
|
|
3
3
|
import * as z from "zod";
|
4
4
|
|
5
5
|
/**
|
6
|
-
* Shape from parsing a status
|
6
|
+
* Shape from parsing a status assertion response in case of 201.
|
7
7
|
*/
|
8
|
-
export const
|
9
|
-
|
8
|
+
export const StatusAssertionResponse = z.object({
|
9
|
+
status_assertion_responses: z.array(z.string()),
|
10
10
|
});
|
11
11
|
|
12
12
|
/**
|
13
|
-
* Type from parsing a status
|
14
|
-
* Inferred from {@link
|
13
|
+
* Type from parsing a status assertion response in case of 201.
|
14
|
+
* Inferred from {@link StatusAssertionResponse}.
|
15
15
|
*/
|
16
|
-
export type
|
17
|
-
typeof StatusAttestationResponse
|
18
|
-
>;
|
16
|
+
export type StatusAssertionResponse = z.infer<typeof StatusAssertionResponse>;
|
19
17
|
|
20
|
-
|
21
|
-
* Type for a parsed status attestation.
|
22
|
-
*/
|
23
|
-
export type ParsedStatusAttestation = z.infer<typeof ParsedStatusAttestation>;
|
18
|
+
export type ParsedStatusAssertion = z.infer<typeof ParsedStatusAssertion>;
|
24
19
|
|
25
20
|
/**
|
26
|
-
* Shape for parsing a status
|
21
|
+
* Shape for parsing a successful status assertion in a JWT.
|
27
22
|
*/
|
28
|
-
export const
|
23
|
+
export const ParsedStatusAssertion = z.object({
|
29
24
|
header: z.object({
|
30
|
-
typ: z.literal("status-
|
25
|
+
typ: z.literal("status-assertion+jwt"),
|
31
26
|
alg: z.string(),
|
32
27
|
kid: z.string().optional(),
|
33
28
|
}),
|
34
29
|
payload: z.object({
|
30
|
+
iss: z.string(),
|
31
|
+
credential_status_type: z.string(),
|
32
|
+
credential_status_detail: z
|
33
|
+
.object({
|
34
|
+
state: z.string(),
|
35
|
+
description: z.string(),
|
36
|
+
})
|
37
|
+
.optional(),
|
35
38
|
credential_hash_alg: z.string(),
|
36
39
|
credential_hash: z.string(),
|
37
40
|
cnf: z.object({
|
@@ -41,3 +44,47 @@ export const ParsedStatusAttestation = z.object({
|
|
41
44
|
iat: UnixTime,
|
42
45
|
}),
|
43
46
|
});
|
47
|
+
|
48
|
+
export type ParsedStatusAssertionError = z.infer<
|
49
|
+
typeof ParsedStatusAssertionError
|
50
|
+
>;
|
51
|
+
|
52
|
+
/**
|
53
|
+
* The JWT that contains the errors occurred for the status assertion request.
|
54
|
+
* @see https://italia.github.io/eid-wallet-it-docs/versione-corrente/en/credential-revocation.html#http-status-assertion-response
|
55
|
+
*/
|
56
|
+
export const ParsedStatusAssertionError = z.object({
|
57
|
+
header: z.object({
|
58
|
+
typ: z.literal("status-assertion-error+jwt"),
|
59
|
+
alg: z.string(),
|
60
|
+
kid: z.string().optional(),
|
61
|
+
}),
|
62
|
+
payload: z.object({
|
63
|
+
credential_hash_alg: z.string(),
|
64
|
+
credential_hash: z.string(),
|
65
|
+
error: z.string(),
|
66
|
+
error_description: z.string(),
|
67
|
+
}),
|
68
|
+
});
|
69
|
+
|
70
|
+
/**
|
71
|
+
* The status assertion response that might include either a successful assertion or an error
|
72
|
+
*/
|
73
|
+
export type ParsedStatusAssertionResponse = z.infer<
|
74
|
+
typeof ParsedStatusAssertionResponse
|
75
|
+
>;
|
76
|
+
export const ParsedStatusAssertionResponse = z.union([
|
77
|
+
ParsedStatusAssertion,
|
78
|
+
ParsedStatusAssertionError,
|
79
|
+
]);
|
80
|
+
|
81
|
+
export enum StatusType {
|
82
|
+
VALID = "0x00",
|
83
|
+
INVALID = "0x01",
|
84
|
+
SUSPENDED = "0x02",
|
85
|
+
}
|
86
|
+
|
87
|
+
export type InvalidStatusErrorReason = {
|
88
|
+
error: string;
|
89
|
+
error_description: string;
|
90
|
+
};
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import { decode } from "../sd-jwt";
|
2
|
+
import { thumbprint } from "@pagopa/io-react-native-jwt";
|
3
|
+
import type { Out } from "./misc";
|
4
|
+
import type { ObtainCredential } from "../credential/issuance";
|
5
|
+
import type { JWK } from "./jwk";
|
6
|
+
import { IoWalletError } from "./errors";
|
7
|
+
|
8
|
+
const SD_JWT = ["vc+sd-jwt", "dc+sd-jwt"];
|
9
|
+
|
10
|
+
/**
|
11
|
+
* Extracts a JWK from a credential.
|
12
|
+
* @param credential - The credential string, which can be in SD-JWT or CBOR format.
|
13
|
+
* @param format - The format of the credential
|
14
|
+
* @return A Promise that resolves to a JWK object if the credential is in SD-JWT format and contains a JWK, or undefined otherwise.
|
15
|
+
*/
|
16
|
+
export const extractJwkFromCredential = async (
|
17
|
+
credential: Out<ObtainCredential>["credential"],
|
18
|
+
format: Out<ObtainCredential>["format"]
|
19
|
+
): Promise<JWK> => {
|
20
|
+
if (SD_JWT.includes(format)) {
|
21
|
+
// 1. SD-JWT case
|
22
|
+
const decoded = decode(credential);
|
23
|
+
const jwk = decoded.sdJwt.payload.cnf.jwk;
|
24
|
+
if (jwk) {
|
25
|
+
return { ...jwk, kid: await thumbprint(jwk) };
|
26
|
+
}
|
27
|
+
}
|
28
|
+
throw new IoWalletError(`Credential format ${format} not supported`);
|
29
|
+
};
|
package/src/utils/crypto.ts
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
import {
|
2
|
-
getPublicKey,
|
3
|
-
sign,
|
4
|
-
generate,
|
5
2
|
deleteKey,
|
3
|
+
generate,
|
4
|
+
getPublicKeyFixed,
|
5
|
+
sign,
|
6
6
|
} from "@pagopa/io-react-native-crypto";
|
7
7
|
import { v4 as uuidv4 } from "uuid";
|
8
|
-
import {
|
9
|
-
import { fixBase64EncodingOnKey } from "./jwk";
|
8
|
+
import { type CryptoContext, thumbprint } from "@pagopa/io-react-native-jwt";
|
10
9
|
|
11
10
|
/**
|
12
11
|
* Create a CryptoContext bound to a key pair.
|
@@ -17,22 +16,15 @@ import { fixBase64EncodingOnKey } from "./jwk";
|
|
17
16
|
*/
|
18
17
|
export const createCryptoContextFor = (keytag: string): CryptoContext => {
|
19
18
|
return {
|
20
|
-
/**
|
21
|
-
* Retrieve the public key of the pair.
|
22
|
-
* If the key pair doesn't exist yet, an error is raised
|
23
|
-
* @returns The public key.
|
24
|
-
*/
|
25
19
|
async getPublicKey() {
|
26
|
-
return
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
kid: await thumbprint(jwk),
|
35
|
-
}));
|
20
|
+
return getPublicKeyFixed(keytag).then(async (jwk) => ({
|
21
|
+
...jwk,
|
22
|
+
// Keys in the TEE are not stored with their KID, which is supposed to be assigned when they are included in JWK sets.
|
23
|
+
// (that is, KID is not a propoerty of the key itself, but it's property used to identify a key in a set).
|
24
|
+
// We assume the convention we use the thumbprint of the public key as KID, thus for easy development we decided to evaluate KID here
|
25
|
+
// However the values is an arbitrary string that might be anything
|
26
|
+
kid: await thumbprint(jwk),
|
27
|
+
}));
|
36
28
|
},
|
37
29
|
/**
|
38
30
|
* Get a signature for a provided value.
|
package/src/utils/jwk.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { decode, removePadding } from "@pagopa/io-react-native-jwt";
|
1
|
+
import { decode, removePadding, thumbprint } from "@pagopa/io-react-native-jwt";
|
2
2
|
import { z } from "zod";
|
3
3
|
|
4
4
|
export type JWK = z.infer<typeof JWK>;
|
@@ -65,3 +65,17 @@ export const JWKS = z.object({
|
|
65
65
|
});
|
66
66
|
|
67
67
|
export type JWTDecodeResult = ReturnType<typeof decode>;
|
68
|
+
|
69
|
+
/**
|
70
|
+
* Utility function that checks if two JWKs have the same thumbprint.
|
71
|
+
* @param jwkA The first JWK
|
72
|
+
* @param jwkB The second JWK
|
73
|
+
* @returns Whether the thumbprints match
|
74
|
+
*/
|
75
|
+
export const isSameThumbprint = async (jwkA: JWK, jwkB: JWK) => {
|
76
|
+
const [thumbprintJwkA, thumbprintJwkB] = await Promise.all([
|
77
|
+
thumbprint(jwkA),
|
78
|
+
thumbprint(jwkB),
|
79
|
+
]);
|
80
|
+
return thumbprintJwkA === thumbprintJwkB;
|
81
|
+
};
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"names":["_misc","require","_ioReactNativeJwt","_uuid","_types","_errors","_logging","statusAttestation","issuerConf","credential","credentialCryptoContext","appFetch","arguments","length","undefined","fetch","jwk","getPublicKey","credentialHash","getCredentialHashWithouDiscloures","statusAttUrl","openid_credential_issuer","status_attestation_endpoint","credentialPop","SignJWT","setPayload","aud","jti","uuidv4","toString","credential_hash","credential_hash_alg","setProtectedHeader","alg","typ","kid","setIssuedAt","setExpirationTime","sign","body","credential_pop","Logger","log","LogLevel","DEBUG","result","method","headers","JSON","stringify","then","hasStatusOrThrow","raw","json","StatusAttestationResponse","parse","catch","handleStatusAttestationError","status_attestation","exports","e","UnexpectedStatusCodeError","ResponseErrorBuilder","IssuerResponseError","handle","code","IssuerResponseErrorCodes","CredentialInvalidStatus","message","StatusAttestationRequestFailed","buildFrom"],"sourceRoot":"../../../../src","sources":["credential/status/02-status-attestation.ts"],"mappings":";;;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AAMA,IAAAC,iBAAA,GAAAD,OAAA;AACA,IAAAE,KAAA,GAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AACA,IAAAI,OAAA,GAAAJ,OAAA;AAMA,IAAAK,QAAA,GAAAL,OAAA;AAWA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMM,iBAAoC,GAAG,eAAAA,CAClDC,UAAU,EACVC,UAAU,EACVC,uBAAuB,EAEpB;EAAA,IADHC,QAA8B,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAGG,KAAK;EAEtC,MAAMC,GAAG,GAAG,MAAMN,uBAAuB,CAACO,YAAY,CAAC,CAAC;EACxD,MAAMC,cAAc,GAAG,MAAM,IAAAC,uCAAiC,EAACV,UAAU,CAAC;EAC1E,MAAMW,YAAY,GAChBZ,UAAU,CAACa,wBAAwB,CAACC,2BAA2B;EACjE,MAAMC,aAAa,GAAG,MAAM,IAAIC,yBAAO,CAACd,uBAAuB,CAAC,CAC7De,UAAU,CAAC;IACVC,GAAG,EAAEN,YAAY;IACjBO,GAAG,EAAE,IAAAC,QAAM,EAAC,CAAC,CAACC,QAAQ,CAAC,CAAC;IACxBC,eAAe,EAAEZ,cAAc;IAC/Ba,mBAAmB,EAAE;EACvB,CAAC,CAAC,CACDC,kBAAkB,CAAC;IAClBC,GAAG,EAAE,OAAO;IACZC,GAAG,EAAE,gCAAgC;IACrCC,GAAG,EAAEnB,GAAG,CAACmB;EACX,CAAC,CAAC,CACDC,WAAW,CAAC,CAAC,CACbC,iBAAiB,CAAC,IAAI,CAAC,CACvBC,IAAI,CAAC,CAAC;EAET,MAAMC,IAAI,GAAG;IACXC,cAAc,EAAEjB;EAClB,CAAC;EAEDkB,eAAM,CAACC,GAAG,CAACC,iBAAQ,CAACC,KAAK,EAAG,mBAAkBrB,aAAc,EAAC,CAAC;EAE9D,MAAMsB,MAAM,GAAG,MAAMlC,QAAQ,CAACS,YAAY,EAAE;IAC1C0B,MAAM,EAAE,MAAM;IACdC,OAAO,EAAE;MACP,cAAc,EAAE;IAClB,CAAC;IACDR,IAAI,EAAES,IAAI,CAACC,SAAS,CAACV,IAAI;EAC3B,CAAC,CAAC,CACCW,IAAI,CAAC,IAAAC,sBAAgB,EAAC,GAAG,CAAC,CAAC,CAC3BD,IAAI,CAAEE,GAAG,IAAKA,GAAG,CAACC,IAAI,CAAC,CAAC,CAAC,CACzBH,IAAI,CAAEG,IAAI,IAAKC,gCAAyB,CAACC,KAAK,CAACF,IAAI,CAAC,CAAC,CACrDG,KAAK,CAACC,4BAA4B,CAAC;EAEtC,OAAO;IAAElD,iBAAiB,EAAEsC,MAAM,CAACa;EAAmB,CAAC;AACzD,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AALAC,OAAA,CAAApD,iBAAA,GAAAA,iBAAA;AAMA,MAAMkD,4BAA4B,GAAIG,CAAU,IAAK;EACnD,IAAI,EAAEA,CAAC,YAAYC,iCAAyB,CAAC,EAAE;IAC7C,MAAMD,CAAC;EACT;EAEA,MAAM,IAAIE,4BAAoB,CAACC,2BAAmB,CAAC,CAChDC,MAAM,CAAC,GAAG,EAAE;IACXC,IAAI,EAAEC,gCAAwB,CAACC,uBAAuB;IACtDC,OAAO,EAAE;EACX,CAAC,CAAC,CACDJ,MAAM,CAAC,GAAG,EAAE;IACXC,IAAI,EAAEC,gCAAwB,CAACG,8BAA8B;IAC7DD,OAAO,EAAG;EACZ,CAAC,CAAC,CACDE,SAAS,CAACV,CAAC,CAAC;AACjB,CAAC"}
|
@@ -1,55 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.verifyAndParseStatusAttestation = void 0;
|
7
|
-
var _errors = require("../../utils/errors");
|
8
|
-
var _ioReactNativeJwt = require("@pagopa/io-react-native-jwt");
|
9
|
-
var _types = require("./types");
|
10
|
-
var _logging = require("../../utils/logging");
|
11
|
-
/**
|
12
|
-
* Given a status attestation, verifies that:
|
13
|
-
* - It's in the supported format;
|
14
|
-
* - The attestation is correctly signed;
|
15
|
-
* - It's bound to the given key.
|
16
|
-
* @param issuerConf The Issuer configuration returned by {@link evaluateIssuerTrust}
|
17
|
-
* @param statusAttestation The encoded status attestation returned by {@link statusAttestation}
|
18
|
-
* @param context.credentialCryptoContext The crypto context used to obtain the credential in {@link obtainCredential}
|
19
|
-
* @returns A parsed status attestation
|
20
|
-
* @throws {IoWalletError} If the credential signature is not verified with the Issuer key set
|
21
|
-
* @throws {IoWalletError} If the credential is not bound to the provided user key
|
22
|
-
* @throws {IoWalletError} If the credential data fail to parse
|
23
|
-
*/
|
24
|
-
const verifyAndParseStatusAttestation = async (issuerConf, rawStatusAttestation, context) => {
|
25
|
-
try {
|
26
|
-
const {
|
27
|
-
statusAttestation
|
28
|
-
} = rawStatusAttestation;
|
29
|
-
const {
|
30
|
-
credentialCryptoContext
|
31
|
-
} = context;
|
32
|
-
await (0, _ioReactNativeJwt.verify)(statusAttestation, issuerConf.openid_credential_issuer.jwks.keys);
|
33
|
-
const decodedJwt = (0, _ioReactNativeJwt.decode)(statusAttestation);
|
34
|
-
const parsedStatusAttestation = _types.ParsedStatusAttestation.parse({
|
35
|
-
header: decodedJwt.protectedHeader,
|
36
|
-
payload: decodedJwt.payload
|
37
|
-
});
|
38
|
-
_logging.Logger.log(_logging.LogLevel.DEBUG, `Parsed status attestation: ${JSON.stringify(parsedStatusAttestation)}`);
|
39
|
-
const holderBindingKey = await credentialCryptoContext.getPublicKey();
|
40
|
-
const {
|
41
|
-
cnf
|
42
|
-
} = parsedStatusAttestation.payload;
|
43
|
-
if (!cnf.jwk.kid || cnf.jwk.kid !== holderBindingKey.kid) {
|
44
|
-
_logging.Logger.log(_logging.LogLevel.ERROR, `Failed to verify holder binding for status attestation, expected kid: ${holderBindingKey.kid}, got: ${parsedStatusAttestation.payload.cnf.jwk.kid}`);
|
45
|
-
throw new _errors.IoWalletError(`Failed to verify holder binding for status attestation, expected kid: ${holderBindingKey.kid}, got: ${parsedStatusAttestation.payload.cnf.jwk.kid}`);
|
46
|
-
}
|
47
|
-
return {
|
48
|
-
parsedStatusAttestation
|
49
|
-
};
|
50
|
-
} catch (e) {
|
51
|
-
throw new _errors.IoWalletError(`Failed to verify status attestation: ${JSON.stringify(e)}`);
|
52
|
-
}
|
53
|
-
};
|
54
|
-
exports.verifyAndParseStatusAttestation = verifyAndParseStatusAttestation;
|
55
|
-
//# sourceMappingURL=03-verify-and-parse-status-attestation.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"names":["_errors","require","_ioReactNativeJwt","_types","_logging","verifyAndParseStatusAttestation","issuerConf","rawStatusAttestation","context","statusAttestation","credentialCryptoContext","verify","openid_credential_issuer","jwks","keys","decodedJwt","decodeJwt","parsedStatusAttestation","ParsedStatusAttestation","parse","header","protectedHeader","payload","Logger","log","LogLevel","DEBUG","JSON","stringify","holderBindingKey","getPublicKey","cnf","jwk","kid","ERROR","IoWalletError","e","exports"],"sourceRoot":"../../../../src","sources":["credential/status/03-verify-and-parse-status-attestation.ts"],"mappings":";;;;;;AACA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,iBAAA,GAAAD,OAAA;AAEA,IAAAE,MAAA,GAAAF,OAAA;AAEA,IAAAG,QAAA,GAAAH,OAAA;AAUA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMI,+BAAgE,GAC3E,MAAAA,CAAOC,UAAU,EAAEC,oBAAoB,EAAEC,OAAO,KAAK;EACnD,IAAI;IACF,MAAM;MAAEC;IAAkB,CAAC,GAAGF,oBAAoB;IAClD,MAAM;MAAEG;IAAwB,CAAC,GAAGF,OAAO;IAE3C,MAAM,IAAAG,wBAAM,EACVF,iBAAiB,EACjBH,UAAU,CAACM,wBAAwB,CAACC,IAAI,CAACC,IAC3C,CAAC;IAED,MAAMC,UAAU,GAAG,IAAAC,wBAAS,EAACP,iBAAiB,CAAC;IAC/C,MAAMQ,uBAAuB,GAAGC,8BAAuB,CAACC,KAAK,CAAC;MAC5DC,MAAM,EAAEL,UAAU,CAACM,eAAe;MAClCC,OAAO,EAAEP,UAAU,CAACO;IACtB,CAAC,CAAC;IAEFC,eAAM,CAACC,GAAG,CACRC,iBAAQ,CAACC,KAAK,EACb,8BAA6BC,IAAI,CAACC,SAAS,CAACX,uBAAuB,CAAE,EACxE,CAAC;IAED,MAAMY,gBAAgB,GAAG,MAAMnB,uBAAuB,CAACoB,YAAY,CAAC,CAAC;IACrE,MAAM;MAAEC;IAAI,CAAC,GAAGd,uBAAuB,CAACK,OAAO;IAC/C,IAAI,CAACS,GAAG,CAACC,GAAG,CAACC,GAAG,IAAIF,GAAG,CAACC,GAAG,CAACC,GAAG,KAAKJ,gBAAgB,CAACI,GAAG,EAAE;MACxDV,eAAM,CAACC,GAAG,CACRC,iBAAQ,CAACS,KAAK,EACb,yEAAwEL,gBAAgB,CAACI,GAAI,UAAShB,uBAAuB,CAACK,OAAO,CAACS,GAAG,CAACC,GAAG,CAACC,GAAI,EACrJ,CAAC;MACD,MAAM,IAAIE,qBAAa,CACpB,yEAAwEN,gBAAgB,CAACI,GAAI,UAAShB,uBAAuB,CAACK,OAAO,CAACS,GAAG,CAACC,GAAG,CAACC,GAAI,EACrJ,CAAC;IACH;IAEA,OAAO;MAAEhB;IAAwB,CAAC;EACpC,CAAC,CAAC,OAAOmB,CAAC,EAAE;IACV,MAAM,IAAID,qBAAa,CACpB,wCAAuCR,IAAI,CAACC,SAAS,CAACQ,CAAC,CAAE,EAC5D,CAAC;EACH;AACF,CAAC;AAACC,OAAA,CAAAhC,+BAAA,GAAAA,+BAAA"}
|