axiam-sdk 1.0.0-alpha
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/LICENSE +201 -0
- package/README.md +231 -0
- package/dist/amqp/index.d.cts +246 -0
- package/dist/amqp/index.d.ts +246 -0
- package/dist/amqp/index.js +179 -0
- package/dist/amqp/index.js.map +1 -0
- package/dist/amqp/index.mjs +166 -0
- package/dist/amqp/index.mjs.map +1 -0
- package/dist/client-B2SB-C7c.d.cts +207 -0
- package/dist/client-B2SB-C7c.d.ts +207 -0
- package/dist/grpc/index.d.cts +106 -0
- package/dist/grpc/index.d.ts +106 -0
- package/dist/grpc/index.js +908 -0
- package/dist/grpc/index.js.map +1 -0
- package/dist/grpc/index.mjs +868 -0
- package/dist/grpc/index.mjs.map +1 -0
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +561 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +552 -0
- package/dist/index.mjs.map +1 -0
- package/dist/jwks-CX-nhD6A.d.cts +29 -0
- package/dist/jwks-CX-nhD6A.d.ts +29 -0
- package/dist/middleware/index.d.cts +153 -0
- package/dist/middleware/index.d.ts +153 -0
- package/dist/middleware/index.js +232 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/index.mjs +220 -0
- package/dist/middleware/index.mjs.map +1 -0
- package/dist/node/index.d.cts +103 -0
- package/dist/node/index.d.ts +103 -0
- package/dist/node/index.js +717 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/index.mjs +700 -0
- package/dist/node/index.mjs.map +1 -0
- package/dist/rest/index.d.cts +27 -0
- package/dist/rest/index.d.ts +27 -0
- package/dist/rest/index.js +561 -0
- package/dist/rest/index.js.map +1 -0
- package/dist/rest/index.mjs +552 -0
- package/dist/rest/index.mjs.map +1 -0
- package/dist/sensitive-CWBH_tHr.d.cts +12 -0
- package/dist/sensitive-CWBH_tHr.d.ts +12 -0
- package/package.json +145 -0
|
@@ -0,0 +1,908 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var grpc2 = require('@grpc/grpc-js');
|
|
4
|
+
var axios = require('axios');
|
|
5
|
+
var toughCookie = require('tough-cookie');
|
|
6
|
+
var axiosCookiejarSupport = require('axios-cookiejar-support');
|
|
7
|
+
|
|
8
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
|
|
10
|
+
function _interopNamespace(e) {
|
|
11
|
+
if (e && e.__esModule) return e;
|
|
12
|
+
var n = Object.create(null);
|
|
13
|
+
if (e) {
|
|
14
|
+
Object.keys(e).forEach(function (k) {
|
|
15
|
+
if (k !== 'default') {
|
|
16
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
17
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
get: function () { return e[k]; }
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
n.default = e;
|
|
25
|
+
return Object.freeze(n);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
var grpc2__namespace = /*#__PURE__*/_interopNamespace(grpc2);
|
|
29
|
+
var axios__default = /*#__PURE__*/_interopDefault(axios);
|
|
30
|
+
|
|
31
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
32
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
33
|
+
}) : x)(function(x) {
|
|
34
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
35
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
36
|
+
});
|
|
37
|
+
function authInterceptor(session) {
|
|
38
|
+
return (options, nextCall) => {
|
|
39
|
+
return new grpc2__namespace.InterceptingCall(nextCall(options), {
|
|
40
|
+
start(metadata, listener, next) {
|
|
41
|
+
const token = session.tokenManager.cachedAccessToken();
|
|
42
|
+
if (token) {
|
|
43
|
+
metadata.add("authorization", `Bearer ${token.expose()}`);
|
|
44
|
+
}
|
|
45
|
+
metadata.add("x-tenant-id", session.tenantHeaderValue);
|
|
46
|
+
next(metadata, listener);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/core/errors.ts
|
|
53
|
+
var AxiamError = class extends Error {
|
|
54
|
+
constructor(message) {
|
|
55
|
+
super(message);
|
|
56
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var AuthError = class _AuthError extends AxiamError {
|
|
60
|
+
constructor(message) {
|
|
61
|
+
super(message);
|
|
62
|
+
this.name = "AuthError";
|
|
63
|
+
Object.setPrototypeOf(this, _AuthError.prototype);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
var AuthzError = class _AuthzError extends AxiamError {
|
|
67
|
+
action;
|
|
68
|
+
resourceId;
|
|
69
|
+
constructor(message, action, resourceId) {
|
|
70
|
+
super(message);
|
|
71
|
+
this.name = "AuthzError";
|
|
72
|
+
this.action = action;
|
|
73
|
+
this.resourceId = resourceId;
|
|
74
|
+
Object.setPrototypeOf(this, _AuthzError.prototype);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
var NetworkError = class _NetworkError extends AxiamError {
|
|
78
|
+
cause;
|
|
79
|
+
constructor(message, cause) {
|
|
80
|
+
super(message);
|
|
81
|
+
this.name = "NetworkError";
|
|
82
|
+
this.cause = cause;
|
|
83
|
+
Object.setPrototypeOf(this, _NetworkError.prototype);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// src/core/errorMapper.ts
|
|
88
|
+
var GrpcStatus = {
|
|
89
|
+
PERMISSION_DENIED: 7,
|
|
90
|
+
UNAUTHENTICATED: 16
|
|
91
|
+
};
|
|
92
|
+
function extractAuthzFieldsFromBody(body) {
|
|
93
|
+
if (body === null || typeof body !== "object") {
|
|
94
|
+
return {};
|
|
95
|
+
}
|
|
96
|
+
const record = body;
|
|
97
|
+
const action = typeof record.action === "string" ? record.action : void 0;
|
|
98
|
+
const resourceId = typeof record.resource_id === "string" ? record.resource_id : void 0;
|
|
99
|
+
return { action, resourceId };
|
|
100
|
+
}
|
|
101
|
+
var SAFE_RESPONSE_HEADERS = /* @__PURE__ */ new Set([
|
|
102
|
+
"content-type",
|
|
103
|
+
"content-length",
|
|
104
|
+
"date",
|
|
105
|
+
"server",
|
|
106
|
+
"retry-after",
|
|
107
|
+
"x-request-id",
|
|
108
|
+
"x-tenant-id"
|
|
109
|
+
]);
|
|
110
|
+
var REDACTED_HEADER = "[REDACTED]";
|
|
111
|
+
function sanitizeAxiosError(err) {
|
|
112
|
+
if (err === null || typeof err !== "object") {
|
|
113
|
+
return err;
|
|
114
|
+
}
|
|
115
|
+
const candidate = err;
|
|
116
|
+
if (candidate.response === null || typeof candidate.response !== "object") {
|
|
117
|
+
return err;
|
|
118
|
+
}
|
|
119
|
+
const response = candidate.response;
|
|
120
|
+
if (response.headers === null || typeof response.headers !== "object") {
|
|
121
|
+
return err;
|
|
122
|
+
}
|
|
123
|
+
const sanitizedHeaders = { ...response.headers };
|
|
124
|
+
for (const key of Object.keys(sanitizedHeaders)) {
|
|
125
|
+
if (!SAFE_RESPONSE_HEADERS.has(key.toLowerCase())) {
|
|
126
|
+
sanitizedHeaders[key] = REDACTED_HEADER;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
...err,
|
|
131
|
+
response: {
|
|
132
|
+
...response,
|
|
133
|
+
headers: sanitizedHeaders
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function mapHttpStatusToError(status, message, ctx) {
|
|
138
|
+
if (status === 401) {
|
|
139
|
+
return new AuthError(message);
|
|
140
|
+
}
|
|
141
|
+
if (status === 403 || status === 409) {
|
|
142
|
+
const fromBody = extractAuthzFieldsFromBody(ctx?.body);
|
|
143
|
+
return new AuthzError(message, fromBody.action ?? ctx?.action, fromBody.resourceId ?? ctx?.resourceId);
|
|
144
|
+
}
|
|
145
|
+
return new NetworkError(message, sanitizeAxiosError(ctx?.cause));
|
|
146
|
+
}
|
|
147
|
+
function mapGrpcStatusToError(code, message) {
|
|
148
|
+
if (code === GrpcStatus.UNAUTHENTICATED) {
|
|
149
|
+
return new AuthError(message);
|
|
150
|
+
}
|
|
151
|
+
if (code === GrpcStatus.PERMISSION_DENIED) {
|
|
152
|
+
return new AuthzError(message);
|
|
153
|
+
}
|
|
154
|
+
return new NetworkError(message);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/core/sensitive.ts
|
|
158
|
+
var REDACTED = "[SENSITIVE]";
|
|
159
|
+
var NODE_INSPECT_CUSTOM = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
|
|
160
|
+
var Sensitive = class {
|
|
161
|
+
#value;
|
|
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
|
+
toString() {
|
|
170
|
+
return REDACTED;
|
|
171
|
+
}
|
|
172
|
+
toJSON() {
|
|
173
|
+
return REDACTED;
|
|
174
|
+
}
|
|
175
|
+
[NODE_INSPECT_CUSTOM]() {
|
|
176
|
+
return REDACTED;
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// src/core/csrf.ts
|
|
181
|
+
var CSRF_METHODS = /* @__PURE__ */ new Set(["post", "put", "patch", "delete"]);
|
|
182
|
+
var CSRF_COOKIE_REGEX = /(?:^|;\s*)axiam_csrf=([^;]*)/;
|
|
183
|
+
function readCsrfCookie(cookieString) {
|
|
184
|
+
const match = CSRF_COOKIE_REGEX.exec(cookieString);
|
|
185
|
+
if (!match) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
return decodeURIComponent(match[1]);
|
|
189
|
+
}
|
|
190
|
+
function csrfHeaderForMethod(method, cookieString) {
|
|
191
|
+
if (!CSRF_METHODS.has(method.toLowerCase())) {
|
|
192
|
+
return void 0;
|
|
193
|
+
}
|
|
194
|
+
const token = readCsrfCookie(cookieString);
|
|
195
|
+
return token ?? void 0;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// src/core/singleFlightRefresh.ts
|
|
199
|
+
function createRefreshGuard() {
|
|
200
|
+
let refreshPromise = null;
|
|
201
|
+
const guard = ((doRefresh) => {
|
|
202
|
+
if (!refreshPromise) {
|
|
203
|
+
refreshPromise = doRefresh().finally(() => {
|
|
204
|
+
refreshPromise = null;
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return refreshPromise;
|
|
208
|
+
});
|
|
209
|
+
guard.__reset = () => {
|
|
210
|
+
refreshPromise = null;
|
|
211
|
+
};
|
|
212
|
+
return guard;
|
|
213
|
+
}
|
|
214
|
+
createRefreshGuard();
|
|
215
|
+
|
|
216
|
+
// src/core/config.ts
|
|
217
|
+
var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
218
|
+
|
|
219
|
+
// src/grpc/callWithRefresh.ts
|
|
220
|
+
function isGrpcServiceError(err) {
|
|
221
|
+
return typeof err === "object" && err !== null && "code" in err;
|
|
222
|
+
}
|
|
223
|
+
async function callWithRefresh(session, fn) {
|
|
224
|
+
try {
|
|
225
|
+
return await fn();
|
|
226
|
+
} catch (err) {
|
|
227
|
+
if (isGrpcServiceError(err) && err.code === GrpcStatus.UNAUTHENTICATED) {
|
|
228
|
+
await session.refreshGuard(session.doRefresh);
|
|
229
|
+
await session.tokenManager.syncFromJar();
|
|
230
|
+
try {
|
|
231
|
+
return await fn();
|
|
232
|
+
} catch (retryErr) {
|
|
233
|
+
throw toAxiamError(retryErr);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
throw toAxiamError(err);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function toAxiamError(err) {
|
|
240
|
+
if (isGrpcServiceError(err) && typeof err.code === "number") {
|
|
241
|
+
return mapGrpcStatusToError(err.code, err.message ?? "gRPC call failed");
|
|
242
|
+
}
|
|
243
|
+
return err;
|
|
244
|
+
}
|
|
245
|
+
function toWireRequest(req) {
|
|
246
|
+
return {
|
|
247
|
+
tenant_id: req.tenantId,
|
|
248
|
+
subject_id: req.subjectId,
|
|
249
|
+
action: req.action,
|
|
250
|
+
resource_id: req.resourceId,
|
|
251
|
+
scope: req.scope
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function fromWireResponse(resp) {
|
|
255
|
+
return {
|
|
256
|
+
allowed: resp.allowed,
|
|
257
|
+
reason: resp.deny_reason ? resp.deny_reason : void 0
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
function promisifyUnary(call, request) {
|
|
261
|
+
return new Promise((resolve, reject) => {
|
|
262
|
+
call(request, new grpc2__namespace.Metadata(), (error, response) => {
|
|
263
|
+
if (error) {
|
|
264
|
+
reject(error);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
resolve(response);
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
function buildCredentials(baseUrl, customCa, allowInsecure) {
|
|
272
|
+
const isSecure = baseUrl.startsWith("https://") || baseUrl.startsWith("grpcs://");
|
|
273
|
+
if (!isSecure) {
|
|
274
|
+
if (!allowInsecure) {
|
|
275
|
+
throw new Error(
|
|
276
|
+
`AXIAM gRPC refuses to open an insecure (plaintext) channel to "${baseUrl}": bearer tokens and tenant metadata would be sent in cleartext. Use an https:// or grpcs:// target, or pass { allowInsecure: true } to opt in explicitly for a trusted local/dev channel (CONTRACT.md \xA76).`
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
console.warn(
|
|
280
|
+
`AXIAM gRPC: opening an INSECURE (plaintext) channel to "${baseUrl}" because allowInsecure was set. Bearer tokens and tenant metadata are transmitted in cleartext \u2014 never use this outside a trusted local/dev environment.`
|
|
281
|
+
);
|
|
282
|
+
return grpc2__namespace.ChannelCredentials.createInsecure();
|
|
283
|
+
}
|
|
284
|
+
if (customCa) {
|
|
285
|
+
return grpc2__namespace.ChannelCredentials.createSsl(Buffer.from(customCa, "utf8"));
|
|
286
|
+
}
|
|
287
|
+
return grpc2__namespace.ChannelCredentials.createSsl();
|
|
288
|
+
}
|
|
289
|
+
function grpcTarget(baseUrl) {
|
|
290
|
+
const url = new URL(baseUrl);
|
|
291
|
+
return url.port ? `${url.hostname}:${url.port}` : url.hostname;
|
|
292
|
+
}
|
|
293
|
+
var jsonCodec = () => ({
|
|
294
|
+
serialize: (value) => Buffer.from(JSON.stringify(value)),
|
|
295
|
+
deserialize: (bytes) => JSON.parse(bytes.toString("utf8"))
|
|
296
|
+
});
|
|
297
|
+
function buildAuthorizationServiceClient(baseUrl, credentials, interceptors) {
|
|
298
|
+
const checkAccessCodec = jsonCodec();
|
|
299
|
+
const checkAccessRespCodec = jsonCodec();
|
|
300
|
+
const batchCodec = jsonCodec();
|
|
301
|
+
const batchRespCodec = jsonCodec();
|
|
302
|
+
const ServiceClientConstructor = grpc2__namespace.makeClientConstructor(
|
|
303
|
+
{
|
|
304
|
+
checkAccess: {
|
|
305
|
+
path: "/axiam.v1.AuthorizationService/CheckAccess",
|
|
306
|
+
requestStream: false,
|
|
307
|
+
responseStream: false,
|
|
308
|
+
requestSerialize: checkAccessCodec.serialize,
|
|
309
|
+
requestDeserialize: checkAccessCodec.deserialize,
|
|
310
|
+
responseSerialize: checkAccessRespCodec.serialize,
|
|
311
|
+
responseDeserialize: checkAccessRespCodec.deserialize
|
|
312
|
+
},
|
|
313
|
+
batchCheckAccess: {
|
|
314
|
+
path: "/axiam.v1.AuthorizationService/BatchCheckAccess",
|
|
315
|
+
requestStream: false,
|
|
316
|
+
responseStream: false,
|
|
317
|
+
requestSerialize: batchCodec.serialize,
|
|
318
|
+
requestDeserialize: batchCodec.deserialize,
|
|
319
|
+
responseSerialize: batchRespCodec.serialize,
|
|
320
|
+
responseDeserialize: batchRespCodec.deserialize
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
"axiam.v1.AuthorizationService"
|
|
324
|
+
);
|
|
325
|
+
return new ServiceClientConstructor(grpcTarget(baseUrl), credentials, {
|
|
326
|
+
interceptors
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
var AuthzGrpcClient = class {
|
|
330
|
+
#session;
|
|
331
|
+
#inner;
|
|
332
|
+
constructor(session, options, clientFactory = buildAuthorizationServiceClient) {
|
|
333
|
+
this.#session = session;
|
|
334
|
+
const credentials = buildCredentials(options.baseUrl, options.customCa, options.allowInsecure ?? false);
|
|
335
|
+
this.#inner = clientFactory(options.baseUrl, credentials, [authInterceptor(session)]);
|
|
336
|
+
}
|
|
337
|
+
/** `CheckAccess` over gRPC (SC#2 Node half). UNAUTHENTICATED triggers exactly one shared-guard refresh + one retry (§9.3). */
|
|
338
|
+
async checkAccess(request) {
|
|
339
|
+
const wireRequest = toWireRequest(request);
|
|
340
|
+
const response = await callWithRefresh(
|
|
341
|
+
this.#session,
|
|
342
|
+
() => promisifyUnary(this.#inner.checkAccess.bind(this.#inner), wireRequest)
|
|
343
|
+
);
|
|
344
|
+
return fromWireResponse(response);
|
|
345
|
+
}
|
|
346
|
+
/** `BatchCheckAccess` over gRPC — results preserve input order (§1). */
|
|
347
|
+
async batchCheck(requests) {
|
|
348
|
+
const wireRequest = { requests: requests.map(toWireRequest) };
|
|
349
|
+
const response = await callWithRefresh(
|
|
350
|
+
this.#session,
|
|
351
|
+
() => promisifyUnary(this.#inner.batchCheckAccess.bind(this.#inner), wireRequest)
|
|
352
|
+
);
|
|
353
|
+
return response.results.map(fromWireResponse);
|
|
354
|
+
}
|
|
355
|
+
/** Close the underlying gRPC channel. */
|
|
356
|
+
close() {
|
|
357
|
+
this.#inner.close();
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
var PEM_MARKER = "-----BEGIN CERTIFICATE-----";
|
|
361
|
+
var SharedSession = class {
|
|
362
|
+
/** 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
|
+
axios;
|
|
364
|
+
/** The AXIAM server base URL this session was constructed with (`AxiamClientOptions.baseUrl`). */
|
|
365
|
+
baseUrl;
|
|
366
|
+
/** The resolved tenant identifier (`tenantSlug` or `tenantId`) injected as the `X-Tenant-ID` header on every same-origin request (§5.2). */
|
|
367
|
+
tenantHeaderValue;
|
|
368
|
+
/** Mutable CSRF token store — populated by the request/response interceptors (D-05). */
|
|
369
|
+
csrfToken;
|
|
370
|
+
/** Set true once a successful login/verifyMfa has completed. */
|
|
371
|
+
authenticated = false;
|
|
372
|
+
/**
|
|
373
|
+
* Per-instance single-flight refresh guard (CR-02, D-13). Shared across
|
|
374
|
+
* this session's REST and gRPC transports (rest/interceptors.ts,
|
|
375
|
+
* grpc/callWithRefresh.ts both call `session.refreshGuard(...)`), but
|
|
376
|
+
* NEVER shared with a different SharedSession/NodeSession instance.
|
|
377
|
+
*
|
|
378
|
+
* @internal SDK-internal transport wiring, not intended to be invoked
|
|
379
|
+
* directly by SDK consumers — refreshes are driven automatically by the
|
|
380
|
+
* response interceptor (rest) or `callWithRefresh` (grpc).
|
|
381
|
+
*/
|
|
382
|
+
refreshGuard;
|
|
383
|
+
constructor(options, axiosInstance, tenantHeaderValue) {
|
|
384
|
+
this.axios = axiosInstance;
|
|
385
|
+
this.baseUrl = options.baseUrl;
|
|
386
|
+
this.tenantHeaderValue = tenantHeaderValue;
|
|
387
|
+
this.refreshGuard = createRefreshGuard();
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Host-isolation guard (3A, defense in depth): returns `true` when `url`
|
|
391
|
+
* targets a host other than this session's base origin — an absolute
|
|
392
|
+
* third-party URL, or a redirect that axios/the browser resolved off-origin.
|
|
393
|
+
* The tenant identifier and CSRF token must never be attached to such a
|
|
394
|
+
* request. A relative/host-less `url` (the normal case, merged against
|
|
395
|
+
* `baseUrl`) is same-origin and returns `false`. Mirrors the Python SDK's
|
|
396
|
+
* `_prepare_request` guard. Malformed input fails closed (treated as
|
|
397
|
+
* foreign).
|
|
398
|
+
*/
|
|
399
|
+
isForeignHost(url) {
|
|
400
|
+
if (!url) {
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
try {
|
|
404
|
+
const target = new URL(url, this.baseUrl);
|
|
405
|
+
return target.host !== new URL(this.baseUrl).host;
|
|
406
|
+
} catch {
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
function resolveTenantHeaderValue(options) {
|
|
412
|
+
if (options.tenantSlug) {
|
|
413
|
+
return options.tenantSlug;
|
|
414
|
+
}
|
|
415
|
+
if (options.tenantId) {
|
|
416
|
+
return options.tenantId;
|
|
417
|
+
}
|
|
418
|
+
throw new Error(
|
|
419
|
+
"AxiamClient construction requires a tenant: provide either tenantSlug or tenantId (CONTRACT.md \xA75)."
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
function maybeBuildHttpsAgent(customCa) {
|
|
423
|
+
if (!customCa) {
|
|
424
|
+
return void 0;
|
|
425
|
+
}
|
|
426
|
+
if (!customCa.includes(PEM_MARKER)) {
|
|
427
|
+
throw new Error(
|
|
428
|
+
'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
if (typeof process === "undefined") {
|
|
432
|
+
return void 0;
|
|
433
|
+
}
|
|
434
|
+
const https = __require("https");
|
|
435
|
+
return new https.Agent({ ca: customCa });
|
|
436
|
+
}
|
|
437
|
+
function createSession(options) {
|
|
438
|
+
const tenantHeaderValue = resolveTenantHeaderValue(options);
|
|
439
|
+
if (options.customCa !== void 0 && !options.customCa.includes(PEM_MARKER)) {
|
|
440
|
+
throw new Error(
|
|
441
|
+
'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
const httpsAgent = maybeBuildHttpsAgent(options.customCa);
|
|
445
|
+
const axiosInstance = axios__default.default.create({
|
|
446
|
+
baseURL: options.baseUrl,
|
|
447
|
+
withCredentials: true,
|
|
448
|
+
timeout: options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
|
|
449
|
+
...httpsAgent ? { httpsAgent } : {}
|
|
450
|
+
});
|
|
451
|
+
const session = new SharedSession(options, axiosInstance, tenantHeaderValue);
|
|
452
|
+
axiosInstance.interceptors.request.use((config) => {
|
|
453
|
+
if (session.isForeignHost(config.url)) {
|
|
454
|
+
return config;
|
|
455
|
+
}
|
|
456
|
+
config.headers = config.headers ?? {};
|
|
457
|
+
config.headers["X-Tenant-ID"] = session.tenantHeaderValue;
|
|
458
|
+
return config;
|
|
459
|
+
});
|
|
460
|
+
return session;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// src/rest/interceptors.ts
|
|
464
|
+
var SKIP_REFRESH = ["/api/v1/auth/refresh", "/api/v1/auth/login", "/api/v1/auth/logout"];
|
|
465
|
+
function installCsrfInterceptor(axiosInstance, session) {
|
|
466
|
+
axiosInstance.interceptors.request.use((config) => {
|
|
467
|
+
if (session.isForeignHost(config.url)) {
|
|
468
|
+
return config;
|
|
469
|
+
}
|
|
470
|
+
const method = (config.method ?? "get").toLowerCase();
|
|
471
|
+
const cookieString = typeof document !== "undefined" ? document.cookie : "";
|
|
472
|
+
const csrfToken = typeof document !== "undefined" ? csrfHeaderForMethod(method, cookieString) : csrfHeaderForMethod(method, session.csrfToken ? `axiam_csrf=${session.csrfToken}` : "");
|
|
473
|
+
if (csrfToken) {
|
|
474
|
+
config.headers = config.headers ?? {};
|
|
475
|
+
config.headers["X-CSRF-Token"] = csrfToken;
|
|
476
|
+
}
|
|
477
|
+
return config;
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
function installRefreshInterceptor(axiosInstance, session) {
|
|
481
|
+
axiosInstance.interceptors.response.use(
|
|
482
|
+
(response) => response,
|
|
483
|
+
async (error) => {
|
|
484
|
+
const originalRequest = error.config;
|
|
485
|
+
if (!originalRequest || !error.response) {
|
|
486
|
+
return Promise.reject(error);
|
|
487
|
+
}
|
|
488
|
+
const status = error.response.status;
|
|
489
|
+
const url = originalRequest.url ?? "";
|
|
490
|
+
const isSkipRefresh = SKIP_REFRESH.some((skipUrl) => url.includes(skipUrl));
|
|
491
|
+
if (status === 401 && !originalRequest._retry && !isSkipRefresh && session.authenticated) {
|
|
492
|
+
originalRequest._retry = true;
|
|
493
|
+
try {
|
|
494
|
+
await session.refreshGuard(async () => {
|
|
495
|
+
await session.axios.post("/api/v1/auth/refresh", {});
|
|
496
|
+
});
|
|
497
|
+
return axiosInstance(originalRequest);
|
|
498
|
+
} catch (refreshError) {
|
|
499
|
+
session.authenticated = false;
|
|
500
|
+
session.csrfToken = void 0;
|
|
501
|
+
return Promise.reject(
|
|
502
|
+
mapHttpStatusToError(401, "session refresh failed; re-authentication required", {
|
|
503
|
+
cause: refreshError
|
|
504
|
+
})
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
if (status === 401 && isSkipRefresh) {
|
|
509
|
+
return Promise.reject(mapHttpStatusToError(401, "authentication failed"));
|
|
510
|
+
}
|
|
511
|
+
return Promise.reject(error);
|
|
512
|
+
}
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
function installInterceptors(axiosInstance, session) {
|
|
516
|
+
installCsrfInterceptor(axiosInstance, session);
|
|
517
|
+
installRefreshInterceptor(axiosInstance, session);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// src/rest/auth.ts
|
|
521
|
+
var LOGIN_PATH = "/api/v1/auth/login";
|
|
522
|
+
var MFA_VERIFY_PATH = "/api/v1/auth/mfa/verify";
|
|
523
|
+
var REFRESH_PATH = "/api/v1/auth/refresh";
|
|
524
|
+
var LOGOUT_PATH = "/api/v1/auth/logout";
|
|
525
|
+
function loginSuccessToResult(wire) {
|
|
526
|
+
return {
|
|
527
|
+
status: "authenticated",
|
|
528
|
+
user: { id: wire.user.id, username: wire.user.username, email: wire.user.email },
|
|
529
|
+
sessionId: wire.session_id,
|
|
530
|
+
expiresIn: wire.expires_in
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
function mfaRequiredToResult(wire) {
|
|
534
|
+
return {
|
|
535
|
+
status: "mfa_required",
|
|
536
|
+
mfaToken: wire.challenge_token,
|
|
537
|
+
availableMethods: wire.available_methods
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
function extractErrorMessage(err) {
|
|
541
|
+
if (err && typeof err === "object" && "message" in err && typeof err.message === "string") {
|
|
542
|
+
return err.message;
|
|
543
|
+
}
|
|
544
|
+
return "request failed";
|
|
545
|
+
}
|
|
546
|
+
async function login(client, email, password) {
|
|
547
|
+
const body = { username_or_email: email, password };
|
|
548
|
+
try {
|
|
549
|
+
const response = await client.session.axios.post(
|
|
550
|
+
LOGIN_PATH,
|
|
551
|
+
body
|
|
552
|
+
);
|
|
553
|
+
if (response.status === 202) {
|
|
554
|
+
return mfaRequiredToResult(response.data);
|
|
555
|
+
}
|
|
556
|
+
client.session.authenticated = true;
|
|
557
|
+
await client.session.onAuthenticated?.();
|
|
558
|
+
return loginSuccessToResult(response.data);
|
|
559
|
+
} catch (err) {
|
|
560
|
+
const status = extractAxiosStatus(err);
|
|
561
|
+
if (status !== void 0) {
|
|
562
|
+
throw mapHttpStatusToError(status, extractErrorMessage(extractAxiosData(err)) ?? "login failed", {
|
|
563
|
+
cause: err
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
throw new NetworkError("login request failed", sanitizeAxiosError(err));
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
async function verifyMfa(client, mfaToken, code) {
|
|
570
|
+
const body = { challenge_token: mfaToken, totp_code: code };
|
|
571
|
+
try {
|
|
572
|
+
const response = await client.session.axios.post(MFA_VERIFY_PATH, body);
|
|
573
|
+
client.session.authenticated = true;
|
|
574
|
+
await client.session.onAuthenticated?.();
|
|
575
|
+
return loginSuccessToResult(response.data);
|
|
576
|
+
} catch (err) {
|
|
577
|
+
const status = extractAxiosStatus(err);
|
|
578
|
+
if (status !== void 0) {
|
|
579
|
+
throw mapHttpStatusToError(status, extractErrorMessage(extractAxiosData(err)) ?? "verifyMfa failed", {
|
|
580
|
+
cause: err
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
throw new NetworkError("verifyMfa request failed", sanitizeAxiosError(err));
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
async function refresh(client) {
|
|
587
|
+
try {
|
|
588
|
+
await client.session.axios.post(REFRESH_PATH, {});
|
|
589
|
+
} catch (err) {
|
|
590
|
+
const status = extractAxiosStatus(err);
|
|
591
|
+
if (status !== void 0) {
|
|
592
|
+
throw mapHttpStatusToError(status, extractErrorMessage(extractAxiosData(err)) ?? "refresh failed", {
|
|
593
|
+
cause: err
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
throw new NetworkError("refresh request failed", sanitizeAxiosError(err));
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
async function logout(client) {
|
|
600
|
+
try {
|
|
601
|
+
await client.session.axios.post(LOGOUT_PATH, {});
|
|
602
|
+
} catch (err) {
|
|
603
|
+
const status = extractAxiosStatus(err);
|
|
604
|
+
if (status !== void 0) {
|
|
605
|
+
throw mapHttpStatusToError(status, extractErrorMessage(extractAxiosData(err)) ?? "logout failed", {
|
|
606
|
+
cause: err
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
throw new NetworkError("logout request failed", sanitizeAxiosError(err));
|
|
610
|
+
} finally {
|
|
611
|
+
client.session.authenticated = false;
|
|
612
|
+
client.session.csrfToken = void 0;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
function extractAxiosStatus(err) {
|
|
616
|
+
if (err && typeof err === "object" && "response" in err) {
|
|
617
|
+
const response = err.response;
|
|
618
|
+
return response?.status;
|
|
619
|
+
}
|
|
620
|
+
return void 0;
|
|
621
|
+
}
|
|
622
|
+
function extractAxiosData(err) {
|
|
623
|
+
if (err && typeof err === "object" && "response" in err) {
|
|
624
|
+
const response = err.response;
|
|
625
|
+
return response?.data;
|
|
626
|
+
}
|
|
627
|
+
return void 0;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// src/rest/authz.ts
|
|
631
|
+
var CHECK_PATH = "/api/v1/authz/check";
|
|
632
|
+
var BATCH_CHECK_PATH = "/api/v1/authz/check/batch";
|
|
633
|
+
function toWireBody(check) {
|
|
634
|
+
return {
|
|
635
|
+
action: check.action,
|
|
636
|
+
resource_id: check.resourceId,
|
|
637
|
+
scope: check.scope,
|
|
638
|
+
subject_id: check.subjectId
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
function fromWireDecision(wire) {
|
|
642
|
+
return { allowed: wire.allowed, reason: wire.reason };
|
|
643
|
+
}
|
|
644
|
+
async function checkAccess(client, check) {
|
|
645
|
+
try {
|
|
646
|
+
const response = await client.session.axios.post(CHECK_PATH, toWireBody(check));
|
|
647
|
+
return fromWireDecision(response.data);
|
|
648
|
+
} catch (err) {
|
|
649
|
+
throw mapAuthzError(err, check.action, check.resourceId);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
async function can(client, action, resourceId, scope) {
|
|
653
|
+
const decision = await checkAccess(client, { action, resourceId, scope });
|
|
654
|
+
return decision.allowed;
|
|
655
|
+
}
|
|
656
|
+
async function batchCheck(client, checks) {
|
|
657
|
+
const body = { checks: checks.map(toWireBody) };
|
|
658
|
+
try {
|
|
659
|
+
const response = await client.session.axios.post(BATCH_CHECK_PATH, body);
|
|
660
|
+
return response.data.results.map(fromWireDecision);
|
|
661
|
+
} catch (err) {
|
|
662
|
+
throw mapAuthzError(err);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
function mapAuthzError(err, action, resourceId) {
|
|
666
|
+
if (err && typeof err === "object" && "response" in err) {
|
|
667
|
+
const response = err.response;
|
|
668
|
+
if (response?.status !== void 0) {
|
|
669
|
+
if (response.status === 403) {
|
|
670
|
+
return mapHttpStatusToError(403, response.data?.message ?? "authorization denied", {
|
|
671
|
+
action,
|
|
672
|
+
resourceId,
|
|
673
|
+
body: response.data
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
return mapHttpStatusToError(response.status, response.data?.message ?? "authz request failed", {
|
|
677
|
+
action,
|
|
678
|
+
resourceId,
|
|
679
|
+
body: response.data,
|
|
680
|
+
cause: err
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
return new NetworkError("authz request failed", err);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// src/rest/client.ts
|
|
688
|
+
var AxiamClient = class {
|
|
689
|
+
/** @internal — exposed for auth.ts/authz.ts method implementations and other transports (D-13). */
|
|
690
|
+
session;
|
|
691
|
+
/**
|
|
692
|
+
* @param options client configuration (§5/§6).
|
|
693
|
+
* @param session optional pre-built session to adopt instead of the default
|
|
694
|
+
* browser `SharedSession`. This is the injection point for the Node
|
|
695
|
+
* persona (SDK-Q05): a Node REST consumer builds a `NodeSession` (cookie
|
|
696
|
+
* jar + CSRF/refresh token sync) via `createNodeClient`/`createNodeSession`
|
|
697
|
+
* from the Node-only `axiam-sdk/node` (or `axiam-sdk/grpc`) subpath and it
|
|
698
|
+
* is adopted here, so httpOnly login/refresh cookies persist. When omitted
|
|
699
|
+
* the browser `SharedSession` is built by default — the Node modules are
|
|
700
|
+
* NEVER statically imported from this browser-safe module, so a `/rest`
|
|
701
|
+
* browser bundle keeps pulling zero Node dependencies (SC#1).
|
|
702
|
+
*/
|
|
703
|
+
constructor(options, session) {
|
|
704
|
+
this.session = session ?? createSession(options);
|
|
705
|
+
installInterceptors(this.session.axios, this.session);
|
|
706
|
+
}
|
|
707
|
+
/** `POST /api/v1/auth/login` (§1, D-18). */
|
|
708
|
+
login(email, password) {
|
|
709
|
+
return login(this, email, password);
|
|
710
|
+
}
|
|
711
|
+
/** `POST /api/v1/auth/mfa/verify` (§1, D-18). Completes the two-phase flow started by login(). */
|
|
712
|
+
verifyMfa(mfaToken, code) {
|
|
713
|
+
return verifyMfa(this, mfaToken, code);
|
|
714
|
+
}
|
|
715
|
+
/** `POST /api/v1/auth/refresh` (§1). Usually driven reactively by the response interceptor (D-07). */
|
|
716
|
+
refresh() {
|
|
717
|
+
return refresh(this);
|
|
718
|
+
}
|
|
719
|
+
/** `POST /api/v1/auth/logout` (§1). Clears session csrf/auth state. */
|
|
720
|
+
logout() {
|
|
721
|
+
return logout(this);
|
|
722
|
+
}
|
|
723
|
+
/** `POST /api/v1/authz/check` (§1, FND-04). */
|
|
724
|
+
checkAccess(check) {
|
|
725
|
+
return checkAccess(this, check);
|
|
726
|
+
}
|
|
727
|
+
/** `can` — alias for checkAccess targeting browser/UI scenarios (§1 note). */
|
|
728
|
+
can(action, resourceId, scope) {
|
|
729
|
+
return can(this, action, resourceId, scope);
|
|
730
|
+
}
|
|
731
|
+
/** `POST /api/v1/authz/check/batch` (§1). Results preserve input order. */
|
|
732
|
+
batchCheck(checks) {
|
|
733
|
+
return batchCheck(this, checks);
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
var ACCESS_COOKIE = "axiam_access";
|
|
737
|
+
var REFRESH_COOKIE = "axiam_refresh";
|
|
738
|
+
var CSRF_COOKIE = "axiam_csrf";
|
|
739
|
+
function createJar() {
|
|
740
|
+
return new toughCookie.CookieJar();
|
|
741
|
+
}
|
|
742
|
+
function wrapAxios(instance, jar) {
|
|
743
|
+
const wrapped = axiosCookiejarSupport.wrapper(instance);
|
|
744
|
+
wrapped.defaults.jar = jar;
|
|
745
|
+
return wrapped;
|
|
746
|
+
}
|
|
747
|
+
async function extractCookieValue(jar, url, name) {
|
|
748
|
+
const cookies = await jar.getCookies(url);
|
|
749
|
+
return cookies.find((c) => c.key === name)?.value;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// src/node/tokenManager.ts
|
|
753
|
+
var REFRESH_COOKIE_PATH = "/api/v1/auth/refresh";
|
|
754
|
+
var TokenManager = class {
|
|
755
|
+
#jar;
|
|
756
|
+
#baseUrl;
|
|
757
|
+
#tenantId;
|
|
758
|
+
/**
|
|
759
|
+
* Non-blocking cached copy of the current access token, kept in sync via
|
|
760
|
+
* `syncFromJar()` after each REST call/refresh. Read via
|
|
761
|
+
* `cachedAccessToken()` without ever touching the jar's async API — this
|
|
762
|
+
* is the fast-path the grpc-js interceptor's synchronous `start()` needs
|
|
763
|
+
* (Pitfall 3).
|
|
764
|
+
*/
|
|
765
|
+
#cachedAccess = null;
|
|
766
|
+
constructor(jar, baseUrl, tenantId) {
|
|
767
|
+
this.#jar = jar;
|
|
768
|
+
this.#baseUrl = baseUrl;
|
|
769
|
+
this.#tenantId = tenantId;
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Synchronous, best-effort read of the last-known access token. Returns
|
|
773
|
+
* `null` if no token has been cached yet (no request/refresh has
|
|
774
|
+
* completed since construction, or `syncFromJar()` was never called).
|
|
775
|
+
*/
|
|
776
|
+
cachedAccessToken() {
|
|
777
|
+
return this.#cachedAccess;
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* Async read of the current `axiam_refresh` cookie value from the jar,
|
|
781
|
+
* wrapped in Sensitive<T>. Queried against the refresh endpoint's URL
|
|
782
|
+
* since the cookie is path-scoped to `/api/v1/auth/refresh` (not visible
|
|
783
|
+
* at the bare base URL).
|
|
784
|
+
*/
|
|
785
|
+
async refreshTokenValue() {
|
|
786
|
+
const value = await extractCookieValue(this.#jar, `${this.#baseUrl}${REFRESH_COOKIE_PATH}`, REFRESH_COOKIE);
|
|
787
|
+
return value === void 0 ? null : new Sensitive(value);
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* Refresh the in-memory cached access token from the jar. Call this after
|
|
791
|
+
* every REST request/refresh so the synchronous gRPC-interceptor fast-path
|
|
792
|
+
* (`cachedAccessToken()`) stays current (Pitfall 3).
|
|
793
|
+
*/
|
|
794
|
+
async syncFromJar() {
|
|
795
|
+
const value = await extractCookieValue(this.#jar, this.#baseUrl, ACCESS_COOKIE);
|
|
796
|
+
this.#cachedAccess = value === void 0 ? null : new Sensitive(value);
|
|
797
|
+
}
|
|
798
|
+
/** The resolved tenant identifier (slug or UUID form), if known. */
|
|
799
|
+
tenantId() {
|
|
800
|
+
return this.#tenantId;
|
|
801
|
+
}
|
|
802
|
+
/** Set/update the resolved tenant identifier (e.g. after decoding the access token's tenant_id claim). */
|
|
803
|
+
setTenantId(tenantId) {
|
|
804
|
+
this.#tenantId = tenantId;
|
|
805
|
+
}
|
|
806
|
+
/** Clear all cached token state (used by logout()). */
|
|
807
|
+
clear() {
|
|
808
|
+
this.#cachedAccess = null;
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
|
|
812
|
+
// src/node/jwks.ts
|
|
813
|
+
var JWKS_PATH = "/oauth2/jwks";
|
|
814
|
+
var COOLDOWN_DURATION_MS = 6e4;
|
|
815
|
+
var TIMEOUT_DURATION_MS = 5e3;
|
|
816
|
+
function createVerifier(baseUrl) {
|
|
817
|
+
let jwksPromise = null;
|
|
818
|
+
async function getJwks() {
|
|
819
|
+
if (!jwksPromise) {
|
|
820
|
+
jwksPromise = import('jose').then(
|
|
821
|
+
({ createRemoteJWKSet }) => createRemoteJWKSet(new URL(`${baseUrl}${JWKS_PATH}`), {
|
|
822
|
+
cooldownDuration: COOLDOWN_DURATION_MS,
|
|
823
|
+
timeoutDuration: TIMEOUT_DURATION_MS
|
|
824
|
+
})
|
|
825
|
+
);
|
|
826
|
+
}
|
|
827
|
+
return jwksPromise;
|
|
828
|
+
}
|
|
829
|
+
return {
|
|
830
|
+
async verifyAccessToken(token) {
|
|
831
|
+
const { jwtVerify } = await import('jose');
|
|
832
|
+
const jwks = await getJwks();
|
|
833
|
+
const { payload } = await jwtVerify(token, jwks, { algorithms: ["EdDSA"] });
|
|
834
|
+
return payload;
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
// src/node/session.ts
|
|
840
|
+
var NodeSession = class extends SharedSession {
|
|
841
|
+
tokenManager;
|
|
842
|
+
jwksVerifier;
|
|
843
|
+
#jar;
|
|
844
|
+
constructor(options, base, tokenManager, jwksVerifier, jar) {
|
|
845
|
+
super(options, base.axios, base.tenantHeaderValue);
|
|
846
|
+
this.tokenManager = tokenManager;
|
|
847
|
+
this.jwksVerifier = jwksVerifier;
|
|
848
|
+
this.#jar = jar;
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* Drives the actual `POST /api/v1/auth/refresh` HTTP call. Passed to this
|
|
852
|
+
* session's per-instance `refreshGuard` (inherited from SharedSession) by
|
|
853
|
+
* both REST's reactive interceptor (rest/interceptors.ts) and gRPC's
|
|
854
|
+
* callWithRefresh — since `refreshGuard` is scoped to THIS session
|
|
855
|
+
* instance (CR-02, D-13), both transports share exactly one in-flight
|
|
856
|
+
* refresh for this session regardless of which one triggers it, while
|
|
857
|
+
* remaining fully independent from any other session's guard.
|
|
858
|
+
*/
|
|
859
|
+
doRefresh = async () => {
|
|
860
|
+
await this.axios.post("/api/v1/auth/refresh", {});
|
|
861
|
+
await this.tokenManager.syncFromJar();
|
|
862
|
+
await this.#syncCsrfFromJar();
|
|
863
|
+
};
|
|
864
|
+
/**
|
|
865
|
+
* Session-level hook (CR-01, D-05) invoked by rest/auth.ts after a
|
|
866
|
+
* successful login()/verifyMfa(). Refreshes the cached access token (so
|
|
867
|
+
* the gRPC interceptor's synchronous fast-path is current immediately
|
|
868
|
+
* after login) and reads the `axiam_csrf` cookie out of the jar into
|
|
869
|
+
* `session.csrfToken`, mirroring TokenManager.syncFromJar()'s pattern.
|
|
870
|
+
*/
|
|
871
|
+
onAuthenticated = async () => {
|
|
872
|
+
await this.tokenManager.syncFromJar();
|
|
873
|
+
await this.#syncCsrfFromJar();
|
|
874
|
+
};
|
|
875
|
+
async #syncCsrfFromJar() {
|
|
876
|
+
this.csrfToken = await extractCookieValue(this.#jar, this.baseUrl, CSRF_COOKIE);
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
function createNodeSession(options) {
|
|
880
|
+
const base = createSession(options);
|
|
881
|
+
const jar = createJar();
|
|
882
|
+
wrapAxios(base.axios, jar);
|
|
883
|
+
const tokenManager = new TokenManager(jar, options.baseUrl, base.tenantHeaderValue);
|
|
884
|
+
const jwksVerifier = createVerifier(options.baseUrl);
|
|
885
|
+
return new NodeSession(options, base, tokenManager, jwksVerifier, jar);
|
|
886
|
+
}
|
|
887
|
+
function createNodeClient(options) {
|
|
888
|
+
return new AxiamClient(options, createNodeSession(options));
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
exports.ACCESS_COOKIE = ACCESS_COOKIE;
|
|
892
|
+
exports.AuthzGrpcClient = AuthzGrpcClient;
|
|
893
|
+
exports.CSRF_COOKIE = CSRF_COOKIE;
|
|
894
|
+
exports.JWKS_PATH = JWKS_PATH;
|
|
895
|
+
exports.NodeSession = NodeSession;
|
|
896
|
+
exports.REFRESH_COOKIE = REFRESH_COOKIE;
|
|
897
|
+
exports.TokenManager = TokenManager;
|
|
898
|
+
exports.authInterceptor = authInterceptor;
|
|
899
|
+
exports.buildAuthorizationServiceClient = buildAuthorizationServiceClient;
|
|
900
|
+
exports.callWithRefresh = callWithRefresh;
|
|
901
|
+
exports.createJar = createJar;
|
|
902
|
+
exports.createNodeClient = createNodeClient;
|
|
903
|
+
exports.createNodeSession = createNodeSession;
|
|
904
|
+
exports.createVerifier = createVerifier;
|
|
905
|
+
exports.extractCookieValue = extractCookieValue;
|
|
906
|
+
exports.wrapAxios = wrapAxios;
|
|
907
|
+
//# sourceMappingURL=index.js.map
|
|
908
|
+
//# sourceMappingURL=index.js.map
|