@moxt-ai/mobius-sdk 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +31 -0
  2. package/dist/bot-owner-resources.d.ts +274 -0
  3. package/dist/bot-owner-resources.d.ts.map +1 -0
  4. package/dist/bot-owner-resources.js +748 -0
  5. package/dist/bot-owner-resources.js.map +1 -0
  6. package/dist/bot-resources.d.ts +152 -0
  7. package/dist/bot-resources.d.ts.map +1 -0
  8. package/dist/bot-resources.js +469 -0
  9. package/dist/bot-resources.js.map +1 -0
  10. package/dist/bot-service-client.d.ts +204 -0
  11. package/dist/bot-service-client.d.ts.map +1 -0
  12. package/dist/bot-service-client.js +587 -0
  13. package/dist/bot-service-client.js.map +1 -0
  14. package/dist/browser.d.ts +166 -0
  15. package/dist/browser.d.ts.map +1 -0
  16. package/dist/browser.js +379 -0
  17. package/dist/browser.js.map +1 -0
  18. package/dist/channel.d.ts +3 -0
  19. package/dist/channel.d.ts.map +1 -0
  20. package/dist/channel.js +3 -0
  21. package/dist/channel.js.map +1 -0
  22. package/dist/client.d.ts +116 -0
  23. package/dist/client.d.ts.map +1 -0
  24. package/dist/client.js +624 -0
  25. package/dist/client.js.map +1 -0
  26. package/dist/errors.d.ts +24 -0
  27. package/dist/errors.d.ts.map +1 -0
  28. package/dist/errors.js +47 -0
  29. package/dist/errors.js.map +1 -0
  30. package/dist/examples/browser.d.ts +13 -0
  31. package/dist/examples/browser.d.ts.map +1 -0
  32. package/dist/examples/browser.js +23 -0
  33. package/dist/examples/browser.js.map +1 -0
  34. package/dist/examples/moxt-server.d.ts +38 -0
  35. package/dist/examples/moxt-server.d.ts.map +1 -0
  36. package/dist/examples/moxt-server.js +81 -0
  37. package/dist/examples/moxt-server.js.map +1 -0
  38. package/dist/examples/node.d.ts +2 -0
  39. package/dist/examples/node.d.ts.map +1 -0
  40. package/dist/examples/node.js +20 -0
  41. package/dist/examples/node.js.map +1 -0
  42. package/dist/examples/platform-server.d.ts +2 -0
  43. package/dist/examples/platform-server.d.ts.map +1 -0
  44. package/dist/examples/platform-server.js +39 -0
  45. package/dist/examples/platform-server.js.map +1 -0
  46. package/dist/examples/workflow.d.ts +20 -0
  47. package/dist/examples/workflow.d.ts.map +1 -0
  48. package/dist/examples/workflow.js +76 -0
  49. package/dist/examples/workflow.js.map +1 -0
  50. package/dist/index.d.ts +10 -0
  51. package/dist/index.d.ts.map +1 -0
  52. package/dist/index.js +9 -0
  53. package/dist/index.js.map +1 -0
  54. package/dist/platform.d.ts +184 -0
  55. package/dist/platform.d.ts.map +1 -0
  56. package/dist/platform.js +434 -0
  57. package/dist/platform.js.map +1 -0
  58. package/dist/project-authentication.d.ts +239 -0
  59. package/dist/project-authentication.d.ts.map +1 -0
  60. package/dist/project-authentication.js +752 -0
  61. package/dist/project-authentication.js.map +1 -0
  62. package/dist/resources.d.ts +602 -0
  63. package/dist/resources.d.ts.map +1 -0
  64. package/dist/resources.js +1411 -0
  65. package/dist/resources.js.map +1 -0
  66. package/dist/server.d.ts +7 -0
  67. package/dist/server.d.ts.map +1 -0
  68. package/dist/server.js +7 -0
  69. package/dist/server.js.map +1 -0
  70. package/dist/service-client.d.ts +392 -0
  71. package/dist/service-client.d.ts.map +1 -0
  72. package/dist/service-client.js +1061 -0
  73. package/dist/service-client.js.map +1 -0
  74. package/package.json +92 -0
