@sphereon/oid4vci-client 0.10.3 → 0.10.4-next.119
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 +24 -5
- package/dist/AccessTokenClient.d.ts +5 -5
- package/dist/AccessTokenClient.d.ts.map +1 -1
- package/dist/AccessTokenClient.js +51 -37
- package/dist/AccessTokenClient.js.map +1 -1
- package/dist/AccessTokenClientV1_0_11.d.ts +29 -0
- package/dist/AccessTokenClientV1_0_11.d.ts.map +1 -0
- package/dist/AccessTokenClientV1_0_11.js +209 -0
- package/dist/AccessTokenClientV1_0_11.js.map +1 -0
- package/dist/AuthorizationCodeClient.d.ts +9 -4
- package/dist/AuthorizationCodeClient.d.ts.map +1 -1
- package/dist/AuthorizationCodeClient.js +102 -18
- package/dist/AuthorizationCodeClient.js.map +1 -1
- package/dist/AuthorizationCodeClientV1_0_11.d.ts +9 -0
- package/dist/AuthorizationCodeClientV1_0_11.d.ts.map +1 -0
- package/dist/AuthorizationCodeClientV1_0_11.js +134 -0
- package/dist/AuthorizationCodeClientV1_0_11.js.map +1 -0
- package/dist/CredentialOfferClient.d.ts.map +1 -1
- package/dist/CredentialOfferClient.js +18 -13
- package/dist/CredentialOfferClient.js.map +1 -1
- package/dist/CredentialOfferClientV1_0_11.d.ts +10 -0
- package/dist/CredentialOfferClientV1_0_11.d.ts.map +1 -0
- package/dist/CredentialOfferClientV1_0_11.js +101 -0
- package/dist/CredentialOfferClientV1_0_11.js.map +1 -0
- package/dist/CredentialOfferClientV1_0_13.d.ts +10 -0
- package/dist/CredentialOfferClientV1_0_13.d.ts.map +1 -0
- package/dist/CredentialOfferClientV1_0_13.js +94 -0
- package/dist/CredentialOfferClientV1_0_13.js.map +1 -0
- package/dist/CredentialRequestClient.d.ts +20 -7
- package/dist/CredentialRequestClient.d.ts.map +1 -1
- package/dist/CredentialRequestClient.js +46 -30
- package/dist/CredentialRequestClient.js.map +1 -1
- package/dist/CredentialRequestClientBuilder.d.ts +11 -6
- package/dist/CredentialRequestClientBuilder.d.ts.map +1 -1
- package/dist/CredentialRequestClientBuilder.js +22 -9
- package/dist/CredentialRequestClientBuilder.js.map +1 -1
- package/dist/CredentialRequestClientBuilderV1_0_11.d.ts +48 -0
- package/dist/CredentialRequestClientBuilderV1_0_11.d.ts.map +1 -0
- package/dist/CredentialRequestClientBuilderV1_0_11.js +121 -0
- package/dist/CredentialRequestClientBuilderV1_0_11.js.map +1 -0
- package/dist/CredentialRequestClientV1_0_11.d.ts +50 -0
- package/dist/CredentialRequestClientV1_0_11.d.ts.map +1 -0
- package/dist/CredentialRequestClientV1_0_11.js +151 -0
- package/dist/CredentialRequestClientV1_0_11.js.map +1 -0
- package/dist/MetadataClient.d.ts +5 -15
- package/dist/MetadataClient.d.ts.map +1 -1
- package/dist/MetadataClient.js +41 -44
- package/dist/MetadataClient.js.map +1 -1
- package/dist/MetadataClientV1_0_11.d.ts +31 -0
- package/dist/MetadataClientV1_0_11.d.ts.map +1 -0
- package/dist/MetadataClientV1_0_11.js +182 -0
- package/dist/MetadataClientV1_0_11.js.map +1 -0
- package/dist/MetadataClientV1_0_13.d.ts +31 -0
- package/dist/MetadataClientV1_0_13.d.ts.map +1 -0
- package/dist/MetadataClientV1_0_13.js +181 -0
- package/dist/MetadataClientV1_0_13.js.map +1 -0
- package/dist/OpenID4VCIClient.d.ts +14 -19
- package/dist/OpenID4VCIClient.d.ts.map +1 -1
- package/dist/OpenID4VCIClient.js +111 -61
- package/dist/OpenID4VCIClient.js.map +1 -1
- package/dist/OpenID4VCIClientV1_0_11.d.ts +108 -0
- package/dist/OpenID4VCIClientV1_0_11.d.ts.map +1 -0
- package/dist/OpenID4VCIClientV1_0_11.js +449 -0
- package/dist/OpenID4VCIClientV1_0_11.js.map +1 -0
- package/dist/OpenID4VCIClientV1_0_13.d.ts +112 -0
- package/dist/OpenID4VCIClientV1_0_13.d.ts.map +1 -0
- package/dist/OpenID4VCIClientV1_0_13.js +478 -0
- package/dist/OpenID4VCIClientV1_0_13.js.map +1 -0
- package/dist/ProofOfPossessionBuilder.d.ts +14 -3
- package/dist/ProofOfPossessionBuilder.d.ts.map +1 -1
- package/dist/ProofOfPossessionBuilder.js +20 -21
- package/dist/ProofOfPossessionBuilder.js.map +1 -1
- package/dist/functions/OpenIDUtils.d.ts +12 -0
- package/dist/functions/OpenIDUtils.d.ts.map +1 -0
- package/dist/functions/OpenIDUtils.js +37 -0
- package/dist/functions/OpenIDUtils.js.map +1 -0
- package/dist/functions/index.d.ts +2 -3
- package/dist/functions/index.d.ts.map +1 -1
- package/dist/functions/index.js +2 -3
- package/dist/functions/index.js.map +1 -1
- package/dist/functions/notifications.d.ts +4 -0
- package/dist/functions/notifications.d.ts.map +1 -0
- package/dist/functions/notifications.js +39 -0
- package/dist/functions/notifications.js.map +1 -0
- package/dist/index.d.ts +13 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -1
- package/dist/index.js.map +1 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +5 -0
- package/dist/types/index.js.map +1 -1
- package/lib/AccessTokenClient.ts +59 -34
- package/lib/AccessTokenClientV1_0_11.ts +250 -0
- package/lib/AuthorizationCodeClient.ts +131 -28
- package/lib/AuthorizationCodeClientV1_0_11.ts +170 -0
- package/lib/CredentialOfferClient.ts +21 -8
- package/lib/CredentialOfferClientV1_0_11.ts +112 -0
- package/lib/CredentialOfferClientV1_0_13.ts +103 -0
- package/lib/CredentialRequestClient.ts +65 -26
- package/lib/CredentialRequestClientBuilder.ts +34 -16
- package/lib/CredentialRequestClientBuilderV1_0_11.ts +163 -0
- package/lib/CredentialRequestClientV1_0_11.ts +197 -0
- package/lib/MetadataClient.ts +64 -49
- package/lib/MetadataClientV1_0_11.ts +189 -0
- package/lib/MetadataClientV1_0_13.ts +188 -0
- package/lib/OpenID4VCIClient.ts +132 -68
- package/lib/OpenID4VCIClientV1_0_11.ts +635 -0
- package/lib/OpenID4VCIClientV1_0_13.ts +677 -0
- package/lib/ProofOfPossessionBuilder.ts +41 -11
- package/lib/__tests__/AccessTokenClient.spec.ts +40 -12
- package/lib/__tests__/AuthorizationDetailsBuilder.spec.ts +0 -12
- package/lib/__tests__/CredentialRequestClient.spec.ts +87 -50
- package/lib/__tests__/CredentialRequestClientBuilder.spec.ts +18 -12
- package/lib/__tests__/CredentialRequestClientV1_0_11.spec.ts +317 -0
- package/lib/__tests__/EBSIE2E.spec.test.ts +2 -2
- package/lib/__tests__/HttpUtils.spec.ts +1 -1
- package/lib/__tests__/IT.spec.ts +264 -14
- package/lib/__tests__/IssuanceInitiation.spec.ts +59 -4
- package/lib/__tests__/IssuanceInitiationV1_0_11.spec.ts +62 -0
- package/lib/__tests__/MattrE2E.spec.test.ts +2 -2
- package/lib/__tests__/MetadataClient.spec.ts +53 -3
- package/lib/__tests__/MetadataMocks.ts +42 -2
- package/lib/__tests__/OpenID4VCIClient.spec.ts +58 -2
- package/lib/__tests__/{OpenID4VCIClientPAR.spec.ts → OpenID4VCIClientPARV1_0_11.spec.ts} +5 -5
- package/lib/__tests__/OpenID4VCIClientV1_0_11.spec.ts +226 -0
- package/lib/__tests__/OpenID4VCIClientV1_0_13.spec.ts +204 -0
- package/lib/__tests__/ProofOfPossessionBuilder.spec.ts +1 -1
- package/lib/__tests__/SdJwt.spec.ts +36 -30
- package/lib/__tests__/SphereonE2E.spec.test.ts +10 -7
- package/lib/__tests__/data/VciDataFixtures.ts +712 -27
- package/lib/functions/OpenIDUtils.ts +25 -0
- package/lib/functions/index.ts +2 -3
- package/lib/functions/notifications.ts +32 -0
- package/lib/index.ts +16 -1
- package/lib/types/index.ts +6 -0
- package/package.json +4 -4
- package/dist/functions/ProofUtil.d.ts +0 -30
- package/dist/functions/ProofUtil.d.ts.map +0 -1
- package/dist/functions/ProofUtil.js +0 -106
- package/dist/functions/ProofUtil.js.map +0 -1
- package/lib/functions/ProofUtil.ts +0 -128
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthorizationServerMetadata,
|
|
3
|
+
AuthorizationServerType,
|
|
4
|
+
CredentialIssuerMetadataV1_0_11,
|
|
5
|
+
CredentialOfferPayload,
|
|
6
|
+
CredentialOfferRequestWithBaseUrl,
|
|
7
|
+
EndpointMetadataResultV1_0_11,
|
|
8
|
+
getIssuerFromCredentialOfferPayload,
|
|
9
|
+
IssuerMetadataV1_0_08,
|
|
10
|
+
OpenIDResponse,
|
|
11
|
+
WellKnownEndpoints,
|
|
12
|
+
} from '@sphereon/oid4vci-common';
|
|
13
|
+
import Debug from 'debug';
|
|
14
|
+
|
|
15
|
+
import { retrieveWellknown } from './functions/OpenIDUtils';
|
|
16
|
+
|
|
17
|
+
const debug = Debug('sphereon:oid4vci:metadata');
|
|
18
|
+
|
|
19
|
+
export class MetadataClientV1_0_11 {
|
|
20
|
+
/**
|
|
21
|
+
* Retrieve metadata using the Initiation obtained from a previous step
|
|
22
|
+
*
|
|
23
|
+
* @param credentialOffer
|
|
24
|
+
*/
|
|
25
|
+
public static async retrieveAllMetadataFromCredentialOffer(
|
|
26
|
+
credentialOffer: CredentialOfferRequestWithBaseUrl,
|
|
27
|
+
): Promise<EndpointMetadataResultV1_0_11> {
|
|
28
|
+
return MetadataClientV1_0_11.retrieveAllMetadataFromCredentialOfferRequest(credentialOffer.credential_offer);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Retrieve the metada using the initiation request obtained from a previous step
|
|
33
|
+
* @param request
|
|
34
|
+
*/
|
|
35
|
+
public static async retrieveAllMetadataFromCredentialOfferRequest(request: CredentialOfferPayload): Promise<EndpointMetadataResultV1_0_11> {
|
|
36
|
+
const issuer = getIssuerFromCredentialOfferPayload(request);
|
|
37
|
+
if (issuer) {
|
|
38
|
+
return MetadataClientV1_0_11.retrieveAllMetadata(issuer);
|
|
39
|
+
}
|
|
40
|
+
throw new Error("can't retrieve metadata from CredentialOfferRequest. No issuer field is present");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Retrieve all metadata from an issuer
|
|
45
|
+
* @param issuer The issuer URL
|
|
46
|
+
* @param opts
|
|
47
|
+
*/
|
|
48
|
+
public static async retrieveAllMetadata(issuer: string, opts?: { errorOnNotFound: boolean }): Promise<EndpointMetadataResultV1_0_11> {
|
|
49
|
+
let token_endpoint: string | undefined;
|
|
50
|
+
let credential_endpoint: string | undefined;
|
|
51
|
+
let deferred_credential_endpoint: string | undefined;
|
|
52
|
+
let authorization_endpoint: string | undefined;
|
|
53
|
+
let authorizationServerType: AuthorizationServerType = 'OID4VCI';
|
|
54
|
+
let authorization_server: string = issuer;
|
|
55
|
+
const oid4vciResponse = await MetadataClientV1_0_11.retrieveOpenID4VCIServerMetadata(issuer, { errorOnNotFound: false }); // We will handle errors later, given we will also try other metadata locations
|
|
56
|
+
let credentialIssuerMetadata = oid4vciResponse?.successBody;
|
|
57
|
+
if (credentialIssuerMetadata) {
|
|
58
|
+
debug(`Issuer ${issuer} OID4VCI well-known server metadata\r\n${JSON.stringify(credentialIssuerMetadata)}`);
|
|
59
|
+
credential_endpoint = credentialIssuerMetadata.credential_endpoint;
|
|
60
|
+
deferred_credential_endpoint = credentialIssuerMetadata.deferred_credential_endpoint;
|
|
61
|
+
if (credentialIssuerMetadata.token_endpoint) {
|
|
62
|
+
token_endpoint = credentialIssuerMetadata.token_endpoint;
|
|
63
|
+
}
|
|
64
|
+
if (credentialIssuerMetadata.authorization_server) {
|
|
65
|
+
authorization_server = credentialIssuerMetadata.authorization_server;
|
|
66
|
+
}
|
|
67
|
+
if (credentialIssuerMetadata.authorization_endpoint) {
|
|
68
|
+
authorization_endpoint = credentialIssuerMetadata.authorization_endpoint;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// No specific OID4VCI endpoint. Either can be an OAuth2 AS or an OIDC IDP. Let's start with OIDC first
|
|
72
|
+
let response: OpenIDResponse<AuthorizationServerMetadata> = await retrieveWellknown(
|
|
73
|
+
authorization_server,
|
|
74
|
+
WellKnownEndpoints.OPENID_CONFIGURATION,
|
|
75
|
+
{
|
|
76
|
+
errorOnNotFound: false,
|
|
77
|
+
},
|
|
78
|
+
);
|
|
79
|
+
let authMetadata = response.successBody;
|
|
80
|
+
if (authMetadata) {
|
|
81
|
+
debug(`Issuer ${issuer} has OpenID Connect Server metadata in well-known location`);
|
|
82
|
+
authorizationServerType = 'OIDC';
|
|
83
|
+
} else {
|
|
84
|
+
// Now let's do OAuth2
|
|
85
|
+
response = await retrieveWellknown(authorization_server, WellKnownEndpoints.OAUTH_AS, { errorOnNotFound: false });
|
|
86
|
+
authMetadata = response.successBody;
|
|
87
|
+
}
|
|
88
|
+
if (!authMetadata) {
|
|
89
|
+
// We will always throw an error, no matter whether the user provided the option not to, because this is bad.
|
|
90
|
+
if (issuer !== authorization_server) {
|
|
91
|
+
throw Error(`Issuer ${issuer} provided a separate authorization server ${authorization_server}, but that server did not provide metadata`);
|
|
92
|
+
}
|
|
93
|
+
} else {
|
|
94
|
+
if (!authorizationServerType) {
|
|
95
|
+
authorizationServerType = 'OAuth 2.0';
|
|
96
|
+
}
|
|
97
|
+
debug(`Issuer ${issuer} has ${authorizationServerType} Server metadata in well-known location`);
|
|
98
|
+
if (!authMetadata.authorization_endpoint) {
|
|
99
|
+
console.warn(
|
|
100
|
+
`Issuer ${issuer} of type ${authorizationServerType} has no authorization_endpoint! Will use ${authorization_endpoint}. This only works for pre-authorized flows`,
|
|
101
|
+
);
|
|
102
|
+
} else if (authorization_endpoint && authMetadata.authorization_endpoint !== authorization_endpoint) {
|
|
103
|
+
throw Error(
|
|
104
|
+
`Credential issuer has a different authorization_endpoint (${authorization_endpoint}) from the Authorization Server (${authMetadata.authorization_endpoint})`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
authorization_endpoint = authMetadata.authorization_endpoint;
|
|
108
|
+
if (!authMetadata.token_endpoint) {
|
|
109
|
+
throw Error(`Authorization Sever ${authorization_server} did not provide a token_endpoint`);
|
|
110
|
+
} else if (token_endpoint && authMetadata.token_endpoint !== token_endpoint) {
|
|
111
|
+
throw Error(
|
|
112
|
+
`Credential issuer has a different token_endpoint (${token_endpoint}) from the Authorization Server (${authMetadata.token_endpoint})`,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
token_endpoint = authMetadata.token_endpoint;
|
|
116
|
+
if (authMetadata.credential_endpoint) {
|
|
117
|
+
if (credential_endpoint && authMetadata.credential_endpoint !== credential_endpoint) {
|
|
118
|
+
debug(
|
|
119
|
+
`Credential issuer has a different credential_endpoint (${credential_endpoint}) from the Authorization Server (${authMetadata.credential_endpoint}). Will use the issuer value`,
|
|
120
|
+
);
|
|
121
|
+
} else {
|
|
122
|
+
credential_endpoint = authMetadata.credential_endpoint;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (authMetadata.deferred_credential_endpoint) {
|
|
126
|
+
if (deferred_credential_endpoint && authMetadata.deferred_credential_endpoint !== deferred_credential_endpoint) {
|
|
127
|
+
debug(
|
|
128
|
+
`Credential issuer has a different deferred_credential_endpoint (${deferred_credential_endpoint}) from the Authorization Server (${authMetadata.deferred_credential_endpoint}). Will use the issuer value`,
|
|
129
|
+
);
|
|
130
|
+
} else {
|
|
131
|
+
deferred_credential_endpoint = authMetadata.deferred_credential_endpoint;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!authorization_endpoint) {
|
|
137
|
+
debug(`Issuer ${issuer} does not expose authorization_endpoint, so only pre-auth will be supported`);
|
|
138
|
+
}
|
|
139
|
+
if (!token_endpoint) {
|
|
140
|
+
debug(`Issuer ${issuer} does not have a token_endpoint listed in well-known locations!`);
|
|
141
|
+
if (opts?.errorOnNotFound) {
|
|
142
|
+
throw Error(`Could not deduce the token_endpoint for ${issuer}`);
|
|
143
|
+
} else {
|
|
144
|
+
token_endpoint = `${issuer}${issuer.endsWith('/') ? 'token' : '/token'}`;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (!credential_endpoint) {
|
|
148
|
+
debug(`Issuer ${issuer} does not have a credential_endpoint listed in well-known locations!`);
|
|
149
|
+
if (opts?.errorOnNotFound) {
|
|
150
|
+
throw Error(`Could not deduce the credential endpoint for ${issuer}`);
|
|
151
|
+
} else {
|
|
152
|
+
credential_endpoint = `${issuer}${issuer.endsWith('/') ? 'credential' : '/credential'}`;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (!credentialIssuerMetadata && authMetadata) {
|
|
157
|
+
// Apparently everything worked out and the issuer is exposing everything in oAuth2/OIDC well-knowns. Spec is vague about this situation, but we can support it
|
|
158
|
+
credentialIssuerMetadata = authMetadata as CredentialIssuerMetadataV1_0_11;
|
|
159
|
+
}
|
|
160
|
+
debug(`Issuer ${issuer} token endpoint ${token_endpoint}, credential endpoint ${credential_endpoint}`);
|
|
161
|
+
return {
|
|
162
|
+
issuer,
|
|
163
|
+
token_endpoint,
|
|
164
|
+
credential_endpoint,
|
|
165
|
+
deferred_credential_endpoint,
|
|
166
|
+
authorization_server,
|
|
167
|
+
authorization_endpoint,
|
|
168
|
+
authorizationServerType,
|
|
169
|
+
credentialIssuerMetadata: credentialIssuerMetadata as unknown as Partial<AuthorizationServerMetadata> & IssuerMetadataV1_0_08,
|
|
170
|
+
authorizationServerMetadata: authMetadata,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Retrieve only the OID4VCI metadata for the issuer. So no OIDC/OAuth2 metadata
|
|
176
|
+
*
|
|
177
|
+
* @param issuerHost The issuer hostname
|
|
178
|
+
*/
|
|
179
|
+
public static async retrieveOpenID4VCIServerMetadata(
|
|
180
|
+
issuerHost: string,
|
|
181
|
+
opts?: {
|
|
182
|
+
errorOnNotFound?: boolean;
|
|
183
|
+
},
|
|
184
|
+
): Promise<OpenIDResponse<CredentialIssuerMetadataV1_0_11> | undefined> {
|
|
185
|
+
return retrieveWellknown(issuerHost, WellKnownEndpoints.OPENID4VCI_ISSUER, {
|
|
186
|
+
errorOnNotFound: opts?.errorOnNotFound === undefined ? true : opts.errorOnNotFound,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthorizationServerMetadata,
|
|
3
|
+
AuthorizationServerType,
|
|
4
|
+
CredentialIssuerMetadataV1_0_13,
|
|
5
|
+
CredentialOfferPayloadV1_0_13,
|
|
6
|
+
CredentialOfferRequestWithBaseUrl,
|
|
7
|
+
EndpointMetadataResultV1_0_13,
|
|
8
|
+
getIssuerFromCredentialOfferPayload,
|
|
9
|
+
IssuerMetadataV1_0_13,
|
|
10
|
+
OpenIDResponse,
|
|
11
|
+
WellKnownEndpoints,
|
|
12
|
+
} from '@sphereon/oid4vci-common';
|
|
13
|
+
import Debug from 'debug';
|
|
14
|
+
|
|
15
|
+
import { retrieveWellknown } from './functions/OpenIDUtils';
|
|
16
|
+
|
|
17
|
+
const debug = Debug('sphereon:oid4vci:metadata');
|
|
18
|
+
|
|
19
|
+
export class MetadataClientV1_0_13 {
|
|
20
|
+
/**
|
|
21
|
+
* Retrieve metadata using the Initiation obtained from a previous step
|
|
22
|
+
*
|
|
23
|
+
* @param credentialOffer
|
|
24
|
+
*/
|
|
25
|
+
public static async retrieveAllMetadataFromCredentialOffer(
|
|
26
|
+
credentialOffer: CredentialOfferRequestWithBaseUrl,
|
|
27
|
+
): Promise<EndpointMetadataResultV1_0_13> {
|
|
28
|
+
return MetadataClientV1_0_13.retrieveAllMetadataFromCredentialOfferRequest(credentialOffer.credential_offer as CredentialOfferPayloadV1_0_13);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Retrieve the metada using the initiation request obtained from a previous step
|
|
33
|
+
* @param request
|
|
34
|
+
*/
|
|
35
|
+
public static async retrieveAllMetadataFromCredentialOfferRequest(request: CredentialOfferPayloadV1_0_13): Promise<EndpointMetadataResultV1_0_13> {
|
|
36
|
+
const issuer = getIssuerFromCredentialOfferPayload(request);
|
|
37
|
+
if (issuer) {
|
|
38
|
+
return MetadataClientV1_0_13.retrieveAllMetadata(issuer);
|
|
39
|
+
}
|
|
40
|
+
throw new Error("can't retrieve metadata from CredentialOfferRequest. No issuer field is present");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Retrieve all metadata from an issuer
|
|
45
|
+
* @param issuer The issuer URL
|
|
46
|
+
* @param opts
|
|
47
|
+
*/
|
|
48
|
+
public static async retrieveAllMetadata(issuer: string, opts?: { errorOnNotFound: boolean }): Promise<EndpointMetadataResultV1_0_13> {
|
|
49
|
+
let token_endpoint: string | undefined;
|
|
50
|
+
let credential_endpoint: string | undefined;
|
|
51
|
+
let deferred_credential_endpoint: string | undefined;
|
|
52
|
+
let authorization_endpoint: string | undefined;
|
|
53
|
+
let authorizationServerType: AuthorizationServerType = 'OID4VCI';
|
|
54
|
+
let authorization_servers: string[] = [issuer];
|
|
55
|
+
const oid4vciResponse = await MetadataClientV1_0_13.retrieveOpenID4VCIServerMetadata(issuer, { errorOnNotFound: false }); // We will handle errors later, given we will also try other metadata locations
|
|
56
|
+
let credentialIssuerMetadata = oid4vciResponse?.successBody;
|
|
57
|
+
if (credentialIssuerMetadata) {
|
|
58
|
+
debug(`Issuer ${issuer} OID4VCI well-known server metadata\r\n${JSON.stringify(credentialIssuerMetadata)}`);
|
|
59
|
+
credential_endpoint = credentialIssuerMetadata.credential_endpoint;
|
|
60
|
+
deferred_credential_endpoint = credentialIssuerMetadata.deferred_credential_endpoint;
|
|
61
|
+
if (credentialIssuerMetadata.token_endpoint) {
|
|
62
|
+
token_endpoint = credentialIssuerMetadata.token_endpoint;
|
|
63
|
+
}
|
|
64
|
+
if (credentialIssuerMetadata.authorization_servers) {
|
|
65
|
+
authorization_servers = credentialIssuerMetadata.authorization_servers;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// No specific OID4VCI endpoint. Either can be an OAuth2 AS or an OIDC IDP. Let's start with OIDC first
|
|
69
|
+
// TODO: for now we're taking just the first one
|
|
70
|
+
let response: OpenIDResponse<AuthorizationServerMetadata> = await retrieveWellknown(
|
|
71
|
+
authorization_servers[0],
|
|
72
|
+
WellKnownEndpoints.OPENID_CONFIGURATION,
|
|
73
|
+
{
|
|
74
|
+
errorOnNotFound: false,
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
let authMetadata = response.successBody;
|
|
78
|
+
if (authMetadata) {
|
|
79
|
+
debug(`Issuer ${issuer} has OpenID Connect Server metadata in well-known location`);
|
|
80
|
+
authorizationServerType = 'OIDC';
|
|
81
|
+
} else {
|
|
82
|
+
// Now let's do OAuth2
|
|
83
|
+
// TODO: for now we're taking just the first one
|
|
84
|
+
response = await retrieveWellknown(authorization_servers[0], WellKnownEndpoints.OAUTH_AS, { errorOnNotFound: false });
|
|
85
|
+
authMetadata = response.successBody;
|
|
86
|
+
}
|
|
87
|
+
if (!authMetadata) {
|
|
88
|
+
// We will always throw an error, no matter whether the user provided the option not to, because this is bad.
|
|
89
|
+
if (!authorization_servers.includes(issuer)) {
|
|
90
|
+
throw Error(`Issuer ${issuer} provided a separate authorization server ${authorization_servers}, but that server did not provide metadata`);
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
if (!authorizationServerType) {
|
|
94
|
+
authorizationServerType = 'OAuth 2.0';
|
|
95
|
+
}
|
|
96
|
+
debug(`Issuer ${issuer} has ${authorizationServerType} Server metadata in well-known location`);
|
|
97
|
+
if (!authMetadata.authorization_endpoint) {
|
|
98
|
+
console.warn(
|
|
99
|
+
`Issuer ${issuer} of type ${authorizationServerType} has no authorization_endpoint! Will use ${authorization_endpoint}. This only works for pre-authorized flows`,
|
|
100
|
+
);
|
|
101
|
+
} else if (authorization_endpoint && authMetadata.authorization_endpoint !== authorization_endpoint) {
|
|
102
|
+
throw Error(
|
|
103
|
+
`Credential issuer has a different authorization_endpoint (${authorization_endpoint}) from the Authorization Server (${authMetadata.authorization_endpoint})`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
authorization_endpoint = authMetadata.authorization_endpoint;
|
|
107
|
+
if (!authMetadata.token_endpoint) {
|
|
108
|
+
throw Error(`Authorization Sever ${authorization_servers} did not provide a token_endpoint`);
|
|
109
|
+
} else if (token_endpoint && authMetadata.token_endpoint !== token_endpoint) {
|
|
110
|
+
throw Error(
|
|
111
|
+
`Credential issuer has a different token_endpoint (${token_endpoint}) from the Authorization Server (${authMetadata.token_endpoint})`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
token_endpoint = authMetadata.token_endpoint;
|
|
115
|
+
if (authMetadata.credential_endpoint) {
|
|
116
|
+
if (credential_endpoint && authMetadata.credential_endpoint !== credential_endpoint) {
|
|
117
|
+
debug(
|
|
118
|
+
`Credential issuer has a different credential_endpoint (${credential_endpoint}) from the Authorization Server (${authMetadata.credential_endpoint}). Will use the issuer value`,
|
|
119
|
+
);
|
|
120
|
+
} else {
|
|
121
|
+
credential_endpoint = authMetadata.credential_endpoint;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (authMetadata.deferred_credential_endpoint) {
|
|
125
|
+
if (deferred_credential_endpoint && authMetadata.deferred_credential_endpoint !== deferred_credential_endpoint) {
|
|
126
|
+
debug(
|
|
127
|
+
`Credential issuer has a different deferred_credential_endpoint (${deferred_credential_endpoint}) from the Authorization Server (${authMetadata.deferred_credential_endpoint}). Will use the issuer value`,
|
|
128
|
+
);
|
|
129
|
+
} else {
|
|
130
|
+
deferred_credential_endpoint = authMetadata.deferred_credential_endpoint;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (!authorization_endpoint) {
|
|
136
|
+
debug(`Issuer ${issuer} does not expose authorization_endpoint, so only pre-auth will be supported`);
|
|
137
|
+
}
|
|
138
|
+
if (!token_endpoint) {
|
|
139
|
+
debug(`Issuer ${issuer} does not have a token_endpoint listed in well-known locations!`);
|
|
140
|
+
if (opts?.errorOnNotFound) {
|
|
141
|
+
throw Error(`Could not deduce the token_endpoint for ${issuer}`);
|
|
142
|
+
} else {
|
|
143
|
+
token_endpoint = `${issuer}${issuer.endsWith('/') ? 'token' : '/token'}`;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (!credential_endpoint) {
|
|
147
|
+
debug(`Issuer ${issuer} does not have a credential_endpoint listed in well-known locations!`);
|
|
148
|
+
if (opts?.errorOnNotFound) {
|
|
149
|
+
throw Error(`Could not deduce the credential endpoint for ${issuer}`);
|
|
150
|
+
} else {
|
|
151
|
+
credential_endpoint = `${issuer}${issuer.endsWith('/') ? 'credential' : '/credential'}`;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (!credentialIssuerMetadata && authMetadata) {
|
|
156
|
+
// Apparently everything worked out and the issuer is exposing everything in oAuth2/OIDC well-knowns. Spec is vague about this situation, but we can support it
|
|
157
|
+
credentialIssuerMetadata = authMetadata as CredentialIssuerMetadataV1_0_13;
|
|
158
|
+
}
|
|
159
|
+
debug(`Issuer ${issuer} token endpoint ${token_endpoint}, credential endpoint ${credential_endpoint}`);
|
|
160
|
+
return {
|
|
161
|
+
issuer,
|
|
162
|
+
token_endpoint,
|
|
163
|
+
credential_endpoint,
|
|
164
|
+
deferred_credential_endpoint,
|
|
165
|
+
authorization_server: authorization_servers[0],
|
|
166
|
+
authorization_endpoint,
|
|
167
|
+
authorizationServerType,
|
|
168
|
+
credentialIssuerMetadata: credentialIssuerMetadata,
|
|
169
|
+
authorizationServerMetadata: authMetadata,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Retrieve only the OID4VCI metadata for the issuer. So no OIDC/OAuth2 metadata
|
|
175
|
+
*
|
|
176
|
+
* @param issuerHost The issuer hostname
|
|
177
|
+
*/
|
|
178
|
+
public static async retrieveOpenID4VCIServerMetadata(
|
|
179
|
+
issuerHost: string,
|
|
180
|
+
opts?: {
|
|
181
|
+
errorOnNotFound?: boolean;
|
|
182
|
+
},
|
|
183
|
+
): Promise<OpenIDResponse<IssuerMetadataV1_0_13> | undefined> {
|
|
184
|
+
return retrieveWellknown(issuerHost, WellKnownEndpoints.OPENID4VCI_ISSUER, {
|
|
185
|
+
errorOnNotFound: opts?.errorOnNotFound === undefined ? true : opts.errorOnNotFound,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|