axiam-sdk 1.0.0-alpha2 → 1.0.0-alpha21

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.
Files changed (64) hide show
  1. package/README.md +219 -8
  2. package/dist/amqp/index.d.cts +1 -1
  3. package/dist/amqp/index.d.ts +1 -1
  4. package/dist/amqp/index.js +4 -0
  5. package/dist/amqp/index.js.map +1 -1
  6. package/dist/amqp/index.mjs +4 -0
  7. package/dist/amqp/index.mjs.map +1 -1
  8. package/dist/{authzCore-xwmCivHH.d.ts → authzCore-DtSN59if.d.ts} +19 -2
  9. package/dist/{authzCore-CV-Td-V1.d.cts → authzCore-DuRI1aJf.d.cts} +19 -2
  10. package/dist/client-B_FqmZ0i.d.cts +72 -0
  11. package/dist/client-Dn1fs5MH.d.ts +72 -0
  12. package/dist/cookieJar-BTDnjjbL.d.cts +122 -0
  13. package/dist/cookieJar-Cl8Lq9dL.d.ts +122 -0
  14. package/dist/grpc/index.d.cts +113 -6
  15. package/dist/grpc/index.d.ts +113 -6
  16. package/dist/grpc/index.js +435 -18
  17. package/dist/grpc/index.js.map +1 -1
  18. package/dist/grpc/index.mjs +434 -19
  19. package/dist/grpc/index.mjs.map +1 -1
  20. package/dist/index.d.cts +4 -2
  21. package/dist/index.d.ts +4 -2
  22. package/dist/index.js +209 -13
  23. package/dist/index.js.map +1 -1
  24. package/dist/index.mjs +204 -14
  25. package/dist/index.mjs.map +1 -1
  26. package/dist/jwks-BgojMGC5.d.cts +152 -0
  27. package/dist/jwks-BgojMGC5.d.ts +152 -0
  28. package/dist/middleware/index.d.cts +208 -8
  29. package/dist/middleware/index.d.ts +208 -8
  30. package/dist/middleware/index.js +1107 -1
  31. package/dist/middleware/index.js.map +1 -1
  32. package/dist/middleware/index.mjs +1100 -3
  33. package/dist/middleware/index.mjs.map +1 -1
  34. package/dist/nestjs/index.d.cts +2 -2
  35. package/dist/nestjs/index.d.ts +2 -2
  36. package/dist/nestjs/index.js +3 -0
  37. package/dist/nestjs/index.js.map +1 -1
  38. package/dist/nestjs/index.mjs +3 -0
  39. package/dist/nestjs/index.mjs.map +1 -1
  40. package/dist/node/index.d.cts +50 -94
  41. package/dist/node/index.d.ts +50 -94
  42. package/dist/node/index.js +1039 -15
  43. package/dist/node/index.js.map +1 -1
  44. package/dist/node/index.mjs +1023 -16
  45. package/dist/node/index.mjs.map +1 -1
  46. package/dist/oidcState-BGxqWUun.d.cts +598 -0
  47. package/dist/oidcState-CkFuCjpM.d.ts +598 -0
  48. package/dist/rest/index.d.cts +107 -5
  49. package/dist/rest/index.d.ts +107 -5
  50. package/dist/rest/index.js +209 -13
  51. package/dist/rest/index.js.map +1 -1
  52. package/dist/rest/index.mjs +204 -14
  53. package/dist/rest/index.mjs.map +1 -1
  54. package/dist/sensitive-BC_s7Rrd.d.cts +39 -0
  55. package/dist/sensitive-BC_s7Rrd.d.ts +39 -0
  56. package/dist/session-BWUWhhyB.d.cts +154 -0
  57. package/dist/session-BWUWhhyB.d.ts +154 -0
  58. package/package.json +2 -2
  59. package/dist/client-BPg9kfKV.d.ts +0 -150
  60. package/dist/client-CAeQw1a-.d.cts +0 -150
  61. package/dist/jwks-CX-nhD6A.d.cts +0 -29
  62. package/dist/jwks-CX-nhD6A.d.ts +0 -29
  63. package/dist/sensitive-CWBH_tHr.d.cts +0 -12
  64. package/dist/sensitive-CWBH_tHr.d.ts +0 -12
