@innvoid/getmarket-sdk 0.2.8 → 0.2.9

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.
@@ -1,662 +0,0 @@
1
- import {
2
- HEADER_INTERNAL_API_KEY,
3
- getRequestContextFromHeaders
4
- } from "./chunk-KXXIMSON.js";
5
-
6
- // src/middlewares/parseHeaders.ts
7
- function parseHeaders(req, _res, next) {
8
- req.context = getRequestContextFromHeaders(req.headers);
9
- next();
10
- }
11
-
12
- // src/middlewares/requestId.ts
13
- import { randomUUID, randomBytes } from "crypto";
14
- var REQUEST_ID_HEADER = "x-request-id";
15
- var REQUEST_ID_HEADER_ALT = "x-requestid";
16
- var RESPONSE_REQUEST_ID_HEADER = "X-Request-Id";
17
- function requestId(req, res, next) {
18
- const headerId = req.headers[REQUEST_ID_HEADER] || req.headers[REQUEST_ID_HEADER_ALT];
19
- const id = headerId?.trim() || randomUUID();
20
- req.requestId = id;
21
- res.locals.requestId = id;
22
- res.setHeader(RESPONSE_REQUEST_ID_HEADER, id);
23
- next();
24
- }
25
-
26
- // src/middlewares/internalAuth.ts
27
- import fs from "fs";
28
- import crypto from "crypto";
29
-
30
- // src/middlewares/respond.ts
31
- function sendOk(_req, res, data, statusCode = 200) {
32
- return res.status(statusCode).json({ ok: true, data, requestId: res.locals?.requestId ?? null });
33
- }
34
- function sendError(_req, res, statusCode, code, message, details) {
35
- return res.status(statusCode).json({
36
- ok: false,
37
- error: { code, message, ...details !== void 0 ? { details } : {} },
38
- requestId: res.locals?.requestId ?? null
39
- });
40
- }
41
-
42
- // src/middlewares/internalAuth.ts
43
- function readSecretFile(path) {
44
- if (!path) return null;
45
- try {
46
- const v = fs.readFileSync(path, "utf8").trim();
47
- return v.length ? v : null;
48
- } catch {
49
- return null;
50
- }
51
- }
52
- function splitKeys(v) {
53
- if (!v) return [];
54
- return v.split(",").map((s) => s.trim()).filter(Boolean);
55
- }
56
- function getExpectedKeys() {
57
- const fileKey = readSecretFile(process.env.INTERNAL_API_KEY_FILE);
58
- const envKey = (process.env.INTERNAL_API_KEY || "").trim();
59
- const raw = fileKey || envKey;
60
- return splitKeys(raw);
61
- }
62
- function extractToken(req) {
63
- const apiKey = (req.header(HEADER_INTERNAL_API_KEY) || "").trim();
64
- return apiKey || null;
65
- }
66
- function safeEquals(a, b) {
67
- const aa = Buffer.from(a);
68
- const bb = Buffer.from(b);
69
- if (aa.length !== bb.length) return false;
70
- return crypto.timingSafeEqual(aa, bb);
71
- }
72
- function internalAuth(req, res, next) {
73
- const token = extractToken(req);
74
- if (!token) {
75
- return sendError(req, res, 401, "UNAUTHORIZED", `Missing internal api key (${HEADER_INTERNAL_API_KEY})`);
76
- }
77
- const expectedKeys = getExpectedKeys();
78
- if (expectedKeys.length === 0) {
79
- return sendError(
80
- req,
81
- res,
82
- 500,
83
- "MISCONFIGURED_INTERNAL_AUTH",
84
- "Internal api key not configured (INTERNAL_API_KEY or INTERNAL_API_KEY_FILE)"
85
- );
86
- }
87
- const ok = expectedKeys.some((k) => safeEquals(token, k));
88
- if (!ok) {
89
- return sendError(req, res, 403, "FORBIDDEN", "Invalid internal api key");
90
- }
91
- return next();
92
- }
93
-
94
- // src/middlewares/authorization.ts
95
- function getAuth(req) {
96
- return req.auth ?? {};
97
- }
98
- function normalizeCode(v) {
99
- if (!v) return null;
100
- if (typeof v === "string") return v;
101
- if (typeof v === "object") return v.code || v.name || null;
102
- return null;
103
- }
104
- function rolesSet(auth) {
105
- const out = /* @__PURE__ */ new Set();
106
- for (const r of auth.roles || []) {
107
- const c = normalizeCode(r);
108
- if (c) out.add(c);
109
- }
110
- return out;
111
- }
112
- function permsSet(list) {
113
- const out = /* @__PURE__ */ new Set();
114
- for (const p of list || []) {
115
- const c = normalizeCode(p);
116
- if (c) out.add(c);
117
- }
118
- return out;
119
- }
120
- function requireAuthContext() {
121
- return (req, res, next) => {
122
- if (!req.auth) {
123
- return sendError(req, res, 401, "UNAUTHORIZED", "Missing auth context");
124
- }
125
- return next();
126
- };
127
- }
128
- function isSysAdmin(auth, sysAdminRole) {
129
- const have = rolesSet(auth);
130
- return have.has(sysAdminRole);
131
- }
132
- function requirePermissions(perms, options) {
133
- const sysAdminBypass = options?.sysAdminBypass !== false;
134
- const sysAdminRole = options?.sysAdminRole || "SYS_ADMIN";
135
- return (req, res, next) => {
136
- const auth = getAuth(req);
137
- if (sysAdminBypass && isSysAdmin(auth, sysAdminRole)) return next();
138
- const allow = permsSet(auth.permissions);
139
- const deny = permsSet(auth.denied_permissions);
140
- for (const p of perms) {
141
- if (deny.has(p)) {
142
- return sendError(req, res, 403, "FORBIDDEN", `Denied permission: ${p}`, {
143
- denied: p
144
- });
145
- }
146
- }
147
- const missing = perms.filter((p) => !allow.has(p));
148
- if (missing.length) {
149
- return sendError(req, res, 403, "FORBIDDEN", "Missing permissions", {
150
- missing,
151
- mode: "ALL"
152
- });
153
- }
154
- return next();
155
- };
156
- }
157
- function requireAnyPermission(perms, options) {
158
- const sysAdminBypass = options?.sysAdminBypass !== false;
159
- const sysAdminRole = options?.sysAdminRole || "SYS_ADMIN";
160
- return (req, res, next) => {
161
- const auth = getAuth(req);
162
- if (sysAdminBypass && isSysAdmin(auth, sysAdminRole)) return next();
163
- const allow = permsSet(auth.permissions);
164
- const deny = permsSet(auth.denied_permissions);
165
- for (const p of perms) {
166
- if (deny.has(p)) {
167
- return sendError(req, res, 403, "FORBIDDEN", `Denied permission: ${p}`, {
168
- denied: p
169
- });
170
- }
171
- }
172
- const ok = perms.some((p) => allow.has(p));
173
- if (!ok) {
174
- return sendError(req, res, 403, "FORBIDDEN", "Permission denied", {
175
- required: perms,
176
- mode: "ANY"
177
- });
178
- }
179
- return next();
180
- };
181
- }
182
- function requireRoles(roles, options) {
183
- const sysAdminBypass = options?.sysAdminBypass !== false;
184
- const sysAdminRole = options?.sysAdminRole || "SYS_ADMIN";
185
- return (req, res, next) => {
186
- const auth = getAuth(req);
187
- if (sysAdminBypass && isSysAdmin(auth, sysAdminRole)) return next();
188
- const have = rolesSet(auth);
189
- if (!roles.some((r) => have.has(r))) {
190
- return sendError(req, res, 403, "FORBIDDEN", "Role not allowed", {
191
- required: roles,
192
- mode: "ANY"
193
- });
194
- }
195
- return next();
196
- };
197
- }
198
- function requireRolesOrAnyPermission(roles, perms, options) {
199
- const sysAdminBypass = options?.sysAdminBypass !== false;
200
- const sysAdminRole = options?.sysAdminRole || "SYS_ADMIN";
201
- return (req, res, next) => {
202
- const auth = getAuth(req);
203
- if (sysAdminBypass && isSysAdmin(auth, sysAdminRole)) return next();
204
- const haveRoles = rolesSet(auth);
205
- const allow = permsSet(auth.permissions);
206
- const deny = permsSet(auth.denied_permissions);
207
- for (const p of perms) {
208
- if (deny.has(p)) {
209
- return sendError(req, res, 403, "FORBIDDEN", `Denied permission: ${p}`, {
210
- denied: p
211
- });
212
- }
213
- }
214
- const okRole = roles.some((r) => haveRoles.has(r));
215
- const okPerm = perms.some((p) => allow.has(p));
216
- if (!okRole && !okPerm) {
217
- return sendError(req, res, 403, "FORBIDDEN", "Access denied", {
218
- roles,
219
- permissions: perms,
220
- mode: "ROLES_OR_PERMS_ANY"
221
- });
222
- }
223
- return next();
224
- };
225
- }
226
-
227
- // src/auth/jwt.ts
228
- import fs2 from "fs";
229
- import jwt from "jsonwebtoken";
230
- function readFileIfExists(path) {
231
- if (!path) return null;
232
- try {
233
- const v = fs2.readFileSync(path, "utf8").trim();
234
- return v.length ? v : null;
235
- } catch {
236
- return null;
237
- }
238
- }
239
- function getBearerToken(req) {
240
- const auth = String(req?.headers?.authorization || "");
241
- if (!auth.startsWith("Bearer ")) return null;
242
- const token = auth.slice(7).trim();
243
- return token.length ? token : null;
244
- }
245
- function normalizeUid(v) {
246
- const s = String(v ?? "").trim();
247
- return s.length ? s : null;
248
- }
249
- function readRs256PublicKey() {
250
- const fromFile = readFileIfExists(process.env.JWT_PUBLIC_KEY_PATH);
251
- if (fromFile) return fromFile;
252
- const fromEnv = String(
253
- process.env.AUTH_JWT_PUBLIC_KEY || process.env.AUTH_RSA_PUBLIC_KEY || ""
254
- ).replace(/\\n/g, "\n").trim();
255
- if (fromEnv) return fromEnv;
256
- throw new Error(
257
- "Missing RS256 public key (JWT_PUBLIC_KEY_PATH / AUTH_JWT_PUBLIC_KEY / AUTH_RSA_PUBLIC_KEY)"
258
- );
259
- }
260
- function verifyBackendJwtRS256(raw) {
261
- const publicKey = readRs256PublicKey();
262
- const audience = process.env.JWT_AUDIENCE || process.env.AUTH_JWT_AUDIENCE || "getmarket.api";
263
- const issuer = process.env.JWT_ISSUER || process.env.AUTH_JWT_ISSUER || "getmarket-auth";
264
- return jwt.verify(raw, publicKey, {
265
- algorithms: ["RS256"],
266
- audience,
267
- issuer
268
- });
269
- }
270
- function extractEmployeeUid(decoded) {
271
- const direct = normalizeUid(decoded?.employee_uid) ?? normalizeUid(decoded?.employee?.uid);
272
- if (direct) return direct;
273
- const sub = normalizeUid(decoded?.sub);
274
- if (!sub) return null;
275
- const match = /^emp:(.+)$/i.exec(sub);
276
- return match?.[1] ? normalizeUid(match[1]) : null;
277
- }
278
- function extractCustomerUid(decoded) {
279
- const direct = normalizeUid(decoded?.customer_uid) ?? normalizeUid(decoded?.customer?.uid);
280
- if (direct) return direct;
281
- const sub = normalizeUid(decoded?.sub);
282
- if (!sub) return null;
283
- const match = /^cus:(.+)$/i.exec(sub);
284
- return match?.[1] ? normalizeUid(match[1]) : null;
285
- }
286
-
287
- // src/auth/middleware.ts
288
- function createAuthMiddleware(opts) {
289
- const {
290
- subject,
291
- allowFirebaseIdToken = false,
292
- requireSubject = true,
293
- hydrate
294
- } = opts;
295
- return async (req, res, next) => {
296
- const token = getBearerToken(req);
297
- if (!token) {
298
- return res.status(401).json({
299
- ok: false,
300
- code: "AUTH_MISSING_TOKEN",
301
- message: "Missing Authorization Bearer token"
302
- });
303
- }
304
- const headerCtx = req.context || {};
305
- const company_uid = normalizeUid(headerCtx.company_uid);
306
- const branch_uid = normalizeUid(headerCtx.branch_uid);
307
- try {
308
- const decoded = verifyBackendJwtRS256(token);
309
- const baseCtx = {
310
- tokenType: "backend",
311
- subject,
312
- company_uid: company_uid ?? void 0,
313
- branch_uid: branch_uid ?? void 0,
314
- roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
315
- permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
316
- denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : [],
317
- session: {
318
- jti: decoded?.jti,
319
- device_id: decoded?.device_id,
320
- expires_at: decoded?.exp
321
- }
322
- };
323
- if (subject === "employee") {
324
- baseCtx.employee_uid = extractEmployeeUid(decoded) ?? void 0;
325
- } else {
326
- baseCtx.customer_uid = extractCustomerUid(decoded) ?? void 0;
327
- }
328
- const hydrated = await hydrate({
329
- decoded,
330
- req,
331
- subject,
332
- company_uid,
333
- branch_uid
334
- });
335
- Object.assign(baseCtx, hydrated);
336
- if (subject === "employee" && !baseCtx.employee_uid) {
337
- return res.status(401).json({
338
- ok: false,
339
- code: "AUTH_EMPLOYEE_UID_MISSING",
340
- message: "employee_uid missing in token/context (expected employee_uid or sub=emp:<uid>)"
341
- });
342
- }
343
- if (subject === "customer" && !baseCtx.customer_uid) {
344
- return res.status(401).json({
345
- ok: false,
346
- code: "AUTH_CUSTOMER_UID_MISSING",
347
- message: "customer_uid missing in token/context (expected customer_uid or sub=cus:<uid>)"
348
- });
349
- }
350
- if (requireSubject) {
351
- if (subject === "employee" && !baseCtx.employee) {
352
- return res.status(401).json({
353
- ok: false,
354
- code: "AUTH_EMPLOYEE_NOT_FOUND",
355
- message: "Employee not resolved by hydrator"
356
- });
357
- }
358
- if (subject === "customer" && !baseCtx.customer) {
359
- return res.status(401).json({
360
- ok: false,
361
- code: "AUTH_CUSTOMER_NOT_FOUND",
362
- message: "Customer not resolved by hydrator"
363
- });
364
- }
365
- }
366
- req.auth = baseCtx;
367
- return next();
368
- } catch {
369
- if (!allowFirebaseIdToken) {
370
- return res.status(401).json({
371
- ok: false,
372
- code: "AUTH_INVALID_TOKEN",
373
- message: "Invalid or expired token"
374
- });
375
- }
376
- try {
377
- const { default: admin } = await import("firebase-admin");
378
- const firebaseDecoded = await admin.auth().verifyIdToken(token);
379
- if (firebaseDecoded.email && firebaseDecoded.email_verified === false) {
380
- return res.status(401).json({
381
- ok: false,
382
- code: "AUTH_EMAIL_NOT_VERIFIED",
383
- message: "Email not verified"
384
- });
385
- }
386
- req.auth = {
387
- tokenType: "backend",
388
- subject,
389
- firebase: firebaseDecoded,
390
- company_uid: company_uid ?? void 0,
391
- branch_uid: branch_uid ?? void 0,
392
- companies: [],
393
- roles: [],
394
- permissions: [],
395
- denied_permissions: []
396
- };
397
- return next();
398
- } catch {
399
- return res.status(401).json({
400
- ok: false,
401
- code: "AUTH_INVALID_TOKEN",
402
- message: "Invalid or expired token"
403
- });
404
- }
405
- }
406
- };
407
- }
408
-
409
- // src/auth/authentication.ts
410
- function deriveCompanyBranch(decoded, companyUid, branchUid) {
411
- const companiesFromToken = Array.isArray(decoded?.companies) ? decoded.companies : [];
412
- const company = decoded?.company ?? (companyUid ? companiesFromToken.find((c) => c?.uid === companyUid) : null) ?? null;
413
- const branch = decoded?.branch ?? (branchUid && company?.branches ? (company.branches || []).find((b) => b?.uid === branchUid) : null) ?? null;
414
- return {
415
- companiesFromToken,
416
- company,
417
- branch
418
- };
419
- }
420
- var authEmployeeRequired = createAuthMiddleware({
421
- subject: "employee",
422
- allowFirebaseIdToken: false,
423
- requireSubject: false,
424
- hydrate: async ({ decoded, company_uid, branch_uid }) => {
425
- const employee_uid = extractEmployeeUid(decoded) ?? normalizeUid(decoded?.employee?.uid);
426
- const { companiesFromToken, company, branch } = deriveCompanyBranch(
427
- decoded,
428
- company_uid,
429
- branch_uid
430
- );
431
- const employee = decoded?.employee && typeof decoded.employee === "object" ? decoded.employee : employee_uid ? { uid: employee_uid, email: decoded?.email ?? null } : void 0;
432
- return {
433
- employee_uid: employee_uid ?? void 0,
434
- employee,
435
- companies: companiesFromToken,
436
- company,
437
- branch,
438
- roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
439
- permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
440
- denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
441
- };
442
- }
443
- });
444
- var authCustomerRequired = createAuthMiddleware({
445
- subject: "customer",
446
- allowFirebaseIdToken: false,
447
- requireSubject: false,
448
- hydrate: async ({ decoded, company_uid, branch_uid }) => {
449
- const customer_uid = extractCustomerUid(decoded) ?? normalizeUid(decoded?.customer?.uid);
450
- const { companiesFromToken, company, branch } = deriveCompanyBranch(
451
- decoded,
452
- company_uid,
453
- branch_uid
454
- );
455
- const customer = decoded?.customer && typeof decoded.customer === "object" ? decoded.customer : customer_uid ? { uid: customer_uid } : void 0;
456
- return {
457
- customer_uid: customer_uid ?? void 0,
458
- customer,
459
- companies: companiesFromToken,
460
- company,
461
- branch,
462
- roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
463
- permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
464
- denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
465
- };
466
- }
467
- });
468
- var authEmployeeAllowFirebase = createAuthMiddleware({
469
- subject: "employee",
470
- allowFirebaseIdToken: true,
471
- requireSubject: false,
472
- hydrate: async ({ decoded, company_uid, branch_uid }) => {
473
- const employee_uid = extractEmployeeUid(decoded) ?? normalizeUid(decoded?.employee?.uid);
474
- const { companiesFromToken, company, branch } = deriveCompanyBranch(
475
- decoded,
476
- company_uid,
477
- branch_uid
478
- );
479
- const employee = decoded?.employee && typeof decoded.employee === "object" ? decoded.employee : employee_uid ? { uid: employee_uid, email: decoded?.email ?? null } : void 0;
480
- return {
481
- employee_uid: employee_uid ?? void 0,
482
- employee,
483
- companies: companiesFromToken,
484
- company,
485
- branch,
486
- roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
487
- permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
488
- denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
489
- };
490
- }
491
- });
492
- var authCustomerAllowFirebase = createAuthMiddleware({
493
- subject: "customer",
494
- allowFirebaseIdToken: true,
495
- requireSubject: false,
496
- hydrate: async ({ decoded, company_uid, branch_uid }) => {
497
- const customer_uid = extractCustomerUid(decoded) ?? normalizeUid(decoded?.customer?.uid);
498
- const { companiesFromToken, company, branch } = deriveCompanyBranch(
499
- decoded,
500
- company_uid,
501
- branch_uid
502
- );
503
- const customer = decoded?.customer && typeof decoded.customer === "object" ? decoded.customer : customer_uid ? { uid: customer_uid } : void 0;
504
- return {
505
- customer_uid: customer_uid ?? void 0,
506
- customer,
507
- companies: companiesFromToken,
508
- company,
509
- branch,
510
- roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
511
- permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
512
- denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
513
- };
514
- }
515
- });
516
-
517
- // src/middlewares/guards.ts
518
- function normalizeRole(r) {
519
- if (!r) return null;
520
- if (typeof r === "string") return r;
521
- return r.code || r.name || null;
522
- }
523
- function normalizePerm(p) {
524
- if (!p) return null;
525
- if (typeof p === "string") return p;
526
- return p.code || p.name || null;
527
- }
528
- function isSysAdmin2(roles) {
529
- if (!Array.isArray(roles)) return false;
530
- return roles.some((r) => normalizeRole(r) === "SYS_ADMIN");
531
- }
532
- function getAuth2(req) {
533
- return req.auth ?? {};
534
- }
535
- function permissionSets(auth) {
536
- const allow = new Set((auth.permissions ?? []).map(normalizePerm).filter(Boolean));
537
- const deny = new Set((auth.denied_permissions ?? []).map(normalizePerm).filter(Boolean));
538
- return { allow, deny };
539
- }
540
- function roleSet(auth) {
541
- return new Set((auth.roles ?? []).map(normalizeRole).filter(Boolean));
542
- }
543
- function allowSysAdminOrAnyPermission(...perms) {
544
- const required = (perms ?? []).filter(Boolean);
545
- return [
546
- parseHeaders,
547
- authEmployeeRequired,
548
- (req, res, next) => {
549
- const auth = getAuth2(req);
550
- if (isSysAdmin2(auth.roles)) return next();
551
- const { allow, deny } = permissionSets(auth);
552
- for (const p of required) {
553
- if (deny.has(p)) {
554
- return sendError(req, res, 403, "FORBIDDEN", `Denied permission: ${p}`, { denied: p });
555
- }
556
- }
557
- const ok = required.some((p) => allow.has(p));
558
- if (!ok) {
559
- return sendError(req, res, 403, "FORBIDDEN", "Missing permissions (ANY)", { required });
560
- }
561
- return next();
562
- }
563
- ];
564
- }
565
- function allowSysAdminOrPermissionsAll(...perms) {
566
- const required = (perms ?? []).filter(Boolean);
567
- return [
568
- parseHeaders,
569
- authEmployeeRequired,
570
- (req, res, next) => {
571
- const auth = getAuth2(req);
572
- if (isSysAdmin2(auth.roles)) return next();
573
- const { allow, deny } = permissionSets(auth);
574
- for (const p of required) {
575
- if (deny.has(p)) {
576
- return sendError(req, res, 403, "FORBIDDEN", `Denied permission: ${p}`, { denied: p });
577
- }
578
- }
579
- const missing = required.filter((p) => !allow.has(p));
580
- if (missing.length) {
581
- return sendError(req, res, 403, "FORBIDDEN", "Missing permissions (ALL)", { required, missing });
582
- }
583
- return next();
584
- }
585
- ];
586
- }
587
- function allowSysAdminOrRoles(...roles) {
588
- const required = (roles ?? []).filter(Boolean);
589
- return [
590
- parseHeaders,
591
- authEmployeeRequired,
592
- (req, res, next) => {
593
- const auth = getAuth2(req);
594
- if (isSysAdmin2(auth.roles)) return next();
595
- const have = roleSet(auth);
596
- const ok = required.some((r) => have.has(r));
597
- if (!ok) {
598
- return sendError(req, res, 403, "FORBIDDEN", "Role not allowed", { required });
599
- }
600
- return next();
601
- }
602
- ];
603
- }
604
- function allowSysAdminOrRolesOrAnyPermission(roles, permissions) {
605
- const requiredRoles = (Array.isArray(roles) ? roles : [roles]).filter(Boolean);
606
- const requiredPerms = (Array.isArray(permissions) ? permissions : [permissions]).filter(Boolean);
607
- return [
608
- parseHeaders,
609
- authEmployeeRequired,
610
- (req, res, next) => {
611
- const auth = getAuth2(req);
612
- if (isSysAdmin2(auth.roles)) return next();
613
- const { allow, deny } = permissionSets(auth);
614
- for (const p of requiredPerms) {
615
- if (deny.has(p)) {
616
- return sendError(req, res, 403, "FORBIDDEN", `Denied: ${p}`, { permission: p });
617
- }
618
- }
619
- const haveRoles = roleSet(auth);
620
- if (requiredRoles.some((r) => haveRoles.has(r))) return next();
621
- if (requiredPerms.some((p) => allow.has(p))) return next();
622
- return sendError(req, res, 403, "FORBIDDEN", "Permission denied", {
623
- roles: requiredRoles,
624
- permissions: requiredPerms,
625
- mode: "ROLES_OR_ANY_PERMISSION"
626
- });
627
- }
628
- ];
629
- }
630
- function allowAuthAdminOrPerm(permission) {
631
- return allowSysAdminOrRolesOrAnyPermission(["AUTH_ADMIN"], [permission]);
632
- }
633
-
634
- export {
635
- parseHeaders,
636
- requestId,
637
- sendOk,
638
- sendError,
639
- internalAuth,
640
- requireAuthContext,
641
- requirePermissions,
642
- requireAnyPermission,
643
- requireRoles,
644
- requireRolesOrAnyPermission,
645
- getBearerToken,
646
- normalizeUid,
647
- readRs256PublicKey,
648
- verifyBackendJwtRS256,
649
- extractEmployeeUid,
650
- extractCustomerUid,
651
- createAuthMiddleware,
652
- authEmployeeRequired,
653
- authCustomerRequired,
654
- authEmployeeAllowFirebase,
655
- authCustomerAllowFirebase,
656
- allowSysAdminOrAnyPermission,
657
- allowSysAdminOrPermissionsAll,
658
- allowSysAdminOrRoles,
659
- allowSysAdminOrRolesOrAnyPermission,
660
- allowAuthAdminOrPerm
661
- };
662
- //# sourceMappingURL=chunk-DT3AM34L.js.map