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