@lepistabioinformatics/mycelium-sdk 2.0.0
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 +190 -0
- package/README.md +121 -0
- package/dist/chunk-VPKHPMDS.js +682 -0
- package/dist/chunk-VPKHPMDS.js.map +1 -0
- package/dist/express/index.cjs +706 -0
- package/dist/express/index.cjs.map +1 -0
- package/dist/express/index.d.cts +18 -0
- package/dist/express/index.d.ts +18 -0
- package/dist/express/index.js +80 -0
- package/dist/express/index.js.map +1 -0
- package/dist/fastify/index.cjs +785 -0
- package/dist/fastify/index.cjs.map +1 -0
- package/dist/fastify/index.d.cts +15 -0
- package/dist/fastify/index.d.ts +15 -0
- package/dist/fastify/index.js +148 -0
- package/dist/fastify/index.js.map +1 -0
- package/dist/index.cjs +711 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +138 -0
- package/dist/index.d.ts +138 -0
- package/dist/index.js +69 -0
- package/dist/index.js.map +1 -0
- package/dist/profile-CUKBifyR.d.cts +463 -0
- package/dist/profile-CUKBifyR.d.ts +463 -0
- package/package.json +86 -0
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
8
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
20
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
21
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
22
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
23
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
|
+
mod
|
|
25
|
+
));
|
|
26
|
+
|
|
27
|
+
// src/errors.ts
|
|
28
|
+
var MyceliumError = class extends Error {
|
|
29
|
+
code;
|
|
30
|
+
expTrue;
|
|
31
|
+
constructor(message, code, expTrue = false) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.name = "MyceliumError";
|
|
34
|
+
this.code = code;
|
|
35
|
+
this.expTrue = expTrue;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var InsufficientPrivilegesError = class extends MyceliumError {
|
|
39
|
+
filteringState;
|
|
40
|
+
constructor(message, filteringState = []) {
|
|
41
|
+
super(message, "MYC00019", true);
|
|
42
|
+
this.name = "InsufficientPrivilegesError";
|
|
43
|
+
this.filteringState = filteringState;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var InsufficientLicensesError = class extends MyceliumError {
|
|
47
|
+
constructor(message = "Insufficient licenses to perform these action") {
|
|
48
|
+
super(message, "MYC00019", true);
|
|
49
|
+
this.name = "InsufficientLicensesError";
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var ProfileDecodingError = class extends MyceliumError {
|
|
53
|
+
constructor(message) {
|
|
54
|
+
super(message, "MYC00020", false);
|
|
55
|
+
this.name = "ProfileDecodingError";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/settings.ts
|
|
60
|
+
var DEFAULT_PROFILE_KEY = "x-mycelium-profile";
|
|
61
|
+
var DEFAULT_EMAIL_KEY = "x-mycelium-email";
|
|
62
|
+
var DEFAULT_SCOPE_KEY = "x-mycelium-scope";
|
|
63
|
+
var DEFAULT_MYCELIUM_ROLE_KEY = "x-mycelium-role";
|
|
64
|
+
var DEFAULT_REQUEST_ID_KEY = "x-mycelium-request-id";
|
|
65
|
+
var DEFAULT_CONNECTION_STRING_KEY = "x-mycelium-connection-string";
|
|
66
|
+
var DEFAULT_TENANT_ID_KEY = "x-mycelium-tenant-id";
|
|
67
|
+
|
|
68
|
+
// src/models/permission.ts
|
|
69
|
+
import { z } from "zod";
|
|
70
|
+
var Permission = {
|
|
71
|
+
READ: 0,
|
|
72
|
+
WRITE: 1
|
|
73
|
+
};
|
|
74
|
+
var PermissionSchema = z.union([
|
|
75
|
+
z.literal(0),
|
|
76
|
+
z.literal(1)
|
|
77
|
+
]);
|
|
78
|
+
function permissionToInt(permission) {
|
|
79
|
+
return permission;
|
|
80
|
+
}
|
|
81
|
+
function permissionFromInt(value) {
|
|
82
|
+
if (value === Permission.READ || value === Permission.WRITE) {
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
throw new Error(`Invalid permission code: ${value}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/models/verboseStatus.ts
|
|
89
|
+
import { z as z2 } from "zod";
|
|
90
|
+
var VerboseStatusSchema = z2.enum([
|
|
91
|
+
"unverified",
|
|
92
|
+
"verified",
|
|
93
|
+
"inactive",
|
|
94
|
+
"archived",
|
|
95
|
+
"deleted",
|
|
96
|
+
"unknown"
|
|
97
|
+
]);
|
|
98
|
+
|
|
99
|
+
// src/models/accountMetaKey.ts
|
|
100
|
+
import { z as z3 } from "zod";
|
|
101
|
+
var KNOWN_KEYS = [
|
|
102
|
+
"phone_number",
|
|
103
|
+
"telegram_user",
|
|
104
|
+
"whatsapp_user",
|
|
105
|
+
"locale"
|
|
106
|
+
];
|
|
107
|
+
var AccountMetaKeySchema = z3.string().refine(
|
|
108
|
+
(value) => KNOWN_KEYS.includes(value) || value.startsWith("custom:"),
|
|
109
|
+
{
|
|
110
|
+
message: "must be one of phone_number, telegram_user, whatsapp_user, locale, or a custom:<key> string"
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
var ProfileMetaSchema = z3.record(AccountMetaKeySchema, z3.string());
|
|
114
|
+
|
|
115
|
+
// src/models/owner.ts
|
|
116
|
+
import { z as z4 } from "zod";
|
|
117
|
+
var OwnerSchema = z4.object({
|
|
118
|
+
id: z4.string().uuid(),
|
|
119
|
+
email: z4.string(),
|
|
120
|
+
firstName: z4.string().optional(),
|
|
121
|
+
lastName: z4.string().optional(),
|
|
122
|
+
username: z4.string().optional(),
|
|
123
|
+
isPrincipal: z4.boolean()
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// src/models/licensedResource.ts
|
|
127
|
+
import { z as z5 } from "zod";
|
|
128
|
+
var LicensedResourceSchema = z5.object({
|
|
129
|
+
accId: z5.string().uuid(),
|
|
130
|
+
sysAcc: z5.boolean(),
|
|
131
|
+
tenantId: z5.string().uuid(),
|
|
132
|
+
accName: z5.string(),
|
|
133
|
+
role: z5.string(),
|
|
134
|
+
roleId: z5.string().uuid(),
|
|
135
|
+
perm: PermissionSchema,
|
|
136
|
+
verified: z5.boolean(),
|
|
137
|
+
permitFlags: z5.array(z5.string()).optional(),
|
|
138
|
+
denyFlags: z5.array(z5.string()).optional()
|
|
139
|
+
});
|
|
140
|
+
function licensedResourceFromUrlString(value) {
|
|
141
|
+
let url;
|
|
142
|
+
try {
|
|
143
|
+
url = new URL(value, "http://placeholder.local");
|
|
144
|
+
} catch {
|
|
145
|
+
throw new Error(`Invalid licensed resource URL string: ${value}`);
|
|
146
|
+
}
|
|
147
|
+
const segments = url.pathname.split("/").filter(Boolean);
|
|
148
|
+
if (segments.length !== 6 || segments[0] !== "t" || segments[2] !== "a" || segments[4] !== "r") {
|
|
149
|
+
throw new Error(`Invalid path format in licensed resource URL: ${value}`);
|
|
150
|
+
}
|
|
151
|
+
const tenantId = segments[1];
|
|
152
|
+
const accId = segments[3];
|
|
153
|
+
const roleId = segments[5];
|
|
154
|
+
const pParam = url.searchParams.get("p");
|
|
155
|
+
if (pParam === null) {
|
|
156
|
+
throw new Error(`Parameter 'p' not found in: ${value}`);
|
|
157
|
+
}
|
|
158
|
+
const sepIndex = pParam.lastIndexOf(":");
|
|
159
|
+
if (sepIndex === -1) {
|
|
160
|
+
throw new Error(`Invalid 'p' parameter format in: ${value}`);
|
|
161
|
+
}
|
|
162
|
+
const role = pParam.slice(0, sepIndex);
|
|
163
|
+
const perm = permissionFromInt(Number.parseInt(pParam.slice(sepIndex + 1), 10));
|
|
164
|
+
const sParam = url.searchParams.get("s");
|
|
165
|
+
const vParam = url.searchParams.get("v");
|
|
166
|
+
const nParam = url.searchParams.get("n");
|
|
167
|
+
if (sParam === null || vParam === null || nParam === null) {
|
|
168
|
+
throw new Error(`Missing 's'/'v'/'n' parameter in: ${value}`);
|
|
169
|
+
}
|
|
170
|
+
if (sParam !== "0" && sParam !== "1") {
|
|
171
|
+
throw new Error(`Invalid 's' parameter value in: ${value}`);
|
|
172
|
+
}
|
|
173
|
+
if (vParam !== "0" && vParam !== "1") {
|
|
174
|
+
throw new Error(`Invalid 'v' parameter value in: ${value}`);
|
|
175
|
+
}
|
|
176
|
+
const accName = Buffer.from(nParam, "base64").toString("utf-8");
|
|
177
|
+
const pfParam = url.searchParams.get("pf");
|
|
178
|
+
const dfParam = url.searchParams.get("df");
|
|
179
|
+
return LicensedResourceSchema.parse({
|
|
180
|
+
accId,
|
|
181
|
+
sysAcc: sParam === "1",
|
|
182
|
+
tenantId,
|
|
183
|
+
accName,
|
|
184
|
+
role,
|
|
185
|
+
roleId,
|
|
186
|
+
perm,
|
|
187
|
+
verified: vParam === "1",
|
|
188
|
+
permitFlags: pfParam ? pfParam.split(",") : void 0,
|
|
189
|
+
denyFlags: dfParam ? dfParam.split(",") : void 0
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// src/models/licensedResources.ts
|
|
194
|
+
import { z as z6 } from "zod";
|
|
195
|
+
function tagRecordsOrUrls(raw) {
|
|
196
|
+
if (raw && typeof raw === "object") {
|
|
197
|
+
const obj = raw;
|
|
198
|
+
const hasRecords = "records" in obj;
|
|
199
|
+
const hasUrls = "urls" in obj;
|
|
200
|
+
if (hasRecords && !hasUrls) {
|
|
201
|
+
return { kind: "records", records: obj.records };
|
|
202
|
+
}
|
|
203
|
+
if (hasUrls && !hasRecords) {
|
|
204
|
+
return { kind: "urls", urls: obj.urls };
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return raw;
|
|
208
|
+
}
|
|
209
|
+
var LicensedResourcesSchema = z6.preprocess(
|
|
210
|
+
tagRecordsOrUrls,
|
|
211
|
+
z6.discriminatedUnion("kind", [
|
|
212
|
+
z6.object({
|
|
213
|
+
kind: z6.literal("records"),
|
|
214
|
+
records: z6.array(LicensedResourceSchema)
|
|
215
|
+
}),
|
|
216
|
+
z6.object({
|
|
217
|
+
kind: z6.literal("urls"),
|
|
218
|
+
urls: z6.array(z6.string())
|
|
219
|
+
})
|
|
220
|
+
])
|
|
221
|
+
);
|
|
222
|
+
function toLicensesVector(lr) {
|
|
223
|
+
if (!lr) return [];
|
|
224
|
+
if (lr.kind === "records") return lr.records;
|
|
225
|
+
return lr.urls.map(licensedResourceFromUrlString);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/models/tenantOwnership.ts
|
|
229
|
+
import { z as z7 } from "zod";
|
|
230
|
+
var TenantOwnershipSchema = z7.object({
|
|
231
|
+
id: z7.string().uuid(),
|
|
232
|
+
name: z7.string(),
|
|
233
|
+
since: z7.coerce.date()
|
|
234
|
+
});
|
|
235
|
+
function reinsertUuidHyphens(noHyphens) {
|
|
236
|
+
if (!/^[0-9a-fA-F]{32}$/.test(noHyphens)) {
|
|
237
|
+
throw new Error(`Invalid unhyphenated UUID: ${noHyphens}`);
|
|
238
|
+
}
|
|
239
|
+
return [
|
|
240
|
+
noHyphens.slice(0, 8),
|
|
241
|
+
noHyphens.slice(8, 12),
|
|
242
|
+
noHyphens.slice(12, 16),
|
|
243
|
+
noHyphens.slice(16, 20),
|
|
244
|
+
noHyphens.slice(20, 32)
|
|
245
|
+
].join("-");
|
|
246
|
+
}
|
|
247
|
+
function tenantOwnershipFromUrlString(value) {
|
|
248
|
+
let url;
|
|
249
|
+
try {
|
|
250
|
+
url = new URL(value, "http://placeholder.local");
|
|
251
|
+
} catch {
|
|
252
|
+
throw new Error(`Invalid tenant ownership URL string: ${value}`);
|
|
253
|
+
}
|
|
254
|
+
const segments = url.pathname.split("/").filter(Boolean);
|
|
255
|
+
if (segments.length !== 2 || segments[0] !== "tid") {
|
|
256
|
+
throw new Error(`Invalid path format in tenant ownership URL: ${value}`);
|
|
257
|
+
}
|
|
258
|
+
const id = reinsertUuidHyphens(segments[1]);
|
|
259
|
+
const since = url.searchParams.get("since");
|
|
260
|
+
const nameEncoded = url.searchParams.get("name");
|
|
261
|
+
if (since === null || nameEncoded === null) {
|
|
262
|
+
throw new Error(`Missing 'since'/'name' parameter in: ${value}`);
|
|
263
|
+
}
|
|
264
|
+
const name = Buffer.from(nameEncoded, "base64").toString("utf-8");
|
|
265
|
+
return TenantOwnershipSchema.parse({ id, name, since });
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/models/tenantsOwnership.ts
|
|
269
|
+
import { z as z8 } from "zod";
|
|
270
|
+
function tagRecordsOrUrls2(raw) {
|
|
271
|
+
if (raw && typeof raw === "object") {
|
|
272
|
+
const obj = raw;
|
|
273
|
+
const hasRecords = "records" in obj;
|
|
274
|
+
const hasUrls = "urls" in obj;
|
|
275
|
+
if (hasRecords && !hasUrls) {
|
|
276
|
+
return { kind: "records", records: obj.records };
|
|
277
|
+
}
|
|
278
|
+
if (hasUrls && !hasRecords) {
|
|
279
|
+
return { kind: "urls", urls: obj.urls };
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return raw;
|
|
283
|
+
}
|
|
284
|
+
var TenantsOwnershipSchema = z8.preprocess(
|
|
285
|
+
tagRecordsOrUrls2,
|
|
286
|
+
z8.discriminatedUnion("kind", [
|
|
287
|
+
z8.object({
|
|
288
|
+
kind: z8.literal("records"),
|
|
289
|
+
records: z8.array(TenantOwnershipSchema)
|
|
290
|
+
}),
|
|
291
|
+
z8.object({
|
|
292
|
+
kind: z8.literal("urls"),
|
|
293
|
+
urls: z8.array(z8.string())
|
|
294
|
+
})
|
|
295
|
+
])
|
|
296
|
+
);
|
|
297
|
+
function toOwnershipVector(to) {
|
|
298
|
+
if (!to) return [];
|
|
299
|
+
if (to.kind === "records") return to.records;
|
|
300
|
+
return to.urls.map(tenantOwnershipFromUrlString);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// src/models/profile.ts
|
|
304
|
+
import { z as z9 } from "zod";
|
|
305
|
+
var ProfileSchema = z9.object({
|
|
306
|
+
owners: z9.array(OwnerSchema).default([]),
|
|
307
|
+
accId: z9.string().uuid(),
|
|
308
|
+
isSubscription: z9.boolean(),
|
|
309
|
+
isStaff: z9.boolean(),
|
|
310
|
+
isManager: z9.boolean().default(false),
|
|
311
|
+
ownerIsActive: z9.boolean(),
|
|
312
|
+
accountIsActive: z9.boolean(),
|
|
313
|
+
accountWasApproved: z9.boolean(),
|
|
314
|
+
accountWasArchived: z9.boolean(),
|
|
315
|
+
accountWasDeleted: z9.boolean(),
|
|
316
|
+
verboseStatus: VerboseStatusSchema.optional(),
|
|
317
|
+
licensedResources: LicensedResourcesSchema.optional(),
|
|
318
|
+
tenantsOwnership: TenantsOwnershipSchema.optional(),
|
|
319
|
+
meta: ProfileMetaSchema.optional(),
|
|
320
|
+
filteringState: z9.array(z9.string()).optional()
|
|
321
|
+
});
|
|
322
|
+
function appendState(current, entry) {
|
|
323
|
+
const state = current ?? [];
|
|
324
|
+
return [...state, `${state.length + 1}:${entry}`];
|
|
325
|
+
}
|
|
326
|
+
var Profile = class _Profile {
|
|
327
|
+
data;
|
|
328
|
+
constructor(data) {
|
|
329
|
+
this.data = data;
|
|
330
|
+
}
|
|
331
|
+
static fromValidated(data) {
|
|
332
|
+
return new _Profile(data);
|
|
333
|
+
}
|
|
334
|
+
get accId() {
|
|
335
|
+
return this.data.accId;
|
|
336
|
+
}
|
|
337
|
+
get owners() {
|
|
338
|
+
return this.data.owners;
|
|
339
|
+
}
|
|
340
|
+
get isStaff() {
|
|
341
|
+
return this.data.isStaff;
|
|
342
|
+
}
|
|
343
|
+
get isManager() {
|
|
344
|
+
return this.data.isManager;
|
|
345
|
+
}
|
|
346
|
+
get licensedResources() {
|
|
347
|
+
return this.data.licensedResources;
|
|
348
|
+
}
|
|
349
|
+
get tenantsOwnership() {
|
|
350
|
+
return this.data.tenantsOwnership;
|
|
351
|
+
}
|
|
352
|
+
get filteringState() {
|
|
353
|
+
return this.data.filteringState;
|
|
354
|
+
}
|
|
355
|
+
withData(partial) {
|
|
356
|
+
return new _Profile({ ...this.data, ...partial });
|
|
357
|
+
}
|
|
358
|
+
filterLicensedResources(predicate) {
|
|
359
|
+
const records = toLicensesVector(this.data.licensedResources).filter(
|
|
360
|
+
predicate
|
|
361
|
+
);
|
|
362
|
+
return records.length === 0 ? void 0 : { kind: "records", records };
|
|
363
|
+
}
|
|
364
|
+
withPermission(permission) {
|
|
365
|
+
if (!this.data.licensedResources) return this;
|
|
366
|
+
const licensedResources = this.filterLicensedResources(
|
|
367
|
+
(r) => permissionToInt(r.perm) >= permissionToInt(permission)
|
|
368
|
+
);
|
|
369
|
+
return this.withData({
|
|
370
|
+
licensedResources,
|
|
371
|
+
filteringState: appendState(
|
|
372
|
+
this.data.filteringState,
|
|
373
|
+
`permission:${permissionToInt(permission)}`
|
|
374
|
+
)
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
withReadAccess() {
|
|
378
|
+
return this.withPermission(Permission.READ);
|
|
379
|
+
}
|
|
380
|
+
withWriteAccess() {
|
|
381
|
+
return this.withPermission(Permission.WRITE);
|
|
382
|
+
}
|
|
383
|
+
onTenant(tenantId) {
|
|
384
|
+
const licensedResources = this.filterLicensedResources(
|
|
385
|
+
(r) => r.tenantId === tenantId
|
|
386
|
+
);
|
|
387
|
+
return this.withData({
|
|
388
|
+
licensedResources,
|
|
389
|
+
filteringState: appendState(
|
|
390
|
+
this.data.filteringState,
|
|
391
|
+
`tenantId:${tenantId}`
|
|
392
|
+
)
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
withRoles(roles) {
|
|
396
|
+
const licensedResources = this.filterLicensedResources(
|
|
397
|
+
(r) => roles.includes(r.role)
|
|
398
|
+
);
|
|
399
|
+
return this.withData({
|
|
400
|
+
licensedResources,
|
|
401
|
+
filteringState: appendState(
|
|
402
|
+
this.data.filteringState,
|
|
403
|
+
`role:${roles.join(",")}`
|
|
404
|
+
)
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
onAccount(accountId) {
|
|
408
|
+
const licensedResources = this.filterLicensedResources(
|
|
409
|
+
(r) => r.accId === accountId
|
|
410
|
+
);
|
|
411
|
+
return this.withData({
|
|
412
|
+
licensedResources,
|
|
413
|
+
filteringState: appendState(
|
|
414
|
+
this.data.filteringState,
|
|
415
|
+
`accountId:${accountId}`
|
|
416
|
+
)
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
hasPermitFlags(flags) {
|
|
420
|
+
const licensedResources = this.filterLicensedResources((r) => {
|
|
421
|
+
if (!r.permitFlags) return false;
|
|
422
|
+
return flags.every((f) => r.permitFlags?.includes(f));
|
|
423
|
+
});
|
|
424
|
+
return this.withData({
|
|
425
|
+
licensedResources,
|
|
426
|
+
filteringState: appendState(
|
|
427
|
+
this.data.filteringState,
|
|
428
|
+
`permittedFlags:${flags.join(",")}`
|
|
429
|
+
)
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
hasNotDenyFlags(flags) {
|
|
433
|
+
const licensedResources = this.filterLicensedResources((r) => {
|
|
434
|
+
if (!r.denyFlags) return true;
|
|
435
|
+
return !flags.some((f) => r.denyFlags?.includes(f));
|
|
436
|
+
});
|
|
437
|
+
return this.withData({
|
|
438
|
+
licensedResources,
|
|
439
|
+
filteringState: appendState(
|
|
440
|
+
this.data.filteringState,
|
|
441
|
+
`deniedFlags:${flags.join(",")}`
|
|
442
|
+
)
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
onTenantAsManager(tenantId, permission) {
|
|
446
|
+
const profile = this.onTenant(tenantId).withPermission(permission).withRoles(["tenant-manager"]);
|
|
447
|
+
return profile.withData({
|
|
448
|
+
filteringState: appendState(profile.data.filteringState, `isTenantManager:true`)
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
withSystemAccountsAccess() {
|
|
452
|
+
const licensedResources = this.filterLicensedResources((r) => r.sysAcc);
|
|
453
|
+
return this.withData({
|
|
454
|
+
licensedResources,
|
|
455
|
+
filteringState: appendState(
|
|
456
|
+
this.data.filteringState,
|
|
457
|
+
`isAccStd:true`
|
|
458
|
+
)
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
withTenantOwnershipOrError(tenantId) {
|
|
462
|
+
const tenants = toOwnershipVector(this.data.tenantsOwnership);
|
|
463
|
+
if (tenants.some((t) => t.id === tenantId)) {
|
|
464
|
+
return this.withData({
|
|
465
|
+
filteringState: appendState(
|
|
466
|
+
this.data.filteringState,
|
|
467
|
+
`tenantOwnership:${tenantId}`
|
|
468
|
+
)
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
throw new InsufficientPrivilegesError(
|
|
472
|
+
`Insufficient privileges to perform these action (no tenant ownership): ${(this.data.filteringState ?? []).join(", ")}`,
|
|
473
|
+
this.data.filteringState
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
hasAdminPrivileges() {
|
|
477
|
+
return this.data.isStaff || this.data.isManager;
|
|
478
|
+
}
|
|
479
|
+
hasAdminPrivilegesOrError() {
|
|
480
|
+
if (!this.hasAdminPrivileges()) {
|
|
481
|
+
throw new InsufficientPrivilegesError(
|
|
482
|
+
"Current account has no administration privileges",
|
|
483
|
+
this.data.filteringState
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
getIdsOrError() {
|
|
488
|
+
const ids = toLicensesVector(this.data.licensedResources).map(
|
|
489
|
+
(r) => r.accId
|
|
490
|
+
);
|
|
491
|
+
if (ids.length === 0 && !this.hasAdminPrivileges()) {
|
|
492
|
+
throw new InsufficientPrivilegesError(
|
|
493
|
+
`Insufficient privileges to perform these action (no ids): ${(this.data.filteringState ?? []).join(", ")}`,
|
|
494
|
+
this.data.filteringState
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
return ids;
|
|
498
|
+
}
|
|
499
|
+
getTenantWidePermissionOrError(tenantId, permission) {
|
|
500
|
+
if (this.data.isStaff) {
|
|
501
|
+
return { kind: "HasStaffPrivileges" };
|
|
502
|
+
}
|
|
503
|
+
if (this.data.isManager) {
|
|
504
|
+
return { kind: "HasManagerPrivileges" };
|
|
505
|
+
}
|
|
506
|
+
const tenants = toOwnershipVector(this.data.tenantsOwnership);
|
|
507
|
+
if (tenants.some((t) => t.id === tenantId)) {
|
|
508
|
+
return { kind: "HasTenantWidePrivileges", tenantId };
|
|
509
|
+
}
|
|
510
|
+
try {
|
|
511
|
+
this.onTenantAsManager(tenantId, permission).getIdsOrError();
|
|
512
|
+
return { kind: "HasTenantWidePrivileges", tenantId };
|
|
513
|
+
} catch {
|
|
514
|
+
}
|
|
515
|
+
throw new InsufficientPrivilegesError(
|
|
516
|
+
`Insufficient privileges to perform these action (no tenant wide permission): ${(this.data.filteringState ?? []).join(", ")}`,
|
|
517
|
+
this.data.filteringState
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
getRelatedAccountsOrTenantWidePermissionOrError(tenantId, permission) {
|
|
521
|
+
try {
|
|
522
|
+
return this.getTenantWidePermissionOrError(tenantId, permission);
|
|
523
|
+
} catch {
|
|
524
|
+
return this.getRelatedAccountOrError();
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
getRelatedAccountOrError() {
|
|
528
|
+
if (this.data.isStaff) {
|
|
529
|
+
return { kind: "HasStaffPrivileges" };
|
|
530
|
+
}
|
|
531
|
+
if (this.data.isManager) {
|
|
532
|
+
return { kind: "HasManagerPrivileges" };
|
|
533
|
+
}
|
|
534
|
+
if (this.data.licensedResources) {
|
|
535
|
+
const records = toLicensesVector(this.data.licensedResources);
|
|
536
|
+
if (records.length === 0) {
|
|
537
|
+
throw new InsufficientLicensesError();
|
|
538
|
+
}
|
|
539
|
+
return {
|
|
540
|
+
kind: "AllowedAccounts",
|
|
541
|
+
accounts: records.map((r) => r.accId)
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
throw new InsufficientPrivilegesError(
|
|
545
|
+
`Insufficient privileges to perform these action (no accounts): ${(this.data.filteringState ?? []).join(", ")}`,
|
|
546
|
+
this.data.filteringState
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
getOwnersIds() {
|
|
550
|
+
return this.data.owners.map((o) => o.id);
|
|
551
|
+
}
|
|
552
|
+
profileString() {
|
|
553
|
+
return `profile/${this.data.accId}`;
|
|
554
|
+
}
|
|
555
|
+
profileRedacted() {
|
|
556
|
+
const redactedEmails = this.data.owners.map((o) => redactEmail(o.email));
|
|
557
|
+
return `profile/${this.data.accId} owners: [${redactedEmails.join(", ")}]`;
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
function redactEmail(email) {
|
|
561
|
+
const [local, domain] = email.split("@");
|
|
562
|
+
if (!domain || !local) return email;
|
|
563
|
+
const visible = local.slice(0, 1);
|
|
564
|
+
return `${visible}${"*".repeat(Math.max(local.length - 1, 1))}@${domain}`;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// src/functions/decodeAndDecompressProfileFromBase64.ts
|
|
568
|
+
import { zstdCompressSync, zstdDecompressSync } from "zlib";
|
|
569
|
+
function decodeAndDecompressProfileFromBase64(input, opts = {}) {
|
|
570
|
+
const strict = opts.strict ?? true;
|
|
571
|
+
if (typeof input === "string" && input.length === 0 || Buffer.isBuffer(input) && input.length === 0) {
|
|
572
|
+
throw new ProfileDecodingError("Profile input is empty");
|
|
573
|
+
}
|
|
574
|
+
const inputBytes = Buffer.isBuffer(input) ? input : Buffer.from(input, "utf-8");
|
|
575
|
+
const base64Text = inputBytes.toString("utf-8");
|
|
576
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(base64Text) || base64Text.length % 4 !== 0) {
|
|
577
|
+
throw new ProfileDecodingError(
|
|
578
|
+
`Failed to decode base64 profile: not valid base64`
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
let decoded;
|
|
582
|
+
try {
|
|
583
|
+
decoded = Buffer.from(base64Text, "base64");
|
|
584
|
+
} catch (cause) {
|
|
585
|
+
throw new ProfileDecodingError(`Failed to decode base64 profile: ${cause}`);
|
|
586
|
+
}
|
|
587
|
+
let decompressed;
|
|
588
|
+
try {
|
|
589
|
+
decompressed = zstdDecompressSync(decoded);
|
|
590
|
+
} catch (cause) {
|
|
591
|
+
if (strict) {
|
|
592
|
+
throw new ProfileDecodingError(
|
|
593
|
+
`Failed to decompress zstd profile: ${cause}`
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
decompressed = decoded;
|
|
597
|
+
}
|
|
598
|
+
let profileString;
|
|
599
|
+
try {
|
|
600
|
+
profileString = decompressed.toString("utf-8");
|
|
601
|
+
} catch (cause) {
|
|
602
|
+
throw new ProfileDecodingError(
|
|
603
|
+
`Failed to convert decompressed profile to string: ${cause}`
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
let profileJson;
|
|
607
|
+
try {
|
|
608
|
+
profileJson = JSON.parse(profileString);
|
|
609
|
+
} catch (cause) {
|
|
610
|
+
throw new ProfileDecodingError(`Failed to deserialize profile: ${cause}`);
|
|
611
|
+
}
|
|
612
|
+
const result = ProfileSchema.safeParse(profileJson);
|
|
613
|
+
if (!result.success) {
|
|
614
|
+
throw new ProfileDecodingError(
|
|
615
|
+
`Failed to deserialize profile: ${formatZodError(result.error)}`
|
|
616
|
+
);
|
|
617
|
+
}
|
|
618
|
+
return Profile.fromValidated(result.data);
|
|
619
|
+
}
|
|
620
|
+
function safeDecodeAndDecompressProfileFromBase64(input, opts = {}) {
|
|
621
|
+
try {
|
|
622
|
+
const profile = decodeAndDecompressProfileFromBase64(input, opts);
|
|
623
|
+
return { success: true, profile };
|
|
624
|
+
} catch (error) {
|
|
625
|
+
if (error instanceof ProfileDecodingError) {
|
|
626
|
+
return { success: false, error };
|
|
627
|
+
}
|
|
628
|
+
return {
|
|
629
|
+
success: false,
|
|
630
|
+
error: new ProfileDecodingError(String(error))
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
function compressAndEncodeProfileToBase64(profile) {
|
|
635
|
+
const json = JSON.stringify(profile.data);
|
|
636
|
+
const compressed = zstdCompressSync(Buffer.from(json, "utf-8"));
|
|
637
|
+
return compressed.toString("base64");
|
|
638
|
+
}
|
|
639
|
+
function formatZodError(error) {
|
|
640
|
+
return error.issues.map((issue) => {
|
|
641
|
+
const path = issue.path.length > 0 ? issue.path.join(".") : "(root)";
|
|
642
|
+
return `field '${path}' ${issue.message}`;
|
|
643
|
+
}).join("; ");
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
export {
|
|
647
|
+
__commonJS,
|
|
648
|
+
__toESM,
|
|
649
|
+
MyceliumError,
|
|
650
|
+
InsufficientPrivilegesError,
|
|
651
|
+
InsufficientLicensesError,
|
|
652
|
+
ProfileDecodingError,
|
|
653
|
+
DEFAULT_PROFILE_KEY,
|
|
654
|
+
DEFAULT_EMAIL_KEY,
|
|
655
|
+
DEFAULT_SCOPE_KEY,
|
|
656
|
+
DEFAULT_MYCELIUM_ROLE_KEY,
|
|
657
|
+
DEFAULT_REQUEST_ID_KEY,
|
|
658
|
+
DEFAULT_CONNECTION_STRING_KEY,
|
|
659
|
+
DEFAULT_TENANT_ID_KEY,
|
|
660
|
+
Permission,
|
|
661
|
+
PermissionSchema,
|
|
662
|
+
permissionToInt,
|
|
663
|
+
permissionFromInt,
|
|
664
|
+
VerboseStatusSchema,
|
|
665
|
+
AccountMetaKeySchema,
|
|
666
|
+
ProfileMetaSchema,
|
|
667
|
+
OwnerSchema,
|
|
668
|
+
LicensedResourceSchema,
|
|
669
|
+
licensedResourceFromUrlString,
|
|
670
|
+
LicensedResourcesSchema,
|
|
671
|
+
toLicensesVector,
|
|
672
|
+
TenantOwnershipSchema,
|
|
673
|
+
tenantOwnershipFromUrlString,
|
|
674
|
+
TenantsOwnershipSchema,
|
|
675
|
+
toOwnershipVector,
|
|
676
|
+
ProfileSchema,
|
|
677
|
+
Profile,
|
|
678
|
+
decodeAndDecompressProfileFromBase64,
|
|
679
|
+
safeDecodeAndDecompressProfileFromBase64,
|
|
680
|
+
compressAndEncodeProfileToBase64
|
|
681
|
+
};
|
|
682
|
+
//# sourceMappingURL=chunk-VPKHPMDS.js.map
|