@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.
- package/dist/chunk-HNOUEVHW.js +410 -0
- package/dist/chunk-HNOUEVHW.js.map +1 -0
- package/dist/index.cjs +217 -276
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -10
- package/dist/index.d.ts +4 -10
- package/dist/index.js +192 -17
- package/dist/index.js.map +1 -1
- package/dist/middlewares/index.cjs +88 -329
- package/dist/middlewares/index.cjs.map +1 -1
- package/dist/middlewares/index.d.cts +34 -40
- package/dist/middlewares/index.d.ts +34 -40
- package/dist/middlewares/index.js +1 -1
- package/package.json +2 -2
- package/dist/chunk-DT3AM34L.js +0 -662
- package/dist/chunk-DT3AM34L.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -45,10 +45,6 @@ __export(src_exports, {
|
|
|
45
45
|
allowSysAdminOrPermissionsAll: () => allowSysAdminOrPermissionsAll,
|
|
46
46
|
allowSysAdminOrRoles: () => allowSysAdminOrRoles,
|
|
47
47
|
allowSysAdminOrRolesOrAnyPermission: () => allowSysAdminOrRolesOrAnyPermission,
|
|
48
|
-
authCustomerAllowFirebase: () => authCustomerAllowFirebase,
|
|
49
|
-
authCustomerRequired: () => authCustomerRequired,
|
|
50
|
-
authEmployeeAllowFirebase: () => authEmployeeAllowFirebase,
|
|
51
|
-
authEmployeeRequired: () => authEmployeeRequired,
|
|
52
48
|
buildInternalHeaders: () => buildInternalHeaders,
|
|
53
49
|
closeCache: () => closeCache,
|
|
54
50
|
createAuthMiddleware: () => createAuthMiddleware,
|
|
@@ -654,6 +650,9 @@ function internalAuth(req, res, next) {
|
|
|
654
650
|
function getAuth(req) {
|
|
655
651
|
return req.auth ?? {};
|
|
656
652
|
}
|
|
653
|
+
function hasAuthContext(req) {
|
|
654
|
+
return !!req.auth;
|
|
655
|
+
}
|
|
657
656
|
function normalizeCode(v) {
|
|
658
657
|
if (!v) return null;
|
|
659
658
|
if (typeof v === "string") return v;
|
|
@@ -678,7 +677,7 @@ function permsSet(list) {
|
|
|
678
677
|
}
|
|
679
678
|
function requireAuthContext() {
|
|
680
679
|
return (req, res, next) => {
|
|
681
|
-
if (!req
|
|
680
|
+
if (!hasAuthContext(req)) {
|
|
682
681
|
return sendError(req, res, 401, "UNAUTHORIZED", "Missing auth context");
|
|
683
682
|
}
|
|
684
683
|
return next();
|
|
@@ -692,6 +691,9 @@ function requirePermissions(perms, options) {
|
|
|
692
691
|
const sysAdminBypass = options?.sysAdminBypass !== false;
|
|
693
692
|
const sysAdminRole = options?.sysAdminRole || "SYS_ADMIN";
|
|
694
693
|
return (req, res, next) => {
|
|
694
|
+
if (!hasAuthContext(req)) {
|
|
695
|
+
return sendError(req, res, 401, "UNAUTHORIZED", "Missing auth context");
|
|
696
|
+
}
|
|
695
697
|
const auth = getAuth(req);
|
|
696
698
|
if (sysAdminBypass && isSysAdmin(auth, sysAdminRole)) return next();
|
|
697
699
|
const allow = permsSet(auth.permissions);
|
|
@@ -717,6 +719,9 @@ function requireAnyPermission(perms, options) {
|
|
|
717
719
|
const sysAdminBypass = options?.sysAdminBypass !== false;
|
|
718
720
|
const sysAdminRole = options?.sysAdminRole || "SYS_ADMIN";
|
|
719
721
|
return (req, res, next) => {
|
|
722
|
+
if (!hasAuthContext(req)) {
|
|
723
|
+
return sendError(req, res, 401, "UNAUTHORIZED", "Missing auth context");
|
|
724
|
+
}
|
|
720
725
|
const auth = getAuth(req);
|
|
721
726
|
if (sysAdminBypass && isSysAdmin(auth, sysAdminRole)) return next();
|
|
722
727
|
const allow = permsSet(auth.permissions);
|
|
@@ -742,6 +747,9 @@ function requireRoles(roles, options) {
|
|
|
742
747
|
const sysAdminBypass = options?.sysAdminBypass !== false;
|
|
743
748
|
const sysAdminRole = options?.sysAdminRole || "SYS_ADMIN";
|
|
744
749
|
return (req, res, next) => {
|
|
750
|
+
if (!hasAuthContext(req)) {
|
|
751
|
+
return sendError(req, res, 401, "UNAUTHORIZED", "Missing auth context");
|
|
752
|
+
}
|
|
745
753
|
const auth = getAuth(req);
|
|
746
754
|
if (sysAdminBypass && isSysAdmin(auth, sysAdminRole)) return next();
|
|
747
755
|
const have = rolesSet(auth);
|
|
@@ -758,6 +766,9 @@ function requireRolesOrAnyPermission(roles, perms, options) {
|
|
|
758
766
|
const sysAdminBypass = options?.sysAdminBypass !== false;
|
|
759
767
|
const sysAdminRole = options?.sysAdminRole || "SYS_ADMIN";
|
|
760
768
|
return (req, res, next) => {
|
|
769
|
+
if (!hasAuthContext(req)) {
|
|
770
|
+
return sendError(req, res, 401, "UNAUTHORIZED", "Missing auth context");
|
|
771
|
+
}
|
|
761
772
|
const auth = getAuth(req);
|
|
762
773
|
if (sysAdminBypass && isSysAdmin(auth, sysAdminRole)) return next();
|
|
763
774
|
const haveRoles = rolesSet(auth);
|
|
@@ -783,6 +794,157 @@ function requireRolesOrAnyPermission(roles, perms, options) {
|
|
|
783
794
|
};
|
|
784
795
|
}
|
|
785
796
|
|
|
797
|
+
// src/middlewares/guards.ts
|
|
798
|
+
function normalizeRole(r) {
|
|
799
|
+
if (!r) return null;
|
|
800
|
+
if (typeof r === "string") return r;
|
|
801
|
+
return r.code || r.name || null;
|
|
802
|
+
}
|
|
803
|
+
function normalizePerm(p) {
|
|
804
|
+
if (!p) return null;
|
|
805
|
+
if (typeof p === "string") return p;
|
|
806
|
+
return p.code || p.name || null;
|
|
807
|
+
}
|
|
808
|
+
function getAuth2(req) {
|
|
809
|
+
return req.auth ?? {};
|
|
810
|
+
}
|
|
811
|
+
function roleSet(auth) {
|
|
812
|
+
return new Set(
|
|
813
|
+
(auth.roles ?? []).map(normalizeRole).filter(Boolean)
|
|
814
|
+
);
|
|
815
|
+
}
|
|
816
|
+
function permissionSets(auth) {
|
|
817
|
+
const allow = new Set(
|
|
818
|
+
(auth.permissions ?? []).map(normalizePerm).filter(Boolean)
|
|
819
|
+
);
|
|
820
|
+
const deny = new Set(
|
|
821
|
+
(auth.denied_permissions ?? []).map(normalizePerm).filter(Boolean)
|
|
822
|
+
);
|
|
823
|
+
return { allow, deny };
|
|
824
|
+
}
|
|
825
|
+
function normalizeHandlers(auth) {
|
|
826
|
+
if (!auth) return [];
|
|
827
|
+
return Array.isArray(auth) ? auth : [auth];
|
|
828
|
+
}
|
|
829
|
+
function buildBaseChain(options) {
|
|
830
|
+
const chain = [];
|
|
831
|
+
if (options?.includeParseHeaders !== false) {
|
|
832
|
+
chain.push(parseHeaders);
|
|
833
|
+
}
|
|
834
|
+
chain.push(...normalizeHandlers(options?.auth));
|
|
835
|
+
return chain;
|
|
836
|
+
}
|
|
837
|
+
function hasSysAdmin(auth, options) {
|
|
838
|
+
const sysAdminBypass = options?.sysAdminBypass !== false;
|
|
839
|
+
if (!sysAdminBypass) return false;
|
|
840
|
+
const sysAdminRole = options?.sysAdminRole || "SYS_ADMIN";
|
|
841
|
+
return roleSet(auth).has(sysAdminRole);
|
|
842
|
+
}
|
|
843
|
+
function allowSysAdminOrAnyPermission(perms, options) {
|
|
844
|
+
const required = (Array.isArray(perms) ? perms : [perms]).filter(Boolean);
|
|
845
|
+
return [
|
|
846
|
+
...buildBaseChain(options),
|
|
847
|
+
(req, res, next) => {
|
|
848
|
+
const auth = getAuth2(req);
|
|
849
|
+
if (hasSysAdmin(auth, options)) return next();
|
|
850
|
+
const { allow, deny } = permissionSets(auth);
|
|
851
|
+
for (const p of required) {
|
|
852
|
+
if (deny.has(p)) {
|
|
853
|
+
return sendError(req, res, 403, "FORBIDDEN", `Denied permission: ${p}`, {
|
|
854
|
+
denied: p
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
const ok = required.some((p) => allow.has(p));
|
|
859
|
+
if (!ok) {
|
|
860
|
+
return sendError(req, res, 403, "FORBIDDEN", "Missing permissions (ANY)", {
|
|
861
|
+
required,
|
|
862
|
+
mode: "ANY"
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
return next();
|
|
866
|
+
}
|
|
867
|
+
];
|
|
868
|
+
}
|
|
869
|
+
function allowSysAdminOrPermissionsAll(perms, options) {
|
|
870
|
+
const required = (Array.isArray(perms) ? perms : [perms]).filter(Boolean);
|
|
871
|
+
return [
|
|
872
|
+
...buildBaseChain(options),
|
|
873
|
+
(req, res, next) => {
|
|
874
|
+
const auth = getAuth2(req);
|
|
875
|
+
if (hasSysAdmin(auth, options)) return next();
|
|
876
|
+
const { allow, deny } = permissionSets(auth);
|
|
877
|
+
for (const p of required) {
|
|
878
|
+
if (deny.has(p)) {
|
|
879
|
+
return sendError(req, res, 403, "FORBIDDEN", `Denied permission: ${p}`, {
|
|
880
|
+
denied: p
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
const missing = required.filter((p) => !allow.has(p));
|
|
885
|
+
if (missing.length) {
|
|
886
|
+
return sendError(req, res, 403, "FORBIDDEN", "Missing permissions (ALL)", {
|
|
887
|
+
required,
|
|
888
|
+
missing,
|
|
889
|
+
mode: "ALL"
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
return next();
|
|
893
|
+
}
|
|
894
|
+
];
|
|
895
|
+
}
|
|
896
|
+
function allowSysAdminOrRoles(roles, options) {
|
|
897
|
+
const required = (Array.isArray(roles) ? roles : [roles]).filter(Boolean);
|
|
898
|
+
return [
|
|
899
|
+
...buildBaseChain(options),
|
|
900
|
+
(req, res, next) => {
|
|
901
|
+
const auth = getAuth2(req);
|
|
902
|
+
if (hasSysAdmin(auth, options)) return next();
|
|
903
|
+
const have = roleSet(auth);
|
|
904
|
+
const ok = required.some((r) => have.has(r));
|
|
905
|
+
if (!ok) {
|
|
906
|
+
return sendError(req, res, 403, "FORBIDDEN", "Role not allowed", {
|
|
907
|
+
required,
|
|
908
|
+
mode: "ANY"
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
return next();
|
|
912
|
+
}
|
|
913
|
+
];
|
|
914
|
+
}
|
|
915
|
+
function allowSysAdminOrRolesOrAnyPermission(roles, permissions, options) {
|
|
916
|
+
const requiredRoles = (Array.isArray(roles) ? roles : [roles]).filter(Boolean);
|
|
917
|
+
const requiredPerms = (Array.isArray(permissions) ? permissions : [permissions]).filter(Boolean);
|
|
918
|
+
return [
|
|
919
|
+
...buildBaseChain(options),
|
|
920
|
+
(req, res, next) => {
|
|
921
|
+
const auth = getAuth2(req);
|
|
922
|
+
if (hasSysAdmin(auth, options)) return next();
|
|
923
|
+
const { allow, deny } = permissionSets(auth);
|
|
924
|
+
const haveRoles = roleSet(auth);
|
|
925
|
+
for (const p of requiredPerms) {
|
|
926
|
+
if (deny.has(p)) {
|
|
927
|
+
return sendError(req, res, 403, "FORBIDDEN", `Denied permission: ${p}`, {
|
|
928
|
+
denied: p
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
const okRole = requiredRoles.some((r) => haveRoles.has(r));
|
|
933
|
+
if (okRole) return next();
|
|
934
|
+
const okPerm = requiredPerms.some((p) => allow.has(p));
|
|
935
|
+
if (okPerm) return next();
|
|
936
|
+
return sendError(req, res, 403, "FORBIDDEN", "Permission denied", {
|
|
937
|
+
roles: requiredRoles,
|
|
938
|
+
permissions: requiredPerms,
|
|
939
|
+
mode: "ROLES_OR_ANY_PERMISSION"
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
];
|
|
943
|
+
}
|
|
944
|
+
function allowAuthAdminOrPerm(permission, options) {
|
|
945
|
+
return allowSysAdminOrRolesOrAnyPermission(["AUTH_ADMIN"], [permission], options);
|
|
946
|
+
}
|
|
947
|
+
|
|
786
948
|
// src/auth/jwt.ts
|
|
787
949
|
var import_fs2 = __toESM(require("fs"), 1);
|
|
788
950
|
var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1);
|
|
@@ -844,6 +1006,13 @@ function extractCustomerUid(decoded) {
|
|
|
844
1006
|
}
|
|
845
1007
|
|
|
846
1008
|
// src/auth/middleware.ts
|
|
1009
|
+
function sendAuthError(res, code, message, status = 401) {
|
|
1010
|
+
return res.status(status).json({
|
|
1011
|
+
ok: false,
|
|
1012
|
+
code,
|
|
1013
|
+
message
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
847
1016
|
function createAuthMiddleware(opts) {
|
|
848
1017
|
const {
|
|
849
1018
|
subject,
|
|
@@ -854,11 +1023,11 @@ function createAuthMiddleware(opts) {
|
|
|
854
1023
|
return async (req, res, next) => {
|
|
855
1024
|
const token = getBearerToken(req);
|
|
856
1025
|
if (!token) {
|
|
857
|
-
return
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
1026
|
+
return sendAuthError(
|
|
1027
|
+
res,
|
|
1028
|
+
"AUTH_MISSING_TOKEN",
|
|
1029
|
+
"Missing Authorization Bearer token"
|
|
1030
|
+
);
|
|
862
1031
|
}
|
|
863
1032
|
const headerCtx = req.context || {};
|
|
864
1033
|
const company_uid = normalizeUid(headerCtx.company_uid);
|
|
@@ -893,56 +1062,56 @@ function createAuthMiddleware(opts) {
|
|
|
893
1062
|
});
|
|
894
1063
|
Object.assign(baseCtx, hydrated);
|
|
895
1064
|
if (subject === "employee" && !baseCtx.employee_uid) {
|
|
896
|
-
return
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
1065
|
+
return sendAuthError(
|
|
1066
|
+
res,
|
|
1067
|
+
"AUTH_EMPLOYEE_UID_MISSING",
|
|
1068
|
+
"employee_uid missing in token/context (expected employee_uid or sub=emp:<uid>)"
|
|
1069
|
+
);
|
|
901
1070
|
}
|
|
902
1071
|
if (subject === "customer" && !baseCtx.customer_uid) {
|
|
903
|
-
return
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
1072
|
+
return sendAuthError(
|
|
1073
|
+
res,
|
|
1074
|
+
"AUTH_CUSTOMER_UID_MISSING",
|
|
1075
|
+
"customer_uid missing in token/context (expected customer_uid or sub=cus:<uid>)"
|
|
1076
|
+
);
|
|
908
1077
|
}
|
|
909
1078
|
if (requireSubject) {
|
|
910
1079
|
if (subject === "employee" && !baseCtx.employee) {
|
|
911
|
-
return
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
1080
|
+
return sendAuthError(
|
|
1081
|
+
res,
|
|
1082
|
+
"AUTH_EMPLOYEE_NOT_FOUND",
|
|
1083
|
+
"Employee not resolved by hydrator"
|
|
1084
|
+
);
|
|
916
1085
|
}
|
|
917
1086
|
if (subject === "customer" && !baseCtx.customer) {
|
|
918
|
-
return
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
1087
|
+
return sendAuthError(
|
|
1088
|
+
res,
|
|
1089
|
+
"AUTH_CUSTOMER_NOT_FOUND",
|
|
1090
|
+
"Customer not resolved by hydrator"
|
|
1091
|
+
);
|
|
923
1092
|
}
|
|
924
1093
|
}
|
|
925
1094
|
req.auth = baseCtx;
|
|
926
1095
|
return next();
|
|
927
|
-
} catch {
|
|
1096
|
+
} catch (backendErr) {
|
|
928
1097
|
if (!allowFirebaseIdToken) {
|
|
929
|
-
return
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
1098
|
+
return sendAuthError(
|
|
1099
|
+
res,
|
|
1100
|
+
"AUTH_INVALID_TOKEN",
|
|
1101
|
+
"Invalid or expired token"
|
|
1102
|
+
);
|
|
934
1103
|
}
|
|
935
1104
|
try {
|
|
936
1105
|
const { default: admin } = await import("firebase-admin");
|
|
937
1106
|
const firebaseDecoded = await admin.auth().verifyIdToken(token);
|
|
938
1107
|
if (firebaseDecoded.email && firebaseDecoded.email_verified === false) {
|
|
939
|
-
return
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
1108
|
+
return sendAuthError(
|
|
1109
|
+
res,
|
|
1110
|
+
"AUTH_EMAIL_NOT_VERIFIED",
|
|
1111
|
+
"Email not verified"
|
|
1112
|
+
);
|
|
944
1113
|
}
|
|
945
|
-
|
|
1114
|
+
const firebaseCtx = {
|
|
946
1115
|
tokenType: "backend",
|
|
947
1116
|
subject,
|
|
948
1117
|
firebase: firebaseDecoded,
|
|
@@ -953,243 +1122,19 @@ function createAuthMiddleware(opts) {
|
|
|
953
1122
|
permissions: [],
|
|
954
1123
|
denied_permissions: []
|
|
955
1124
|
};
|
|
1125
|
+
req.auth = firebaseCtx;
|
|
956
1126
|
return next();
|
|
957
1127
|
} catch {
|
|
958
|
-
return
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
1128
|
+
return sendAuthError(
|
|
1129
|
+
res,
|
|
1130
|
+
"AUTH_INVALID_TOKEN",
|
|
1131
|
+
"Invalid or expired token"
|
|
1132
|
+
);
|
|
963
1133
|
}
|
|
964
1134
|
}
|
|
965
1135
|
};
|
|
966
1136
|
}
|
|
967
1137
|
|
|
968
|
-
// src/auth/authentication.ts
|
|
969
|
-
function deriveCompanyBranch(decoded, companyUid, branchUid) {
|
|
970
|
-
const companiesFromToken = Array.isArray(decoded?.companies) ? decoded.companies : [];
|
|
971
|
-
const company = decoded?.company ?? (companyUid ? companiesFromToken.find((c) => c?.uid === companyUid) : null) ?? null;
|
|
972
|
-
const branch = decoded?.branch ?? (branchUid && company?.branches ? (company.branches || []).find((b) => b?.uid === branchUid) : null) ?? null;
|
|
973
|
-
return {
|
|
974
|
-
companiesFromToken,
|
|
975
|
-
company,
|
|
976
|
-
branch
|
|
977
|
-
};
|
|
978
|
-
}
|
|
979
|
-
var authEmployeeRequired = createAuthMiddleware({
|
|
980
|
-
subject: "employee",
|
|
981
|
-
allowFirebaseIdToken: false,
|
|
982
|
-
requireSubject: false,
|
|
983
|
-
hydrate: async ({ decoded, company_uid, branch_uid }) => {
|
|
984
|
-
const employee_uid = extractEmployeeUid(decoded) ?? normalizeUid(decoded?.employee?.uid);
|
|
985
|
-
const { companiesFromToken, company, branch } = deriveCompanyBranch(
|
|
986
|
-
decoded,
|
|
987
|
-
company_uid,
|
|
988
|
-
branch_uid
|
|
989
|
-
);
|
|
990
|
-
const employee = decoded?.employee && typeof decoded.employee === "object" ? decoded.employee : employee_uid ? { uid: employee_uid, email: decoded?.email ?? null } : void 0;
|
|
991
|
-
return {
|
|
992
|
-
employee_uid: employee_uid ?? void 0,
|
|
993
|
-
employee,
|
|
994
|
-
companies: companiesFromToken,
|
|
995
|
-
company,
|
|
996
|
-
branch,
|
|
997
|
-
roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
|
|
998
|
-
permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
|
|
999
|
-
denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
|
|
1000
|
-
};
|
|
1001
|
-
}
|
|
1002
|
-
});
|
|
1003
|
-
var authCustomerRequired = createAuthMiddleware({
|
|
1004
|
-
subject: "customer",
|
|
1005
|
-
allowFirebaseIdToken: false,
|
|
1006
|
-
requireSubject: false,
|
|
1007
|
-
hydrate: async ({ decoded, company_uid, branch_uid }) => {
|
|
1008
|
-
const customer_uid = extractCustomerUid(decoded) ?? normalizeUid(decoded?.customer?.uid);
|
|
1009
|
-
const { companiesFromToken, company, branch } = deriveCompanyBranch(
|
|
1010
|
-
decoded,
|
|
1011
|
-
company_uid,
|
|
1012
|
-
branch_uid
|
|
1013
|
-
);
|
|
1014
|
-
const customer = decoded?.customer && typeof decoded.customer === "object" ? decoded.customer : customer_uid ? { uid: customer_uid } : void 0;
|
|
1015
|
-
return {
|
|
1016
|
-
customer_uid: customer_uid ?? void 0,
|
|
1017
|
-
customer,
|
|
1018
|
-
companies: companiesFromToken,
|
|
1019
|
-
company,
|
|
1020
|
-
branch,
|
|
1021
|
-
roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
|
|
1022
|
-
permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
|
|
1023
|
-
denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
|
|
1024
|
-
};
|
|
1025
|
-
}
|
|
1026
|
-
});
|
|
1027
|
-
var authEmployeeAllowFirebase = createAuthMiddleware({
|
|
1028
|
-
subject: "employee",
|
|
1029
|
-
allowFirebaseIdToken: true,
|
|
1030
|
-
requireSubject: false,
|
|
1031
|
-
hydrate: async ({ decoded, company_uid, branch_uid }) => {
|
|
1032
|
-
const employee_uid = extractEmployeeUid(decoded) ?? normalizeUid(decoded?.employee?.uid);
|
|
1033
|
-
const { companiesFromToken, company, branch } = deriveCompanyBranch(
|
|
1034
|
-
decoded,
|
|
1035
|
-
company_uid,
|
|
1036
|
-
branch_uid
|
|
1037
|
-
);
|
|
1038
|
-
const employee = decoded?.employee && typeof decoded.employee === "object" ? decoded.employee : employee_uid ? { uid: employee_uid, email: decoded?.email ?? null } : void 0;
|
|
1039
|
-
return {
|
|
1040
|
-
employee_uid: employee_uid ?? void 0,
|
|
1041
|
-
employee,
|
|
1042
|
-
companies: companiesFromToken,
|
|
1043
|
-
company,
|
|
1044
|
-
branch,
|
|
1045
|
-
roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
|
|
1046
|
-
permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
|
|
1047
|
-
denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
|
|
1048
|
-
};
|
|
1049
|
-
}
|
|
1050
|
-
});
|
|
1051
|
-
var authCustomerAllowFirebase = createAuthMiddleware({
|
|
1052
|
-
subject: "customer",
|
|
1053
|
-
allowFirebaseIdToken: true,
|
|
1054
|
-
requireSubject: false,
|
|
1055
|
-
hydrate: async ({ decoded, company_uid, branch_uid }) => {
|
|
1056
|
-
const customer_uid = extractCustomerUid(decoded) ?? normalizeUid(decoded?.customer?.uid);
|
|
1057
|
-
const { companiesFromToken, company, branch } = deriveCompanyBranch(
|
|
1058
|
-
decoded,
|
|
1059
|
-
company_uid,
|
|
1060
|
-
branch_uid
|
|
1061
|
-
);
|
|
1062
|
-
const customer = decoded?.customer && typeof decoded.customer === "object" ? decoded.customer : customer_uid ? { uid: customer_uid } : void 0;
|
|
1063
|
-
return {
|
|
1064
|
-
customer_uid: customer_uid ?? void 0,
|
|
1065
|
-
customer,
|
|
1066
|
-
companies: companiesFromToken,
|
|
1067
|
-
company,
|
|
1068
|
-
branch,
|
|
1069
|
-
roles: Array.isArray(decoded?.roles) ? decoded.roles : [],
|
|
1070
|
-
permissions: Array.isArray(decoded?.permissions) ? decoded.permissions : [],
|
|
1071
|
-
denied_permissions: Array.isArray(decoded?.denied_permissions) ? decoded.denied_permissions : []
|
|
1072
|
-
};
|
|
1073
|
-
}
|
|
1074
|
-
});
|
|
1075
|
-
|
|
1076
|
-
// src/middlewares/guards.ts
|
|
1077
|
-
function normalizeRole(r) {
|
|
1078
|
-
if (!r) return null;
|
|
1079
|
-
if (typeof r === "string") return r;
|
|
1080
|
-
return r.code || r.name || null;
|
|
1081
|
-
}
|
|
1082
|
-
function normalizePerm(p) {
|
|
1083
|
-
if (!p) return null;
|
|
1084
|
-
if (typeof p === "string") return p;
|
|
1085
|
-
return p.code || p.name || null;
|
|
1086
|
-
}
|
|
1087
|
-
function isSysAdmin2(roles) {
|
|
1088
|
-
if (!Array.isArray(roles)) return false;
|
|
1089
|
-
return roles.some((r) => normalizeRole(r) === "SYS_ADMIN");
|
|
1090
|
-
}
|
|
1091
|
-
function getAuth2(req) {
|
|
1092
|
-
return req.auth ?? {};
|
|
1093
|
-
}
|
|
1094
|
-
function permissionSets(auth) {
|
|
1095
|
-
const allow = new Set((auth.permissions ?? []).map(normalizePerm).filter(Boolean));
|
|
1096
|
-
const deny = new Set((auth.denied_permissions ?? []).map(normalizePerm).filter(Boolean));
|
|
1097
|
-
return { allow, deny };
|
|
1098
|
-
}
|
|
1099
|
-
function roleSet(auth) {
|
|
1100
|
-
return new Set((auth.roles ?? []).map(normalizeRole).filter(Boolean));
|
|
1101
|
-
}
|
|
1102
|
-
function allowSysAdminOrAnyPermission(...perms) {
|
|
1103
|
-
const required = (perms ?? []).filter(Boolean);
|
|
1104
|
-
return [
|
|
1105
|
-
parseHeaders,
|
|
1106
|
-
authEmployeeRequired,
|
|
1107
|
-
(req, res, next) => {
|
|
1108
|
-
const auth = getAuth2(req);
|
|
1109
|
-
if (isSysAdmin2(auth.roles)) return next();
|
|
1110
|
-
const { allow, deny } = permissionSets(auth);
|
|
1111
|
-
for (const p of required) {
|
|
1112
|
-
if (deny.has(p)) {
|
|
1113
|
-
return sendError(req, res, 403, "FORBIDDEN", `Denied permission: ${p}`, { denied: p });
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
const ok = required.some((p) => allow.has(p));
|
|
1117
|
-
if (!ok) {
|
|
1118
|
-
return sendError(req, res, 403, "FORBIDDEN", "Missing permissions (ANY)", { required });
|
|
1119
|
-
}
|
|
1120
|
-
return next();
|
|
1121
|
-
}
|
|
1122
|
-
];
|
|
1123
|
-
}
|
|
1124
|
-
function allowSysAdminOrPermissionsAll(...perms) {
|
|
1125
|
-
const required = (perms ?? []).filter(Boolean);
|
|
1126
|
-
return [
|
|
1127
|
-
parseHeaders,
|
|
1128
|
-
authEmployeeRequired,
|
|
1129
|
-
(req, res, next) => {
|
|
1130
|
-
const auth = getAuth2(req);
|
|
1131
|
-
if (isSysAdmin2(auth.roles)) return next();
|
|
1132
|
-
const { allow, deny } = permissionSets(auth);
|
|
1133
|
-
for (const p of required) {
|
|
1134
|
-
if (deny.has(p)) {
|
|
1135
|
-
return sendError(req, res, 403, "FORBIDDEN", `Denied permission: ${p}`, { denied: p });
|
|
1136
|
-
}
|
|
1137
|
-
}
|
|
1138
|
-
const missing = required.filter((p) => !allow.has(p));
|
|
1139
|
-
if (missing.length) {
|
|
1140
|
-
return sendError(req, res, 403, "FORBIDDEN", "Missing permissions (ALL)", { required, missing });
|
|
1141
|
-
}
|
|
1142
|
-
return next();
|
|
1143
|
-
}
|
|
1144
|
-
];
|
|
1145
|
-
}
|
|
1146
|
-
function allowSysAdminOrRoles(...roles) {
|
|
1147
|
-
const required = (roles ?? []).filter(Boolean);
|
|
1148
|
-
return [
|
|
1149
|
-
parseHeaders,
|
|
1150
|
-
authEmployeeRequired,
|
|
1151
|
-
(req, res, next) => {
|
|
1152
|
-
const auth = getAuth2(req);
|
|
1153
|
-
if (isSysAdmin2(auth.roles)) return next();
|
|
1154
|
-
const have = roleSet(auth);
|
|
1155
|
-
const ok = required.some((r) => have.has(r));
|
|
1156
|
-
if (!ok) {
|
|
1157
|
-
return sendError(req, res, 403, "FORBIDDEN", "Role not allowed", { required });
|
|
1158
|
-
}
|
|
1159
|
-
return next();
|
|
1160
|
-
}
|
|
1161
|
-
];
|
|
1162
|
-
}
|
|
1163
|
-
function allowSysAdminOrRolesOrAnyPermission(roles, permissions) {
|
|
1164
|
-
const requiredRoles = (Array.isArray(roles) ? roles : [roles]).filter(Boolean);
|
|
1165
|
-
const requiredPerms = (Array.isArray(permissions) ? permissions : [permissions]).filter(Boolean);
|
|
1166
|
-
return [
|
|
1167
|
-
parseHeaders,
|
|
1168
|
-
authEmployeeRequired,
|
|
1169
|
-
(req, res, next) => {
|
|
1170
|
-
const auth = getAuth2(req);
|
|
1171
|
-
if (isSysAdmin2(auth.roles)) return next();
|
|
1172
|
-
const { allow, deny } = permissionSets(auth);
|
|
1173
|
-
for (const p of requiredPerms) {
|
|
1174
|
-
if (deny.has(p)) {
|
|
1175
|
-
return sendError(req, res, 403, "FORBIDDEN", `Denied: ${p}`, { permission: p });
|
|
1176
|
-
}
|
|
1177
|
-
}
|
|
1178
|
-
const haveRoles = roleSet(auth);
|
|
1179
|
-
if (requiredRoles.some((r) => haveRoles.has(r))) return next();
|
|
1180
|
-
if (requiredPerms.some((p) => allow.has(p))) return next();
|
|
1181
|
-
return sendError(req, res, 403, "FORBIDDEN", "Permission denied", {
|
|
1182
|
-
roles: requiredRoles,
|
|
1183
|
-
permissions: requiredPerms,
|
|
1184
|
-
mode: "ROLES_OR_ANY_PERMISSION"
|
|
1185
|
-
});
|
|
1186
|
-
}
|
|
1187
|
-
];
|
|
1188
|
-
}
|
|
1189
|
-
function allowAuthAdminOrPerm(permission) {
|
|
1190
|
-
return allowSysAdminOrRolesOrAnyPermission(["AUTH_ADMIN"], [permission]);
|
|
1191
|
-
}
|
|
1192
|
-
|
|
1193
1138
|
// src/internalHttpClient.ts
|
|
1194
1139
|
var import_fs3 = __toESM(require("fs"), 1);
|
|
1195
1140
|
var InternalHttpError = class extends Error {
|
|
@@ -1890,10 +1835,6 @@ function isUid(value) {
|
|
|
1890
1835
|
allowSysAdminOrPermissionsAll,
|
|
1891
1836
|
allowSysAdminOrRoles,
|
|
1892
1837
|
allowSysAdminOrRolesOrAnyPermission,
|
|
1893
|
-
authCustomerAllowFirebase,
|
|
1894
|
-
authCustomerRequired,
|
|
1895
|
-
authEmployeeAllowFirebase,
|
|
1896
|
-
authEmployeeRequired,
|
|
1897
1838
|
buildInternalHeaders,
|
|
1898
1839
|
closeCache,
|
|
1899
1840
|
createAuthMiddleware,
|