@@ -0,0 +1,752 @@
1
+ import { MOBIUS_API_PREFIX } from "@moxt-ai/mobius-protocol";
2
+ import { MobiusApiError, MobiusCancellationError, MobiusConnectionError, MobiusError, MobiusProtocolError, MobiusTimeoutError, MobiusValidationError, } from "./errors.js";
3
+ const DEFAULT_OPERATION_TIMEOUT_MILLISECONDS = 20_000;
4
+ const MAXIMUM_RESPONSE_LENGTH = 8 * 1024 * 1024;
5
+ const PROJECT_PERMISSIONS = Object.freeze([
6
+ "application:manage",
7
+ "artifact:read",
8
+ "artifact:write",
9
+ "bot:behavior:write",
10
+ "bot:binding:write",
11
+ "bot:connection:manage",
12
+ "bot:profile:write",
13
+ "bot:read",
14
+ "conversation:create",
15
+ "conversation:read",
16
+ "conversation:turn:cancel",
17
+ "conversation:turn:create",
18
+ "identity-binding:manage",
19
+ "interaction:respond",
20
+ "owner-credential:manage",
21
+ "project:event:read",
22
+ "project:read",
23
+ "runtime:pair",
24
+ "runtime:read",
25
+ "runtime:revoke",
26
+ "session:delete",
27
+ "session:read",
28
+ "session:write",
29
+ ]);
30
+ export const MOBIUS_PROJECT_PERMISSIONS = PROJECT_PERMISSIONS;
31
+ function record(value, name) {
32
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
33
+ throw new MobiusProtocolError(`The service returned an invalid ${name}`);
34
+ }
35
+ return Object.freeze(Object.fromEntries(Object.entries(value)));
36
+ }
37
+ function records(value, key, name) {
38
+ const field = value[key];
39
+ if (!Array.isArray(field)) {
40
+ throw new MobiusProtocolError(`The service returned an invalid ${name}`);
41
+ }
42
+ return Object.freeze([...field]);
43
+ }
44
+ function text(value, key, name, allowEmpty = false) {
45
+ const field = value[key];
46
+ if (typeof field !== "string" || (!allowEmpty && field.length === 0)) {
47
+ throw new MobiusProtocolError(`The service returned an invalid ${name}`);
48
+ }
49
+ return field;
50
+ }
51
+ function identifierField(value, key, name) {
52
+ const field = text(value, key, name);
53
+ if (!/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$/.test(field)) {
54
+ throw new MobiusProtocolError(`The service returned an invalid ${name}`);
55
+ }
56
+ return field;
57
+ }
58
+ function optionalIdentifierField(value, key, name) {
59
+ const field = text(value, key, name, true);
60
+ if (field.length > 0 && !/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$/.test(field)) {
61
+ throw new MobiusProtocolError(`The service returned an invalid ${name}`);
62
+ }
63
+ return field;
64
+ }
65
+ function publicCredentialIdField(value, key, name) {
66
+ const field = text(value, key, name);
67
+ if (!/^[a-zA-Z0-9_-]{1,128}$/.test(field)) {
68
+ throw new MobiusProtocolError(`The service returned an invalid ${name}`);
69
+ }
70
+ return field;
71
+ }
72
+ function timestamp(value, key, name, allowEmpty = false) {
73
+ const field = text(value, key, name, allowEmpty);
74
+ if (field.length > 0 && Number.isNaN(Date.parse(field))) {
75
+ throw new MobiusProtocolError(`The service returned an invalid ${name}`);
76
+ }
77
+ return field;
78
+ }
79
+ function booleanField(value, key, name) {
80
+ const field = value[key];
81
+ if (typeof field !== "boolean") {
82
+ throw new MobiusProtocolError(`The service returned an invalid ${name}`);
83
+ }
84
+ return field;
85
+ }
86
+ function permissionList(value, name) {
87
+ if (!Array.isArray(value) ||
88
+ value.length === 0 ||
89
+ value.some((permission) => typeof permission !== "string" || !PROJECT_PERMISSIONS.includes(permission)) ||
90
+ new Set(value).size !== value.length) {
91
+ throw new MobiusProtocolError(`The service returned invalid ${name}`);
92
+ }
93
+ return Object.freeze(value.filter((permission) => typeof permission === "string"));
94
+ }
95
+ function requestPermissionList(value) {
96
+ if (value.length === 0 ||
97
+ value.some((permission) => !PROJECT_PERMISSIONS.includes(permission)) ||
98
+ new Set(value).size !== value.length) {
99
+ throw new MobiusValidationError("The Project permissions are invalid");
100
+ }
101
+ return Object.freeze([...value]);
102
+ }
103
+ function identifier(value, name) {
104
+ if (!/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$/.test(value)) {
105
+ throw new MobiusValidationError(`The ${name} identifier is invalid`);
106
+ }
107
+ return value;
108
+ }
109
+ function positiveInteger(value, name) {
110
+ if (!Number.isSafeInteger(value) || value < 1) {
111
+ throw new MobiusValidationError(`The ${name} is invalid`);
112
+ }
113
+ return value;
114
+ }
115
+ function nonnegativeInteger(value, name) {
116
+ if (!Number.isSafeInteger(value) || value < 0) {
117
+ throw new MobiusValidationError(`The ${name} is invalid`);
118
+ }
119
+ return value;
120
+ }
121
+ function hasControlCharacters(value) {
122
+ for (const character of value) {
123
+ const codePoint = character.codePointAt(0) ?? 0;
124
+ if (codePoint <= 31 || codePoint === 127) {
125
+ return true;
126
+ }
127
+ }
128
+ return false;
129
+ }
130
+ function credential(value) {
131
+ if (value.length === 0 || value.length > 16 * 1024 || hasControlCharacters(value)) {
132
+ throw new MobiusValidationError("The credential is invalid");
133
+ }
134
+ return value;
135
+ }
136
+ function normalizeEndpoint(value) {
137
+ let endpoint;
138
+ try {
139
+ endpoint = new URL(value);
140
+ }
141
+ catch (cause) {
142
+ throw new MobiusValidationError("The Mobius endpoint is invalid", { cause });
143
+ }
144
+ if ((endpoint.protocol !== "http:" && endpoint.protocol !== "https:") ||
145
+ endpoint.username.length > 0 ||
146
+ endpoint.password.length > 0 ||
147
+ endpoint.search.length > 0 ||
148
+ endpoint.hash.length > 0) {
149
+ throw new MobiusValidationError("The Mobius endpoint is invalid");
150
+ }
151
+ if (endpoint.pathname === "/") {
152
+ endpoint.pathname = `${MOBIUS_API_PREFIX}/`;
153
+ }
154
+ else if (!endpoint.pathname.endsWith("/")) {
155
+ endpoint.pathname = `${endpoint.pathname}/`;
156
+ }
157
+ return endpoint;
158
+ }
159
+ class PlatformProjectAuthenticationTransport {
160
+ fetch = async (request) => await globalThis.fetch(request);
161
+ }
162
+ class ProjectAuthenticationDeadline {
163
+ #controller = new AbortController();
164
+ #requestSignal;
165
+ #timeoutId;
166
+ #timedOut = false;
167
+ constructor(requestSignal, timeoutMilliseconds) {
168
+ this.#requestSignal = requestSignal;
169
+ if (requestSignal.aborted) {
170
+ this.#controller.abort(requestSignal.reason);
171
+ }
172
+ else {
173
+ requestSignal.addEventListener("abort", this.abortFromRequest, { once: true });
174
+ }
175
+ this.#timeoutId = globalThis.setTimeout(this.abortFromTimeout, timeoutMilliseconds);
176
+ }
177
+ get signal() {
178
+ return this.#controller.signal;
179
+ }
180
+ finish = () => {
181
+ globalThis.clearTimeout(this.#timeoutId);
182
+ this.#requestSignal.removeEventListener("abort", this.abortFromRequest);
183
+ };
184
+ translate = (cause) => {
185
+ if (this.#requestSignal.aborted) {
186
+ throw new MobiusCancellationError({ cause });
187
+ }
188
+ if (this.#timedOut) {
189
+ throw new MobiusTimeoutError({ cause });
190
+ }
191
+ if (cause instanceof MobiusError) {
192
+ throw cause;
193
+ }
194
+ throw new MobiusConnectionError("The Mobius authentication service could not be reached", { cause });
195
+ };
196
+ abortFromRequest = () => {
197
+ this.#controller.abort(this.#requestSignal.reason);
198
+ };
199
+ abortFromTimeout = () => {
200
+ this.#timedOut = true;
201
+ this.#controller.abort(new Error("The Mobius authentication operation timed out"));
202
+ };
203
+ }
204
+ async function boundedResponseText(response, signal) {
205
+ const declaredLength = response.headers.get("content-length");
206
+ if (declaredLength !== null && Number(declaredLength) > MAXIMUM_RESPONSE_LENGTH) {
207
+ throw new MobiusProtocolError("The authentication service response is too large");
208
+ }
209
+ if (response.body === null) {
210
+ return "";
211
+ }
212
+ const reader = response.body.getReader();
213
+ const chunks = [];
214
+ let length = 0;
215
+ try {
216
+ while (true) {
217
+ signal.throwIfAborted();
218
+ const result = await reader.read();
219
+ if (result.done) {
220
+ break;
221
+ }
222
+ length += result.value.byteLength;
223
+ if (length > MAXIMUM_RESPONSE_LENGTH) {
224
+ await reader.cancel();
225
+ throw new MobiusProtocolError("The authentication service response is too large");
226
+ }
227
+ chunks.push(result.value);
228
+ }
229
+ }
230
+ finally {
231
+ reader.releaseLock();
232
+ }
233
+ const bytes = new Uint8Array(length);
234
+ let offset = 0;
235
+ for (const chunk of chunks) {
236
+ bytes.set(chunk, offset);
237
+ offset += chunk.byteLength;
238
+ }
239
+ try {
240
+ return new TextDecoder("utf-8", { fatal: true, ignoreBOM: false }).decode(bytes);
241
+ }
242
+ catch (cause) {
243
+ throw new MobiusProtocolError("The authentication service returned invalid UTF-8", { cause });
244
+ }
245
+ }
246
+ async function responseValue(response, signal) {
247
+ const encoded = await boundedResponseText(response, signal);
248
+ try {
249
+ return JSON.parse(encoded);
250
+ }
251
+ catch (cause) {
252
+ throw new MobiusProtocolError("The authentication service returned invalid JSON", { cause });
253
+ }
254
+ }
255
+ async function throwApiError(response, signal) {
256
+ const value = record(await responseValue(response, signal), "authentication error response");
257
+ const error = record(value["error"], "authentication error response");
258
+ const code = error["code"];
259
+ const message = error["message"];
260
+ if (typeof code !== "string" || typeof message !== "string") {
261
+ throw new MobiusProtocolError("The authentication service returned an invalid error response");
262
+ }
263
+ throw new MobiusApiError(code, message, response.status);
264
+ }
265
+ class ProjectAuthenticationRequestExecutor {
266
+ #actorReference;
267
+ #credentialProvider;
268
+ #endpoint;
269
+ #operationTimeoutMilliseconds;
270
+ #transport;
271
+ constructor(options) {
272
+ this.#endpoint = normalizeEndpoint(options.endpoint);
273
+ this.#credentialProvider = options.credentialProvider;
274
+ this.#operationTimeoutMilliseconds = positiveInteger(options.operationTimeoutMilliseconds ?? DEFAULT_OPERATION_TIMEOUT_MILLISECONDS, "operation timeout");
275
+ this.#transport = options.transport ?? new PlatformProjectAuthenticationTransport();
276
+ const actorReference = options.actorReference ?? "";
277
+ if (actorReference.length > 200 || hasControlCharacters(actorReference)) {
278
+ throw new MobiusValidationError("The Mobius actor reference is invalid");
279
+ }
280
+ this.#actorReference = actorReference;
281
+ }
282
+ projectGet = async (path, signal) => await this.request(path, "GET", "", this.#credentialProvider, this.#actorReference, signal);
283
+ projectPost = async (path, body, signal) => await this.request(path, "POST", this.encode(body), this.#credentialProvider, this.#actorReference, signal);
284
+ projectDelete = async (path, signal) => await this.request(path, "DELETE", "", this.#credentialProvider, this.#actorReference, signal);
285
+ externalPost = async (path, body, externalCredentialProvider, signal) => await this.request(path, "POST", this.encode(body), externalCredentialProvider, "", signal);
286
+ encode = (body) => {
287
+ const encoded = JSON.stringify(body);
288
+ if (typeof encoded !== "string") {
289
+ throw new MobiusValidationError("The authentication request body is invalid");
290
+ }
291
+ return encoded;
292
+ };
293
+ request = async (path, method, body, credentialProvider, actorReference, signal) => {
294
+ const deadline = new ProjectAuthenticationDeadline(signal, this.#operationTimeoutMilliseconds);
295
+ try {
296
+ const headers = new Headers({ accept: "application/json" });
297
+ headers.set("authorization", `Bearer ${credential(await credentialProvider.credential(deadline.signal))}`);
298
+ if (actorReference.length > 0) {
299
+ headers.set("x-mobius-actor-ref", actorReference);
300
+ }
301
+ if (body.length > 0) {
302
+ headers.set("content-type", "application/json");
303
+ }
304
+ let request;
305
+ if (body.length > 0) {
306
+ request = new Request(new URL(path, this.#endpoint), {
307
+ body,
308
+ headers,
309
+ method,
310
+ signal: deadline.signal,
311
+ });
312
+ }
313
+ else {
314
+ request = new Request(new URL(path, this.#endpoint), {
315
+ headers,
316
+ method,
317
+ signal: deadline.signal,
318
+ });
319
+ }
320
+ const response = await this.#transport.fetch(request);
321
+ if (!response.ok) {
322
+ return await throwApiError(response, deadline.signal);
323
+ }
324
+ return await responseValue(response, deadline.signal);
325
+ }
326
+ catch (cause) {
327
+ return deadline.translate(cause);
328
+ }
329
+ finally {
330
+ deadline.finish();
331
+ }
332
+ };
333
+ }
334
+ export class MobiusCredentialLifecycle {
335
+ static Current = new MobiusCredentialLifecycle("current");
336
+ static Replaced = new MobiusCredentialLifecycle("replaced");
337
+ static Revoked = new MobiusCredentialLifecycle("revoked");
338
+ value;
339
+ constructor(value) {
340
+ this.value = value;
341
+ }
342
+ toJSON = () => this.value;
343
+ }
344
+ export class MobiusCredentialReplacement {
345
+ }
346
+ export class MobiusNoCredentialReplacement extends MobiusCredentialReplacement {
347
+ exists = false;
348
+ }
349
+ export class MobiusRecordedCredentialReplacement extends MobiusCredentialReplacement {
350
+ credentialId;
351
+ exists = true;
352
+ overlapEndsAt;
353
+ constructor(credentialId, overlapEndsAt) {
354
+ super();
355
+ this.credentialId = credentialId;
356
+ this.overlapEndsAt = overlapEndsAt;
357
+ }
358
+ }
359
+ export class MobiusCredentialState {
360
+ }
361
+ export class MobiusCurrentCredentialState extends MobiusCredentialState {
362
+ lifecycle = MobiusCredentialLifecycle.Current;
363
+ }
364
+ export class MobiusReplacedCredentialState extends MobiusCredentialState {
365
+ lifecycle = MobiusCredentialLifecycle.Replaced;
366
+ replacement;
367
+ constructor(replacement) {
368
+ super();
369
+ this.replacement = replacement;
370
+ }
371
+ }
372
+ export class MobiusRevokedCredentialState extends MobiusCredentialState {
373
+ lifecycle = MobiusCredentialLifecycle.Revoked;
374
+ replacement;
375
+ revokedAt;
376
+ constructor(revokedAt, replacement) {
377
+ super();
378
+ this.revokedAt = revokedAt;
379
+ this.replacement = replacement;
380
+ }
381
+ }
382
+ function credentialState(value, name) {
383
+ const overlapEndsAt = timestamp(value, "overlapEndsAt", name, true);
384
+ const replacementCredentialId = optionalIdentifierField(value, "replacedByCredentialId", name);
385
+ const revokedAt = timestamp(value, "revokedAt", name, true);
386
+ if ((overlapEndsAt.length === 0) !== (replacementCredentialId.length === 0)) {
387
+ throw new MobiusProtocolError(`The service returned an invalid ${name}`);
388
+ }
389
+ const replacement = replacementCredentialId.length === 0
390
+ ? new MobiusNoCredentialReplacement()
391
+ : new MobiusRecordedCredentialReplacement(replacementCredentialId, overlapEndsAt);
392
+ if (revokedAt.length > 0) {
393
+ return new MobiusRevokedCredentialState(revokedAt, replacement);
394
+ }
395
+ if (replacement instanceof MobiusRecordedCredentialReplacement) {
396
+ return new MobiusReplacedCredentialState(replacement);
397
+ }
398
+ return new MobiusCurrentCredentialState();
399
+ }
400
+ export class MobiusProjectApplicationLifecycle {
401
+ static Active = new MobiusProjectApplicationLifecycle("active");
402
+ static Revoked = new MobiusProjectApplicationLifecycle("revoked");
403
+ value;
404
+ constructor(value) {
405
+ this.value = value;
406
+ }
407
+ toJSON = () => this.value;
408
+ }
409
+ export class MobiusProjectApplicationState {
410
+ }
411
+ export class MobiusActiveProjectApplicationState extends MobiusProjectApplicationState {
412
+ lifecycle = MobiusProjectApplicationLifecycle.Active;
413
+ }
414
+ export class MobiusRevokedProjectApplicationState extends MobiusProjectApplicationState {
415
+ lifecycle = MobiusProjectApplicationLifecycle.Revoked;
416
+ revokedAt;
417
+ constructor(revokedAt) {
418
+ super();
419
+ this.revokedAt = revokedAt;
420
+ }
421
+ }
422
+ function applicationState(value) {
423
+ const lifecycle = value["lifecycle"];
424
+ const revokedAt = timestamp(value, "revokedAt", "Project application", true);
425
+ if (lifecycle === "active" && revokedAt.length === 0) {
426
+ return new MobiusActiveProjectApplicationState();
427
+ }
428
+ if (lifecycle === "revoked" && revokedAt.length > 0) {
429
+ return new MobiusRevokedProjectApplicationState(revokedAt);
430
+ }
431
+ throw new MobiusProtocolError("The service returned an invalid Project application");
432
+ }
433
+ export class MobiusProjectApplicationCredential {
434
+ credentialId;
435
+ expiresAt;
436
+ issuedAt;
437
+ lastUsedAt;
438
+ publicCredentialId;
439
+ state;
440
+ constructor(value) {
441
+ const resource = record(value, "Project application credential");
442
+ this.credentialId = identifierField(resource, "credentialId", "Project application credential");
443
+ this.publicCredentialId = publicCredentialIdField(resource, "publicCredentialId", "Project application credential");
444
+ this.issuedAt = timestamp(resource, "issuedAt", "Project application credential");
445
+ this.expiresAt = timestamp(resource, "expiresAt", "Project application credential");
446
+ this.lastUsedAt = timestamp(resource, "lastUsedAt", "Project application credential");
447
+ this.state = credentialState(resource, "Project application credential");
448
+ }
449
+ }
450
+ export class MobiusProjectApplication {
451
+ applicationId;
452
+ createdAt;
453
+ credentials;
454
+ name;
455
+ permissions;
456
+ projectId;
457
+ state;
458
+ updatedAt;
459
+ constructor(value) {
460
+ const resource = record(value, "Project application");
461
+ this.applicationId = identifierField(resource, "applicationId", "Project application");
462
+ this.projectId = identifierField(resource, "projectId", "Project application");
463
+ this.name = text(resource, "name", "Project application");
464
+ this.permissions = permissionList(resource["permissions"], "Project application permissions");
465
+ this.state = applicationState(resource);
466
+ this.createdAt = timestamp(resource, "createdAt", "Project application");
467
+ this.updatedAt = timestamp(resource, "updatedAt", "Project application");
468
+ this.credentials = Object.freeze(records(resource, "credentials", "Project application credentials").map((credentialResource) => new MobiusProjectApplicationCredential(credentialResource)));
469
+ }
470
+ }
471
+ export class MobiusIssuedApplicationCredential {
472
+ applicationId;
473
+ credential;
474
+ credentialId;
475
+ expiresAt;
476
+ issuedAt;
477
+ publicCredentialId;
478
+ constructor(value) {
479
+ const resource = record(value, "issued Project application credential");
480
+ this.applicationId = identifierField(resource, "applicationId", "issued Project application credential");
481
+ this.credential = credential(text(resource, "credential", "issued Project application credential"));
482
+ this.credentialId = identifierField(resource, "credentialId", "issued Project application credential");
483
+ this.publicCredentialId = publicCredentialIdField(resource, "publicCredentialId", "issued Project application credential");
484
+ this.issuedAt = timestamp(resource, "issuedAt", "issued Project application credential");
485
+ this.expiresAt = timestamp(resource, "expiresAt", "issued Project application credential");
486
+ }
487
+ }
488
+ export class MobiusRotatedApplicationCredential extends MobiusIssuedApplicationCredential {
489
+ overlapEndsAt;
490
+ replacedCredentialId;
491
+ constructor(value) {
492
+ super(value);
493
+ const resource = record(value, "rotated Project application credential");
494
+ this.overlapEndsAt = timestamp(resource, "overlapEndsAt", "rotated Project application credential");
495
+ this.replacedCredentialId = identifierField(resource, "replacedCredentialId", "rotated Project application credential");
496
+ }
497
+ }
498
+ export class MobiusExternalIdentityBinding {
499
+ bindingId;
500
+ createdAt;
501
+ grantId;
502
+ issuer;
503
+ lastVerifiedAt;
504
+ permissions;
505
+ projectId;
506
+ providerId;
507
+ subject;
508
+ constructor(value) {
509
+ const resource = record(value, "external identity binding");
510
+ this.bindingId = identifierField(resource, "bindingId", "external identity binding");
511
+ this.grantId = identifierField(resource, "grantId", "external identity binding");
512
+ this.projectId = identifierField(resource, "projectId", "external identity binding");
513
+ this.providerId = identifierField(resource, "providerId", "external identity binding");
514
+ this.issuer = text(resource, "issuer", "external identity binding");
515
+ this.subject = text(resource, "subject", "external identity binding");
516
+ this.permissions = permissionList(resource["permissions"], "external identity binding permissions");
517
+ this.createdAt = timestamp(resource, "createdAt", "external identity binding");
518
+ this.lastVerifiedAt = timestamp(resource, "lastVerifiedAt", "external identity binding");
519
+ }
520
+ }
521
+ export class MobiusAttachedExternalIdentity extends MobiusExternalIdentityBinding {
522
+ ownerCredentialRevoked;
523
+ constructor(value) {
524
+ super(value);
525
+ this.ownerCredentialRevoked = booleanField(record(value, "attached external identity"), "ownerCredentialRevoked", "attached external identity");
526
+ }
527
+ }
528
+ export class MobiusIssuedProjectAccessCredential {
529
+ credential;
530
+ credentialId;
531
+ expiresAt;
532
+ permissions;
533
+ projectId;
534
+ constructor(value) {
535
+ const resource = record(value, "issued Mobius access credential");
536
+ this.credential = credential(text(resource, "credential", "issued Mobius access credential"));
537
+ this.credentialId = identifierField(resource, "credentialId", "issued Mobius access credential");
538
+ this.projectId = identifierField(resource, "projectId", "issued Mobius access credential");
539
+ this.expiresAt = timestamp(resource, "expiresAt", "issued Mobius access credential");
540
+ this.permissions = permissionList(resource["permissions"], "Mobius access credential permissions");
541
+ }
542
+ }
543
+ export class MobiusOwnerCredential {
544
+ createdAt;
545
+ credentialId;
546
+ lastUsedAt;
547
+ state;
548
+ constructor(value) {
549
+ const resource = record(value, "accountless owner credential");
550
+ this.credentialId = identifierField(resource, "credentialId", "accountless owner credential");
551
+ this.createdAt = timestamp(resource, "createdAt", "accountless owner credential");
552
+ this.lastUsedAt = timestamp(resource, "lastUsedAt", "accountless owner credential");
553
+ this.state = credentialState(resource, "accountless owner credential");
554
+ }
555
+ }
556
+ export class MobiusOwnerCredentialDirectory {
557
+ credentials;
558
+ recoveryWarning;
559
+ constructor(value) {
560
+ const resource = record(value, "accountless owner credential directory");
561
+ this.credentials = Object.freeze(records(resource, "credentials", "accountless owner credentials").map((credentialResource) => new MobiusOwnerCredential(credentialResource)));
562
+ this.recoveryWarning = text(resource, "recoveryWarning", "accountless owner recovery warning");
563
+ }
564
+ }
565
+ export class MobiusRotatedOwnerCredential {
566
+ credential;
567
+ credentialId;
568
+ issuedAt;
569
+ overlapEndsAt;
570
+ replacedCredentialId;
571
+ constructor(value) {
572
+ const resource = record(value, "rotated accountless owner credential");
573
+ this.credential = credential(text(resource, "credential", "rotated accountless owner credential"));
574
+ this.credentialId = identifierField(resource, "credentialId", "rotated accountless owner credential");
575
+ this.replacedCredentialId = identifierField(resource, "replacedCredentialId", "rotated accountless owner credential");
576
+ this.issuedAt = timestamp(resource, "issuedAt", "rotated accountless owner credential");
577
+ this.overlapEndsAt = timestamp(resource, "overlapEndsAt", "rotated accountless owner credential");
578
+ }
579
+ }
580
+ export class MobiusAuthenticationAuditEvent {
581
+ action;
582
+ actorId;
583
+ actorKind;
584
+ actorRef;
585
+ auditId;
586
+ details;
587
+ outcome;
588
+ recordedAt;
589
+ subjectId;
590
+ constructor(value) {
591
+ const resource = record(value, "authentication audit event");
592
+ this.auditId = identifierField(resource, "auditId", "authentication audit event");
593
+ this.actorKind = text(resource, "actorKind", "authentication audit event");
594
+ this.actorId = text(resource, "actorId", "authentication audit event");
595
+ this.actorRef = text(resource, "actorRef", "authentication audit event", true);
596
+ this.action = text(resource, "action", "authentication audit event");
597
+ this.subjectId = text(resource, "subjectId", "authentication audit event");
598
+ this.outcome = text(resource, "outcome", "authentication audit event");
599
+ this.details = record(resource["details"], "authentication audit details");
600
+ this.recordedAt = timestamp(resource, "recordedAt", "authentication audit event");
601
+ }
602
+ }
603
+ export class RotatingMobiusCredentialProvider {
604
+ #credential;
605
+ constructor(initialCredential) {
606
+ this.#credential = credential(initialCredential);
607
+ }
608
+ credential = async (signal) => {
609
+ signal.throwIfAborted();
610
+ return this.#credential;
611
+ };
612
+ replace = (replacement) => {
613
+ this.#credential = credential(replacement.credential);
614
+ };
615
+ }
616
+ class ExchangedCredentialState {
617
+ }
618
+ class EmptyExchangedCredentialState extends ExchangedCredentialState {
619
+ isUsable = (_now, _refreshAheadMilliseconds) => false;
620
+ read = () => {
621
+ throw new MobiusProtocolError("A Mobius access credential has not been exchanged");
622
+ };
623
+ }
624
+ class ReadyExchangedCredentialState extends ExchangedCredentialState {
625
+ #credential;
626
+ #expiresAt;
627
+ constructor(value) {
628
+ super();
629
+ this.#credential = value.credential;
630
+ this.#expiresAt = Date.parse(value.expiresAt);
631
+ }
632
+ isUsable = (now, refreshAheadMilliseconds) => this.#expiresAt - now > refreshAheadMilliseconds;
633
+ read = () => this.#credential;
634
+ }
635
+ export class ExchangingMobiusCredentialProvider {
636
+ #client;
637
+ #externalCredentialProvider;
638
+ #lifetimeSeconds;
639
+ #providerId;
640
+ #refreshAheadMilliseconds;
641
+ #state = new EmptyExchangedCredentialState();
642
+ constructor(client, providerId, externalCredentialProvider, lifetimeSeconds, refreshAheadMilliseconds = 30_000) {
643
+ this.#client = client;
644
+ this.#providerId = identifier(providerId, "external provider");
645
+ this.#externalCredentialProvider = externalCredentialProvider;
646
+ this.#lifetimeSeconds = positiveInteger(lifetimeSeconds, "access credential lifetime");
647
+ this.#refreshAheadMilliseconds = nonnegativeInteger(refreshAheadMilliseconds, "credential refresh window");
648
+ }
649
+ credential = async (signal) => {
650
+ signal.throwIfAborted();
651
+ if (this.#state.isUsable(Date.now(), this.#refreshAheadMilliseconds)) {
652
+ return this.#state.read();
653
+ }
654
+ const exchanged = await this.#client.exchangeExternalIdentity({ expiresInSeconds: this.#lifetimeSeconds, providerId: this.#providerId }, this.#externalCredentialProvider, signal);
655
+ this.#state = new ReadyExchangedCredentialState(exchanged);
656
+ return this.#state.read();
657
+ };
658
+ }
659
+ export class MobiusProjectApplicationResource {
660
+ #client;
661
+ constructor(client) {
662
+ this.#client = client;
663
+ }
664
+ create = async (request, signal) => new MobiusProjectApplication(await this.#client.projectPost(`v1/projects/${encodeURIComponent(this.#client.projectId)}/applications`, { name: request.name, permissions: requestPermissionList(request.permissions) }, signal));
665
+ list = async (signal) => {
666
+ const value = record(await this.#client.projectGet(`v1/projects/${encodeURIComponent(this.#client.projectId)}/applications`, signal), "Project application directory");
667
+ return Object.freeze(records(value, "applications", "Project application directory").map((application) => new MobiusProjectApplication(application)));
668
+ };
669
+ revoke = async (applicationId, signal) => {
670
+ await this.#client.projectPost(`v1/project-applications/${encodeURIComponent(identifier(applicationId, "Project application"))}/revoke`, {}, signal);
671
+ };
672
+ }
673
+ export class MobiusApplicationCredentialResource {
674
+ #client;
675
+ constructor(client) {
676
+ this.#client = client;
677
+ }
678
+ issue = async (applicationId, request, signal) => new MobiusIssuedApplicationCredential(await this.#client.projectPost(`v1/project-applications/${encodeURIComponent(identifier(applicationId, "Project application"))}/credentials`, { expiresInSeconds: positiveInteger(request.expiresInSeconds, "credential lifetime") }, signal));
679
+ rotate = async (credentialId, request, signal) => new MobiusRotatedApplicationCredential(await this.#client.projectPost(`v1/project-application-credentials/${encodeURIComponent(identifier(credentialId, "application credential"))}/rotate`, {
680
+ expiresInSeconds: positiveInteger(request.expiresInSeconds, "credential lifetime"),
681
+ overlapSeconds: nonnegativeInteger(request.overlapSeconds, "credential overlap"),
682
+ }, signal));
683
+ revoke = async (credentialId, signal) => {
684
+ await this.#client.projectPost(`v1/project-application-credentials/${encodeURIComponent(identifier(credentialId, "application credential"))}/revoke`, {}, signal);
685
+ };
686
+ }
687
+ export class MobiusExternalIdentityBindingResource {
688
+ #client;
689
+ constructor(client) {
690
+ this.#client = client;
691
+ }
692
+ attach = async (request, signal) => new MobiusAttachedExternalIdentity(await this.#client.projectPost(`v1/projects/${encodeURIComponent(this.#client.projectId)}/external-identity-bindings`, {
693
+ externalCredential: credential(request.externalCredential),
694
+ permissions: requestPermissionList(request.permissions),
695
+ providerId: identifier(request.providerId, "external provider"),
696
+ revokeOwnerCredential: request.revokeOwnerCredential,
697
+ }, signal));
698
+ list = async (signal) => {
699
+ const value = record(await this.#client.projectGet(`v1/projects/${encodeURIComponent(this.#client.projectId)}/external-identity-bindings`, signal), "external identity binding directory");
700
+ return Object.freeze(records(value, "bindings", "external identity binding directory").map((binding) => new MobiusExternalIdentityBinding(binding)));
701
+ };
702
+ revoke = async (bindingId, signal) => {
703
+ await this.#client.projectDelete(`v1/projects/${encodeURIComponent(this.#client.projectId)}/external-identity-bindings/${encodeURIComponent(identifier(bindingId, "external identity binding"))}`, signal);
704
+ };
705
+ }
706
+ export class MobiusOwnerCredentialResource {
707
+ #client;
708
+ constructor(client) {
709
+ this.#client = client;
710
+ }
711
+ list = async (signal) => new MobiusOwnerCredentialDirectory(await this.#client.projectGet(`v1/projects/${encodeURIComponent(this.#client.projectId)}/owner-credentials`, signal));
712
+ rotate = async (credentialId, request, signal) => new MobiusRotatedOwnerCredential(await this.#client.projectPost(`v1/project-owner-credentials/${encodeURIComponent(identifier(credentialId, "owner credential"))}/rotate`, { overlapSeconds: nonnegativeInteger(request.overlapSeconds, "credential overlap") }, signal));
713
+ revoke = async (credentialId, signal) => {
714
+ await this.#client.projectPost(`v1/project-owner-credentials/${encodeURIComponent(identifier(credentialId, "owner credential"))}/revoke`, {}, signal);
715
+ };
716
+ }
717
+ export class MobiusAuthenticationAuditResource {
718
+ #client;
719
+ constructor(client) {
720
+ this.#client = client;
721
+ }
722
+ list = async (limit, signal) => {
723
+ const value = record(await this.#client.projectGet(`v1/projects/${encodeURIComponent(this.#client.projectId)}/authentication-audit-events?limit=${positiveInteger(limit, "audit page size").toString()}`, signal), "authentication audit event directory");
724
+ return Object.freeze(records(value, "events", "authentication audit event directory").map((event) => new MobiusAuthenticationAuditEvent(event)));
725
+ };
726
+ }
727
+ export class MobiusProjectAuthenticationClient {
728
+ applicationCredentials;
729
+ applications;
730
+ auditEvents;
731
+ externalIdentityBindings;
732
+ ownerCredentials;
733
+ projectId;
734
+ #requests;
735
+ constructor(options) {
736
+ this.projectId = identifier(options.projectId, "Project");
737
+ this.#requests = new ProjectAuthenticationRequestExecutor(options);
738
+ this.applications = new MobiusProjectApplicationResource(this);
739
+ this.applicationCredentials = new MobiusApplicationCredentialResource(this);
740
+ this.externalIdentityBindings = new MobiusExternalIdentityBindingResource(this);
741
+ this.ownerCredentials = new MobiusOwnerCredentialResource(this);
742
+ this.auditEvents = new MobiusAuthenticationAuditResource(this);
743
+ }
744
+ exchangeExternalIdentity = async (request, externalCredentialProvider, signal) => new MobiusIssuedProjectAccessCredential(await this.#requests.externalPost(`v1/auth/exchanges/${encodeURIComponent(identifier(request.providerId, "external provider"))}`, {
745
+ expiresInSeconds: positiveInteger(request.expiresInSeconds, "access credential lifetime"),
746
+ projectId: this.projectId,
747
+ }, externalCredentialProvider, signal));
748
+ projectDelete = async (path, signal) => await this.#requests.projectDelete(path, signal);
749
+ projectGet = async (path, signal) => await this.#requests.projectGet(path, signal);
750
+ projectPost = async (path, body, signal) => await this.#requests.projectPost(path, body, signal);
751
+ }
752
+ //# sourceMappingURL=project-authentication.js.map