axiam-sdk 1.0.0-alpha8 → 1.0.0-alpha9

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/README.md CHANGED
@@ -17,7 +17,7 @@ Official TypeScript/JavaScript client SDK for [AXIAM](https://github.com/ilpanic
17
17
 
18
18
  ## Contract conformance
19
19
 
20
- This SDK conforms to CONTRACT.md §1–§11.
20
+ This SDK conforms to CONTRACT.md §1–§11 (including §6.1 mTLS client certificates).
21
21
 
22
22
  See [`CONTRACT.md`](./CONTRACT.md) for the full cross-language behavioral contract.
23
23
 
@@ -63,6 +63,34 @@ const client = new AxiamClient({
63
63
  });
64
64
  ```
65
65
 
66
+ ### mTLS / client certificates (Node only)
67
+
68
+ For IoT devices and service accounts authenticated by **mutual TLS** (CONTRACT.md §6.1),
69
+ pass a PEM client-certificate chain and its PEM private key. The identity is presented on
70
+ **both** the REST and gRPC transports of the same client. Presenting a client certificate
71
+ **never** relaxes server verification — strict TLS stays on.
72
+
73
+ ```typescript
74
+ import { readFileSync } from 'node:fs';
75
+ import { AxiamClient } from 'axiam-sdk';
76
+
77
+ const client = new AxiamClient({
78
+ baseUrl: 'https://iam.example.com',
79
+ tenantSlug: 'acme',
80
+ clientCert: readFileSync('device.crt', 'utf8'), // PEM certificate chain
81
+ clientKey: readFileSync('device.key', 'utf8'), // PEM private key (PKCS#8 or PKCS#1)
82
+ // customCa: readFileSync('ca.crt', 'utf8'), // optional server-trust CA (§6)
83
+ });
84
+ ```
85
+
86
+ - `clientCert` and `clientKey` are **all-or-nothing** — providing exactly one throws at
87
+ construction, and each is validated to be PEM-shaped (as `customCa` is).
88
+ - The private key is secret material: it is passed straight to the Node TLS stack and is
89
+ never retained on a public property, logged, or serialized (CONTRACT.md §7).
90
+ - **Node only.** Browsers cannot present a client certificate from JavaScript, so the
91
+ browser build validates the PEM shape then ignores `clientCert`/`clientKey` — exactly as
92
+ it already ignores `customCa`.
93
+
66
94
  ## Usage per persona
67
95
 
68
96
  ### Browser — login + authz (`axiam-sdk` / `axiam-sdk/rest`)
@@ -311,8 +339,10 @@ try {
311
339
  - Token-carrying values are wrapped in `Sensitive<T>` — `toString()`/`toJSON()`/
312
340
  `util.inspect` all redact to `[SENSITIVE]`; the raw value is only reachable via an
313
341
  explicit accessor (CONTRACT.md §7).
314
- - Strict TLS verification is always on; the only escape hatch is the constructor's
315
- `customCa` option for self-signed development environments (CONTRACT.md §6).
342
+ - Strict TLS verification is always on; the only server-trust escape hatch is the
343
+ constructor's `customCa` option for self-signed development environments (CONTRACT.md §6).
344
+ - Optional mutual TLS (mTLS): a PEM `clientCert`/`clientKey` client identity (Node only) is
345
+ presented on both REST and gRPC without ever relaxing server verification (CONTRACT.md §6.1).
316
346
  - AMQP messages are HMAC-SHA256 verified before your handler ever sees them; verification
317
347
  failures are nacked without requeue (CONTRACT.md §8).
318
348
 
@@ -23,6 +23,22 @@ interface AxiamClientOptions {
23
23
  tenantId?: string;
24
24
  /** PEM-encoded custom CA certificate, for self-signed/dev environments (§6). */
25
25
  customCa?: string;
26
+ /**
27
+ * PEM-encoded client-certificate chain for mutual TLS (§6.1). Presented to
28
+ * the server to authenticate an IoT device / service account. MUST be
29
+ * provided together with {@link AxiamClientOptions.clientKey}. Node only —
30
+ * ignored in the browser (browsers cannot present a client certificate from
31
+ * JS). Presenting a client certificate NEVER relaxes server verification.
32
+ */
33
+ clientCert?: string;
34
+ /**
35
+ * PEM-encoded private key (PKCS#8 or PKCS#1) matching {@link
36
+ * AxiamClientOptions.clientCert} (§6.1). Secret material: it is passed
37
+ * straight to the Node TLS stack and is never retained on a public property,
38
+ * logged, or serialized (§7). MUST be provided together with `clientCert`.
39
+ * Node only — ignored in the browser.
40
+ */
41
+ clientKey?: string;
26
42
  /** Connection timeout in milliseconds. Defaults to DEFAULT_CONNECT_TIMEOUT_MS. */
27
43
  connectTimeoutMs?: number;
28
44
  /** Request timeout in milliseconds. Defaults to DEFAULT_REQUEST_TIMEOUT_MS. */
@@ -23,6 +23,22 @@ interface AxiamClientOptions {
23
23
  tenantId?: string;
24
24
  /** PEM-encoded custom CA certificate, for self-signed/dev environments (§6). */
25
25
  customCa?: string;
26
+ /**
27
+ * PEM-encoded client-certificate chain for mutual TLS (§6.1). Presented to
28
+ * the server to authenticate an IoT device / service account. MUST be
29
+ * provided together with {@link AxiamClientOptions.clientKey}. Node only —
30
+ * ignored in the browser (browsers cannot present a client certificate from
31
+ * JS). Presenting a client certificate NEVER relaxes server verification.
32
+ */
33
+ clientCert?: string;
34
+ /**
35
+ * PEM-encoded private key (PKCS#8 or PKCS#1) matching {@link
36
+ * AxiamClientOptions.clientCert} (§6.1). Secret material: it is passed
37
+ * straight to the Node TLS stack and is never retained on a public property,
38
+ * logged, or serialized (§7). MUST be provided together with `clientCert`.
39
+ * Node only — ignored in the browser.
40
+ */
41
+ clientKey?: string;
26
42
  /** Connection timeout in milliseconds. Defaults to DEFAULT_CONNECT_TIMEOUT_MS. */
27
43
  connectTimeoutMs?: number;
28
44
  /** Request timeout in milliseconds. Defaults to DEFAULT_REQUEST_TIMEOUT_MS. */
@@ -5,7 +5,7 @@ export { ACCESS_COOKIE, CSRF_COOKIE, REFRESH_COOKIE, TokenManager, createJar, cr
5
5
  import { a as AccessDecision } from '../types-BVkMoqLU.cjs';
6
6
  export { A as AxiamClaims, J as JWKS_PATH, V as Verifier, c as createVerifier } from '../jwks-B8dPy2c6.cjs';
7
7
  import 'tough-cookie';
8
- import '../client-CAeQw1a-.cjs';
8
+ import '../client-BxskJdoj.cjs';
9
9
  import 'axios';
10
10
  import '../sensitive-CWBH_tHr.cjs';
11
11
 
@@ -95,6 +95,10 @@ declare class AuthzGrpcClient {
95
95
  baseUrl: string;
96
96
  customCa?: string;
97
97
  allowInsecure?: boolean;
98
+ /** PEM client-certificate chain for mutual TLS (§6.1); pair with `clientKey`. */
99
+ clientCert?: string;
100
+ /** PEM private key for mutual TLS (§6.1); pair with `clientCert`. Secret (§7). */
101
+ clientKey?: string;
98
102
  }, clientFactory?: AuthorizationServiceClientFactory);
99
103
  /** `CheckAccess` over gRPC (SC#2 Node half). UNAUTHENTICATED triggers exactly one shared-guard refresh + one retry (§9.3). */
100
104
  checkAccess(request: CheckAccessRequest): Promise<AccessDecision>;
@@ -5,7 +5,7 @@ export { ACCESS_COOKIE, CSRF_COOKIE, REFRESH_COOKIE, TokenManager, createJar, cr
5
5
  import { a as AccessDecision } from '../types-BVkMoqLU.js';
6
6
  export { A as AxiamClaims, J as JWKS_PATH, V as Verifier, c as createVerifier } from '../jwks-B8dPy2c6.js';
7
7
  import 'tough-cookie';
8
- import '../client-BPg9kfKV.js';
8
+ import '../client-DYRT_ZCn.js';
9
9
  import 'axios';
10
10
  import '../sensitive-CWBH_tHr.js';
11
11
 
@@ -95,6 +95,10 @@ declare class AuthzGrpcClient {
95
95
  baseUrl: string;
96
96
  customCa?: string;
97
97
  allowInsecure?: boolean;
98
+ /** PEM client-certificate chain for mutual TLS (§6.1); pair with `clientKey`. */
99
+ clientCert?: string;
100
+ /** PEM private key for mutual TLS (§6.1); pair with `clientCert`. Secret (§7). */
101
+ clientKey?: string;
98
102
  }, clientFactory?: AuthorizationServiceClientFactory);
99
103
  /** `CheckAccess` over gRPC (SC#2 Node half). UNAUTHENTICATED triggers exactly one shared-guard refresh + one retry (§9.3). */
100
104
  checkAccess(request: CheckAccessRequest): Promise<AccessDecision>;
@@ -215,6 +215,31 @@ createRefreshGuard();
215
215
 
216
216
  // src/core/config.ts
217
217
  var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
218
+ var CERT_PEM_MARKER = "-----BEGIN CERTIFICATE-----";
219
+ var PRIVATE_KEY_PEM_RE = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY-----/;
220
+ function resolveClientIdentity(options) {
221
+ const hasCert = options.clientCert !== void 0;
222
+ const hasKey = options.clientKey !== void 0;
223
+ if (hasCert !== hasKey) {
224
+ throw new Error(
225
+ "clientCert and clientKey must be provided together for mutual TLS (one was given without the other) (CONTRACT.md \xA76.1)."
226
+ );
227
+ }
228
+ if (!hasCert || !hasKey) {
229
+ return void 0;
230
+ }
231
+ if (!options.clientCert.includes(CERT_PEM_MARKER)) {
232
+ throw new Error(
233
+ 'clientCert must be a PEM-encoded certificate chain (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76.1).'
234
+ );
235
+ }
236
+ if (!PRIVATE_KEY_PEM_RE.test(options.clientKey)) {
237
+ throw new Error(
238
+ 'clientKey must be a PEM-encoded private key (expected to contain "-----BEGIN PRIVATE KEY-----" or "-----BEGIN RSA PRIVATE KEY-----") (CONTRACT.md \xA76.1).'
239
+ );
240
+ }
241
+ return { cert: options.clientCert, key: new Sensitive(options.clientKey) };
242
+ }
218
243
 
219
244
  // src/grpc/callWithRefresh.ts
220
245
  function isGrpcServiceError(err) {
@@ -268,7 +293,7 @@ function promisifyUnary(call, request) {
268
293
  });
269
294
  });
270
295
  }
271
- function buildCredentials(baseUrl, customCa, allowInsecure) {
296
+ function buildCredentials(baseUrl, customCa, allowInsecure, identity) {
272
297
  const isSecure = baseUrl.startsWith("https://") || baseUrl.startsWith("grpcs://");
273
298
  if (!isSecure) {
274
299
  if (!allowInsecure) {
@@ -281,8 +306,16 @@ function buildCredentials(baseUrl, customCa, allowInsecure) {
281
306
  );
282
307
  return grpc2__namespace.ChannelCredentials.createInsecure();
283
308
  }
309
+ const rootCerts = customCa ? Buffer.from(customCa, "utf8") : null;
310
+ if (identity) {
311
+ return grpc2__namespace.ChannelCredentials.createSsl(
312
+ rootCerts,
313
+ Buffer.from(identity.key.expose(), "utf8"),
314
+ Buffer.from(identity.cert, "utf8")
315
+ );
316
+ }
284
317
  if (customCa) {
285
- return grpc2__namespace.ChannelCredentials.createSsl(Buffer.from(customCa, "utf8"));
318
+ return grpc2__namespace.ChannelCredentials.createSsl(rootCerts);
286
319
  }
287
320
  return grpc2__namespace.ChannelCredentials.createSsl();
288
321
  }
@@ -331,7 +364,13 @@ var AuthzGrpcClient = class {
331
364
  #inner;
332
365
  constructor(session, options, clientFactory = buildAuthorizationServiceClient) {
333
366
  this.#session = session;
334
- const credentials = buildCredentials(options.baseUrl, options.customCa, options.allowInsecure ?? false);
367
+ const identity = resolveClientIdentity(options);
368
+ const credentials = buildCredentials(
369
+ options.baseUrl,
370
+ options.customCa,
371
+ options.allowInsecure ?? false,
372
+ identity
373
+ );
335
374
  this.#inner = clientFactory(options.baseUrl, credentials, [authInterceptor(session)]);
336
375
  }
337
376
  /** `CheckAccess` over gRPC (SC#2 Node half). UNAUTHENTICATED triggers exactly one shared-guard refresh + one retry (§9.3). */
@@ -357,7 +396,7 @@ var AuthzGrpcClient = class {
357
396
  this.#inner.close();
358
397
  }
359
398
  };
360
- var PEM_MARKER = "-----BEGIN CERTIFICATE-----";
399
+ var PEM_MARKER = CERT_PEM_MARKER;
361
400
  var SharedSession = class {
362
401
  /** The underlying axios instance this session's requests are issued through, pre-configured with `baseUrl`, credential forwarding, and (Node) an optional custom-CA `httpsAgent`. */
363
402
  axios;
@@ -419,11 +458,12 @@ function resolveTenantHeaderValue(options) {
419
458
  "AxiamClient construction requires a tenant: provide either tenantSlug or tenantId (CONTRACT.md \xA75)."
420
459
  );
421
460
  }
422
- function maybeBuildHttpsAgent(customCa) {
461
+ function maybeBuildHttpsAgent(customCa, identity) {
423
462
  if (!customCa) {
424
- return void 0;
425
- }
426
- if (!customCa.includes(PEM_MARKER)) {
463
+ if (!identity) {
464
+ return void 0;
465
+ }
466
+ } else if (!customCa.includes(PEM_MARKER)) {
427
467
  throw new Error(
428
468
  'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
429
469
  );
@@ -432,7 +472,11 @@ function maybeBuildHttpsAgent(customCa) {
432
472
  return void 0;
433
473
  }
434
474
  const https = __require("https");
435
- return new https.Agent({ ca: customCa });
475
+ return new https.Agent({
476
+ ...customCa ? { ca: customCa } : {},
477
+ // rejectUnauthorized is intentionally left at its secure default (true).
478
+ ...identity ? { cert: identity.cert, key: identity.key.expose() } : {}
479
+ });
436
480
  }
437
481
  function createSession(options) {
438
482
  const tenantHeaderValue = resolveTenantHeaderValue(options);
@@ -441,7 +485,8 @@ function createSession(options) {
441
485
  'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
442
486
  );
443
487
  }
444
- const httpsAgent = maybeBuildHttpsAgent(options.customCa);
488
+ const clientIdentity = resolveClientIdentity(options);
489
+ const httpsAgent = maybeBuildHttpsAgent(options.customCa, clientIdentity);
445
490
  const axiosInstance = axios__default.default.create({
446
491
  baseURL: options.baseUrl,
447
492
  withCredentials: true,