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.
- package/README.md +219 -8
- package/dist/amqp/index.d.cts +1 -1
- package/dist/amqp/index.d.ts +1 -1
- package/dist/amqp/index.js +4 -0
- package/dist/amqp/index.js.map +1 -1
- package/dist/amqp/index.mjs +4 -0
- package/dist/amqp/index.mjs.map +1 -1
- package/dist/{authzCore-xwmCivHH.d.ts → authzCore-DtSN59if.d.ts} +19 -2
- package/dist/{authzCore-CV-Td-V1.d.cts → authzCore-DuRI1aJf.d.cts} +19 -2
- package/dist/client-B_FqmZ0i.d.cts +72 -0
- package/dist/client-Dn1fs5MH.d.ts +72 -0
- package/dist/cookieJar-BTDnjjbL.d.cts +122 -0
- package/dist/cookieJar-Cl8Lq9dL.d.ts +122 -0
- package/dist/grpc/index.d.cts +113 -6
- package/dist/grpc/index.d.ts +113 -6
- package/dist/grpc/index.js +435 -18
- package/dist/grpc/index.js.map +1 -1
- package/dist/grpc/index.mjs +434 -19
- package/dist/grpc/index.mjs.map +1 -1
- package/dist/index.d.cts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +209 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +204 -14
- package/dist/index.mjs.map +1 -1
- package/dist/jwks-BgojMGC5.d.cts +152 -0
- package/dist/jwks-BgojMGC5.d.ts +152 -0
- package/dist/middleware/index.d.cts +208 -8
- package/dist/middleware/index.d.ts +208 -8
- package/dist/middleware/index.js +1107 -1
- package/dist/middleware/index.js.map +1 -1
- package/dist/middleware/index.mjs +1100 -3
- package/dist/middleware/index.mjs.map +1 -1
- package/dist/nestjs/index.d.cts +2 -2
- package/dist/nestjs/index.d.ts +2 -2
- package/dist/nestjs/index.js +3 -0
- package/dist/nestjs/index.js.map +1 -1
- package/dist/nestjs/index.mjs +3 -0
- package/dist/nestjs/index.mjs.map +1 -1
- package/dist/node/index.d.cts +50 -94
- package/dist/node/index.d.ts +50 -94
- package/dist/node/index.js +1039 -15
- package/dist/node/index.js.map +1 -1
- package/dist/node/index.mjs +1023 -16
- package/dist/node/index.mjs.map +1 -1
- package/dist/oidcState-BGxqWUun.d.cts +598 -0
- package/dist/oidcState-CkFuCjpM.d.ts +598 -0
- package/dist/rest/index.d.cts +107 -5
- package/dist/rest/index.d.ts +107 -5
- package/dist/rest/index.js +209 -13
- package/dist/rest/index.js.map +1 -1
- package/dist/rest/index.mjs +204 -14
- package/dist/rest/index.mjs.map +1 -1
- package/dist/sensitive-BC_s7Rrd.d.cts +39 -0
- package/dist/sensitive-BC_s7Rrd.d.ts +39 -0
- package/dist/session-BWUWhhyB.d.cts +154 -0
- package/dist/session-BWUWhhyB.d.ts +154 -0
- package/package.json +2 -2
- package/dist/client-BPg9kfKV.d.ts +0 -150
- package/dist/client-CAeQw1a-.d.cts +0 -150
- package/dist/jwks-CX-nhD6A.d.cts +0 -29
- package/dist/jwks-CX-nhD6A.d.ts +0 -29
- package/dist/sensitive-CWBH_tHr.d.cts +0 -12
- package/dist/sensitive-CWBH_tHr.d.ts +0 -12
package/dist/grpc/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import * as grpc2 from '@grpc/grpc-js';
|
|
|
2
2
|
import axios from 'axios';
|
|
3
3
|
import { CookieJar } from 'tough-cookie';
|
|
4
4
|
import { wrapper } from 'axios-cookiejar-support';
|
|
5
|
+
import { timingSafeEqual } from 'crypto';
|
|
5
6
|
|
|
6
7
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
7
8
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
@@ -32,14 +33,40 @@ var AxiamError = class extends Error {
|
|
|
32
33
|
}
|
|
33
34
|
};
|
|
34
35
|
var AuthError = class _AuthError extends AxiamError {
|
|
35
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Optional stable, machine-readable reason code for the failure.
|
|
38
|
+
*
|
|
39
|
+
* Populated by the CONTRACT.md §12.4 ID-token validation checklist with
|
|
40
|
+
* one of the seven codes §12.3 rule 3 enumerates (see
|
|
41
|
+
* {@link IdTokenFailureReason}); `undefined` for every other `AuthError`.
|
|
42
|
+
* It is a *code*, never free text, so callers can branch on it without
|
|
43
|
+
* parsing `message`.
|
|
44
|
+
*/
|
|
45
|
+
reason;
|
|
46
|
+
constructor(message, reason) {
|
|
36
47
|
super(message);
|
|
37
48
|
this.name = "AuthError";
|
|
49
|
+
this.reason = reason;
|
|
38
50
|
Object.setPrototypeOf(this, _AuthError.prototype);
|
|
39
51
|
}
|
|
40
52
|
};
|
|
53
|
+
var OAuthProtocolError = class _OAuthProtocolError extends AuthError {
|
|
54
|
+
/** The RFC 6749 `error` code (e.g. `"invalid_grant"`, `"invalid_client"`, `"unsupported_grant_type"`). */
|
|
55
|
+
error;
|
|
56
|
+
/** The server's human-readable `error_description`. Never contains token material. */
|
|
57
|
+
errorDescription;
|
|
58
|
+
constructor(error, errorDescription) {
|
|
59
|
+
super(`${error}: ${errorDescription}`);
|
|
60
|
+
this.name = "OAuthProtocolError";
|
|
61
|
+
this.error = error;
|
|
62
|
+
this.errorDescription = errorDescription;
|
|
63
|
+
Object.setPrototypeOf(this, _OAuthProtocolError.prototype);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
41
66
|
var AuthzError = class _AuthzError extends AxiamError {
|
|
67
|
+
/** The denied action, when the server reported it. */
|
|
42
68
|
action;
|
|
69
|
+
/** The resource the denied action targeted, when the server reported it. */
|
|
43
70
|
resourceId;
|
|
44
71
|
constructor(message, action, resourceId) {
|
|
45
72
|
super(message);
|
|
@@ -50,6 +77,7 @@ var AuthzError = class _AuthzError extends AxiamError {
|
|
|
50
77
|
}
|
|
51
78
|
};
|
|
52
79
|
var NetworkError = class _NetworkError extends AxiamError {
|
|
80
|
+
/** The underlying transport error, with every non-allowlisted response header redacted (§2, X-3). */
|
|
53
81
|
cause;
|
|
54
82
|
constructor(message, cause) {
|
|
55
83
|
super(message);
|
|
@@ -73,6 +101,24 @@ function extractAuthzFieldsFromBody(body) {
|
|
|
73
101
|
const resourceId = typeof record.resource_id === "string" ? record.resource_id : void 0;
|
|
74
102
|
return { action, resourceId };
|
|
75
103
|
}
|
|
104
|
+
var OAUTH2_PATH_PREFIX = "/oauth2/";
|
|
105
|
+
function isOAuth2EndpointUrl(url) {
|
|
106
|
+
if (!url) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
return new URL(url).pathname.startsWith(OAUTH2_PATH_PREFIX);
|
|
111
|
+
} catch {
|
|
112
|
+
return url.startsWith(OAUTH2_PATH_PREFIX) || url.includes(OAUTH2_PATH_PREFIX);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function isOAuth2ErrorBody(body) {
|
|
116
|
+
if (body === null || typeof body !== "object") {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
const record = body;
|
|
120
|
+
return typeof record.error === "string" && typeof record.error_description === "string";
|
|
121
|
+
}
|
|
76
122
|
var SAFE_RESPONSE_HEADERS = /* @__PURE__ */ new Set([
|
|
77
123
|
"content-type",
|
|
78
124
|
"content-length",
|
|
@@ -110,6 +156,9 @@ function sanitizeAxiosError(err) {
|
|
|
110
156
|
};
|
|
111
157
|
}
|
|
112
158
|
function mapHttpStatusToError(status, message, ctx) {
|
|
159
|
+
if ((status === 400 || status === 401) && isOAuth2EndpointUrl(ctx?.url) && isOAuth2ErrorBody(ctx?.body)) {
|
|
160
|
+
return new OAuthProtocolError(ctx.body.error, ctx.body.error_description);
|
|
161
|
+
}
|
|
113
162
|
if (status === 401) {
|
|
114
163
|
return new AuthError(message);
|
|
115
164
|
}
|
|
@@ -134,6 +183,7 @@ var REDACTED = "[SENSITIVE]";
|
|
|
134
183
|
var NODE_INSPECT_CUSTOM = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
|
|
135
184
|
var Sensitive = class {
|
|
136
185
|
#value;
|
|
186
|
+
/** Wrap `value` so it can never be logged or serialized by accident. */
|
|
137
187
|
constructor(value) {
|
|
138
188
|
this.#value = value;
|
|
139
189
|
}
|
|
@@ -141,12 +191,15 @@ var Sensitive = class {
|
|
|
141
191
|
expose() {
|
|
142
192
|
return this.#value;
|
|
143
193
|
}
|
|
194
|
+
/** Returns the `"[SENSITIVE]"` placeholder — never the wrapped value. */
|
|
144
195
|
toString() {
|
|
145
196
|
return REDACTED;
|
|
146
197
|
}
|
|
198
|
+
/** Makes `JSON.stringify()` emit the `"[SENSITIVE]"` placeholder. */
|
|
147
199
|
toJSON() {
|
|
148
200
|
return REDACTED;
|
|
149
201
|
}
|
|
202
|
+
/** Makes `console.log`/`util.inspect` print the `"[SENSITIVE]"` placeholder. */
|
|
150
203
|
[NODE_INSPECT_CUSTOM]() {
|
|
151
204
|
return REDACTED;
|
|
152
205
|
}
|
|
@@ -190,6 +243,31 @@ createRefreshGuard();
|
|
|
190
243
|
|
|
191
244
|
// src/core/config.ts
|
|
192
245
|
var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
246
|
+
var CERT_PEM_MARKER = "-----BEGIN CERTIFICATE-----";
|
|
247
|
+
var PRIVATE_KEY_PEM_RE = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY-----/;
|
|
248
|
+
function resolveClientIdentity(options) {
|
|
249
|
+
const hasCert = options.clientCert !== void 0;
|
|
250
|
+
const hasKey = options.clientKey !== void 0;
|
|
251
|
+
if (hasCert !== hasKey) {
|
|
252
|
+
throw new Error(
|
|
253
|
+
"clientCert and clientKey must be provided together for mutual TLS (one was given without the other) (CONTRACT.md \xA76.1)."
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
if (!hasCert || !hasKey) {
|
|
257
|
+
return void 0;
|
|
258
|
+
}
|
|
259
|
+
if (!options.clientCert.includes(CERT_PEM_MARKER)) {
|
|
260
|
+
throw new Error(
|
|
261
|
+
'clientCert must be a PEM-encoded certificate chain (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76.1).'
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
if (!PRIVATE_KEY_PEM_RE.test(options.clientKey)) {
|
|
265
|
+
throw new Error(
|
|
266
|
+
'clientKey must be a PEM-encoded private key (expected to contain "-----BEGIN PRIVATE KEY-----" or "-----BEGIN RSA PRIVATE KEY-----") (CONTRACT.md \xA76.1).'
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
return { cert: options.clientCert, key: new Sensitive(options.clientKey) };
|
|
270
|
+
}
|
|
193
271
|
|
|
194
272
|
// src/grpc/callWithRefresh.ts
|
|
195
273
|
function isGrpcServiceError(err) {
|
|
@@ -232,6 +310,17 @@ function fromWireResponse(resp) {
|
|
|
232
310
|
reason: resp.deny_reason ? resp.deny_reason : void 0
|
|
233
311
|
};
|
|
234
312
|
}
|
|
313
|
+
function fromWireUserInfo(resp) {
|
|
314
|
+
return {
|
|
315
|
+
sub: resp.sub,
|
|
316
|
+
tenantId: resp.tenant_id,
|
|
317
|
+
orgId: resp.org_id,
|
|
318
|
+
// Preserve absence: a missing (scope-gated) claim stays `undefined` rather
|
|
319
|
+
// than becoming an empty string.
|
|
320
|
+
email: resp.email ? resp.email : void 0,
|
|
321
|
+
preferredUsername: resp.preferred_username ? resp.preferred_username : void 0
|
|
322
|
+
};
|
|
323
|
+
}
|
|
235
324
|
function promisifyUnary(call, request) {
|
|
236
325
|
return new Promise((resolve, reject) => {
|
|
237
326
|
call(request, new grpc2.Metadata(), (error, response) => {
|
|
@@ -243,7 +332,7 @@ function promisifyUnary(call, request) {
|
|
|
243
332
|
});
|
|
244
333
|
});
|
|
245
334
|
}
|
|
246
|
-
function buildCredentials(baseUrl, customCa, allowInsecure) {
|
|
335
|
+
function buildCredentials(baseUrl, customCa, allowInsecure, identity) {
|
|
247
336
|
const isSecure = baseUrl.startsWith("https://") || baseUrl.startsWith("grpcs://");
|
|
248
337
|
if (!isSecure) {
|
|
249
338
|
if (!allowInsecure) {
|
|
@@ -256,8 +345,16 @@ function buildCredentials(baseUrl, customCa, allowInsecure) {
|
|
|
256
345
|
);
|
|
257
346
|
return grpc2.ChannelCredentials.createInsecure();
|
|
258
347
|
}
|
|
348
|
+
const rootCerts = customCa ? Buffer.from(customCa, "utf8") : null;
|
|
349
|
+
if (identity) {
|
|
350
|
+
return grpc2.ChannelCredentials.createSsl(
|
|
351
|
+
rootCerts,
|
|
352
|
+
Buffer.from(identity.key.expose(), "utf8"),
|
|
353
|
+
Buffer.from(identity.cert, "utf8")
|
|
354
|
+
);
|
|
355
|
+
}
|
|
259
356
|
if (customCa) {
|
|
260
|
-
return grpc2.ChannelCredentials.createSsl(
|
|
357
|
+
return grpc2.ChannelCredentials.createSsl(rootCerts);
|
|
261
358
|
}
|
|
262
359
|
return grpc2.ChannelCredentials.createSsl();
|
|
263
360
|
}
|
|
@@ -301,12 +398,39 @@ function buildAuthorizationServiceClient(baseUrl, credentials, interceptors) {
|
|
|
301
398
|
interceptors
|
|
302
399
|
});
|
|
303
400
|
}
|
|
401
|
+
function buildUserInfoServiceClient(baseUrl, credentials, interceptors) {
|
|
402
|
+
const reqCodec = jsonCodec();
|
|
403
|
+
const respCodec = jsonCodec();
|
|
404
|
+
const ServiceClientConstructor = grpc2.makeClientConstructor(
|
|
405
|
+
{
|
|
406
|
+
getUserInfo: {
|
|
407
|
+
path: "/axiam.v1.UserInfoService/GetUserInfo",
|
|
408
|
+
requestStream: false,
|
|
409
|
+
responseStream: false,
|
|
410
|
+
requestSerialize: reqCodec.serialize,
|
|
411
|
+
requestDeserialize: reqCodec.deserialize,
|
|
412
|
+
responseSerialize: respCodec.serialize,
|
|
413
|
+
responseDeserialize: respCodec.deserialize
|
|
414
|
+
}
|
|
415
|
+
},
|
|
416
|
+
"axiam.v1.UserInfoService"
|
|
417
|
+
);
|
|
418
|
+
return new ServiceClientConstructor(grpcTarget(baseUrl), credentials, {
|
|
419
|
+
interceptors
|
|
420
|
+
});
|
|
421
|
+
}
|
|
304
422
|
var AuthzGrpcClient = class {
|
|
305
423
|
#session;
|
|
306
424
|
#inner;
|
|
307
425
|
constructor(session, options, clientFactory = buildAuthorizationServiceClient) {
|
|
308
426
|
this.#session = session;
|
|
309
|
-
const
|
|
427
|
+
const identity = resolveClientIdentity(options);
|
|
428
|
+
const credentials = buildCredentials(
|
|
429
|
+
options.baseUrl,
|
|
430
|
+
options.customCa,
|
|
431
|
+
options.allowInsecure ?? false,
|
|
432
|
+
identity
|
|
433
|
+
);
|
|
310
434
|
this.#inner = clientFactory(options.baseUrl, credentials, [authInterceptor(session)]);
|
|
311
435
|
}
|
|
312
436
|
/** `CheckAccess` over gRPC (SC#2 Node half). UNAUTHENTICATED triggers exactly one shared-guard refresh + one retry (§9.3). */
|
|
@@ -332,7 +456,47 @@ var AuthzGrpcClient = class {
|
|
|
332
456
|
this.#inner.close();
|
|
333
457
|
}
|
|
334
458
|
};
|
|
335
|
-
var
|
|
459
|
+
var UserInfoGrpcClient = class {
|
|
460
|
+
#session;
|
|
461
|
+
#inner;
|
|
462
|
+
constructor(session, options, clientFactory = buildUserInfoServiceClient) {
|
|
463
|
+
this.#session = session;
|
|
464
|
+
const identity = resolveClientIdentity(options);
|
|
465
|
+
const credentials = buildCredentials(
|
|
466
|
+
options.baseUrl,
|
|
467
|
+
options.customCa,
|
|
468
|
+
options.allowInsecure ?? false,
|
|
469
|
+
identity
|
|
470
|
+
);
|
|
471
|
+
this.#inner = clientFactory(options.baseUrl, credentials, [authInterceptor(session)]);
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* `GetUserInfo` over gRPC (CONTRACT.md §1.1). Returns the authenticated
|
|
475
|
+
* caller's identity claims; the request is empty (identity comes from the
|
|
476
|
+
* bearer token in the interceptor-injected metadata).
|
|
477
|
+
*
|
|
478
|
+
* Pre-flight (§1.1.3): with no cached access token this raises `AuthError`
|
|
479
|
+
* client-side, WITHOUT a wire call — the same synchronous cached-token view
|
|
480
|
+
* the auth interceptor reads. A gRPC `UNAUTHENTICATED` response otherwise
|
|
481
|
+
* drives exactly one shared-guard refresh + one retry (§9.3), identical to
|
|
482
|
+
* {@link AuthzGrpcClient.checkAccess}.
|
|
483
|
+
*/
|
|
484
|
+
async getUserInfo() {
|
|
485
|
+
if (this.#session.tokenManager.cachedAccessToken() === null) {
|
|
486
|
+
throw new AuthError("getUserInfo requires a prior successful login() (no access token present)");
|
|
487
|
+
}
|
|
488
|
+
const response = await callWithRefresh(
|
|
489
|
+
this.#session,
|
|
490
|
+
() => promisifyUnary(this.#inner.getUserInfo.bind(this.#inner), {})
|
|
491
|
+
);
|
|
492
|
+
return fromWireUserInfo(response);
|
|
493
|
+
}
|
|
494
|
+
/** Close the underlying gRPC channel. */
|
|
495
|
+
close() {
|
|
496
|
+
this.#inner.close();
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
var PEM_MARKER = CERT_PEM_MARKER;
|
|
336
500
|
var SharedSession = class {
|
|
337
501
|
/** The underlying axios instance this session's requests are issued through, pre-configured with `baseUrl`, credential forwarding, and (Node) an optional custom-CA `httpsAgent`. */
|
|
338
502
|
axios;
|
|
@@ -340,6 +504,28 @@ var SharedSession = class {
|
|
|
340
504
|
baseUrl;
|
|
341
505
|
/** The resolved tenant identifier (`tenantSlug` or `tenantId`) injected as the `X-Tenant-ID` header on every same-origin request (§5.2). */
|
|
342
506
|
tenantHeaderValue;
|
|
507
|
+
/** Configured tenant UUID (`AxiamClientOptions.tenantId`), if the client was built with the UUID form. */
|
|
508
|
+
tenantId;
|
|
509
|
+
/** Configured tenant slug (`AxiamClientOptions.tenantSlug`), if the client was built with the slug form. */
|
|
510
|
+
tenantSlug;
|
|
511
|
+
/** Configured organization UUID (`AxiamClientOptions.orgId`), if supplied (§5). */
|
|
512
|
+
orgId;
|
|
513
|
+
/** Configured organization slug (`AxiamClientOptions.orgSlug`), if supplied (§5). */
|
|
514
|
+
orgSlug;
|
|
515
|
+
/**
|
|
516
|
+
* Resolved tenant UUID used to build the `refresh` body (`RefreshRequest`
|
|
517
|
+
* requires the UUID form, not a slug). Seeded from `orgId`/`tenantId` config
|
|
518
|
+
* when the UUID was supplied, then updated from the access-token `tenant_id`
|
|
519
|
+
* claim after each successful login/refresh (Node persona only — the browser
|
|
520
|
+
* cannot read the httpOnly access-token cookie).
|
|
521
|
+
*/
|
|
522
|
+
resolvedTenantId;
|
|
523
|
+
/**
|
|
524
|
+
* Resolved organization UUID used to build the `refresh` body. Seeded from
|
|
525
|
+
* `orgId` config, then updated from the access-token `org_id` claim after a
|
|
526
|
+
* successful login (Node persona). Mirrors the Rust SDK's `resolved_org_id`.
|
|
527
|
+
*/
|
|
528
|
+
resolvedOrgId;
|
|
343
529
|
/** Mutable CSRF token store — populated by the request/response interceptors (D-05). */
|
|
344
530
|
csrfToken;
|
|
345
531
|
/** Set true once a successful login/verifyMfa has completed. */
|
|
@@ -359,8 +545,55 @@ var SharedSession = class {
|
|
|
359
545
|
this.axios = axiosInstance;
|
|
360
546
|
this.baseUrl = options.baseUrl;
|
|
361
547
|
this.tenantHeaderValue = tenantHeaderValue;
|
|
548
|
+
this.tenantId = options.tenantId;
|
|
549
|
+
this.tenantSlug = options.tenantSlug;
|
|
550
|
+
this.orgId = options.orgId;
|
|
551
|
+
this.orgSlug = options.orgSlug;
|
|
552
|
+
this.resolvedTenantId = options.tenantId;
|
|
553
|
+
this.resolvedOrgId = options.orgId;
|
|
362
554
|
this.refreshGuard = createRefreshGuard();
|
|
363
555
|
}
|
|
556
|
+
/**
|
|
557
|
+
* Build the `POST /api/v1/auth/refresh` request body (§1). The server's
|
|
558
|
+
* `RefreshRequest` requires both `tenant_id` and `org_id` as UUIDs, so this
|
|
559
|
+
* emits the resolved UUIDs — from the access-token claims after login, or
|
|
560
|
+
* from UUID-form construction options as a fallback. Fields that could not
|
|
561
|
+
* be resolved are omitted (the server then answers with a clear 400), rather
|
|
562
|
+
* than sending a slug where a UUID is required.
|
|
563
|
+
*/
|
|
564
|
+
buildRefreshBody() {
|
|
565
|
+
const body = {};
|
|
566
|
+
if (this.resolvedTenantId) {
|
|
567
|
+
body.tenant_id = this.resolvedTenantId;
|
|
568
|
+
}
|
|
569
|
+
if (this.resolvedOrgId) {
|
|
570
|
+
body.org_id = this.resolvedOrgId;
|
|
571
|
+
}
|
|
572
|
+
return body;
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Build the `POST /api/v1/auth/login` request body (§1/§5). Carries the
|
|
576
|
+
* configured tenant and organization identifiers (UUID or slug form) in
|
|
577
|
+
* addition to the credentials — the server resolves the workspace from the
|
|
578
|
+
* body, not the `X-Tenant-ID` header, and rejects a login that omits org
|
|
579
|
+
* context. Undefined identifiers are omitted.
|
|
580
|
+
*/
|
|
581
|
+
buildLoginBody(email, password) {
|
|
582
|
+
const body = { username_or_email: email, password };
|
|
583
|
+
if (this.tenantId) {
|
|
584
|
+
body.tenant_id = this.tenantId;
|
|
585
|
+
}
|
|
586
|
+
if (this.tenantSlug) {
|
|
587
|
+
body.tenant_slug = this.tenantSlug;
|
|
588
|
+
}
|
|
589
|
+
if (this.orgId) {
|
|
590
|
+
body.org_id = this.orgId;
|
|
591
|
+
}
|
|
592
|
+
if (this.orgSlug) {
|
|
593
|
+
body.org_slug = this.orgSlug;
|
|
594
|
+
}
|
|
595
|
+
return body;
|
|
596
|
+
}
|
|
364
597
|
/**
|
|
365
598
|
* Host-isolation guard (3A, defense in depth): returns `true` when `url`
|
|
366
599
|
* targets a host other than this session's base origin — an absolute
|
|
@@ -394,11 +627,12 @@ function resolveTenantHeaderValue(options) {
|
|
|
394
627
|
"AxiamClient construction requires a tenant: provide either tenantSlug or tenantId (CONTRACT.md \xA75)."
|
|
395
628
|
);
|
|
396
629
|
}
|
|
397
|
-
function maybeBuildHttpsAgent(customCa) {
|
|
630
|
+
function maybeBuildHttpsAgent(customCa, identity) {
|
|
398
631
|
if (!customCa) {
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
632
|
+
if (!identity) {
|
|
633
|
+
return void 0;
|
|
634
|
+
}
|
|
635
|
+
} else if (!customCa.includes(PEM_MARKER)) {
|
|
402
636
|
throw new Error(
|
|
403
637
|
'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
|
|
404
638
|
);
|
|
@@ -407,7 +641,11 @@ function maybeBuildHttpsAgent(customCa) {
|
|
|
407
641
|
return void 0;
|
|
408
642
|
}
|
|
409
643
|
const https = __require("https");
|
|
410
|
-
return new https.Agent({
|
|
644
|
+
return new https.Agent({
|
|
645
|
+
...customCa ? { ca: customCa } : {},
|
|
646
|
+
// rejectUnauthorized is intentionally left at its secure default (true).
|
|
647
|
+
...identity ? { cert: identity.cert, key: identity.key.expose() } : {}
|
|
648
|
+
});
|
|
411
649
|
}
|
|
412
650
|
function createSession(options) {
|
|
413
651
|
const tenantHeaderValue = resolveTenantHeaderValue(options);
|
|
@@ -416,7 +654,8 @@ function createSession(options) {
|
|
|
416
654
|
'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
|
|
417
655
|
);
|
|
418
656
|
}
|
|
419
|
-
const
|
|
657
|
+
const clientIdentity = resolveClientIdentity(options);
|
|
658
|
+
const httpsAgent = maybeBuildHttpsAgent(options.customCa, clientIdentity);
|
|
420
659
|
const axiosInstance = axios.create({
|
|
421
660
|
baseURL: options.baseUrl,
|
|
422
661
|
withCredentials: true,
|
|
@@ -436,7 +675,14 @@ function createSession(options) {
|
|
|
436
675
|
}
|
|
437
676
|
|
|
438
677
|
// src/rest/interceptors.ts
|
|
439
|
-
var SKIP_REFRESH = [
|
|
678
|
+
var SKIP_REFRESH = [
|
|
679
|
+
"/api/v1/auth/refresh",
|
|
680
|
+
"/api/v1/auth/login",
|
|
681
|
+
"/api/v1/auth/logout",
|
|
682
|
+
"/oauth2/token",
|
|
683
|
+
"/oauth2/introspect",
|
|
684
|
+
"/oauth2/revoke"
|
|
685
|
+
];
|
|
440
686
|
function installCsrfInterceptor(axiosInstance, session) {
|
|
441
687
|
axiosInstance.interceptors.request.use((config) => {
|
|
442
688
|
if (session.isForeignHost(config.url)) {
|
|
@@ -467,7 +713,8 @@ function installRefreshInterceptor(axiosInstance, session) {
|
|
|
467
713
|
originalRequest._retry = true;
|
|
468
714
|
try {
|
|
469
715
|
await session.refreshGuard(async () => {
|
|
470
|
-
await session.axios.post("/api/v1/auth/refresh",
|
|
716
|
+
await session.axios.post("/api/v1/auth/refresh", session.buildRefreshBody());
|
|
717
|
+
await session.onAuthenticated?.();
|
|
471
718
|
});
|
|
472
719
|
return axiosInstance(originalRequest);
|
|
473
720
|
} catch (refreshError) {
|
|
@@ -481,7 +728,13 @@ function installRefreshInterceptor(axiosInstance, session) {
|
|
|
481
728
|
}
|
|
482
729
|
}
|
|
483
730
|
if (status === 401 && isSkipRefresh) {
|
|
484
|
-
return Promise.reject(
|
|
731
|
+
return Promise.reject(
|
|
732
|
+
mapHttpStatusToError(401, "authentication failed", {
|
|
733
|
+
url,
|
|
734
|
+
body: error.response.data,
|
|
735
|
+
cause: error
|
|
736
|
+
})
|
|
737
|
+
);
|
|
485
738
|
}
|
|
486
739
|
return Promise.reject(error);
|
|
487
740
|
}
|
|
@@ -519,7 +772,7 @@ function extractErrorMessage(err) {
|
|
|
519
772
|
return "request failed";
|
|
520
773
|
}
|
|
521
774
|
async function login(client, email, password) {
|
|
522
|
-
const body =
|
|
775
|
+
const body = client.session.buildLoginBody(email, password);
|
|
523
776
|
try {
|
|
524
777
|
const response = await client.session.axios.post(
|
|
525
778
|
LOGIN_PATH,
|
|
@@ -560,7 +813,8 @@ async function verifyMfa(client, mfaToken, code) {
|
|
|
560
813
|
}
|
|
561
814
|
async function refresh(client) {
|
|
562
815
|
try {
|
|
563
|
-
await client.session.axios.post(REFRESH_PATH,
|
|
816
|
+
await client.session.axios.post(REFRESH_PATH, client.session.buildRefreshBody());
|
|
817
|
+
await client.session.onAuthenticated?.();
|
|
564
818
|
} catch (err) {
|
|
565
819
|
const status = extractAxiosStatus(err);
|
|
566
820
|
if (status !== void 0) {
|
|
@@ -778,22 +1032,124 @@ var TokenManager = class {
|
|
|
778
1032
|
setTenantId(tenantId) {
|
|
779
1033
|
this.#tenantId = tenantId;
|
|
780
1034
|
}
|
|
1035
|
+
/**
|
|
1036
|
+
* Best-effort decode of the `tenant_id`/`org_id` claims out of the cached
|
|
1037
|
+
* access token, WITHOUT signature verification. Used only to populate the
|
|
1038
|
+
* `refresh` request body (`RefreshRequest` requires both UUIDs) — never as a
|
|
1039
|
+
* trust decision: the actual credential is the httpOnly `axiam_access` cookie
|
|
1040
|
+
* the server verifies, and the server re-derives the authoritative `org_id`
|
|
1041
|
+
* from the tenant on refresh (see crates/axiam-api-rest handlers/auth.rs),
|
|
1042
|
+
* so echoing these values back carries no security weight. A malformed or
|
|
1043
|
+
* absent token yields an empty result (the caller then keeps whatever UUIDs
|
|
1044
|
+
* were resolved from construction options).
|
|
1045
|
+
*/
|
|
1046
|
+
claimsFromCachedToken() {
|
|
1047
|
+
const token = this.#cachedAccess?.expose();
|
|
1048
|
+
if (!token) {
|
|
1049
|
+
return {};
|
|
1050
|
+
}
|
|
1051
|
+
const claims = decodeJwtPayload(token);
|
|
1052
|
+
return {
|
|
1053
|
+
tenantId: typeof claims.tenant_id === "string" ? claims.tenant_id : void 0,
|
|
1054
|
+
orgId: typeof claims.org_id === "string" ? claims.org_id : void 0
|
|
1055
|
+
};
|
|
1056
|
+
}
|
|
781
1057
|
/** Clear all cached token state (used by logout()). */
|
|
782
1058
|
clear() {
|
|
783
1059
|
this.#cachedAccess = null;
|
|
784
1060
|
}
|
|
785
1061
|
};
|
|
1062
|
+
function decodeJwtPayload(token) {
|
|
1063
|
+
try {
|
|
1064
|
+
const segments = token.split(".");
|
|
1065
|
+
if (segments.length !== 3) {
|
|
1066
|
+
return {};
|
|
1067
|
+
}
|
|
1068
|
+
const json = Buffer.from(segments[1], "base64url").toString("utf8");
|
|
1069
|
+
const parsed = JSON.parse(json);
|
|
1070
|
+
return typeof parsed === "object" && parsed !== null ? parsed : {};
|
|
1071
|
+
} catch {
|
|
1072
|
+
return {};
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
var MAX_CLOCK_SKEW_SEC = 60;
|
|
1076
|
+
function idTokenAuthError(reason, message) {
|
|
1077
|
+
return new AuthError(`id_token validation failed (${reason}): ${message}`, reason);
|
|
1078
|
+
}
|
|
1079
|
+
function resolveClockSkewSec(clockSkewSec) {
|
|
1080
|
+
if (clockSkewSec === void 0) {
|
|
1081
|
+
return MAX_CLOCK_SKEW_SEC;
|
|
1082
|
+
}
|
|
1083
|
+
return Math.min(Math.max(clockSkewSec, 0), MAX_CLOCK_SKEW_SEC);
|
|
1084
|
+
}
|
|
1085
|
+
function constantTimeEquals(a, b) {
|
|
1086
|
+
const left = Buffer.from(a, "utf8");
|
|
1087
|
+
const right = Buffer.from(b, "utf8");
|
|
1088
|
+
if (left.length !== right.length) {
|
|
1089
|
+
return false;
|
|
1090
|
+
}
|
|
1091
|
+
return timingSafeEqual(left, right);
|
|
1092
|
+
}
|
|
1093
|
+
var ID_TOKEN_ALG = "EdDSA";
|
|
1094
|
+
function assertIdTokenAlg(alg) {
|
|
1095
|
+
if (alg !== ID_TOKEN_ALG) {
|
|
1096
|
+
throw idTokenAuthError(
|
|
1097
|
+
"invalid_alg",
|
|
1098
|
+
`expected alg "${ID_TOKEN_ALG}", got ${typeof alg === "string" ? `"${alg}"` : "no alg header"}`
|
|
1099
|
+
);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
function checkIdTokenClaims(claims, expectations, nowSec = Math.floor(Date.now() / 1e3)) {
|
|
1103
|
+
const skew = resolveClockSkewSec(expectations.clockSkewSec);
|
|
1104
|
+
if (claims.iss !== expectations.issuer) {
|
|
1105
|
+
throw idTokenAuthError("invalid_issuer", "iss does not equal the discovery document issuer");
|
|
1106
|
+
}
|
|
1107
|
+
const audiences = Array.isArray(claims.aud) ? claims.aud : [claims.aud];
|
|
1108
|
+
if (!audiences.includes(expectations.clientId)) {
|
|
1109
|
+
throw idTokenAuthError("invalid_audience", "aud does not contain this client_id");
|
|
1110
|
+
}
|
|
1111
|
+
if (audiences.length > 1 && claims.azp !== expectations.clientId) {
|
|
1112
|
+
throw idTokenAuthError(
|
|
1113
|
+
"invalid_audience",
|
|
1114
|
+
"aud holds multiple audiences and azp is absent or does not equal this client_id"
|
|
1115
|
+
);
|
|
1116
|
+
}
|
|
1117
|
+
if (typeof claims.exp !== "number") {
|
|
1118
|
+
throw idTokenAuthError("token_expired", "exp claim is missing or not a number");
|
|
1119
|
+
}
|
|
1120
|
+
if (claims.exp + skew <= nowSec) {
|
|
1121
|
+
throw idTokenAuthError("token_expired", "exp is in the past");
|
|
1122
|
+
}
|
|
1123
|
+
if (typeof claims.iat !== "number") {
|
|
1124
|
+
throw idTokenAuthError("token_expired", "iat claim is missing or not a number");
|
|
1125
|
+
}
|
|
1126
|
+
if (claims.iat - skew > nowSec) {
|
|
1127
|
+
throw idTokenAuthError("token_expired", "iat is in the future");
|
|
1128
|
+
}
|
|
1129
|
+
if (typeof claims.nbf === "number" && claims.nbf - skew > nowSec) {
|
|
1130
|
+
throw idTokenAuthError("token_expired", "nbf is in the future");
|
|
1131
|
+
}
|
|
1132
|
+
if (expectations.nonce !== void 0) {
|
|
1133
|
+
if (typeof claims.nonce !== "string" || !constantTimeEquals(claims.nonce, expectations.nonce)) {
|
|
1134
|
+
throw idTokenAuthError("nonce_mismatch", "nonce claim is absent or does not match the request nonce");
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
return claims;
|
|
1138
|
+
}
|
|
786
1139
|
|
|
787
1140
|
// src/node/jwks.ts
|
|
788
1141
|
var JWKS_PATH = "/oauth2/jwks";
|
|
789
1142
|
var COOLDOWN_DURATION_MS = 6e4;
|
|
790
1143
|
var TIMEOUT_DURATION_MS = 5e3;
|
|
791
1144
|
function createVerifier(baseUrl) {
|
|
1145
|
+
return createJwksVerifier(`${baseUrl}${JWKS_PATH}`);
|
|
1146
|
+
}
|
|
1147
|
+
function createJwksVerifier(jwksUri) {
|
|
792
1148
|
let jwksPromise = null;
|
|
793
1149
|
async function getJwks() {
|
|
794
1150
|
if (!jwksPromise) {
|
|
795
1151
|
jwksPromise = import('jose').then(
|
|
796
|
-
({ createRemoteJWKSet }) => createRemoteJWKSet(new URL(
|
|
1152
|
+
({ createRemoteJWKSet }) => createRemoteJWKSet(new URL(jwksUri), {
|
|
797
1153
|
cooldownDuration: COOLDOWN_DURATION_MS,
|
|
798
1154
|
timeoutDuration: TIMEOUT_DURATION_MS
|
|
799
1155
|
})
|
|
@@ -807,13 +1163,54 @@ function createVerifier(baseUrl) {
|
|
|
807
1163
|
const jwks = await getJwks();
|
|
808
1164
|
const { payload } = await jwtVerify(token, jwks, { algorithms: ["EdDSA"] });
|
|
809
1165
|
return payload;
|
|
1166
|
+
},
|
|
1167
|
+
async verifyIdToken(token, expectations) {
|
|
1168
|
+
const { decodeProtectedHeader, jwtVerify } = await import('jose');
|
|
1169
|
+
let header;
|
|
1170
|
+
try {
|
|
1171
|
+
header = decodeProtectedHeader(token);
|
|
1172
|
+
} catch {
|
|
1173
|
+
throw idTokenAuthError("invalid_alg", "token is not a well-formed JWS");
|
|
1174
|
+
}
|
|
1175
|
+
assertIdTokenAlg(header.alg);
|
|
1176
|
+
if (!header.kid) {
|
|
1177
|
+
throw idTokenAuthError("unknown_kid", "token carries no kid header");
|
|
1178
|
+
}
|
|
1179
|
+
const jwks = await getJwks();
|
|
1180
|
+
const skew = resolveClockSkewSec(expectations.clockSkewSec);
|
|
1181
|
+
let claims;
|
|
1182
|
+
try {
|
|
1183
|
+
const { payload } = await jwtVerify(token, jwks, {
|
|
1184
|
+
algorithms: [ID_TOKEN_ALG],
|
|
1185
|
+
clockTolerance: skew
|
|
1186
|
+
});
|
|
1187
|
+
claims = payload;
|
|
1188
|
+
} catch (err) {
|
|
1189
|
+
throw mapJoseVerifyError(err);
|
|
1190
|
+
}
|
|
1191
|
+
return checkIdTokenClaims(claims, { ...expectations, clockSkewSec: skew });
|
|
810
1192
|
}
|
|
811
1193
|
};
|
|
812
1194
|
}
|
|
1195
|
+
function mapJoseVerifyError(err) {
|
|
1196
|
+
const code = typeof err === "object" && err !== null && "code" in err ? String(err.code) : "";
|
|
1197
|
+
switch (code) {
|
|
1198
|
+
case "ERR_JWKS_NO_MATCHING_KEY":
|
|
1199
|
+
return idTokenAuthError("unknown_kid", "no JWKS key matches the token kid");
|
|
1200
|
+
case "ERR_JWT_EXPIRED":
|
|
1201
|
+
return idTokenAuthError("token_expired", "exp is in the past");
|
|
1202
|
+
case "ERR_JWT_CLAIM_VALIDATION_FAILED":
|
|
1203
|
+
return idTokenAuthError("token_expired", "a time-based claim (nbf/iat) is not yet valid");
|
|
1204
|
+
default:
|
|
1205
|
+
return idTokenAuthError("invalid_signature", "signature verification failed");
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
813
1208
|
|
|
814
1209
|
// src/node/session.ts
|
|
815
1210
|
var NodeSession = class extends SharedSession {
|
|
1211
|
+
/** Manages the access/refresh token pair backing this session. */
|
|
816
1212
|
tokenManager;
|
|
1213
|
+
/** Local JWKS verifier for validating access tokens without a server round-trip. */
|
|
817
1214
|
jwksVerifier;
|
|
818
1215
|
#jar;
|
|
819
1216
|
constructor(options, base, tokenManager, jwksVerifier, jar) {
|
|
@@ -832,8 +1229,9 @@ var NodeSession = class extends SharedSession {
|
|
|
832
1229
|
* remaining fully independent from any other session's guard.
|
|
833
1230
|
*/
|
|
834
1231
|
doRefresh = async () => {
|
|
835
|
-
await this.axios.post("/api/v1/auth/refresh",
|
|
1232
|
+
await this.axios.post("/api/v1/auth/refresh", this.buildRefreshBody());
|
|
836
1233
|
await this.tokenManager.syncFromJar();
|
|
1234
|
+
this.#resolveIdentifiersFromToken();
|
|
837
1235
|
await this.#syncCsrfFromJar();
|
|
838
1236
|
};
|
|
839
1237
|
/**
|
|
@@ -845,8 +1243,25 @@ var NodeSession = class extends SharedSession {
|
|
|
845
1243
|
*/
|
|
846
1244
|
onAuthenticated = async () => {
|
|
847
1245
|
await this.tokenManager.syncFromJar();
|
|
1246
|
+
this.#resolveIdentifiersFromToken();
|
|
848
1247
|
await this.#syncCsrfFromJar();
|
|
849
1248
|
};
|
|
1249
|
+
/**
|
|
1250
|
+
* Populate `resolvedTenantId`/`resolvedOrgId` from the cached access token's
|
|
1251
|
+
* claims when present, leaving any UUID resolved from construction options in
|
|
1252
|
+
* place otherwise. Best-effort and unverified (see
|
|
1253
|
+
* TokenManager.claimsFromCachedToken) — used purely to build the refresh body.
|
|
1254
|
+
*/
|
|
1255
|
+
#resolveIdentifiersFromToken() {
|
|
1256
|
+
const { tenantId, orgId } = this.tokenManager.claimsFromCachedToken();
|
|
1257
|
+
if (tenantId) {
|
|
1258
|
+
this.resolvedTenantId = tenantId;
|
|
1259
|
+
this.tokenManager.setTenantId(tenantId);
|
|
1260
|
+
}
|
|
1261
|
+
if (orgId) {
|
|
1262
|
+
this.resolvedOrgId = orgId;
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
850
1265
|
async #syncCsrfFromJar() {
|
|
851
1266
|
this.csrfToken = await extractCookieValue(this.#jar, this.baseUrl, CSRF_COOKIE);
|
|
852
1267
|
}
|
|
@@ -863,6 +1278,6 @@ function createNodeClient(options) {
|
|
|
863
1278
|
return new AxiamClient(options, createNodeSession(options));
|
|
864
1279
|
}
|
|
865
1280
|
|
|
866
|
-
export { ACCESS_COOKIE, AuthzGrpcClient, CSRF_COOKIE, JWKS_PATH, NodeSession, REFRESH_COOKIE, TokenManager, authInterceptor, buildAuthorizationServiceClient, callWithRefresh, createJar, createNodeClient, createNodeSession, createVerifier, extractCookieValue, wrapAxios };
|
|
1281
|
+
export { ACCESS_COOKIE, AuthzGrpcClient, CSRF_COOKIE, JWKS_PATH, NodeSession, REFRESH_COOKIE, TokenManager, UserInfoGrpcClient, authInterceptor, buildAuthorizationServiceClient, buildUserInfoServiceClient, callWithRefresh, createJar, createNodeClient, createNodeSession, createVerifier, extractCookieValue, wrapAxios };
|
|
867
1282
|
//# sourceMappingURL=index.mjs.map
|
|
868
1283
|
//# sourceMappingURL=index.mjs.map
|