@sphereon/oid4vci-client 0.21.0 → 0.21.1-next.2

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/dist/index.cjs CHANGED
@@ -53,6 +53,7 @@ __export(index_exports, {
53
53
  createAuthorizationRequestUrl: () => createAuthorizationRequestUrl,
54
54
  createJwtBearerClientAssertion: () => createJwtBearerClientAssertion,
55
55
  createSignedAuthRequestWhenNeeded: () => createSignedAuthRequestWhenNeeded,
56
+ determineWellknownLocations: () => determineWellknownLocations,
56
57
  generateMissingPKCEOpts: () => generateMissingPKCEOpts,
57
58
  handleCredentialOfferUri: () => handleCredentialOfferUri,
58
59
  isUriEncoded: () => isUriEncoded,
@@ -130,14 +131,67 @@ __name(sendNotification, "sendNotification");
130
131
  var import_oid4vci_common4 = require("@sphereon/oid4vci-common");
131
132
  var import_ssi_types2 = require("@sphereon/ssi-types");
132
133
  var logger = import_ssi_types2.Loggers.DEFAULT.get("sphereon:openid4vci:openid-utils");
134
+ var determineWellknownLocations = /* @__PURE__ */ __name((host, endpointType) => {
135
+ const base = host.endsWith("/") ? host.slice(0, -1) : host;
136
+ const legacyLocation = `${base}${endpointType}`;
137
+ try {
138
+ const url = new URL(base);
139
+ let pathname = url.pathname;
140
+ while (pathname.endsWith("/")) {
141
+ pathname = pathname.slice(0, -1);
142
+ }
143
+ if (pathname.length > 0) {
144
+ const rfc8414Location = `${url.origin}${endpointType}${pathname}`;
145
+ return endpointType === import_oid4vci_common4.WellKnownEndpoints.OPENID_CONFIGURATION ? [
146
+ legacyLocation,
147
+ rfc8414Location
148
+ ] : [
149
+ rfc8414Location,
150
+ legacyLocation
151
+ ];
152
+ }
153
+ } catch (error) {
154
+ logger.debug(`host ${host} could not be parsed as URL, using legacy well-known location only`);
155
+ }
156
+ return [
157
+ legacyLocation
158
+ ];
159
+ }, "determineWellknownLocations");
133
160
  var retrieveWellknown = /* @__PURE__ */ __name(async (host, endpointType, opts) => {
134
- const result = await (0, import_oid4vci_common4.getJson)(`${host.endsWith("/") ? host.slice(0, -1) : host}${endpointType}`, {
135
- exceptionOnHttpErrorStatus: opts?.errorOnNotFound
136
- });
137
- if (result.origResponse.status >= 400) {
138
- logger.debug(`host ${host} with endpoint type ${endpointType} status: ${result.origResponse.status}, ${result.origResponse.statusText}`);
161
+ const locations = determineWellknownLocations(host, endpointType);
162
+ let successResult;
163
+ let errorResult;
164
+ let lastError;
165
+ for (const location of locations) {
166
+ try {
167
+ const response = await (0, import_oid4vci_common4.getJson)(location, {
168
+ exceptionOnHttpErrorStatus: false
169
+ });
170
+ if (response.origResponse.status < 400) {
171
+ if (response.successBody !== void 0 && typeof response.successBody === "object" && response.successBody !== null) {
172
+ return response;
173
+ }
174
+ successResult = successResult ?? response;
175
+ } else {
176
+ logger.debug(`host ${host} with endpoint type ${endpointType} at ${location} status: ${response.origResponse.status}, ${response.origResponse.statusText}`);
177
+ errorResult = errorResult ?? response;
178
+ }
179
+ } catch (error) {
180
+ logger.debug(`host ${host} with endpoint type ${endpointType} at ${location} error: ${error instanceof Error ? error.message : error}`);
181
+ lastError = error;
182
+ }
139
183
  }
140
- return result;
184
+ if (successResult) {
185
+ return successResult;
186
+ }
187
+ if (errorResult) {
188
+ if (opts?.errorOnNotFound) {
189
+ const error = JSON.stringify(errorResult.errorBody ?? {});
190
+ throw new Error(error === "{}" ? '{"error": "not found"}' : error);
191
+ }
192
+ return errorResult;
193
+ }
194
+ throw lastError instanceof Error ? lastError : new Error(`Could not retrieve well-known ${endpointType} for host ${host}`);
141
195
  }, "retrieveWellknown");
142
196
 
143
197
  // lib/functions/AccessTokenUtil.ts