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