@thunderid/browser 1.15.0 → 1.17.0

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
@@ -170,6 +170,36 @@ var BackendClient = class BackendClient {
170
170
  }).toString()}`;
171
171
  }
172
172
  /**
173
+ * Switches the session to the organization unit `ouId` at the backend, without leaving the page. On
174
+ * success the cached session is dropped, so the next read returns the unit's; on a refusal the
175
+ * session stays as it was.
176
+ *
177
+ * The unit goes in the query (`ouId`) and in a JSON body (`{"ouId": …}`), so a backend can read it
178
+ * from either. A `401` left after the refresh means the backend has no session to switch, so it is
179
+ * reported as `login_required` whatever its body says.
180
+ *
181
+ * @returns The refusal, with the backend's OAuth `error` when it sent one, or `undefined` on success.
182
+ */
183
+ async switchOrganizationUnit(ouId) {
184
+ const url = `${this.endpoints.ouSwitch}?${new URLSearchParams({ ouId }).toString()}`;
185
+ const readError = async (response$1) => (await response$1.json().catch(() => ({}))).error;
186
+ const body = JSON.stringify({ ouId });
187
+ let response = await this.send(url, "POST", body);
188
+ let error = response.ok ? void 0 : await readError(response);
189
+ if (response.status === 401 && error === "session_expired") {
190
+ if (!await this.refresh()) return { error: "login_required" };
191
+ response = await this.send(url, "POST", body);
192
+ error = response.ok ? void 0 : await readError(response);
193
+ }
194
+ if (response.status === 401) error = "login_required";
195
+ if (response.ok) {
196
+ this.session = void 0;
197
+ return;
198
+ }
199
+ if (error === "login_required") this.markSignedOut();
200
+ return error ? { error } : {};
201
+ }
202
+ /**
173
203
  * Ends the backend session.
174
204
  *
175
205
  * @returns The URL the browser visits to end the session at the authorization server.
@@ -253,11 +283,19 @@ var BackendClient = class BackendClient {
253
283
  if (this.wasSignedIn) queueMicrotask(() => this.markSignedOut());
254
284
  return { isSignedIn: false };
255
285
  }
256
- send(url, method) {
257
- return fetch(url, {
286
+ send(url, method, body) {
287
+ if (body === void 0) return fetch(url, {
258
288
  method,
259
289
  ...BackendClient.requestInit({ Accept: "application/json" })
260
290
  });
291
+ return fetch(url, {
292
+ body,
293
+ method,
294
+ ...BackendClient.requestInit({
295
+ Accept: "application/json",
296
+ "Content-Type": "application/json"
297
+ })
298
+ });
261
299
  }
262
300
  };
263
301
  var BackendClient_default = BackendClient;
@@ -3063,7 +3101,10 @@ var ThunderIDBrowserClient = class ThunderIDBrowserClient extends ThunderIDJavaS
3063
3101
  * - Embedded mode starts an `AUTHENTICATION` flow with `ouId`, presenting the SSO session handles kept
3064
3102
  * from earlier flow steps. A flow that needs input fails with `interaction_required`; render the
3065
3103
  * sign-in for the unit instead.
3066
- * - Backend mode sends the page to the backend's sign-in endpoint with `ouId`.
3104
+ * - Backend mode sends `POST {ouSwitch}?ouId=` with `{"ouId"}` as a JSON body to the backend, which
3105
+ * switches its session, then reads the session again, so the page stays where it is. When the
3106
+ * backend needs the user, or answers `401`, it sends the page to the backend's sign-in endpoint
3107
+ * with `ouId` (unless `redirect` is `false`). The extra parameters do not apply.
3067
3108
  *
3068
3109
  * A failed switch leaves the previous session as it was.
3069
3110
  *
@@ -3072,11 +3113,7 @@ var ThunderIDBrowserClient = class ThunderIDBrowserClient extends ThunderIDJavaS
3072
3113
  async switchOrganizationUnit(organizationUnitId, options = {}) {
3073
3114
  await this.isInitialized();
3074
3115
  if (typeof organizationUnitId !== "string" || organizationUnitId.trim() === "") throw new ThunderIDAPIError("An organization unit identifier is required.", OrganizationUnitSwitchErrorCodes.InvalidOrganizationUnit, "browser", 400);
3075
- if (this._backend) {
3076
- location.href = this._backend.getSignInUrl(`${window.location.pathname}${window.location.search}`, organizationUnitId);
3077
- await SPAUtils_default.waitTillPageRedirect();
3078
- return;
3079
- }
3116
+ if (this._backend) return this._switchOrganizationUnitOnBackend(organizationUnitId, options.redirect !== false);
3080
3117
  const { redirect = true,...params } = options;
3081
3118
  const sm = this.getStorageManager();
3082
3119
  const config = await sm.getConfigData();
@@ -3110,6 +3147,28 @@ var ThunderIDBrowserClient = class ThunderIDBrowserClient extends ThunderIDJavaS
3110
3147
  await SPAUtils_default.waitTillPageRedirect();
3111
3148
  }
3112
3149
  /**
3150
+ * Backend mode: asks the backend's `ouSwitch` endpoint to switch, then reads its session again, so the
3151
+ * page stays where it is. When the backend needs the user, the page goes to the backend's sign-in for
3152
+ * the unit instead, unless `redirect` is off.
3153
+ */
3154
+ async _switchOrganizationUnitOnBackend(organizationUnitId, redirect) {
3155
+ let refusal;
3156
+ try {
3157
+ refusal = await this._backend.switchOrganizationUnit(organizationUnitId);
3158
+ } catch (error) {
3159
+ throw ThunderIDBrowserClient._toSwitchError(void 0, error);
3160
+ }
3161
+ if (refusal) {
3162
+ if (!redirect || !refusal.error || !INTERACTION_ERRORS.includes(refusal.error)) throw ThunderIDBrowserClient._toSwitchError(refusal.error);
3163
+ location.href = this._backend.getSignInUrl(`${window.location.pathname}${window.location.search}`, organizationUnitId);
3164
+ await SPAUtils_default.waitTillPageRedirect();
3165
+ return;
3166
+ }
3167
+ const user = await this.getUser();
3168
+ this._emitHook(Hooks_default.SignIn, user);
3169
+ return user;
3170
+ }
3171
+ /**
3113
3172
  * Switches back-channel by exchanging the session's device secret at the unit's token endpoint.
3114
3173
  *
3115
3174
  * @returns The user, `false` when the server needs the user, or `undefined` when the session holds no