@thunderid/browser 1.9.1 → 1.9.3

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.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Buffer } from 'buffer/index.js';
2
2
  if (typeof window !== 'undefined' && !window.Buffer) { window.Buffer = Buffer; }
3
- import { DEFAULT_THEME, HttpClient, OIDCRequestConstants, ThunderIDAuthException, ThunderIDJavaScriptClient, ThunderIDRuntimeError, TokenConstants, arrayBufferToBase64url, base64urlToArrayBuffer, createPackageComponentLogger, extractEmojiFromUri, extractPkceStorageKeyFromState, generateFlattenedUserProfile, getTokenPermissions, getUsersMe, isEmojiUri, resolveBackendEndpoints, resolveResourceEndpoint } from "@thunderid/javascript";
3
+ import { DEFAULT_THEME, HttpClient, OIDCRequestConstants, ThunderIDAuthException, ThunderIDJavaScriptClient, ThunderIDRuntimeError, TokenConstants, arrayBufferToBase64url, base64urlToArrayBuffer, createPackageComponentLogger, extractEmojiFromUri, extractPkceStorageKeyFromState, generateFlattenedUserProfile, getTokenPermissions, getUsersMe, isEmojiUri, resolveBackendEndpoints, resolveRequestBaseUrl, resolveResourceEndpoint } from "@thunderid/javascript";
4
4
  import base64url from "base64url";
5
5
  import sha256 from "fast-sha256";
6
6
  import { createLocalJWKSet, jwtVerify } from "jose";
@@ -178,18 +178,21 @@ var BackendClient = class BackendClient {
178
178
  return (await response.json()).redirectUrl;
179
179
  }
180
180
  /**
181
- * Maps an API URL to its backend proxy URL.
181
+ * Maps an API URL to its backend proxy URL. A URL already on the proxy is sent as it is, and any
182
+ * other path (`/users`) goes under the proxy. The backend, not the app, knows the API's address.
182
183
  *
183
- * @returns The proxy URL, or `undefined` when the URL is not under the API base and so cannot be
184
- * sent with the session's credentials.
184
+ * @returns The proxy URL, or `undefined` for an absolute URL that isn't on the proxy and so cannot
185
+ * be sent with the session's credentials.
185
186
  */
186
187
  toProxyUrl(url) {
187
- const { apiBaseUrl, proxy } = this.endpoints;
188
- let rest;
189
- if (url === apiBaseUrl || url.startsWith(`${apiBaseUrl}/`) || url.startsWith(`${apiBaseUrl}?`)) rest = url.slice(apiBaseUrl.length);
190
- else if (url.startsWith("/") && !url.startsWith("//")) rest = url;
191
- else return;
192
- return `${proxy}${rest.startsWith("/") ? rest : `/${rest}`}`;
188
+ const { proxy } = this.endpoints;
189
+ const origin = typeof window !== "undefined" ? window.location.origin : "";
190
+ const isPath = (value) => value.startsWith("/") && !value.startsWith("//");
191
+ const isUnder = (base) => url === base || url.startsWith(`${base}/`) || url.startsWith(`${base}?`);
192
+ const absoluteProxy = isPath(proxy) ? `${origin}${proxy}` : proxy;
193
+ const proxyPath = absoluteProxy.startsWith(`${origin}/`) ? absoluteProxy.slice(origin.length) : void 0;
194
+ if (isUnder(proxy) || isUnder(absoluteProxy) || proxyPath !== void 0 && isUnder(proxyPath)) return url;
195
+ return isPath(url) ? `${proxy}${url}` : void 0;
193
196
  }
194
197
  /**
195
198
  * Whether a request to this URL skips the proxy and goes straight from the browser.
@@ -220,11 +223,12 @@ var BackendClient = class BackendClient {
220
223
  return await response.json();
221
224
  }
222
225
  /**
223
- * The headers and credentials mode every backend request is sent with.
226
+ * The headers and credentials mode every backend request is sent with. The cookies go with every
227
+ * request, so a backend on another origin works too when its CORS allows credentials.
224
228
  */
225
229
  static requestInit(headers = {}) {
226
230
  return {
227
- credentials: "same-origin",
231
+ credentials: "include",
228
232
  headers: {
229
233
  ...headers,
230
234
  [BACKEND_REQUEST_HEADER]: BACKEND_REQUEST_HEADER_VALUE
@@ -3083,7 +3087,7 @@ var ThunderIDBrowserClient = class ThunderIDBrowserClient extends ThunderIDJavaS
3083
3087
  await this._validateMethod();
3084
3088
  const configData = await this.getStorageManager().getConfigData();
3085
3089
  const profile = await getUsersMe({
3086
- baseUrl: options?.baseUrl ?? configData?.baseUrl,
3090
+ baseUrl: options?.baseUrl ?? resolveRequestBaseUrl(configData),
3087
3091
  fetcher: createHttpClientFetcher_default(this._browserInstanceId),
3088
3092
  url: resolveResourceEndpoint("usersMe", configData, options?.url)
3089
3093
  });