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
@@ -15,14 +15,40 @@ var AxiamError = class extends Error {
15
15
  }
16
16
  };
17
17
  var AuthError = class _AuthError extends AxiamError {
18
- constructor(message) {
18
+ /**
19
+ * Optional stable, machine-readable reason code for the failure.
20
+ *
21
+ * Populated by the CONTRACT.md §12.4 ID-token validation checklist with
22
+ * one of the seven codes §12.3 rule 3 enumerates (see
23
+ * {@link IdTokenFailureReason}); `undefined` for every other `AuthError`.
24
+ * It is a *code*, never free text, so callers can branch on it without
25
+ * parsing `message`.
26
+ */
27
+ reason;
28
+ constructor(message, reason) {
19
29
  super(message);
20
30
  this.name = "AuthError";
31
+ this.reason = reason;
21
32
  Object.setPrototypeOf(this, _AuthError.prototype);
22
33
  }
23
34
  };
35
+ var OAuthProtocolError = class _OAuthProtocolError extends AuthError {
36
+ /** The RFC 6749 `error` code (e.g. `"invalid_grant"`, `"invalid_client"`, `"unsupported_grant_type"`). */
37
+ error;
38
+ /** The server's human-readable `error_description`. Never contains token material. */
39
+ errorDescription;
40
+ constructor(error, errorDescription) {
41
+ super(`${error}: ${errorDescription}`);
42
+ this.name = "OAuthProtocolError";
43
+ this.error = error;
44
+ this.errorDescription = errorDescription;
45
+ Object.setPrototypeOf(this, _OAuthProtocolError.prototype);
46
+ }
47
+ };
24
48
  var AuthzError = class _AuthzError extends AxiamError {
49
+ /** The denied action, when the server reported it. */
25
50
  action;
51
+ /** The resource the denied action targeted, when the server reported it. */
26
52
  resourceId;
27
53
  constructor(message, action, resourceId) {
28
54
  super(message);
@@ -33,6 +59,7 @@ var AuthzError = class _AuthzError extends AxiamError {
33
59
  }
34
60
  };
35
61
  var NetworkError = class _NetworkError extends AxiamError {
62
+ /** The underlying transport error, with every non-allowlisted response header redacted (§2, X-3). */
36
63
  cause;
37
64
  constructor(message, cause) {
38
65
  super(message);
@@ -52,6 +79,24 @@ function extractAuthzFieldsFromBody(body) {
52
79
  const resourceId = typeof record.resource_id === "string" ? record.resource_id : void 0;
53
80
  return { action, resourceId };
54
81
  }
82
+ var OAUTH2_PATH_PREFIX = "/oauth2/";
83
+ function isOAuth2EndpointUrl(url) {
84
+ if (!url) {
85
+ return false;
86
+ }
87
+ try {
88
+ return new URL(url).pathname.startsWith(OAUTH2_PATH_PREFIX);
89
+ } catch {
90
+ return url.startsWith(OAUTH2_PATH_PREFIX) || url.includes(OAUTH2_PATH_PREFIX);
91
+ }
92
+ }
93
+ function isOAuth2ErrorBody(body) {
94
+ if (body === null || typeof body !== "object") {
95
+ return false;
96
+ }
97
+ const record = body;
98
+ return typeof record.error === "string" && typeof record.error_description === "string";
99
+ }
55
100
  var SAFE_RESPONSE_HEADERS = /* @__PURE__ */ new Set([
56
101
  "content-type",
57
102
  "content-length",
@@ -89,6 +134,9 @@ function sanitizeAxiosError(err) {
89
134
  };
90
135
  }
91
136
  function mapHttpStatusToError(status, message, ctx) {
137
+ if ((status === 400 || status === 401) && isOAuth2EndpointUrl(ctx?.url) && isOAuth2ErrorBody(ctx?.body)) {
138
+ return new OAuthProtocolError(ctx.body.error, ctx.body.error_description);
139
+ }
92
140
  if (status === 401) {
93
141
  return new AuthError(message);
94
142
  }
@@ -99,6 +147,33 @@ function mapHttpStatusToError(status, message, ctx) {
99
147
  return new NetworkError(message, sanitizeAxiosError(ctx?.cause));
100
148
  }
101
149
 
150
+ // src/core/sensitive.ts
151
+ var REDACTED = "[SENSITIVE]";
152
+ var NODE_INSPECT_CUSTOM = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
153
+ var Sensitive = class {
154
+ #value;
155
+ /** Wrap `value` so it can never be logged or serialized by accident. */
156
+ constructor(value) {
157
+ this.#value = value;
158
+ }
159
+ /** @internal package-only accessor. Never pass the return value to a log/serialize sink. */
160
+ expose() {
161
+ return this.#value;
162
+ }
163
+ /** Returns the `"[SENSITIVE]"` placeholder — never the wrapped value. */
164
+ toString() {
165
+ return REDACTED;
166
+ }
167
+ /** Makes `JSON.stringify()` emit the `"[SENSITIVE]"` placeholder. */
168
+ toJSON() {
169
+ return REDACTED;
170
+ }
171
+ /** Makes `console.log`/`util.inspect` print the `"[SENSITIVE]"` placeholder. */
172
+ [NODE_INSPECT_CUSTOM]() {
173
+ return REDACTED;
174
+ }
175
+ };
176
+
102
177
  // src/core/csrf.ts
103
178
  var CSRF_METHODS = /* @__PURE__ */ new Set(["post", "put", "patch", "delete"]);
104
179
  var CSRF_COOKIE_REGEX = /(?:^|;\s*)axiam_csrf=([^;]*)/;
@@ -137,9 +212,34 @@ createRefreshGuard();
137
212
 
138
213
  // src/core/config.ts
139
214
  var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
215
+ var CERT_PEM_MARKER = "-----BEGIN CERTIFICATE-----";
216
+ var PRIVATE_KEY_PEM_RE = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY-----/;
217
+ function resolveClientIdentity(options) {
218
+ const hasCert = options.clientCert !== void 0;
219
+ const hasKey = options.clientKey !== void 0;
220
+ if (hasCert !== hasKey) {
221
+ throw new Error(
222
+ "clientCert and clientKey must be provided together for mutual TLS (one was given without the other) (CONTRACT.md \xA76.1)."
223
+ );
224
+ }
225
+ if (!hasCert || !hasKey) {
226
+ return void 0;
227
+ }
228
+ if (!options.clientCert.includes(CERT_PEM_MARKER)) {
229
+ throw new Error(
230
+ 'clientCert must be a PEM-encoded certificate chain (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76.1).'
231
+ );
232
+ }
233
+ if (!PRIVATE_KEY_PEM_RE.test(options.clientKey)) {
234
+ throw new Error(
235
+ 'clientKey must be a PEM-encoded private key (expected to contain "-----BEGIN PRIVATE KEY-----" or "-----BEGIN RSA PRIVATE KEY-----") (CONTRACT.md \xA76.1).'
236
+ );
237
+ }
238
+ return { cert: options.clientCert, key: new Sensitive(options.clientKey) };
239
+ }
140
240
 
141
241
  // src/rest/session.ts
142
- var PEM_MARKER = "-----BEGIN CERTIFICATE-----";
242
+ var PEM_MARKER = CERT_PEM_MARKER;
143
243
  var SharedSession = class {
144
244
  /** The underlying axios instance this session's requests are issued through, pre-configured with `baseUrl`, credential forwarding, and (Node) an optional custom-CA `httpsAgent`. */
145
245
  axios;
@@ -147,6 +247,28 @@ var SharedSession = class {
147
247
  baseUrl;
148
248
  /** The resolved tenant identifier (`tenantSlug` or `tenantId`) injected as the `X-Tenant-ID` header on every same-origin request (§5.2). */
149
249
  tenantHeaderValue;
250
+ /** Configured tenant UUID (`AxiamClientOptions.tenantId`), if the client was built with the UUID form. */
251
+ tenantId;
252
+ /** Configured tenant slug (`AxiamClientOptions.tenantSlug`), if the client was built with the slug form. */
253
+ tenantSlug;
254
+ /** Configured organization UUID (`AxiamClientOptions.orgId`), if supplied (§5). */
255
+ orgId;
256
+ /** Configured organization slug (`AxiamClientOptions.orgSlug`), if supplied (§5). */
257
+ orgSlug;
258
+ /**
259
+ * Resolved tenant UUID used to build the `refresh` body (`RefreshRequest`
260
+ * requires the UUID form, not a slug). Seeded from `orgId`/`tenantId` config
261
+ * when the UUID was supplied, then updated from the access-token `tenant_id`
262
+ * claim after each successful login/refresh (Node persona only — the browser
263
+ * cannot read the httpOnly access-token cookie).
264
+ */
265
+ resolvedTenantId;
266
+ /**
267
+ * Resolved organization UUID used to build the `refresh` body. Seeded from
268
+ * `orgId` config, then updated from the access-token `org_id` claim after a
269
+ * successful login (Node persona). Mirrors the Rust SDK's `resolved_org_id`.
270
+ */
271
+ resolvedOrgId;
150
272
  /** Mutable CSRF token store — populated by the request/response interceptors (D-05). */
151
273
  csrfToken;
152
274
  /** Set true once a successful login/verifyMfa has completed. */
@@ -166,8 +288,55 @@ var SharedSession = class {
166
288
  this.axios = axiosInstance;
167
289
  this.baseUrl = options.baseUrl;
168
290
  this.tenantHeaderValue = tenantHeaderValue;
291
+ this.tenantId = options.tenantId;
292
+ this.tenantSlug = options.tenantSlug;
293
+ this.orgId = options.orgId;
294
+ this.orgSlug = options.orgSlug;
295
+ this.resolvedTenantId = options.tenantId;
296
+ this.resolvedOrgId = options.orgId;
169
297
  this.refreshGuard = createRefreshGuard();
170
298
  }
299
+ /**
300
+ * Build the `POST /api/v1/auth/refresh` request body (§1). The server's
301
+ * `RefreshRequest` requires both `tenant_id` and `org_id` as UUIDs, so this
302
+ * emits the resolved UUIDs — from the access-token claims after login, or
303
+ * from UUID-form construction options as a fallback. Fields that could not
304
+ * be resolved are omitted (the server then answers with a clear 400), rather
305
+ * than sending a slug where a UUID is required.
306
+ */
307
+ buildRefreshBody() {
308
+ const body = {};
309
+ if (this.resolvedTenantId) {
310
+ body.tenant_id = this.resolvedTenantId;
311
+ }
312
+ if (this.resolvedOrgId) {
313
+ body.org_id = this.resolvedOrgId;
314
+ }
315
+ return body;
316
+ }
317
+ /**
318
+ * Build the `POST /api/v1/auth/login` request body (§1/§5). Carries the
319
+ * configured tenant and organization identifiers (UUID or slug form) in
320
+ * addition to the credentials — the server resolves the workspace from the
321
+ * body, not the `X-Tenant-ID` header, and rejects a login that omits org
322
+ * context. Undefined identifiers are omitted.
323
+ */
324
+ buildLoginBody(email, password) {
325
+ const body = { username_or_email: email, password };
326
+ if (this.tenantId) {
327
+ body.tenant_id = this.tenantId;
328
+ }
329
+ if (this.tenantSlug) {
330
+ body.tenant_slug = this.tenantSlug;
331
+ }
332
+ if (this.orgId) {
333
+ body.org_id = this.orgId;
334
+ }
335
+ if (this.orgSlug) {
336
+ body.org_slug = this.orgSlug;
337
+ }
338
+ return body;
339
+ }
171
340
  /**
172
341
  * Host-isolation guard (3A, defense in depth): returns `true` when `url`
173
342
  * targets a host other than this session's base origin — an absolute
@@ -201,11 +370,12 @@ function resolveTenantHeaderValue(options) {
201
370
  "AxiamClient construction requires a tenant: provide either tenantSlug or tenantId (CONTRACT.md \xA75)."
202
371
  );
203
372
  }
204
- function maybeBuildHttpsAgent(customCa) {
373
+ function maybeBuildHttpsAgent(customCa, identity) {
205
374
  if (!customCa) {
206
- return void 0;
207
- }
208
- if (!customCa.includes(PEM_MARKER)) {
375
+ if (!identity) {
376
+ return void 0;
377
+ }
378
+ } else if (!customCa.includes(PEM_MARKER)) {
209
379
  throw new Error(
210
380
  'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
211
381
  );
@@ -214,7 +384,11 @@ function maybeBuildHttpsAgent(customCa) {
214
384
  return void 0;
215
385
  }
216
386
  const https = __require("https");
217
- return new https.Agent({ ca: customCa });
387
+ return new https.Agent({
388
+ ...customCa ? { ca: customCa } : {},
389
+ // rejectUnauthorized is intentionally left at its secure default (true).
390
+ ...identity ? { cert: identity.cert, key: identity.key.expose() } : {}
391
+ });
218
392
  }
219
393
  function createSession(options) {
220
394
  const tenantHeaderValue = resolveTenantHeaderValue(options);
@@ -223,7 +397,8 @@ function createSession(options) {
223
397
  'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
224
398
  );
225
399
  }
226
- const httpsAgent = maybeBuildHttpsAgent(options.customCa);
400
+ const clientIdentity = resolveClientIdentity(options);
401
+ const httpsAgent = maybeBuildHttpsAgent(options.customCa, clientIdentity);
227
402
  const axiosInstance = axios.create({
228
403
  baseURL: options.baseUrl,
229
404
  withCredentials: true,
@@ -243,7 +418,14 @@ function createSession(options) {
243
418
  }
244
419
 
245
420
  // src/rest/interceptors.ts
246
- var SKIP_REFRESH = ["/api/v1/auth/refresh", "/api/v1/auth/login", "/api/v1/auth/logout"];
421
+ var SKIP_REFRESH = [
422
+ "/api/v1/auth/refresh",
423
+ "/api/v1/auth/login",
424
+ "/api/v1/auth/logout",
425
+ "/oauth2/token",
426
+ "/oauth2/introspect",
427
+ "/oauth2/revoke"
428
+ ];
247
429
  function installCsrfInterceptor(axiosInstance, session) {
248
430
  axiosInstance.interceptors.request.use((config) => {
249
431
  if (session.isForeignHost(config.url)) {
@@ -274,7 +456,8 @@ function installRefreshInterceptor(axiosInstance, session) {
274
456
  originalRequest._retry = true;
275
457
  try {
276
458
  await session.refreshGuard(async () => {
277
- await session.axios.post("/api/v1/auth/refresh", {});
459
+ await session.axios.post("/api/v1/auth/refresh", session.buildRefreshBody());
460
+ await session.onAuthenticated?.();
278
461
  });
279
462
  return axiosInstance(originalRequest);
280
463
  } catch (refreshError) {
@@ -288,7 +471,13 @@ function installRefreshInterceptor(axiosInstance, session) {
288
471
  }
289
472
  }
290
473
  if (status === 401 && isSkipRefresh) {
291
- return Promise.reject(mapHttpStatusToError(401, "authentication failed"));
474
+ return Promise.reject(
475
+ mapHttpStatusToError(401, "authentication failed", {
476
+ url,
477
+ body: error.response.data,
478
+ cause: error
479
+ })
480
+ );
292
481
  }
293
482
  return Promise.reject(error);
294
483
  }
@@ -326,7 +515,7 @@ function extractErrorMessage(err) {
326
515
  return "request failed";
327
516
  }
328
517
  async function login(client, email, password) {
329
- const body = { username_or_email: email, password };
518
+ const body = client.session.buildLoginBody(email, password);
330
519
  try {
331
520
  const response = await client.session.axios.post(
332
521
  LOGIN_PATH,
@@ -367,7 +556,8 @@ async function verifyMfa(client, mfaToken, code) {
367
556
  }
368
557
  async function refresh(client) {
369
558
  try {
370
- await client.session.axios.post(REFRESH_PATH, {});
559
+ await client.session.axios.post(REFRESH_PATH, client.session.buildRefreshBody());
560
+ await client.session.onAuthenticated?.();
371
561
  } catch (err) {
372
562
  const status = extractAxiosStatus(err);
373
563
  if (status !== void 0) {
@@ -547,6 +737,6 @@ async function withRetry(fn, options) {
547
737
  throw lastError;
548
738
  }
549
739
 
550
- export { AxiamClient, SKIP_REFRESH, SharedSession, withRetry };
740
+ export { AuthError, AuthzError, AxiamClient, AxiamError, NetworkError, OAuthProtocolError, SKIP_REFRESH, Sensitive, SharedSession, withRetry };
551
741
  //# sourceMappingURL=index.mjs.map
552
742
  //# sourceMappingURL=index.mjs.map