package/dist/index.d.cts CHANGED
@@ -1,4 +1,6 @@
1
- export { a as AxiamClient, A as AxiamClientOptions, S as SharedSession } from './client-CAeQw1a-.cjs';
2
- export { RetryOptions, SKIP_REFRESH, withRetry } from './rest/index.cjs';
1
+ export { A as AxiamClient } from './client-B_FqmZ0i.cjs';
2
+ export { A as AxiamClientOptions, S as SharedSession } from './session-BWUWhhyB.cjs';
3
+ export { AuthError, AuthzError, AxiamError, IdTokenFailureReason, NetworkError, OAuthProtocolError, RetryOptions, SKIP_REFRESH, withRetry } from './rest/index.cjs';
3
4
  export { A as AccessCheck, a as AccessDecision, b as AxiamUserInfo, L as LoginResult } from './types-BVkMoqLU.cjs';
5
+ export { S as Sensitive } from './sensitive-BC_s7Rrd.cjs';
4
6
  import 'axios';
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- export { a as AxiamClient, A as AxiamClientOptions, S as SharedSession } from './client-BPg9kfKV.js';
2
- export { RetryOptions, SKIP_REFRESH, withRetry } from './rest/index.js';
1
+ export { A as AxiamClient } from './client-Dn1fs5MH.js';
2
+ export { A as AxiamClientOptions, S as SharedSession } from './session-BWUWhhyB.js';
3
+ export { AuthError, AuthzError, AxiamError, IdTokenFailureReason, NetworkError, OAuthProtocolError, RetryOptions, SKIP_REFRESH, withRetry } from './rest/index.js';
3
4
  export { A as AccessCheck, a as AccessDecision, b as AxiamUserInfo, L as LoginResult } from './types-BVkMoqLU.js';
5
+ export { S as Sensitive } from './sensitive-BC_s7Rrd.js';
4
6
  import 'axios';
package/dist/index.js CHANGED
@@ -21,14 +21,40 @@ var AxiamError = class extends Error {
21
21
  }
22
22
  };
