@witnium/witniumchain 0.27.0 → 0.28.1

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
@@ -180,6 +180,9 @@ var WitniumchainClient = class {
180
180
  // jar) get the refresh token in the response body and the SDK stashes it
181
181
  // here as a fallback. Either path drives `refreshAccessToken` identically.
182
182
  refreshToken;
183
+ // The ID token from the last code exchange or refresh, kept for
184
+ // RP-initiated logout (`id_token_hint`). Memory only, like the tokens.
185
+ idToken;
183
186
  // Sentinel: the most recent token response set an HttpOnly refresh cookie.
184
187
  // The SDK can't directly observe an HttpOnly cookie, but the response body
185
188
  // tells us indirectly — server strips `refresh_token` when it sets the
@@ -237,7 +240,7 @@ var WitniumchainClient = class {
237
240
  apiKeys;
238
241
  /** The signed-in member's dashboard signing key on a managed contract. `client.dashboardKey.*`. */
239
242
  dashboardKey;
240
- /** The home org's managed contract: pause, resume, activity. `client.orgContract.*`. */
243
+ /** The home org's managed contract: pause, resume, activity, keys you hold. `client.orgContract.*`. */
241
244
  orgContract;
242
245
  keyRequests;
243
246
  constructor(config) {
@@ -303,36 +306,36 @@ var WitniumchainClient = class {
303
306
  // Backing methods for the `orgUsers` namespace. All resolve the caller's
304
307
  // org + org-admin role server-side from the session — no orgId in the path.
305
308
  getDashboardKey() {
306
- return this.req("GET", "/v1/account/dashboard-key", { auth: "SessionCookie" });
309
+ return this.req("GET", "/v1/account/dashboard-key", { auth: "SessionOrBearer" });
307
310
  }
308
311
  ensureDashboardKey() {
309
- return this.req("POST", "/v1/account/dashboard-key", { auth: "SessionCookie" });
312
+ return this.req("POST", "/v1/account/dashboard-key", { auth: "SessionOrBearer" });
310
313
  }
311
314
  /** Revoke your own added dashboard key on chain; 409 for the organization's base key. */
312
315
  revokeDashboardKey() {
313
- return this.req("DELETE", "/v1/account/dashboard-key", { auth: "SessionCookie", expectNoContent: true });
316
+ return this.req("DELETE", "/v1/account/dashboard-key", { auth: "SessionOrBearer", expectNoContent: true });
314
317
  }
315
318
  pauseOrgContract() {
316
- return this.req("POST", "/v1/orgs/me/contract/pause", { auth: "SessionCookie" });
319
+ return this.req("POST", "/v1/orgs/me/contract/pause", { auth: "SessionOrBearer" });
317
320
  }
318
321
  /**
319
322
  * Add a key you hold to the org's managed contract (plan §17.7): org
320
- * admins, after a step-up. Witnium signs the add; the key counts once
321
- * the returned op confirms.
323
+ * admins, after a step-up (a bearer token needs a fresh `auth_time`).
324
+ * Witnium signs the add; the key counts once the returned op confirms.
322
325
  */
323
326
  addOrgContractKey(body) {
324
- return this.req("POST", "/v1/orgs/me/contract/keys", { auth: "SessionCookie", body });
327
+ return this.req("POST", "/v1/orgs/me/contract/keys", { auth: "SessionOrBearer", body });
325
328
  }
326
329
  /** Revoke a key added with {@link addOrgContractKey} (org admins). */
327
330
  removeOrgContractKey(publicKey) {
328
- return this.req("DELETE", `/v1/orgs/me/contract/keys/${encodeURIComponent(publicKey)}`, { auth: "SessionCookie" });
331
+ return this.req("DELETE", `/v1/orgs/me/contract/keys/${encodeURIComponent(publicKey)}`, { auth: "SessionOrBearer" });
329
332
  }
330
333
  /** Needs a step-up within 5 minutes (`client.security.stepUp`). */
331
334
  resumeOrgContract() {
332
- return this.req("POST", "/v1/orgs/me/contract/resume", { auth: "SessionCookie" });
335
+ return this.req("POST", "/v1/orgs/me/contract/resume", { auth: "SessionOrBearer" });
333
336
  }
334
337
  getContractActivity(limit) {
335
- return this.req("GET", "/v1/orgs/me/owner-operations", { auth: "SessionCookie", query: { limit } });
338
+ return this.req("GET", "/v1/orgs/me/owner-operations", { auth: "SessionOrBearer", query: { limit } });
336
339
  }
337
340
  /** Create an API key (org admin, after a step-up). The secret is in this response only. */
338
341
  createApiKey(body) {
@@ -352,7 +355,7 @@ var WitniumchainClient = class {
352
355
  return this.req("PATCH", "/v1/orgs/me", { auth: "SessionCookie", body });
353
356
  }
354
357
  listMembers() {
355
- return this.req("GET", "/v1/orgs/me/members", { auth: "SessionCookie" });
358
+ return this.req("GET", "/v1/orgs/me/members", { auth: "SessionOrBearer" });
356
359
  }
357
360
  inviteMember(body) {
358
361
  return this.req("POST", "/v1/orgs/me/members/invite", {
@@ -429,17 +432,17 @@ var WitniumchainClient = class {
429
432
  return this.req("POST", "/v1/account/signing-key-requests", { auth: "SessionOrBearer", body });
430
433
  }
431
434
  listSigningKeyRequests() {
432
- return this.req("GET", "/v1/account/signing-key-requests", { auth: "SessionCookie" });
435
+ return this.req("GET", "/v1/account/signing-key-requests", { auth: "SessionOrBearer" });
433
436
  }
434
437
  approveSigningKeyRequest(id, body) {
435
438
  return this.req("POST", `/v1/account/signing-key-requests/${encodeURIComponent(id)}/approve`, {
436
- auth: "SessionCookie",
439
+ auth: "SessionOrBearer",
437
440
  body
438
441
  });
439
442
  }
440
443
  declineSigningKeyRequest(id) {
441
444
  return this.req("POST", `/v1/account/signing-key-requests/${encodeURIComponent(id)}/decline`, {
442
- auth: "SessionCookie",
445
+ auth: "SessionOrBearer",
443
446
  expectNoContent: true
444
447
  });
445
448
  }
@@ -493,12 +496,12 @@ var WitniumchainClient = class {
493
496
  // ────────────────────────────────────────────────────────────────────────
494
497
  createCheckoutSession(body) {
495
498
  return this.req("POST", "/v1/billing/checkout", {
496
- auth: "SessionCookie",
499
+ auth: "SessionOrBearer",
497
500
  body
498
501
  });
499
502
  }
500
503
  createPortalSession() {
501
- return this.req("GET", "/v1/billing/portal", { auth: "SessionCookie" });
504
+ return this.req("GET", "/v1/billing/portal", { auth: "SessionOrBearer" });
502
505
  }
503
506
  // Webhook endpoint is intentionally NOT exposed: only Stripe should call it.
504
507
  // ────────────────────────────────────────────────────────────────────────
@@ -572,13 +575,13 @@ var WitniumchainClient = class {
572
575
  }
573
576
  pauseContract(body) {
574
577
  return this.req("POST", "/v1/contracts/pause", {
575
- auth: "SessionCookie",
578
+ auth: "SessionOrBearer",
576
579
  body
577
580
  });
578
581
  }
579
582
  unpauseContract(body) {
580
583
  return this.req("POST", "/v1/contracts/unpause", {
581
- auth: "SessionCookie",
584
+ auth: "SessionOrBearer",
582
585
  body
583
586
  });
584
587
  }
@@ -679,10 +682,10 @@ var WitniumchainClient = class {
679
682
  // Users / account (/v1/account/*)
680
683
  // ────────────────────────────────────────────────────────────────────────
681
684
  getAccount() {
682
- return this.req("GET", "/v1/account", { auth: "SessionCookie" });
685
+ return this.req("GET", "/v1/account", { auth: "SessionOrBearer" });
683
686
  }
684
687
  getLedger() {
685
- return this.req("GET", "/v1/account/ledger", { auth: "SessionCookie" });
688
+ return this.req("GET", "/v1/account/ledger", { auth: "SessionOrBearer" });
686
689
  }
687
690
  /**
688
691
  * DEV-ONLY: hard-delete the authenticated user + their org + contracts +
@@ -894,6 +897,15 @@ var WitniumchainClient = class {
894
897
  url.searchParams.set("code_challenge", challenge);
895
898
  url.searchParams.set("code_challenge_method", "S256");
896
899
  if (args.prompt) url.searchParams.set("prompt", args.prompt);
900
+ if (args.maxAge !== void 0) {
901
+ if (!Number.isInteger(args.maxAge) || args.maxAge < 0) {
902
+ throw new WitniumchainApiError({
903
+ status: 0,
904
+ message: `WitniumchainClient: beginOAuthLogin \`maxAge\` must be a non-negative integer (got ${args.maxAge}).`
905
+ });
906
+ }
907
+ url.searchParams.set("max_age", String(args.maxAge));
908
+ }
897
909
  const storage = this.verifierStorageOrDefault();
898
910
  storage.set(state, verifier);
899
911
  storage.set(redirectUriKey(state), args.redirectUri);
@@ -1059,6 +1071,7 @@ var WitniumchainClient = class {
1059
1071
  this.accessToken = void 0;
1060
1072
  this.accessTokenFromSession = false;
1061
1073
  this.refreshToken = void 0;
1074
+ this.idToken = void 0;
1062
1075
  this.hasRefreshCookie = false;
1063
1076
  this.sessionExchangeInFlight = void 0;
1064
1077
  this.refreshInFlight = void 0;
@@ -1117,6 +1130,47 @@ var WitniumchainClient = class {
1117
1130
  signOut() {
1118
1131
  this.invalidateCredentials();
1119
1132
  }
1133
+ /**
1134
+ * End the session at the authorization server (OIDC RP-Initiated Logout).
1135
+ *
1136
+ * Builds the `end_session_endpoint` URL with `id_token_hint` (the ID token
1137
+ * from {@link completeOAuthLogin}), `client_id` and
1138
+ * `post_logout_redirect_uri`, then forgets every in-memory credential like
1139
+ * {@link signOut}. The caller does the redirect
1140
+ * (`window.location.assign(result.logoutUrl)`).
1141
+ *
1142
+ * Throws when no `oauthClientId` is configured, when this client holds no
1143
+ * ID token (the login was not completed here, or it did not ask for
1144
+ * `openid`), or when the discovery document has no `end_session_endpoint`.
1145
+ */
1146
+ async beginOAuthLogout(args) {
1147
+ if (!this.oauthClientId) {
1148
+ throw new WitniumchainApiError({
1149
+ status: 0,
1150
+ message: "WitniumchainClient: beginOAuthLogout requires `oauthClientId` to be set on the constructor."
1151
+ });
1152
+ }
1153
+ if (!this.idToken) {
1154
+ throw new WitniumchainApiError({
1155
+ status: 0,
1156
+ message: "WitniumchainClient: beginOAuthLogout needs the ID token from completeOAuthLogin (request the `openid` scope and complete the login with this client)."
1157
+ });
1158
+ }
1159
+ const discovery = await this.fetchDiscovery();
1160
+ if (!discovery.end_session_endpoint) {
1161
+ throw new WitniumchainApiError({
1162
+ status: 0,
1163
+ message: "OIDC discovery doc has no `end_session_endpoint`: the authorization server does not offer RP-initiated logout."
1164
+ });
1165
+ }
1166
+ const url = new URL(discovery.end_session_endpoint);
1167
+ url.searchParams.set("id_token_hint", this.idToken);
1168
+ url.searchParams.set("client_id", this.oauthClientId);
1169
+ url.searchParams.set("post_logout_redirect_uri", args.postLogoutRedirectUri);
1170
+ if (args.state !== void 0) url.searchParams.set("state", args.state);
1171
+ this.invalidateCredentials();
1172
+ return { logoutUrl: url.toString() };
1173
+ }
1120
1174
  // ────────────────────────────────────────────────────────────────────────
1121
1175
  // Internal: OAuth flow helpers
1122
1176
  // ────────────────────────────────────────────────────────────────────────
@@ -1159,6 +1213,7 @@ var WitniumchainClient = class {
1159
1213
  return {
1160
1214
  authorization_endpoint: parsed.authorization_endpoint,
1161
1215
  token_endpoint: parsed.token_endpoint,
1216
+ end_session_endpoint: parsed.end_session_endpoint,
1162
1217
  issuer: parsed.issuer ?? this.baseUrl
1163
1218
  };
1164
1219
  })();
@@ -1210,6 +1265,7 @@ var WitniumchainClient = class {
1210
1265
  });
1211
1266
  }
1212
1267
  this.accessToken = tokens.access_token;
1268
+ if (tokens.id_token) this.idToken = tokens.id_token;
1213
1269
  this.accessTokenFromSession = false;
1214
1270
  if (tokens.refresh_token) {
1215
1271
  this.refreshToken = tokens.refresh_token;
@@ -1299,9 +1355,11 @@ var WitniumchainClient = class {
1299
1355
  headers,
1300
1356
  body: bodyString,
1301
1357
  signal: controller.signal,
1302
- // Send cookies cross-origin when the consumer is a browser using
1303
- // SessionCookie auth via document.cookie.
1304
- credentials: "include"
1358
+ // A request that carries a bearer credential sends no cookies: an
1359
+ // OAuth client on another site must not depend on (or leak) a
1360
+ // third-party cookie. Everything else sends cookies cross-origin for
1361
+ // a browser using the cookie session.
1362
+ credentials: headers["authorization"] === void 0 ? "include" : "omit"
1305
1363
  });
1306
1364
  } catch (err) {
1307
1365
  throw new WitniumchainApiError({
@@ -1424,7 +1482,7 @@ var WitniumchainClient = class {
1424
1482
  return;
1425
1483
  }
1426
1484
  case "SessionOrBearer": {
1427
- if (this.accessToken) headers["authorization"] = `Bearer ${this.accessToken}`;
1485
+ if (this.accessToken && !this.accessTokenFromSession) headers["authorization"] = `Bearer ${this.accessToken}`;
1428
1486
  else if (this.cfg.sessionCookie) headers["cookie"] = `wac_session=${this.cfg.sessionCookie}`;
1429
1487
  return;
1430
1488
  }