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
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
3
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
4
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
5
|
+
}) : x)(function(x) {
|
|
6
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
7
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
// src/core/errors.ts
|
|
11
|
+
var AxiamError = class extends Error {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var AuthError = class _AuthError extends AxiamError {
|
|
18
|
+
constructor(message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = "AuthError";
|
|
21
|
+
Object.setPrototypeOf(this, _AuthError.prototype);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var AuthzError = class _AuthzError extends AxiamError {
|
|
25
|
+
action;
|
|
26
|
+
resourceId;
|
|
27
|
+
constructor(message, action, resourceId) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = "AuthzError";
|
|
30
|
+
this.action = action;
|
|
31
|
+
this.resourceId = resourceId;
|
|
32
|
+
Object.setPrototypeOf(this, _AuthzError.prototype);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var NetworkError = class _NetworkError extends AxiamError {
|
|
36
|
+
cause;
|
|
37
|
+
constructor(message, cause) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "NetworkError";
|
|
40
|
+
this.cause = cause;
|
|
41
|
+
Object.setPrototypeOf(this, _NetworkError.prototype);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/core/errorMapper.ts
|
|
46
|
+
function extractAuthzFieldsFromBody(body) {
|
|
47
|
+
if (body === null || typeof body !== "object") {
|
|
48
|
+
return {};
|
|
49
|
+
}
|
|
50
|
+
const record = body;
|
|
51
|
+
const action = typeof record.action === "string" ? record.action : void 0;
|
|
52
|
+
const resourceId = typeof record.resource_id === "string" ? record.resource_id : void 0;
|
|
53
|
+
return { action, resourceId };
|
|
54
|
+
}
|
|
55
|
+
var SAFE_RESPONSE_HEADERS = /* @__PURE__ */ new Set([
|
|
56
|
+
"content-type",
|
|
57
|
+
"content-length",
|
|
58
|
+
"date",
|
|
59
|
+
"server",
|
|
60
|
+
"retry-after",
|
|
61
|
+
"x-request-id",
|
|
62
|
+
"x-tenant-id"
|
|
63
|
+
]);
|
|
64
|
+
var REDACTED_HEADER = "[REDACTED]";
|
|
65
|
+
function sanitizeAxiosError(err) {
|
|
66
|
+
if (err === null || typeof err !== "object") {
|
|
67
|
+
return err;
|
|
68
|
+
}
|
|
69
|
+
const candidate = err;
|
|
70
|
+
if (candidate.response === null || typeof candidate.response !== "object") {
|
|
71
|
+
return err;
|
|
72
|
+
}
|
|
73
|
+
const response = candidate.response;
|
|
74
|
+
if (response.headers === null || typeof response.headers !== "object") {
|
|
75
|
+
return err;
|
|
76
|
+
}
|
|
77
|
+
const sanitizedHeaders = { ...response.headers };
|
|
78
|
+
for (const key of Object.keys(sanitizedHeaders)) {
|
|
79
|
+
if (!SAFE_RESPONSE_HEADERS.has(key.toLowerCase())) {
|
|
80
|
+
sanitizedHeaders[key] = REDACTED_HEADER;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
...err,
|
|
85
|
+
response: {
|
|
86
|
+
...response,
|
|
87
|
+
headers: sanitizedHeaders
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function mapHttpStatusToError(status, message, ctx) {
|
|
92
|
+
if (status === 401) {
|
|
93
|
+
return new AuthError(message);
|
|
94
|
+
}
|
|
95
|
+
if (status === 403 || status === 409) {
|
|
96
|
+
const fromBody = extractAuthzFieldsFromBody(ctx?.body);
|
|
97
|
+
return new AuthzError(message, fromBody.action ?? ctx?.action, fromBody.resourceId ?? ctx?.resourceId);
|
|
98
|
+
}
|
|
99
|
+
return new NetworkError(message, sanitizeAxiosError(ctx?.cause));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/core/csrf.ts
|
|
103
|
+
var CSRF_METHODS = /* @__PURE__ */ new Set(["post", "put", "patch", "delete"]);
|
|
104
|
+
var CSRF_COOKIE_REGEX = /(?:^|;\s*)axiam_csrf=([^;]*)/;
|
|
105
|
+
function readCsrfCookie(cookieString) {
|
|
106
|
+
const match = CSRF_COOKIE_REGEX.exec(cookieString);
|
|
107
|
+
if (!match) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
return decodeURIComponent(match[1]);
|
|
111
|
+
}
|
|
112
|
+
function csrfHeaderForMethod(method, cookieString) {
|
|
113
|
+
if (!CSRF_METHODS.has(method.toLowerCase())) {
|
|
114
|
+
return void 0;
|
|
115
|
+
}
|
|
116
|
+
const token = readCsrfCookie(cookieString);
|
|
117
|
+
return token ?? void 0;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/core/singleFlightRefresh.ts
|
|
121
|
+
function createRefreshGuard() {
|
|
122
|
+
let refreshPromise = null;
|
|
123
|
+
const guard = ((doRefresh) => {
|
|
124
|
+
if (!refreshPromise) {
|
|
125
|
+
refreshPromise = doRefresh().finally(() => {
|
|
126
|
+
refreshPromise = null;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return refreshPromise;
|
|
130
|
+
});
|
|
131
|
+
guard.__reset = () => {
|
|
132
|
+
refreshPromise = null;
|
|
133
|
+
};
|
|
134
|
+
return guard;
|
|
135
|
+
}
|
|
136
|
+
createRefreshGuard();
|
|
137
|
+
|
|
138
|
+
// src/core/config.ts
|
|
139
|
+
var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
140
|
+
|
|
141
|
+
// src/rest/session.ts
|
|
142
|
+
var PEM_MARKER = "-----BEGIN CERTIFICATE-----";
|
|
143
|
+
var SharedSession = class {
|
|
144
|
+
/** 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
|
+
axios;
|
|
146
|
+
/** The AXIAM server base URL this session was constructed with (`AxiamClientOptions.baseUrl`). */
|
|
147
|
+
baseUrl;
|
|
148
|
+
/** The resolved tenant identifier (`tenantSlug` or `tenantId`) injected as the `X-Tenant-ID` header on every same-origin request (§5.2). */
|
|
149
|
+
tenantHeaderValue;
|
|
150
|
+
/** Mutable CSRF token store — populated by the request/response interceptors (D-05). */
|
|
151
|
+
csrfToken;
|
|
152
|
+
/** Set true once a successful login/verifyMfa has completed. */
|
|
153
|
+
authenticated = false;
|
|
154
|
+
/**
|
|
155
|
+
* Per-instance single-flight refresh guard (CR-02, D-13). Shared across
|
|
156
|
+
* this session's REST and gRPC transports (rest/interceptors.ts,
|
|
157
|
+
* grpc/callWithRefresh.ts both call `session.refreshGuard(...)`), but
|
|
158
|
+
* NEVER shared with a different SharedSession/NodeSession instance.
|
|
159
|
+
*
|
|
160
|
+
* @internal SDK-internal transport wiring, not intended to be invoked
|
|
161
|
+
* directly by SDK consumers — refreshes are driven automatically by the
|
|
162
|
+
* response interceptor (rest) or `callWithRefresh` (grpc).
|
|
163
|
+
*/
|
|
164
|
+
refreshGuard;
|
|
165
|
+
constructor(options, axiosInstance, tenantHeaderValue) {
|
|
166
|
+
this.axios = axiosInstance;
|
|
167
|
+
this.baseUrl = options.baseUrl;
|
|
168
|
+
this.tenantHeaderValue = tenantHeaderValue;
|
|
169
|
+
this.refreshGuard = createRefreshGuard();
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Host-isolation guard (3A, defense in depth): returns `true` when `url`
|
|
173
|
+
* targets a host other than this session's base origin — an absolute
|
|
174
|
+
* third-party URL, or a redirect that axios/the browser resolved off-origin.
|
|
175
|
+
* The tenant identifier and CSRF token must never be attached to such a
|
|
176
|
+
* request. A relative/host-less `url` (the normal case, merged against
|
|
177
|
+
* `baseUrl`) is same-origin and returns `false`. Mirrors the Python SDK's
|
|
178
|
+
* `_prepare_request` guard. Malformed input fails closed (treated as
|
|
179
|
+
* foreign).
|
|
180
|
+
*/
|
|
181
|
+
isForeignHost(url) {
|
|
182
|
+
if (!url) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
const target = new URL(url, this.baseUrl);
|
|
187
|
+
return target.host !== new URL(this.baseUrl).host;
|
|
188
|
+
} catch {
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
function resolveTenantHeaderValue(options) {
|
|
194
|
+
if (options.tenantSlug) {
|
|
195
|
+
return options.tenantSlug;
|
|
196
|
+
}
|
|
197
|
+
if (options.tenantId) {
|
|
198
|
+
return options.tenantId;
|
|
199
|
+
}
|
|
200
|
+
throw new Error(
|
|
201
|
+
"AxiamClient construction requires a tenant: provide either tenantSlug or tenantId (CONTRACT.md \xA75)."
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
function maybeBuildHttpsAgent(customCa) {
|
|
205
|
+
if (!customCa) {
|
|
206
|
+
return void 0;
|
|
207
|
+
}
|
|
208
|
+
if (!customCa.includes(PEM_MARKER)) {
|
|
209
|
+
throw new Error(
|
|
210
|
+
'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
if (typeof process === "undefined") {
|
|
214
|
+
return void 0;
|
|
215
|
+
}
|
|
216
|
+
const https = __require("https");
|
|
217
|
+
return new https.Agent({ ca: customCa });
|
|
218
|
+
}
|
|
219
|
+
function createSession(options) {
|
|
220
|
+
const tenantHeaderValue = resolveTenantHeaderValue(options);
|
|
221
|
+
if (options.customCa !== void 0 && !options.customCa.includes(PEM_MARKER)) {
|
|
222
|
+
throw new Error(
|
|
223
|
+
'customCa must be a PEM-encoded certificate (expected to contain "-----BEGIN CERTIFICATE-----") (CONTRACT.md \xA76).'
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
const httpsAgent = maybeBuildHttpsAgent(options.customCa);
|
|
227
|
+
const axiosInstance = axios.create({
|
|
228
|
+
baseURL: options.baseUrl,
|
|
229
|
+
withCredentials: true,
|
|
230
|
+
timeout: options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
|
|
231
|
+
...httpsAgent ? { httpsAgent } : {}
|
|
232
|
+
});
|
|
233
|
+
const session = new SharedSession(options, axiosInstance, tenantHeaderValue);
|
|
234
|
+
axiosInstance.interceptors.request.use((config) => {
|
|
235
|
+
if (session.isForeignHost(config.url)) {
|
|
236
|
+
return config;
|
|
237
|
+
}
|
|
238
|
+
config.headers = config.headers ?? {};
|
|
239
|
+
config.headers["X-Tenant-ID"] = session.tenantHeaderValue;
|
|
240
|
+
return config;
|
|
241
|
+
});
|
|
242
|
+
return session;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// src/rest/interceptors.ts
|
|
246
|
+
var SKIP_REFRESH = ["/api/v1/auth/refresh", "/api/v1/auth/login", "/api/v1/auth/logout"];
|
|
247
|
+
function installCsrfInterceptor(axiosInstance, session) {
|
|
248
|
+
axiosInstance.interceptors.request.use((config) => {
|
|
249
|
+
if (session.isForeignHost(config.url)) {
|
|
250
|
+
return config;
|
|
251
|
+
}
|
|
252
|
+
const method = (config.method ?? "get").toLowerCase();
|
|
253
|
+
const cookieString = typeof document !== "undefined" ? document.cookie : "";
|
|
254
|
+
const csrfToken = typeof document !== "undefined" ? csrfHeaderForMethod(method, cookieString) : csrfHeaderForMethod(method, session.csrfToken ? `axiam_csrf=${session.csrfToken}` : "");
|
|
255
|
+
if (csrfToken) {
|
|
256
|
+
config.headers = config.headers ?? {};
|
|
257
|
+
config.headers["X-CSRF-Token"] = csrfToken;
|
|
258
|
+
}
|
|
259
|
+
return config;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
function installRefreshInterceptor(axiosInstance, session) {
|
|
263
|
+
axiosInstance.interceptors.response.use(
|
|
264
|
+
(response) => response,
|
|
265
|
+
async (error) => {
|
|
266
|
+
const originalRequest = error.config;
|
|
267
|
+
if (!originalRequest || !error.response) {
|
|
268
|
+
return Promise.reject(error);
|
|
269
|
+
}
|
|
270
|
+
const status = error.response.status;
|
|
271
|
+
const url = originalRequest.url ?? "";
|
|
272
|
+
const isSkipRefresh = SKIP_REFRESH.some((skipUrl) => url.includes(skipUrl));
|
|
273
|
+
if (status === 401 && !originalRequest._retry && !isSkipRefresh && session.authenticated) {
|
|
274
|
+
originalRequest._retry = true;
|
|
275
|
+
try {
|
|
276
|
+
await session.refreshGuard(async () => {
|
|
277
|
+
await session.axios.post("/api/v1/auth/refresh", {});
|
|
278
|
+
});
|
|
279
|
+
return axiosInstance(originalRequest);
|
|
280
|
+
} catch (refreshError) {
|
|
281
|
+
session.authenticated = false;
|
|
282
|
+
session.csrfToken = void 0;
|
|
283
|
+
return Promise.reject(
|
|
284
|
+
mapHttpStatusToError(401, "session refresh failed; re-authentication required", {
|
|
285
|
+
cause: refreshError
|
|
286
|
+
})
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (status === 401 && isSkipRefresh) {
|
|
291
|
+
return Promise.reject(mapHttpStatusToError(401, "authentication failed"));
|
|
292
|
+
}
|
|
293
|
+
return Promise.reject(error);
|
|
294
|
+
}
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
function installInterceptors(axiosInstance, session) {
|
|
298
|
+
installCsrfInterceptor(axiosInstance, session);
|
|
299
|
+
installRefreshInterceptor(axiosInstance, session);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// src/rest/auth.ts
|
|
303
|
+
var LOGIN_PATH = "/api/v1/auth/login";
|
|
304
|
+
var MFA_VERIFY_PATH = "/api/v1/auth/mfa/verify";
|
|
305
|
+
var REFRESH_PATH = "/api/v1/auth/refresh";
|
|
306
|
+
var LOGOUT_PATH = "/api/v1/auth/logout";
|
|
307
|
+
function loginSuccessToResult(wire) {
|
|
308
|
+
return {
|
|
309
|
+
status: "authenticated",
|
|
310
|
+
user: { id: wire.user.id, username: wire.user.username, email: wire.user.email },
|
|
311
|
+
sessionId: wire.session_id,
|
|
312
|
+
expiresIn: wire.expires_in
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
function mfaRequiredToResult(wire) {
|
|
316
|
+
return {
|
|
317
|
+
status: "mfa_required",
|
|
318
|
+
mfaToken: wire.challenge_token,
|
|
319
|
+
availableMethods: wire.available_methods
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
function extractErrorMessage(err) {
|
|
323
|
+
if (err && typeof err === "object" && "message" in err && typeof err.message === "string") {
|
|
324
|
+
return err.message;
|
|
325
|
+
}
|
|
326
|
+
return "request failed";
|
|
327
|
+
}
|
|
328
|
+
async function login(client, email, password) {
|
|
329
|
+
const body = { username_or_email: email, password };
|
|
330
|
+
try {
|
|
331
|
+
const response = await client.session.axios.post(
|
|
332
|
+
LOGIN_PATH,
|
|
333
|
+
body
|
|
334
|
+
);
|
|
335
|
+
if (response.status === 202) {
|
|
336
|
+
return mfaRequiredToResult(response.data);
|
|
337
|
+
}
|
|
338
|
+
client.session.authenticated = true;
|
|
339
|
+
await client.session.onAuthenticated?.();
|
|
340
|
+
return loginSuccessToResult(response.data);
|
|
341
|
+
} catch (err) {
|
|
342
|
+
const status = extractAxiosStatus(err);
|
|
343
|
+
if (status !== void 0) {
|
|
344
|
+
throw mapHttpStatusToError(status, extractErrorMessage(extractAxiosData(err)) ?? "login failed", {
|
|
345
|
+
cause: err
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
throw new NetworkError("login request failed", sanitizeAxiosError(err));
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
async function verifyMfa(client, mfaToken, code) {
|
|
352
|
+
const body = { challenge_token: mfaToken, totp_code: code };
|
|
353
|
+
try {
|
|
354
|
+
const response = await client.session.axios.post(MFA_VERIFY_PATH, body);
|
|
355
|
+
client.session.authenticated = true;
|
|
356
|
+
await client.session.onAuthenticated?.();
|
|
357
|
+
return loginSuccessToResult(response.data);
|
|
358
|
+
} catch (err) {
|
|
359
|
+
const status = extractAxiosStatus(err);
|
|
360
|
+
if (status !== void 0) {
|
|
361
|
+
throw mapHttpStatusToError(status, extractErrorMessage(extractAxiosData(err)) ?? "verifyMfa failed", {
|
|
362
|
+
cause: err
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
throw new NetworkError("verifyMfa request failed", sanitizeAxiosError(err));
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
async function refresh(client) {
|
|
369
|
+
try {
|
|
370
|
+
await client.session.axios.post(REFRESH_PATH, {});
|
|
371
|
+
} catch (err) {
|
|
372
|
+
const status = extractAxiosStatus(err);
|
|
373
|
+
if (status !== void 0) {
|
|
374
|
+
throw mapHttpStatusToError(status, extractErrorMessage(extractAxiosData(err)) ?? "refresh failed", {
|
|
375
|
+
cause: err
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
throw new NetworkError("refresh request failed", sanitizeAxiosError(err));
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
async function logout(client) {
|
|
382
|
+
try {
|
|
383
|
+
await client.session.axios.post(LOGOUT_PATH, {});
|
|
384
|
+
} catch (err) {
|
|
385
|
+
const status = extractAxiosStatus(err);
|
|
386
|
+
if (status !== void 0) {
|
|
387
|
+
throw mapHttpStatusToError(status, extractErrorMessage(extractAxiosData(err)) ?? "logout failed", {
|
|
388
|
+
cause: err
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
throw new NetworkError("logout request failed", sanitizeAxiosError(err));
|
|
392
|
+
} finally {
|
|
393
|
+
client.session.authenticated = false;
|
|
394
|
+
client.session.csrfToken = void 0;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
function extractAxiosStatus(err) {
|
|
398
|
+
if (err && typeof err === "object" && "response" in err) {
|
|
399
|
+
const response = err.response;
|
|
400
|
+
return response?.status;
|
|
401
|
+
}
|
|
402
|
+
return void 0;
|
|
403
|
+
}
|
|
404
|
+
function extractAxiosData(err) {
|
|
405
|
+
if (err && typeof err === "object" && "response" in err) {
|
|
406
|
+
const response = err.response;
|
|
407
|
+
return response?.data;
|
|
408
|
+
}
|
|
409
|
+
return void 0;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// src/rest/authz.ts
|
|
413
|
+
var CHECK_PATH = "/api/v1/authz/check";
|
|
414
|
+
var BATCH_CHECK_PATH = "/api/v1/authz/check/batch";
|
|
415
|
+
function toWireBody(check) {
|
|
416
|
+
return {
|
|
417
|
+
action: check.action,
|
|
418
|
+
resource_id: check.resourceId,
|
|
419
|
+
scope: check.scope,
|
|
420
|
+
subject_id: check.subjectId
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function fromWireDecision(wire) {
|
|
424
|
+
return { allowed: wire.allowed, reason: wire.reason };
|
|
425
|
+
}
|
|
426
|
+
async function checkAccess(client, check) {
|
|
427
|
+
try {
|
|
428
|
+
const response = await client.session.axios.post(CHECK_PATH, toWireBody(check));
|
|
429
|
+
return fromWireDecision(response.data);
|
|
430
|
+
} catch (err) {
|
|
431
|
+
throw mapAuthzError(err, check.action, check.resourceId);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
async function can(client, action, resourceId, scope) {
|
|
435
|
+
const decision = await checkAccess(client, { action, resourceId, scope });
|
|
436
|
+
return decision.allowed;
|
|
437
|
+
}
|
|
438
|
+
async function batchCheck(client, checks) {
|
|
439
|
+
const body = { checks: checks.map(toWireBody) };
|
|
440
|
+
try {
|
|
441
|
+
const response = await client.session.axios.post(BATCH_CHECK_PATH, body);
|
|
442
|
+
return response.data.results.map(fromWireDecision);
|
|
443
|
+
} catch (err) {
|
|
444
|
+
throw mapAuthzError(err);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function mapAuthzError(err, action, resourceId) {
|
|
448
|
+
if (err && typeof err === "object" && "response" in err) {
|
|
449
|
+
const response = err.response;
|
|
450
|
+
if (response?.status !== void 0) {
|
|
451
|
+
if (response.status === 403) {
|
|
452
|
+
return mapHttpStatusToError(403, response.data?.message ?? "authorization denied", {
|
|
453
|
+
action,
|
|
454
|
+
resourceId,
|
|
455
|
+
body: response.data
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
return mapHttpStatusToError(response.status, response.data?.message ?? "authz request failed", {
|
|
459
|
+
action,
|
|
460
|
+
resourceId,
|
|
461
|
+
body: response.data,
|
|
462
|
+
cause: err
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return new NetworkError("authz request failed", err);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// src/rest/client.ts
|
|
470
|
+
var AxiamClient = class {
|
|
471
|
+
/** @internal — exposed for auth.ts/authz.ts method implementations and other transports (D-13). */
|
|
472
|
+
session;
|
|
473
|
+
/**
|
|
474
|
+
* @param options client configuration (§5/§6).
|
|
475
|
+
* @param session optional pre-built session to adopt instead of the default
|
|
476
|
+
* browser `SharedSession`. This is the injection point for the Node
|
|
477
|
+
* persona (SDK-Q05): a Node REST consumer builds a `NodeSession` (cookie
|
|
478
|
+
* jar + CSRF/refresh token sync) via `createNodeClient`/`createNodeSession`
|
|
479
|
+
* from the Node-only `axiam-sdk/node` (or `axiam-sdk/grpc`) subpath and it
|
|
480
|
+
* is adopted here, so httpOnly login/refresh cookies persist. When omitted
|
|
481
|
+
* the browser `SharedSession` is built by default — the Node modules are
|
|
482
|
+
* NEVER statically imported from this browser-safe module, so a `/rest`
|
|
483
|
+
* browser bundle keeps pulling zero Node dependencies (SC#1).
|
|
484
|
+
*/
|
|
485
|
+
constructor(options, session) {
|
|
486
|
+
this.session = session ?? createSession(options);
|
|
487
|
+
installInterceptors(this.session.axios, this.session);
|
|
488
|
+
}
|
|
489
|
+
/** `POST /api/v1/auth/login` (§1, D-18). */
|
|
490
|
+
login(email, password) {
|
|
491
|
+
return login(this, email, password);
|
|
492
|
+
}
|
|
493
|
+
/** `POST /api/v1/auth/mfa/verify` (§1, D-18). Completes the two-phase flow started by login(). */
|
|
494
|
+
verifyMfa(mfaToken, code) {
|
|
495
|
+
return verifyMfa(this, mfaToken, code);
|
|
496
|
+
}
|
|
497
|
+
/** `POST /api/v1/auth/refresh` (§1). Usually driven reactively by the response interceptor (D-07). */
|
|
498
|
+
refresh() {
|
|
499
|
+
return refresh(this);
|
|
500
|
+
}
|
|
501
|
+
/** `POST /api/v1/auth/logout` (§1). Clears session csrf/auth state. */
|
|
502
|
+
logout() {
|
|
503
|
+
return logout(this);
|
|
504
|
+
}
|
|
505
|
+
/** `POST /api/v1/authz/check` (§1, FND-04). */
|
|
506
|
+
checkAccess(check) {
|
|
507
|
+
return checkAccess(this, check);
|
|
508
|
+
}
|
|
509
|
+
/** `can` — alias for checkAccess targeting browser/UI scenarios (§1 note). */
|
|
510
|
+
can(action, resourceId, scope) {
|
|
511
|
+
return can(this, action, resourceId, scope);
|
|
512
|
+
}
|
|
513
|
+
/** `POST /api/v1/authz/check/batch` (§1). Results preserve input order. */
|
|
514
|
+
batchCheck(checks) {
|
|
515
|
+
return batchCheck(this, checks);
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
// src/rest/retry.ts
|
|
520
|
+
function isRetryAfterCarrier(err) {
|
|
521
|
+
return typeof err === "object" && err !== null && "retryAfterMs" in err;
|
|
522
|
+
}
|
|
523
|
+
function sleep(ms) {
|
|
524
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
525
|
+
}
|
|
526
|
+
function backoffDelayMs(attempt) {
|
|
527
|
+
const base = Math.min(1e3 * 2 ** attempt, 8e3);
|
|
528
|
+
const jitter = Math.random() * base * 0.2;
|
|
529
|
+
return base + jitter;
|
|
530
|
+
}
|
|
531
|
+
async function withRetry(fn, options) {
|
|
532
|
+
const maxAttempts = options.idempotent ? options.maxAttempts ?? 3 : 1;
|
|
533
|
+
let lastError;
|
|
534
|
+
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
535
|
+
try {
|
|
536
|
+
return await fn();
|
|
537
|
+
} catch (err) {
|
|
538
|
+
lastError = err;
|
|
539
|
+
const isLastAttempt = attempt === maxAttempts - 1;
|
|
540
|
+
if (!options.idempotent || !(err instanceof NetworkError) || isLastAttempt) {
|
|
541
|
+
throw err;
|
|
542
|
+
}
|
|
543
|
+
const retryAfterMs = isRetryAfterCarrier(err) ? err.retryAfterMs : void 0;
|
|
544
|
+
await sleep(retryAfterMs ?? backoffDelayMs(attempt));
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
throw lastError;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
export { AxiamClient, SKIP_REFRESH, SharedSession, withRetry };
|
|
551
|
+
//# sourceMappingURL=index.mjs.map
|
|
552
|
+
//# sourceMappingURL=index.mjs.map
|