23
23
  var AuthError = class _AuthError extends AxiamError {
24
- constructor(message) {
24
+ /**
25
+ * Optional stable, machine-readable reason code for the failure.
26
+ *
27
+ * Populated by the CONTRACT.md §12.4 ID-token validation checklist with
28
+ * one of the seven codes §12.3 rule 3 enumerates (see
29
+ * {@link IdTokenFailureReason}); `undefined` for every other `AuthError`.
30
+ * It is a *code*, never free text, so callers can branch on it without
31
+ * parsing `message`.
32
+ */
33
+ reason;
34
+ constructor(message, reason) {
25
35
  super(message);
26
36
  this.name = "AuthError";
37
+ this.reason = reason;
27
38
  Object.setPrototypeOf(this, _AuthError.prototype);
28
39
  }
29
40
  };
41
+ var OAuthProtocolError = class _OAuthProtocolError extends AuthError {
42
+ /** The RFC 6749 `error` code (e.g. `"invalid_grant"`, `"invalid_client"`, `"unsupported_grant_type"`). */
43
+ error;
44
+ /** The server's human-readable `error_description`. Never contains token material. */
45
+ errorDescription;
46
+ constructor(error, errorDescription) {
47
+ super(`${error}: ${errorDescription}`);
48
+ this.name = "OAuthProtocolError";
49
+ this.error = error;
50
+ this.errorDescription = errorDescription;
51
+ Object.setPrototypeOf(this, _OAuthProtocolError.prototype);
52
+ }
53
+ };
30
54
  var AuthzError = class _AuthzError extends AxiamError {
55
+ /** The denied action, when the server reported it. */
31
56
  action;
57
+ /** The resource the denied action targeted, when the server reported it. */
32
58
  resourceId;
33
59
  constructor(message, action, resourceId) {
34
60
  super(message);
@@ -39,6 +65,7 @@ var AuthzError = class _AuthzError extends AxiamError {
39
65
  }
40
66
  };
41
67
  var NetworkError = class _NetworkError extends AxiamError {
68
+ /** The underlying transport error, with every non-allowlisted response header redacted (§2, X-3). */
42
69
  cause;
43
70
  constructor(message, cause) {
44
71
  super(message);
@@ -58,6 +85,24 @@ function extractAuthzFieldsFromBody(body) {
58
85
  const resourceId = typeof record.resource_id === "string" ? record.resource_id : void 0;
59
86
  return { action, resourceId };
60
87
  }
88
+ var OAUTH2_PATH_PREFIX = "/oauth2/";
89
+ function isOAuth2EndpointUrl(url) {
90
+ if (!url) {
91
+ return false;
92
+ }
93
+ try {
94
+ return new URL(url).pathname.startsWith(OAUTH2_PATH_PREFIX);
95
+ } catch {
96
+ return url.startsWith(OAUTH2_PATH_PREFIX) || url.includes(OAUTH2_PATH_PREFIX);
97
+ }
98
+ }
99
+ function isOAuth2ErrorBody(body) {
100
+ if (body === null || typeof body !== "object") {
101
+ return false;
102
+ }
103
+ const record = body;
104
+ return typeof record.error === "string" && typeof record.error_description === "string";
105
+ }
61
106
  var SAFE_RESPONSE_HEADERS = /* @__PURE__ */ new Set([
62
107
  "content-type",
63
108
  "content-length",
@@ -95,6 +140,9 @@ function sanitizeAxiosError(err) {
95
140
  };
96
141
  }
97
142
  function mapHttpStatusToError(status, message, ctx) {
143
+ if ((status === 400 || status === 401) && isOAuth2EndpointUrl(ctx?.url) && isOAuth2ErrorBody(ctx?.body)) {
144
+ return new OAuthProtocolError(ctx.body.error, ctx.body.error_description);
145
+ }
98
146
  if (status === 401) {
99
147
  return new AuthError(message);
100
148
  }
@@ -105,6 +153,33 @@ function mapHttpStatusToError(status, message, ctx) {
105
153
  return new NetworkError(message, sanitizeAxiosError(ctx?.cause));
106
154
  }
107
155
 
156
+ // src/core/sensitive.ts
157
+ var REDACTED = "[SENSITIVE]";
158
+ var NODE_INSPECT_CUSTOM = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
159
+ var Sensitive = class {
160
+ #value;
161
+ /** Wrap `value` so it can never be logged or serialized by accident. */
162
+ constructor(value) {
163
+ this.#value = value;
164
+ }
165
+ /** @internal package-only accessor. Never pass the return value to a log/serialize sink. */
166
+ expose() {
167
+ return this.#value;
168
+ }
169
+ /** Returns the `"[SENSITIVE]"` placeholder — never the wrapped value. */
170
+ toString() {
171
+ return REDACTED;
172
+ }
173
+ /** Makes `JSON.stringify()` emit the `"[SENSITIVE]"` placeholder. */
174
+ toJSON() {
175
+ return REDACTED;
176
+ }
177
+ /** Makes `console.log`/`util.inspect` print the `"[SENSITIVE]"` placeholder. */
178
+ [NODE_INSPECT_CUSTOM]() {
179
+ return REDACTED;
180
+ }
181
+ };
182
+
108
183
  // src/core/csrf.ts
109
184
  var CSRF_METHODS = /* @__PURE__ */ new Set(["post", "put", "patch", "delete"]);
110
185
  var CSRF_COOKIE_REGEX = /(?:^|;\s*)axiam_csrf=([^;]*)/;
@@ -143,9 +218,34 @@ createRefreshGuard();
143
218
 
144
219
  // src/core/config.ts
145
220
  var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
221
+ var CERT_PEM_MARKER = "-----BEGIN CERTIFICATE-----";
222
+ var PRIVATE_KEY_PEM_RE = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY-----/;
223
+ function resolveClientIdentity(options) {
224
+ const hasCert = options.clientCert !== void 0;
225
+ const hasKey = options.clientKey !== void 0;
226
+ if (hasCert !== hasKey) {
227
+ throw new Error(
228
+ "clientCert and clientKey must be provided together for mutual TLS (one was given without the other) (CONTRACT.md \xA76.1)."
229
+ );
230
+ }
231
+ if (!hasCert || !hasKey) {
232
+ return void 0;
233
+ }
234
+ if (!options.clientCert.includes(CERT_PEM_MARKER)) {
235
+ throw new Error(
236
+ 'clientCert must be a PEM-encoded certificate chain (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76.1).'
237
+ );
238
+ }
239
+ if (!PRIVATE_KEY_PEM_RE.test(options.clientKey)) {
240
+ throw new Error(
241
+ 'clientKey must be a PEM-encoded private key (expected to contain "-----BEGIN PRIVATE KEY-----" or "-----BEGIN RSA PRIVATE KEY-----") (CONTRACT.md \xA76.1).'
242
+ );
243
+ }
244
+ return { cert: options.clientCert, key: new Sensitive(options.clientKey) };
245
+ }
146
246
 
147
247
  // src/rest/session.ts
148
- var PEM_MARKER = "-----BEGIN CERTIFICATE-----";
248
+ var PEM_MARKER = CERT_PEM_MARKER;
149
249
  var SharedSession = class {
150
250
  /** The underlying axios instance this session's requests are issued through, pre-configured with `baseUrl`, credential forwarding, and (Node) an optional custom-CA `httpsAgent`. */
151
251
  axios;
@@ -153,6 +253,28 @@ var SharedSession = class {
153
253
  baseUrl;
154
254
  /** The resolved tenant identifier (`tenantSlug` or `tenantId`) injected as the `X-Tenant-ID` header on every same-origin request (§5.2). */
155
255
  tenantHeaderValue;
256
+ /** Configured tenant UUID (`AxiamClientOptions.tenantId`), if the client was built with the UUID form. */
257
+ tenantId;
258
+ /** Configured tenant slug (`AxiamClientOptions.tenantSlug`), if the client was built with the slug form. */
259
+ tenantSlug;
260
+ /** Configured organization UUID (`AxiamClientOptions.orgId`), if supplied (§5). */
261
+ orgId;
262
+ /** Configured organization slug (`AxiamClientOptions.orgSlug`), if supplied (§5). */
263
+ orgSlug;
264
+ /**
265
+ * Resolved tenant UUID used to build the `refresh` body (`RefreshRequest`
266
+ * requires the UUID form, not a slug). Seeded from `orgId`/`tenantId` config
267
+ * when the UUID was supplied, then updated from the access-token `tenant_id`
268
+ * claim after each successful login/refresh (Node persona only — the browser
269
+ * cannot read the httpOnly access-token cookie).
270
+ */
271
+ resolvedTenantId;
272
+ /**
273
+ * Resolved organization UUID used to build the `refresh` body. Seeded from
274
+ * `orgId` config, then updated from the access-token `org_id` claim after a
275
+ * successful login (Node persona). Mirrors the Rust SDK's `resolved_org_id`.
276
+ */
277
+ resolvedOrgId;
156
278
  /** Mutable CSRF token store — populated by the request/response interceptors (D-05). */
157
279
  csrfToken;
158
280
  /** Set true once a successful login/verifyMfa has completed. */
@@ -172,8 +294,55 @@ var SharedSession = class {
172
294
  this.axios = axiosInstance;
173
295
  this.baseUrl = options.baseUrl;
174
296
  this.tenantHeaderValue = tenantHeaderValue;
297
+ this.tenantId = options.tenantId;
298
+ this.tenantSlug = options.tenantSlug;
299
+ this.orgId = options.orgId;
300
+ this.orgSlug = options.orgSlug;
301
+ this.resolvedTenantId = options.tenantId;
302
+ this.resolvedOrgId = options.orgId;
175
303
  this.refreshGuard = createRefreshGuard();
176
304
  }
305
+ /**
306
+ * Build the `POST /api/v1/auth/refresh` request body (§1). The server's
307
+ * `RefreshRequest` requires both `tenant_id` and `org_id` as UUIDs, so this
308
+ * emits the resolved UUIDs — from the access-token claims after login, or
309
+ * from UUID-form construction options as a fallback. Fields that could not
310
+ * be resolved are omitted (the server then answers with a clear 400), rather
311
+ * than sending a slug where a UUID is required.
312
+ */
313
+ buildRefreshBody() {
314
+ const body = {};
315
+ if (this.resolvedTenantId) {
316
+ body.tenant_id = this.resolvedTenantId;
317
+ }
318
+ if (this.resolvedOrgId) {
319
+ body.org_id = this.resolvedOrgId;
320
+ }
321
+ return body;
322
+ }
323
+ /**
324
+ * Build the `POST /api/v1/auth/login` request body (§1/§5). Carries the
325
+ * configured tenant and organization identifiers (UUID or slug form) in
326
+ * addition to the credentials — the server resolves the workspace from the
327
+ * body, not the `X-Tenant-ID` header, and rejects a login that omits org
328
+ * context. Undefined identifiers are omitted.
329
+ */
330
+ buildLoginBody(email, password) {
331
+ const body = { username_or_email: email, password };
332
+ if (this.tenantId) {
333
+ body.tenant_id = this.tenantId;
334
+ }
335
+ if (this.tenantSlug) {
336
+ body.tenant_slug = this.tenantSlug;
337
+ }
338
+ if (this.orgId) {
339
+ body.org_id = this.orgId;
340
+ }
341
+ if (this.orgSlug) {
342
+ body.org_slug = this.orgSlug;
343
+ }
344
+ return body;
345
+ }
177
346
  /**
178
347
  * Host-isolation guard (3A, defense in depth): returns `true` when `url`
179
348
  * targets a host other than this session's base origin — an absolute
@@ -207,11 +376,12 @@ function resolveTenantHeaderValue(options) {
207
376
  "AxiamClient construction requires a tenant: provide either tenantSlug or tenantId (CONTRACT.md \xA75)."
208
377
  );
209
378
  }
210
- function maybeBuildHttpsAgent(customCa) {
379
+ function maybeBuildHttpsAgent(customCa, identity) {
211
380
  if (!customCa) {
212
- return void 0;
213
- }
214
- if (!customCa.includes(PEM_MARKER)) {
381
+ if (!identity) {
382
+ return void 0;
383
+ }
384
+ } else if (!customCa.includes(PEM_MARKER)) {
215
385
  throw new Error(
216
386
  'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
217
387
  );
@@ -220,7 +390,11 @@ function maybeBuildHttpsAgent(customCa) {
220
390
  return void 0;
221
391
  }
222
392
  const https = __require("https");
223
- return new https.Agent({ ca: customCa });
393
+ return new https.Agent({
394
+ ...customCa ? { ca: customCa } : {},
395
+ // rejectUnauthorized is intentionally left at its secure default (true).
396
+ ...identity ? { cert: identity.cert, key: identity.key.expose() } : {}
397
+ });
224
398
  }
225
399
  function createSession(options) {
226
400
  const tenantHeaderValue = resolveTenantHeaderValue(options);
@@ -229,7 +403,8 @@ function createSession(options) {
229
403
  'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
230
404
  );
231
405
  }
232
- const httpsAgent = maybeBuildHttpsAgent(options.customCa);
406
+ const clientIdentity = resolveClientIdentity(options);
407
+ const httpsAgent = maybeBuildHttpsAgent(options.customCa, clientIdentity);
233
408
  const axiosInstance = axios__default.default.create({
234
409
  baseURL: options.baseUrl,
235
410
  withCredentials: true,
@@ -249,7 +424,14 @@ function createSession(options) {
249
424
  }
250
425
 
251
426
  // src/rest/interceptors.ts
252
- var SKIP_REFRESH = ["/api/v1/auth/refresh", "/api/v1/auth/login", "/api/v1/auth/logout"];
427
+ var SKIP_REFRESH = [
428
+ "/api/v1/auth/refresh",
429
+ "/api/v1/auth/login",
430
+ "/api/v1/auth/logout",
431
+ "/oauth2/token",
432
+ "/oauth2/introspect",
433
+ "/oauth2/revoke"
434
+ ];
253
435
  function installCsrfInterceptor(axiosInstance, session) {
254
436
  axiosInstance.interceptors.request.use((config) => {
255
437
  if (session.isForeignHost(config.url)) {
@@ -280,7 +462,8 @@ function installRefreshInterceptor(axiosInstance, session) {
280
462
  originalRequest._retry = true;
281
463
  try {
282
464
  await session.refreshGuard(async () => {
283
- await session.axios.post("/api/v1/auth/refresh", {});
465
+ await session.axios.post("/api/v1/auth/refresh", session.buildRefreshBody());
466
+ await session.onAuthenticated?.();
284
467
  });
285
468
  return axiosInstance(originalRequest);
286
469
  } catch (refreshError) {
@@ -294,7 +477,13 @@ function installRefreshInterceptor(axiosInstance, session) {
294
477
  }
295
478
  }
296
479
  if (status === 401 && isSkipRefresh) {
297
- return Promise.reject(mapHttpStatusToError(401, "authentication failed"));
480
+ return Promise.reject(
481
+ mapHttpStatusToError(401, "authentication failed", {
482
+ url,
483
+ body: error.response.data,
484
+ cause: error
485
+ })
486
+ );
298
487
  }
299
488
  return Promise.reject(error);
300
489
  }
@@ -332,7 +521,7 @@ function extractErrorMessage(err) {
332
521
  return "request failed";
333
522
  }
334
523
  async function login(client, email, password) {
335
- const body = { username_or_email: email, password };
524
+ const body = client.session.buildLoginBody(email, password);
336
525
  try {
337
526
  const response = await client.session.axios.post(
338
527
  LOGIN_PATH,
@@ -373,7 +562,8 @@ async function verifyMfa(client, mfaToken, code) {
373
562
  }
374
563
  async function refresh(client) {
375
564
  try {
376
- await client.session.axios.post(REFRESH_PATH, {});
565
+ await client.session.axios.post(REFRESH_PATH, client.session.buildRefreshBody());
566
+ await client.session.onAuthenticated?.();
377
567
  } catch (err) {
378
568
  const status = extractAxiosStatus(err);
379
569
  if (status !== void 0) {
@@ -553,8 +743,14 @@ async function withRetry(fn, options) {
553
743
  throw lastError;
554
744
  }
555
745
 
746
+ exports.AuthError = AuthError;
747
+ exports.AuthzError = AuthzError;
556
748
  exports.AxiamClient = AxiamClient;
749
+ exports.AxiamError = AxiamError;
750
+ exports.NetworkError = NetworkError;
751
+ exports.OAuthProtocolError = OAuthProtocolError;
557
752
  exports.SKIP_REFRESH = SKIP_REFRESH;
753
+ exports.Sensitive = Sensitive;
558
754
  exports.SharedSession = SharedSession;
559
755
  exports.withRetry = withRetry;
560
756
  //# sourceMappingURL=index.js.map