@pagopa/io-react-native-wallet 0.11.1 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/commonjs/client/generated/wallet-provider.js +126 -0
- package/lib/commonjs/client/generated/wallet-provider.js.map +1 -0
- package/lib/commonjs/client/index.js +40 -0
- package/lib/commonjs/client/index.js.map +1 -0
- package/lib/commonjs/credential/issuance/02-evaluate-issuer-trust.js +2 -1
- package/lib/commonjs/credential/issuance/02-evaluate-issuer-trust.js.map +1 -1
- package/lib/commonjs/credential/issuance/03-start-credential-issuance.js +287 -0
- package/lib/commonjs/credential/issuance/03-start-credential-issuance.js.map +1 -0
- package/lib/commonjs/credential/issuance/03-start-user-authorization.js +56 -83
- package/lib/commonjs/credential/issuance/03-start-user-authorization.js.map +1 -1
- package/lib/commonjs/credential/issuance/04-complete-user-authorization.js +88 -0
- package/lib/commonjs/credential/issuance/04-complete-user-authorization.js.map +1 -1
- package/lib/commonjs/credential/issuance/05-authorize-access.js +56 -33
- package/lib/commonjs/credential/issuance/05-authorize-access.js.map +1 -1
- package/lib/commonjs/credential/issuance/06-obtain-credential.js +51 -78
- package/lib/commonjs/credential/issuance/06-obtain-credential.js.map +1 -1
- package/lib/commonjs/credential/issuance/07-verify-and-parse-credential.js +21 -44
- package/lib/commonjs/credential/issuance/07-verify-and-parse-credential.js.map +1 -1
- package/lib/commonjs/credential/issuance/index.js +7 -0
- package/lib/commonjs/credential/issuance/index.js.map +1 -1
- package/lib/commonjs/credential/issuance/types.js +28 -0
- package/lib/commonjs/credential/issuance/types.js.map +1 -0
- package/lib/commonjs/index.js +10 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/pid/sd-jwt/converters.js +5 -9
- package/lib/commonjs/pid/sd-jwt/converters.js.map +1 -1
- package/lib/commonjs/pid/sd-jwt/types.js +3 -3
- package/lib/commonjs/pid/sd-jwt/types.js.map +1 -1
- package/lib/commonjs/sd-jwt/__test__/converters.test.js +1 -1
- package/lib/commonjs/sd-jwt/__test__/converters.test.js.map +1 -1
- package/lib/commonjs/sd-jwt/__test__/index.test.js +30 -43
- package/lib/commonjs/sd-jwt/__test__/index.test.js.map +1 -1
- package/lib/commonjs/sd-jwt/__test__/types.test.js +16 -24
- package/lib/commonjs/sd-jwt/__test__/types.test.js.map +1 -1
- package/lib/commonjs/sd-jwt/index.js +3 -9
- package/lib/commonjs/sd-jwt/index.js.map +1 -1
- package/lib/commonjs/sd-jwt/types.js +11 -16
- package/lib/commonjs/sd-jwt/types.js.map +1 -1
- package/lib/commonjs/trust/types.js +70 -29
- package/lib/commonjs/trust/types.js.map +1 -1
- package/lib/commonjs/utils/auth.js +44 -0
- package/lib/commonjs/utils/auth.js.map +1 -0
- package/lib/commonjs/utils/errors.js +104 -1
- package/lib/commonjs/utils/errors.js.map +1 -1
- package/lib/commonjs/utils/integrity.js +2 -0
- package/lib/commonjs/utils/integrity.js.map +1 -0
- package/lib/commonjs/utils/misc.js +34 -1
- package/lib/commonjs/utils/misc.js.map +1 -1
- package/lib/commonjs/utils/par.js +23 -15
- package/lib/commonjs/utils/par.js.map +1 -1
- package/lib/commonjs/utils/pop.js +33 -0
- package/lib/commonjs/utils/pop.js.map +1 -0
- package/lib/commonjs/wallet-instance/index.js +29 -0
- package/lib/commonjs/wallet-instance/index.js.map +1 -0
- package/lib/commonjs/wallet-instance-attestation/issuing.js +62 -65
- package/lib/commonjs/wallet-instance-attestation/issuing.js.map +1 -1
- package/lib/commonjs/wallet-instance-attestation/types.js +8 -8
- package/lib/commonjs/wallet-instance-attestation/types.js.map +1 -1
- package/lib/module/client/generated/wallet-provider.js +102 -0
- package/lib/module/client/generated/wallet-provider.js.map +1 -0
- package/lib/module/client/index.js +33 -0
- package/lib/module/client/index.js.map +1 -0
- package/lib/module/credential/issuance/02-evaluate-issuer-trust.js +2 -1
- package/lib/module/credential/issuance/02-evaluate-issuer-trust.js.map +1 -1
- package/lib/module/credential/issuance/03-start-credential-issuance.js +276 -0
- package/lib/module/credential/issuance/03-start-credential-issuance.js.map +1 -0
- package/lib/module/credential/issuance/03-start-user-authorization.js +56 -80
- package/lib/module/credential/issuance/03-start-user-authorization.js.map +1 -1
- package/lib/module/credential/issuance/04-complete-user-authorization.js +85 -1
- package/lib/module/credential/issuance/04-complete-user-authorization.js.map +1 -1
- package/lib/module/credential/issuance/05-authorize-access.js +54 -33
- package/lib/module/credential/issuance/05-authorize-access.js.map +1 -1
- package/lib/module/credential/issuance/06-obtain-credential.js +50 -75
- package/lib/module/credential/issuance/06-obtain-credential.js.map +1 -1
- package/lib/module/credential/issuance/07-verify-and-parse-credential.js +21 -44
- package/lib/module/credential/issuance/07-verify-and-parse-credential.js.map +1 -1
- package/lib/module/credential/issuance/index.js +2 -1
- package/lib/module/credential/issuance/index.js.map +1 -1
- package/lib/module/credential/issuance/types.js +18 -0
- package/lib/module/credential/issuance/types.js.map +1 -0
- package/lib/module/index.js +3 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/pid/sd-jwt/converters.js +5 -9
- package/lib/module/pid/sd-jwt/converters.js.map +1 -1
- package/lib/module/pid/sd-jwt/types.js +3 -3
- package/lib/module/pid/sd-jwt/types.js.map +1 -1
- package/lib/module/sd-jwt/__test__/converters.test.js +1 -1
- package/lib/module/sd-jwt/__test__/converters.test.js.map +1 -1
- package/lib/module/sd-jwt/__test__/index.test.js +30 -43
- package/lib/module/sd-jwt/__test__/index.test.js.map +1 -1
- package/lib/module/sd-jwt/__test__/types.test.js +16 -24
- package/lib/module/sd-jwt/__test__/types.test.js.map +1 -1
- package/lib/module/sd-jwt/index.js +3 -9
- package/lib/module/sd-jwt/index.js.map +1 -1
- package/lib/module/sd-jwt/types.js +11 -16
- package/lib/module/sd-jwt/types.js.map +1 -1
- package/lib/module/trust/types.js +70 -29
- package/lib/module/trust/types.js.map +1 -1
- package/lib/module/utils/auth.js +35 -0
- package/lib/module/utils/auth.js.map +1 -0
- package/lib/module/utils/errors.js +98 -0
- package/lib/module/utils/errors.js.map +1 -1
- package/lib/module/utils/integrity.js +2 -0
- package/lib/module/utils/integrity.js.map +1 -0
- package/lib/module/utils/misc.js +31 -0
- package/lib/module/utils/misc.js.map +1 -1
- package/lib/module/utils/par.js +24 -16
- package/lib/module/utils/par.js.map +1 -1
- package/lib/module/utils/pop.js +24 -0
- package/lib/module/utils/pop.js.map +1 -0
- package/lib/module/wallet-instance/index.js +23 -0
- package/lib/module/wallet-instance/index.js.map +1 -0
- package/lib/module/wallet-instance-attestation/issuing.js +63 -67
- package/lib/module/wallet-instance-attestation/issuing.js.map +1 -1
- package/lib/module/wallet-instance-attestation/types.js +8 -8
- package/lib/module/wallet-instance-attestation/types.js.map +1 -1
- package/lib/typescript/client/generated/wallet-provider.d.ts +264 -0
- package/lib/typescript/client/generated/wallet-provider.d.ts.map +1 -0
- package/lib/typescript/client/index.d.ts +7 -0
- package/lib/typescript/client/index.d.ts.map +1 -0
- package/lib/typescript/credential/issuance/01-start-flow.d.ts +1 -0
- package/lib/typescript/credential/issuance/01-start-flow.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/02-evaluate-issuer-trust.d.ts +2 -1
- package/lib/typescript/credential/issuance/02-evaluate-issuer-trust.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/03-start-credential-issuance.d.ts +41 -0
- package/lib/typescript/credential/issuance/03-start-credential-issuance.d.ts.map +1 -0
- package/lib/typescript/credential/issuance/03-start-user-authorization.d.ts +23 -18
- 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 +24 -12
- package/lib/typescript/credential/issuance/04-complete-user-authorization.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/05-authorize-access.d.ts +22 -16
- package/lib/typescript/credential/issuance/05-authorize-access.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/06-obtain-credential.d.ts +19 -26
- package/lib/typescript/credential/issuance/06-obtain-credential.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/07-verify-and-parse-credential.d.ts +10 -15
- package/lib/typescript/credential/issuance/07-verify-and-parse-credential.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/index.d.ts +3 -4
- package/lib/typescript/credential/issuance/index.d.ts.map +1 -1
- package/lib/typescript/credential/issuance/types.d.ts +63 -0
- package/lib/typescript/credential/issuance/types.d.ts.map +1 -0
- package/lib/typescript/credential/presentation/types.d.ts +6 -6
- package/lib/typescript/index.d.ts +6 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/pid/sd-jwt/converters.d.ts.map +1 -1
- package/lib/typescript/pid/sd-jwt/types.d.ts +36 -36
- package/lib/typescript/pid/sd-jwt/types.d.ts.map +1 -1
- package/lib/typescript/sd-jwt/index.d.ts +40 -68
- package/lib/typescript/sd-jwt/index.d.ts.map +1 -1
- package/lib/typescript/sd-jwt/types.d.ts +64 -121
- package/lib/typescript/sd-jwt/types.d.ts.map +1 -1
- package/lib/typescript/trust/index.d.ts +150 -48
- package/lib/typescript/trust/index.d.ts.map +1 -1
- package/lib/typescript/trust/types.d.ts +2838 -1740
- package/lib/typescript/trust/types.d.ts.map +1 -1
- package/lib/typescript/utils/auth.d.ts +52 -0
- package/lib/typescript/utils/auth.d.ts.map +1 -0
- package/lib/typescript/utils/errors.d.ts +48 -0
- package/lib/typescript/utils/errors.d.ts.map +1 -1
- package/lib/typescript/utils/integrity.d.ts +21 -0
- package/lib/typescript/utils/integrity.d.ts.map +1 -0
- package/lib/typescript/utils/misc.d.ts +18 -0
- package/lib/typescript/utils/misc.d.ts.map +1 -1
- package/lib/typescript/utils/par.d.ts +8 -31
- package/lib/typescript/utils/par.d.ts.map +1 -1
- package/lib/typescript/utils/pop.d.ts +26 -0
- package/lib/typescript/utils/pop.d.ts.map +1 -0
- package/lib/typescript/wallet-instance/index.d.ts +7 -0
- package/lib/typescript/wallet-instance/index.d.ts.map +1 -0
- package/lib/typescript/wallet-instance-attestation/issuing.d.ts +17 -4
- package/lib/typescript/wallet-instance-attestation/issuing.d.ts.map +1 -1
- package/lib/typescript/wallet-instance-attestation/types.d.ts +64 -64
- package/lib/typescript/wallet-instance-attestation/types.d.ts.map +1 -1
- package/package.json +9 -5
- package/src/client/generated/wallet-provider.ts +173 -0
- package/src/client/index.ts +53 -0
- package/src/credential/issuance/01-start-flow.ts +1 -0
- package/src/credential/issuance/02-evaluate-issuer-trust.ts +2 -1
- package/src/credential/issuance/03-start-credential-issuance.ts +407 -0
- package/src/credential/issuance/03-start-user-authorization.ts +91 -92
- package/src/credential/issuance/04-complete-user-authorization.ts +114 -13
- package/src/credential/issuance/05-authorize-access.ts +74 -49
- package/src/credential/issuance/06-obtain-credential.ts +77 -111
- package/src/credential/issuance/07-verify-and-parse-credential.ts +30 -67
- package/src/credential/issuance/index.ts +6 -4
- package/src/credential/issuance/types.ts +25 -0
- package/src/index.ts +8 -0
- package/src/pid/sd-jwt/converters.ts +5 -11
- package/src/pid/sd-jwt/types.ts +8 -6
- package/src/sd-jwt/__test__/converters.test.ts +1 -1
- package/src/sd-jwt/__test__/index.test.ts +45 -74
- package/src/sd-jwt/__test__/types.test.ts +21 -33
- package/src/sd-jwt/index.ts +3 -12
- package/src/sd-jwt/types.ts +17 -22
- package/src/trust/types.ts +64 -32
- package/src/utils/auth.ts +37 -0
- package/src/utils/errors.ts +112 -0
- package/src/utils/integrity.ts +23 -0
- package/src/utils/misc.ts +43 -0
- package/src/utils/par.ts +29 -17
- package/src/utils/pop.ts +34 -0
- package/src/wallet-instance/index.ts +29 -0
- package/src/wallet-instance-attestation/issuing.ts +101 -97
- package/src/wallet-instance-attestation/types.ts +12 -8
- package/lib/commonjs/credential/issuance/07-confirm-credential.js +0 -6
- package/lib/commonjs/credential/issuance/07-confirm-credential.js.map +0 -1
- package/lib/commonjs/credential/issuance/08-confirm-credential.js +0 -6
- package/lib/commonjs/credential/issuance/08-confirm-credential.js.map +0 -1
- package/lib/module/credential/issuance/07-confirm-credential.js +0 -2
- package/lib/module/credential/issuance/07-confirm-credential.js.map +0 -1
- package/lib/module/credential/issuance/08-confirm-credential.js +0 -2
- package/lib/module/credential/issuance/08-confirm-credential.js.map +0 -1
- package/lib/typescript/credential/issuance/07-confirm-credential.d.ts +0 -11
- package/lib/typescript/credential/issuance/07-confirm-credential.d.ts.map +0 -1
- package/lib/typescript/credential/issuance/08-confirm-credential.d.ts +0 -11
- package/lib/typescript/credential/issuance/08-confirm-credential.d.ts.map +0 -1
- package/src/credential/issuance/07-confirm-credential.ts +0 -14
- package/src/credential/issuance/08-confirm-credential.ts +0 -14
@@ -1,17 +1,118 @@
|
|
1
|
-
import
|
1
|
+
import {
|
2
|
+
AuthorizationErrorShape,
|
3
|
+
AuthorizationResultShape,
|
4
|
+
type AuthorizationContext,
|
5
|
+
type AuthorizationResult,
|
6
|
+
} from "../../../src/utils/auth";
|
7
|
+
import { until, type Out } from "../../utils/misc";
|
2
8
|
import type { StartUserAuthorization } from "./03-start-user-authorization";
|
9
|
+
import parseUrl from "parse-url";
|
10
|
+
import { AuthorizationError, AuthorizationIdpError } from "../../utils/errors";
|
11
|
+
import type { EvaluateIssuerTrust } from "./02-evaluate-issuer-trust";
|
12
|
+
import { Linking } from "react-native";
|
3
13
|
|
4
14
|
/**
|
5
|
-
* The interface of the phase to complete User authorization.
|
6
|
-
* It may be implemented as a Credential presentation
|
7
|
-
* or with a strong User identification
|
8
|
-
*
|
9
|
-
* @param requestUri The url to reach to complete the user authorization.
|
10
|
-
* @param cliendId Identifies the current client across all the requests of the issuing flow
|
11
|
-
*
|
12
|
-
* @returns the access code to use to request the credental
|
15
|
+
* The interface of the phase to complete User authorization via strong identification when the response mode is "query" and the request credential is a PersonIdentificationData.
|
13
16
|
*/
|
14
|
-
export type
|
15
|
-
|
16
|
-
clientId: Out<StartUserAuthorization>["clientId"]
|
17
|
-
|
17
|
+
export type CompleteUserAuthorizationWithQueryMode = (
|
18
|
+
issuerRequestUri: Out<StartUserAuthorization>["issuerRequestUri"],
|
19
|
+
clientId: Out<StartUserAuthorization>["clientId"],
|
20
|
+
issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
|
21
|
+
idpHint: string,
|
22
|
+
redirectUri: string,
|
23
|
+
authorizationContext?: AuthorizationContext
|
24
|
+
) => Promise<AuthorizationResult>;
|
25
|
+
|
26
|
+
/**
|
27
|
+
* WARNING: This function must be called after {@link startUserAuthorization}. The next function to be called is {@link authorizeAccess}.
|
28
|
+
* The interface of the phase to complete User authorization via strong identification when the response mode is "query" and the request credential is a PersonIdentificationData.
|
29
|
+
* It is used to complete the user authorization by catching the redirectSchema from the authorization server which then contains the authorization response.
|
30
|
+
* This function utilizes the authorization context to open an in-app browser capable of catching the redirectSchema to perform a get request to the authorization endpoint.
|
31
|
+
* If the 302 redirect happens and the redirectSchema is caught, the function will return the authorization response after parsing it from the query string.
|
32
|
+
* @param issuerRequestUri the URI of the issuer where the request is sent
|
33
|
+
* @param clientId Identifies the current client across all the requests of the issuing flow returned by {@link startUserAuthorization}
|
34
|
+
* @param issuerConf The issuer configuration returned by {@link evaluateIssuerTrust}
|
35
|
+
* @param authorizationContext The context to identify the user which will be used to start the authorization. It's needed only when requesting a PersonalIdentificationData credential. The implementantion should open an in-app browser capable of catching the redirectSchema.
|
36
|
+
* If not specified, the default browser is used
|
37
|
+
* @param idphint Unique identifier of the SPID IDP selected by the user
|
38
|
+
* @param redirectUri The url to reach to complete the user authorization which is the custom URL scheme that the Wallet Instance is registered to handle, usually a custom URL or deeplink
|
39
|
+
* @throws {AuthorizationError} if an error occurs during the authorization process
|
40
|
+
* @throws {AuthorizationIdpError} if an error occurs during the authorization process and the error is related to the IDP
|
41
|
+
* @returns the authorization response which contains code, state and iss
|
42
|
+
*/
|
43
|
+
export const completeUserAuthorizationWithQueryMode: CompleteUserAuthorizationWithQueryMode =
|
44
|
+
async (
|
45
|
+
issuerRequestUri,
|
46
|
+
clientId,
|
47
|
+
issuerConf,
|
48
|
+
idpHint,
|
49
|
+
redirectUri,
|
50
|
+
authorizationContext
|
51
|
+
) => {
|
52
|
+
/**
|
53
|
+
* Starts the authorization flow which dependes on the response mode and the request credential.
|
54
|
+
* If the response mode is "query" the authorization flow is handled differently via the authorization context which opens an in-app browser capable of catching the redirectSchema.
|
55
|
+
* The form_post.jwt mode is not currently supported.
|
56
|
+
*/
|
57
|
+
const authzRequestEndpoint =
|
58
|
+
issuerConf.oauth_authorization_server.authorization_endpoint;
|
59
|
+
const params = new URLSearchParams({
|
60
|
+
client_id: clientId,
|
61
|
+
request_uri: issuerRequestUri,
|
62
|
+
idphint: idpHint,
|
63
|
+
});
|
64
|
+
const authUrl = `${authzRequestEndpoint}?${params}`;
|
65
|
+
var authRedirectUrl: string | undefined;
|
66
|
+
|
67
|
+
if (authorizationContext) {
|
68
|
+
const redirectSchema = new URL(redirectUri).protocol.replace(":", "");
|
69
|
+
authRedirectUrl = await authorizationContext
|
70
|
+
.authorize(authUrl, redirectSchema)
|
71
|
+
.catch((e) => {
|
72
|
+
throw new AuthorizationError(e.message);
|
73
|
+
});
|
74
|
+
} else {
|
75
|
+
// handler for redirectUri
|
76
|
+
Linking.addEventListener("url", ({ url }) => {
|
77
|
+
if (url.includes(redirectUri)) {
|
78
|
+
authRedirectUrl = url;
|
79
|
+
}
|
80
|
+
});
|
81
|
+
|
82
|
+
const openAuthUrlInBrowser = Linking.openURL(authUrl);
|
83
|
+
|
84
|
+
/*
|
85
|
+
* Waits for 120 seconds for the identificationRedirectUrl variable to be set
|
86
|
+
* by the custom url handler. If the timeout is exceeded, throw an exception
|
87
|
+
*/
|
88
|
+
const unitAuthRedirectIsNotUndefined = until(
|
89
|
+
() => authRedirectUrl !== undefined,
|
90
|
+
120
|
91
|
+
);
|
92
|
+
|
93
|
+
await Promise.all([openAuthUrlInBrowser, unitAuthRedirectIsNotUndefined]);
|
94
|
+
|
95
|
+
if (authRedirectUrl === undefined) {
|
96
|
+
throw new AuthorizationError("Invalid authentication redirect url");
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
const urlParse = parseUrl(authRedirectUrl);
|
101
|
+
const authRes = AuthorizationResultShape.safeParse(urlParse.query);
|
102
|
+
if (!authRes.success) {
|
103
|
+
const authErr = AuthorizationErrorShape.safeParse(urlParse.query);
|
104
|
+
if (!authErr.success) {
|
105
|
+
throw new AuthorizationError(authRes.error.message); // an error occured while parsing the result and the error
|
106
|
+
}
|
107
|
+
throw new AuthorizationIdpError(
|
108
|
+
authErr.data.error,
|
109
|
+
authErr.data.error_description
|
110
|
+
);
|
111
|
+
}
|
112
|
+
return authRes.data;
|
113
|
+
};
|
114
|
+
|
115
|
+
// TODO: SIW-1120 implement generic credential issuance flow
|
116
|
+
export const completeUserAuthorizationWithFormPostJwtMode = () => {
|
117
|
+
throw new Error("Not implemented");
|
118
|
+
};
|
@@ -1,92 +1,117 @@
|
|
1
|
-
import
|
2
|
-
import { withEphemeralKey } from "../../utils/crypto";
|
3
|
-
import { createDPopToken } from "../../utils/dpop";
|
4
|
-
import type { StartUserAuthorization } from "./03-start-user-authorization";
|
5
|
-
import { hasStatus, type Out } from "../../utils/misc";
|
1
|
+
import { hasStatus, type Out } from "../../../src/utils/misc";
|
6
2
|
import type { EvaluateIssuerTrust } from "./02-evaluate-issuer-trust";
|
3
|
+
import type { StartUserAuthorization } from "./03-start-user-authorization";
|
4
|
+
import { withEphemeralKey } from "../../../src/utils/crypto";
|
5
|
+
import { createDPopToken } from "../../../src/utils/dpop";
|
6
|
+
import uuid from "react-native-uuid";
|
7
|
+
import { createPopToken } from "../../../src/utils/pop";
|
8
|
+
import * as WalletInstanceAttestation from "../../wallet-instance-attestation";
|
9
|
+
import type { CryptoContext } from "@pagopa/io-react-native-jwt";
|
7
10
|
import { ASSERTION_TYPE } from "./const";
|
8
|
-
import
|
11
|
+
import { TokenResponse } from "./types";
|
12
|
+
import { ValidationFailed } from "../../../src/utils/errors";
|
13
|
+
import type { CompleteUserAuthorizationWithQueryMode } from "./04-complete-user-authorization";
|
9
14
|
|
10
15
|
export type AuthorizeAccess = (
|
11
16
|
issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
|
12
|
-
code: Out<
|
17
|
+
code: Out<CompleteUserAuthorizationWithQueryMode>["code"],
|
18
|
+
redirectUri: string,
|
13
19
|
clientId: Out<StartUserAuthorization>["clientId"],
|
20
|
+
codeVerifier: Out<StartUserAuthorization>["codeVerifier"],
|
14
21
|
context: {
|
15
22
|
walletInstanceAttestation: string;
|
16
|
-
walletProviderBaseUrl: string;
|
17
23
|
appFetch?: GlobalFetch["fetch"];
|
24
|
+
wiaCryptoContext: CryptoContext;
|
18
25
|
}
|
19
|
-
) => Promise<{
|
20
|
-
// The access token to grant access to the credential
|
21
|
-
accessToken: string;
|
22
|
-
// The nonce, to prevent reply attacks
|
23
|
-
nonce: string;
|
24
|
-
// Same as input
|
25
|
-
clientId: string;
|
26
|
-
}>;
|
26
|
+
) => Promise<{ accessToken: TokenResponse; tokenRequestSignedDPop: string }>;
|
27
27
|
|
28
28
|
/**
|
29
|
-
*
|
30
|
-
*
|
31
|
-
*
|
32
|
-
*
|
33
|
-
* @param
|
34
|
-
* @param
|
35
|
-
* @param
|
29
|
+
* Creates and sends the DPoP Proof JWT to be presented with the authorization code to the /token endpoint of the authorization server
|
30
|
+
* for requesting the issuance of an access token bound to the public key of the Wallet Instance contained within the DPoP.
|
31
|
+
* This enables the Wallet Instance to request a digital credential.
|
32
|
+
* The DPoP Proof JWT is generated according to the section 4.3 of the DPoP RFC 9449 specification.
|
33
|
+
* @param issuerConf The issuer configuration returned by {@link evaluateIssuerTrust}
|
34
|
+
* @param code The authorization code returned by {@link completeUserAuthorizationWithQueryMode} or {@link completeUserAuthorizationWithFormPost}
|
35
|
+
* @param redirectUri The redirect URI which is the custom URL scheme that the Wallet Instance is registered to handle
|
36
|
+
* @param clientId The client id returned by {@link startUserAuthorization}
|
37
|
+
* @param codeVerifier The code verifier returned by {@link startUserAuthorization}
|
38
|
+
* @param context.walletInstanceAttestation The Wallet Instance's attestation
|
39
|
+
* @param context.wiaCryptoContext The Wallet Instance's crypto context
|
36
40
|
* @param context.appFetch (optional) fetch api implementation. Default: built-in fetch
|
37
|
-
* @
|
41
|
+
* @throws {ValidationFailed} if an error occurs while parsing the token response
|
42
|
+
* @return The token response containing the access token along with the token request signed with DPoP which has to be used in the {@link obtainCredential} step.
|
38
43
|
*/
|
39
44
|
export const authorizeAccess: AuthorizeAccess = async (
|
40
45
|
issuerConf,
|
41
46
|
code,
|
42
47
|
clientId,
|
48
|
+
redirectUri,
|
49
|
+
codeVerifier,
|
43
50
|
context
|
44
|
-
)
|
51
|
+
) => {
|
45
52
|
const {
|
46
53
|
appFetch = fetch,
|
47
54
|
walletInstanceAttestation,
|
48
|
-
|
55
|
+
wiaCryptoContext,
|
49
56
|
} = context;
|
50
57
|
|
51
|
-
const
|
58
|
+
const parEndpoint =
|
59
|
+
issuerConf.oauth_authorization_server.pushed_authorization_request_endpoint;
|
60
|
+
const parUrl = new URL(parEndpoint);
|
61
|
+
const aud = `${parUrl.protocol}//${parUrl.hostname}`;
|
62
|
+
const iss = WalletInstanceAttestation.decode(walletInstanceAttestation)
|
63
|
+
.payload.cnf.jwk.kid;
|
52
64
|
|
65
|
+
const tokenUrl = issuerConf.oauth_authorization_server.token_endpoint;
|
53
66
|
// Use an ephemeral key to be destroyed after use
|
54
|
-
const
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
67
|
+
const tokenRequestSignedDPop = await withEphemeralKey(
|
68
|
+
async (ephimeralContext) => {
|
69
|
+
return await createDPopToken(
|
70
|
+
{
|
71
|
+
htm: "POST",
|
72
|
+
htu: tokenUrl,
|
73
|
+
jti: `${uuid.v4()}`,
|
74
|
+
},
|
75
|
+
ephimeralContext
|
76
|
+
);
|
77
|
+
}
|
78
|
+
);
|
79
|
+
|
80
|
+
const signedWiaPoP = await createPopToken(
|
81
|
+
{
|
82
|
+
jti: `${uuid.v4()}`,
|
83
|
+
aud,
|
84
|
+
iss,
|
85
|
+
},
|
86
|
+
wiaCryptoContext
|
63
87
|
);
|
64
88
|
|
65
|
-
const codeVerifier = `${uuid.v4()}`;
|
66
89
|
const requestBody = {
|
67
|
-
grant_type: "
|
90
|
+
grant_type: "authorization_code",
|
68
91
|
client_id: clientId,
|
69
92
|
code,
|
93
|
+
redirect_uri: redirectUri,
|
70
94
|
code_verifier: codeVerifier,
|
71
95
|
client_assertion_type: ASSERTION_TYPE,
|
72
|
-
client_assertion: walletInstanceAttestation,
|
73
|
-
redirect_uri: walletProviderBaseUrl,
|
96
|
+
client_assertion: walletInstanceAttestation + "~" + signedWiaPoP,
|
74
97
|
};
|
75
|
-
var formBody = new URLSearchParams(requestBody);
|
76
98
|
|
77
|
-
|
99
|
+
const authorizationRequestFormBody = new URLSearchParams(requestBody);
|
100
|
+
const tokenRes = await appFetch(tokenUrl, {
|
78
101
|
method: "POST",
|
79
102
|
headers: {
|
80
103
|
"Content-Type": "application/x-www-form-urlencoded",
|
81
|
-
DPoP:
|
104
|
+
DPoP: tokenRequestSignedDPop,
|
82
105
|
},
|
83
|
-
body:
|
106
|
+
body: authorizationRequestFormBody.toString(),
|
84
107
|
})
|
85
108
|
.then(hasStatus(200))
|
86
109
|
.then((res) => res.json())
|
87
|
-
.then((body) => (
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
110
|
+
.then((body) => TokenResponse.safeParse(body));
|
111
|
+
|
112
|
+
if (!tokenRes.success) {
|
113
|
+
throw new ValidationFailed(tokenRes.error.message);
|
114
|
+
}
|
115
|
+
|
116
|
+
return { accessToken: tokenRes.data, tokenRequestSignedDPop };
|
92
117
|
};
|
@@ -1,161 +1,127 @@
|
|
1
|
-
import * as z from "zod";
|
2
|
-
import uuid from "react-native-uuid";
|
3
1
|
import { SignJWT, type CryptoContext } from "@pagopa/io-react-native-jwt";
|
4
|
-
import { createDPopToken } from "../../utils/dpop";
|
5
|
-
|
6
|
-
import type { StartFlow } from "./01-start-flow";
|
7
|
-
import { hasStatus, type Out } from "../../utils/misc";
|
8
|
-
import type { EvaluateIssuerTrust } from "./02-evaluate-issuer-trust";
|
9
2
|
import type { AuthorizeAccess } from "./05-authorize-access";
|
10
|
-
import {
|
3
|
+
import type { EvaluateIssuerTrust } from "./02-evaluate-issuer-trust";
|
4
|
+
import { hasStatus, type Out } from "../../../src/utils/misc";
|
5
|
+
import type { StartUserAuthorization } from "./03-start-user-authorization";
|
6
|
+
import { ValidationFailed } from "../../../src/utils/errors";
|
7
|
+
import { CredentialResponse } from "./types";
|
8
|
+
|
9
|
+
export type ObtainCredential = (
|
10
|
+
issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
|
11
|
+
accessToken: Out<AuthorizeAccess>["accessToken"],
|
12
|
+
clientId: Out<StartUserAuthorization>["clientId"],
|
13
|
+
credentialDefinition: Out<StartUserAuthorization>["credentialDefinition"],
|
14
|
+
tokenRequestSignedDPop: Out<AuthorizeAccess>["tokenRequestSignedDPop"],
|
15
|
+
context: {
|
16
|
+
credentialCryptoContext: CryptoContext;
|
17
|
+
appFetch?: GlobalFetch["fetch"];
|
18
|
+
}
|
19
|
+
) => Promise<CredentialResponse>;
|
11
20
|
|
12
|
-
/**
|
13
|
-
* Return the signed jwt for nonce proof of possession
|
14
|
-
*/
|
15
21
|
export const createNonceProof = async (
|
16
22
|
nonce: string,
|
17
23
|
issuer: string,
|
18
24
|
audience: string,
|
19
25
|
ctx: CryptoContext
|
20
26
|
): Promise<string> => {
|
27
|
+
const jwk = await ctx.getPublicKey();
|
21
28
|
return new SignJWT(ctx)
|
22
29
|
.setPayload({
|
23
30
|
nonce,
|
24
|
-
jwk: await ctx.getPublicKey(),
|
25
31
|
})
|
26
32
|
.setProtectedHeader({
|
27
|
-
|
33
|
+
typ: "openid4vci-proof+jwt",
|
34
|
+
jwk,
|
28
35
|
})
|
29
36
|
.setAudience(audience)
|
30
37
|
.setIssuer(issuer)
|
31
38
|
.setIssuedAt()
|
32
|
-
.setExpirationTime("
|
39
|
+
.setExpirationTime("5min")
|
33
40
|
.sign();
|
34
41
|
};
|
35
42
|
|
36
|
-
const CredentialEndpointResponse = z.object({
|
37
|
-
credential: z.string(),
|
38
|
-
format: SupportedCredentialFormat,
|
39
|
-
// nonce used to perform multiple credential requests
|
40
|
-
// re-using the same authorization profile
|
41
|
-
c_nonce: z.string(),
|
42
|
-
c_nonce_expires_in: z.number(),
|
43
|
-
});
|
44
|
-
|
45
|
-
export type ObtainCredential = (
|
46
|
-
issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
|
47
|
-
accessToken: Out<AuthorizeAccess>["accessToken"],
|
48
|
-
nonce: Out<AuthorizeAccess>["nonce"],
|
49
|
-
clientId: Out<AuthorizeAccess>["clientId"],
|
50
|
-
credentialType: Out<StartFlow>["credentialType"],
|
51
|
-
credentialFormat: SupportedCredentialFormat,
|
52
|
-
context: {
|
53
|
-
credentialCryptoContext: CryptoContext;
|
54
|
-
walletProviderBaseUrl: string;
|
55
|
-
appFetch?: GlobalFetch["fetch"];
|
56
|
-
}
|
57
|
-
) => Promise<{
|
58
|
-
credential: string;
|
59
|
-
format: SupportedCredentialFormat;
|
60
|
-
nonce: string;
|
61
|
-
}>;
|
62
|
-
|
63
|
-
// Checks whether in the Entity confoguration at least one credential
|
64
|
-
// is defined for the given type and format
|
65
|
-
const isCredentialAvailable = (
|
66
|
-
issuerConf: Out<EvaluateIssuerTrust>["issuerConf"],
|
67
|
-
credentialType: Out<StartFlow>["credentialType"],
|
68
|
-
credentialFormat: SupportedCredentialFormat
|
69
|
-
): boolean =>
|
70
|
-
issuerConf.openid_credential_issuer.credentials_supported.some(
|
71
|
-
(c) =>
|
72
|
-
c.format === credentialFormat &&
|
73
|
-
c.credential_definition.type.includes(credentialType)
|
74
|
-
);
|
75
|
-
|
76
43
|
/**
|
77
|
-
*
|
78
|
-
*
|
79
|
-
*
|
80
|
-
*
|
81
|
-
*
|
82
|
-
* @param
|
83
|
-
* @param
|
84
|
-
* @param
|
85
|
-
* @param
|
86
|
-
* @param
|
44
|
+
* Obtains the credential from the issuer.
|
45
|
+
* The key pair of the credentialCryptoContext is used for Openid4vci proof JWT to be presented with the Access Token and the DPoP Proof JWT at the Credential Endpoint
|
46
|
+
* of the Credential Issuer to request the issuance of a credential linked to the public key contained in the JWT proof.
|
47
|
+
* The Openid4vci proof JWT incapsulates the nonce extracted from the token response from the {@link authorizeAccess} step.
|
48
|
+
* The credential request is sent to the Credential Endpoint of the Credential Issuer via HTTP POST with the type of the credential, its format, the access token and the JWT proof.
|
49
|
+
* @param issuerConf The issuer configuration returned by {@link evaluateIssuerTrust}
|
50
|
+
* @param accessToken The access token response returned by {@link authorizeAccess}
|
51
|
+
* @param clientId The client id returned by {@link startUserAuthorization}
|
52
|
+
* @param credentialDefinition The credential definition of the credential to be obtained returned by {@link startUserAuthorization}
|
53
|
+
* @param tokenRequestSignedDPop The DPoP signed token request returned by {@link authorizeAccess}
|
54
|
+
* @param context.credentialCryptoContext The crypto context used to obtain the credential
|
87
55
|
* @param context.appFetch (optional) fetch api implementation. Default: built-in fetch
|
88
|
-
* @returns The
|
56
|
+
* @returns The credential response containing the credential
|
89
57
|
*/
|
90
58
|
export const obtainCredential: ObtainCredential = async (
|
91
59
|
issuerConf,
|
92
60
|
accessToken,
|
93
|
-
nonce,
|
94
61
|
clientId,
|
95
|
-
|
96
|
-
|
62
|
+
credentialDefinition,
|
63
|
+
tokenRequestSignedDPop,
|
97
64
|
context
|
98
65
|
) => {
|
99
|
-
const {
|
100
|
-
credentialCryptoContext,
|
101
|
-
walletProviderBaseUrl,
|
102
|
-
appFetch = fetch,
|
103
|
-
} = context;
|
104
|
-
|
105
|
-
if (!isCredentialAvailable(issuerConf, credentialType, credentialFormat)) {
|
106
|
-
throw new Error(
|
107
|
-
`The Issuer provides no credential for type ${credentialType} and format ${credentialFormat}`
|
108
|
-
);
|
109
|
-
}
|
66
|
+
const { credentialCryptoContext, appFetch = fetch } = context;
|
110
67
|
|
111
68
|
const credentialUrl = issuerConf.openid_credential_issuer.credential_endpoint;
|
112
69
|
|
113
|
-
/**
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
htm: "POST",
|
119
|
-
htu: credentialUrl,
|
120
|
-
jti: `${uuid.v4()}`,
|
121
|
-
},
|
122
|
-
credentialCryptoContext
|
123
|
-
);
|
124
|
-
|
125
|
-
/** JWT proof token to bind the request nonce
|
126
|
-
to the key that will bind the holder User with the Credential
|
127
|
-
@see https://openid.net/specs/openid-4-verifiable-credential-issuance-1_0.html#name-proof-types */
|
70
|
+
/**
|
71
|
+
* JWT proof token to bind the request nonce to the key that will bind the holder User with the Credential
|
72
|
+
* This is presented along with the access token to the Credential Endpoint as proof of possession of the private key used to sign the Access Token.
|
73
|
+
* @see https://openid.net/specs/openid-4-verifiable-credential-issuance-1_0.html#name-proof-types
|
74
|
+
*/
|
128
75
|
const signedNonceProof = await createNonceProof(
|
129
|
-
|
76
|
+
accessToken.c_nonce,
|
130
77
|
clientId,
|
131
|
-
|
78
|
+
credentialUrl,
|
132
79
|
credentialCryptoContext
|
133
80
|
);
|
134
81
|
|
82
|
+
// Validation of accessTokenResponse.authorization_details if contain credentialDefinition
|
83
|
+
const constainsCredentialDefinition = accessToken.authorization_details.some(
|
84
|
+
(c) =>
|
85
|
+
c.credential_configuration_id ===
|
86
|
+
credentialDefinition.credential_configuration_id &&
|
87
|
+
c.format === credentialDefinition.format &&
|
88
|
+
c.type === credentialDefinition.type
|
89
|
+
);
|
90
|
+
|
91
|
+
if (!constainsCredentialDefinition) {
|
92
|
+
throw new ValidationFailed(
|
93
|
+
"The access token response does not contain the requested credential"
|
94
|
+
);
|
95
|
+
}
|
96
|
+
|
135
97
|
/** The credential request body */
|
136
|
-
const
|
137
|
-
credential_definition:
|
138
|
-
type: [
|
139
|
-
}
|
140
|
-
format:
|
141
|
-
proof:
|
98
|
+
const credentialRequestFormBody = {
|
99
|
+
credential_definition: {
|
100
|
+
type: [credentialDefinition.credential_configuration_id],
|
101
|
+
},
|
102
|
+
format: credentialDefinition.format,
|
103
|
+
proof: {
|
142
104
|
jwt: signedNonceProof,
|
143
105
|
proof_type: "jwt",
|
144
|
-
}
|
145
|
-
}
|
106
|
+
},
|
107
|
+
};
|
146
108
|
|
147
|
-
const
|
109
|
+
const credentialRes = await appFetch(credentialUrl, {
|
148
110
|
method: "POST",
|
149
111
|
headers: {
|
150
|
-
"Content-Type": "application/
|
151
|
-
DPoP:
|
152
|
-
Authorization: accessToken
|
112
|
+
"Content-Type": "application/json",
|
113
|
+
DPoP: tokenRequestSignedDPop,
|
114
|
+
Authorization: `${accessToken.token_type} ${accessToken.access_token}`,
|
153
115
|
},
|
154
|
-
body:
|
116
|
+
body: JSON.stringify(credentialRequestFormBody),
|
155
117
|
})
|
156
118
|
.then(hasStatus(200))
|
157
119
|
.then((res) => res.json())
|
158
|
-
.then(
|
120
|
+
.then((body) => CredentialResponse.safeParse(body));
|
121
|
+
|
122
|
+
if (!credentialRes.success) {
|
123
|
+
throw new ValidationFailed(credentialRes.error.message);
|
124
|
+
}
|
159
125
|
|
160
|
-
return
|
126
|
+
return credentialRes.data;
|
161
127
|
};
|