emilsoftware-utilities 1.6.10 → 1.7.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.
@@ -3,3 +3,4 @@ import { AccessiOptions } from "./AccessiModule";
3
3
  export declare function initializeAccessiModule(app: Application, options: AccessiOptions): Promise<void>;
4
4
  export { AccessiModule } from "./AccessiModule";
5
5
  export * from "./Dtos";
6
+ export { authorizeAccessi, authenticateGen, setAccessiAuthOptions } from "./middleware/authenticateGen";
@@ -23,17 +23,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
23
23
  });
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.AccessiModule = void 0;
26
+ exports.setAccessiAuthOptions = exports.authenticateGen = exports.authorizeAccessi = exports.AccessiModule = void 0;
27
27
  exports.initializeAccessiModule = initializeAccessiModule;
28
28
  const core_1 = require("@nestjs/core");
29
29
  const platform_express_1 = require("@nestjs/platform-express");
30
30
  const AccessiModule_1 = require("./AccessiModule");
31
31
  const Logger_1 = require("../Logger");
32
+ const authenticateGen_1 = require("./middleware/authenticateGen");
32
33
  function initializeAccessiModule(app, options) {
33
34
  return __awaiter(this, void 0, void 0, function* () {
34
35
  const logger = new Logger_1.Logger("initializeAccessiModule");
35
36
  console.log("Accessi initialized");
36
37
  try {
38
+ (0, authenticateGen_1.setAccessiAuthOptions)(options);
37
39
  // Creiamo un'istanza Express separata per NestJS
38
40
  const nestExpressInstance = new platform_express_1.ExpressAdapter(app);
39
41
  // Creiamo l'app NestJS attaccata a Express
@@ -56,4 +58,8 @@ function initializeAccessiModule(app, options) {
56
58
  var AccessiModule_2 = require("./AccessiModule");
57
59
  Object.defineProperty(exports, "AccessiModule", { enumerable: true, get: function () { return AccessiModule_2.AccessiModule; } });
58
60
  __exportStar(require("./Dtos"), exports);
61
+ var authenticateGen_2 = require("./middleware/authenticateGen");
62
+ Object.defineProperty(exports, "authorizeAccessi", { enumerable: true, get: function () { return authenticateGen_2.authorizeAccessi; } });
63
+ Object.defineProperty(exports, "authenticateGen", { enumerable: true, get: function () { return authenticateGen_2.authenticateGen; } });
64
+ Object.defineProperty(exports, "setAccessiAuthOptions", { enumerable: true, get: function () { return authenticateGen_2.setAccessiAuthOptions; } });
59
65
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/accessi-module/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,0DA0BC;AA/BD,uCAA2C;AAC3C,+DAA0D;AAC1D,mDAAgE;AAChE,sCAAmC;AAEnC,SAAsB,uBAAuB,CAAC,GAAgB,EAAE,OAAuB;;QACnF,MAAM,MAAM,GAAW,IAAI,eAAM,CAAC,yBAAyB,CAAC,CAAC;QAE7D,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;QACnC,IAAI,CAAC;YACD,iDAAiD;YACjD,MAAM,mBAAmB,GAAG,IAAI,iCAAc,CAAC,GAAG,CAAC,CAAC;YAEpD,2CAA2C;YAC3C,MAAM,OAAO,GAAG,MAAM,kBAAW,CAAC,MAAM,CAAC,6BAAa,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,mBAAmB,EAAE;gBAC1F,UAAU,EAAE,IAAI;aACnB,CAAC,CAAC;YAEH,OAAO,CAAC,UAAU,EAAE,CAAC;YAErB,OAAO,CAAC,eAAe,CAAC,KAAK,EAAE;gBAC3B,OAAO,EAAE,CAAC,UAAU,EAAE,eAAe,CAAC;aACzC,CAAC,CAAC;YAEH,2DAA2D;YAC3D,MAAM,OAAO,CAAC,IAAI,EAAE,CAAC;QAEzB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,MAAM,CAAC,KAAK,CAAC,qCAAqC,EAAE,KAAK,CAAC,CAAC;YAC3D,MAAM,KAAK,CAAC;QAChB,CAAC;IACL,CAAC;CAAA;AAED,iDAAgD;AAAvC,8GAAA,aAAa,OAAA;AACtB,yCAAuB","sourcesContent":["import { Application } from \"express\";\nimport { NestFactory } from \"@nestjs/core\";\nimport { ExpressAdapter } from \"@nestjs/platform-express\";\nimport { AccessiModule, AccessiOptions } from \"./AccessiModule\";\nimport { Logger } from \"../Logger\";\n\nexport async function initializeAccessiModule(app: Application, options: AccessiOptions) {\n const logger: Logger = new Logger(\"initializeAccessiModule\");\n\n console.log(\"Accessi initialized\");\n try {\n // Creiamo un'istanza Express separata per NestJS\n const nestExpressInstance = new ExpressAdapter(app);\n\n // Creiamo l'app NestJS attaccata a Express\n const nestApp = await NestFactory.create(AccessiModule.forRoot(options), nestExpressInstance, {\n bufferLogs: true\n });\n\n nestApp.enableCors();\n\n nestApp.setGlobalPrefix('api', {\n exclude: ['/swagger', '/swagger/(.*)']\n });\n\n // Note: Swagger setup is now handled by the unified module\n await nestApp.init();\n\n } catch (error) {\n logger.error(\"Errore in initialize AccessiModule:\", error);\n throw error;\n }\n}\n\nexport { AccessiModule } from \"./AccessiModule\";\nexport * from \"./Dtos\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/accessi-module/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,0DA4BC;AAlCD,uCAA2C;AAC3C,+DAA0D;AAC1D,mDAAgE;AAChE,sCAAmC;AACnC,kEAAqE;AAErE,SAAsB,uBAAuB,CAAC,GAAgB,EAAE,OAAuB;;QACnF,MAAM,MAAM,GAAW,IAAI,eAAM,CAAC,yBAAyB,CAAC,CAAC;QAE7D,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;QACnC,IAAI,CAAC;YACD,IAAA,uCAAqB,EAAC,OAAO,CAAC,CAAC;YAE/B,iDAAiD;YACjD,MAAM,mBAAmB,GAAG,IAAI,iCAAc,CAAC,GAAG,CAAC,CAAC;YAEpD,2CAA2C;YAC3C,MAAM,OAAO,GAAG,MAAM,kBAAW,CAAC,MAAM,CAAC,6BAAa,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,mBAAmB,EAAE;gBAC1F,UAAU,EAAE,IAAI;aACnB,CAAC,CAAC;YAEH,OAAO,CAAC,UAAU,EAAE,CAAC;YAErB,OAAO,CAAC,eAAe,CAAC,KAAK,EAAE;gBAC3B,OAAO,EAAE,CAAC,UAAU,EAAE,eAAe,CAAC;aACzC,CAAC,CAAC;YAEH,2DAA2D;YAC3D,MAAM,OAAO,CAAC,IAAI,EAAE,CAAC;QAEzB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,MAAM,CAAC,KAAK,CAAC,qCAAqC,EAAE,KAAK,CAAC,CAAC;YAC3D,MAAM,KAAK,CAAC;QAChB,CAAC;IACL,CAAC;CAAA;AAED,iDAAgD;AAAvC,8GAAA,aAAa,OAAA;AACtB,yCAAuB;AACvB,gEAAwG;AAA/F,mHAAA,gBAAgB,OAAA;AAAE,kHAAA,eAAe,OAAA;AAAE,wHAAA,qBAAqB,OAAA","sourcesContent":["import { Application } from \"express\";\nimport { NestFactory } from \"@nestjs/core\";\nimport { ExpressAdapter } from \"@nestjs/platform-express\";\nimport { AccessiModule, AccessiOptions } from \"./AccessiModule\";\nimport { Logger } from \"../Logger\";\nimport { setAccessiAuthOptions } from \"./middleware/authenticateGen\";\n\nexport async function initializeAccessiModule(app: Application, options: AccessiOptions) {\n const logger: Logger = new Logger(\"initializeAccessiModule\");\n\n console.log(\"Accessi initialized\");\n try {\n setAccessiAuthOptions(options);\n\n // Creiamo un'istanza Express separata per NestJS\n const nestExpressInstance = new ExpressAdapter(app);\n\n // Creiamo l'app NestJS attaccata a Express\n const nestApp = await NestFactory.create(AccessiModule.forRoot(options), nestExpressInstance, {\n bufferLogs: true\n });\n\n nestApp.enableCors();\n\n nestApp.setGlobalPrefix('api', {\n exclude: ['/swagger', '/swagger/(.*)']\n });\n\n // Note: Swagger setup is now handled by the unified module\n await nestApp.init();\n\n } catch (error) {\n logger.error(\"Errore in initialize AccessiModule:\", error);\n throw error;\n }\n}\n\nexport { AccessiModule } from \"./AccessiModule\";\nexport * from \"./Dtos\";\nexport { authorizeAccessi, authenticateGen, setAccessiAuthOptions } from \"./middleware/authenticateGen\";\n"]}
@@ -0,0 +1,12 @@
1
+ import { NextFunction, Request, Response } from "express";
2
+ import { AccessiOptions } from "../AccessiModule";
3
+ export type AccessiAuthorizationOptions = {
4
+ requisiti: {
5
+ codiceMenu: string;
6
+ tipoAbilitazione: number;
7
+ }[];
8
+ tipoControllo?: "AND" | "OR";
9
+ };
10
+ export declare function setAccessiAuthOptions(options: AccessiOptions): void;
11
+ export declare function authorizeAccessi(req: Request, res: Response, next: NextFunction, options?: AccessiAuthorizationOptions): Promise<void | Response<any, Record<string, any>>>;
12
+ export declare const authenticateGen: typeof authorizeAccessi;
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
42
+ });
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.authenticateGen = void 0;
46
+ exports.setAccessiAuthOptions = setAccessiAuthOptions;
47
+ exports.authorizeAccessi = authorizeAccessi;
48
+ const jwt = __importStar(require("jsonwebtoken"));
49
+ const PermissionService_1 = require("../Services/PermissionService/PermissionService");
50
+ let accessiOptionsRef = null;
51
+ function setAccessiAuthOptions(options) {
52
+ accessiOptionsRef = options;
53
+ }
54
+ function resolveCodiceUtente(decoded) {
55
+ var _a, _b, _c, _d, _e;
56
+ return ((_e = (_c = (_b = (_a = decoded === null || decoded === void 0 ? void 0 : decoded.userData) === null || _a === void 0 ? void 0 : _a.utente) === null || _b === void 0 ? void 0 : _b.codiceUtente) !== null && _c !== void 0 ? _c : (_d = decoded === null || decoded === void 0 ? void 0 : decoded.utente) === null || _d === void 0 ? void 0 : _d.codiceUtente) !== null && _e !== void 0 ? _e : decoded === null || decoded === void 0 ? void 0 : decoded.codiceUtente);
57
+ }
58
+ function authorizeAccessi(req, res, next, options) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ var _a, _b, _c, _d, _e;
61
+ try {
62
+ const authHeader = req.headers.authorization;
63
+ if (!authHeader)
64
+ return res.sendStatus(401);
65
+ const token = authHeader.split(" ")[1];
66
+ if (!token)
67
+ return res.sendStatus(401);
68
+ const secret = (_b = (_a = accessiOptionsRef === null || accessiOptionsRef === void 0 ? void 0 : accessiOptionsRef.jwtOptions) === null || _a === void 0 ? void 0 : _a.secret) !== null && _b !== void 0 ? _b : process.env.ACC_JWT_SECRET;
69
+ if (!secret)
70
+ return res.sendStatus(500);
71
+ let decoded;
72
+ try {
73
+ decoded = jwt.verify(token, secret);
74
+ }
75
+ catch (error) {
76
+ return res.sendStatus(401);
77
+ }
78
+ const codiceUtente = resolveCodiceUtente(decoded);
79
+ if (!codiceUtente)
80
+ return res.sendStatus(401);
81
+ const requisiti = (_c = options === null || options === void 0 ? void 0 : options.requisiti) !== null && _c !== void 0 ? _c : [];
82
+ if (requisiti.length > 0) {
83
+ if (!(accessiOptionsRef === null || accessiOptionsRef === void 0 ? void 0 : accessiOptionsRef.databaseOptions))
84
+ return res.sendStatus(500);
85
+ const permissionService = new PermissionService_1.PermissionService(accessiOptionsRef);
86
+ const grantsResult = yield permissionService.getUserRolesAndGrants(codiceUtente);
87
+ const grants = (_d = grantsResult.grants) !== null && _d !== void 0 ? _d : [];
88
+ const hasMenu = (codiceMenu, tipoAbilitazione) => grants.some((g) => {
89
+ var _a;
90
+ return g.codiceMenu == codiceMenu &&
91
+ Number((_a = g.tipoAbilitazione) !== null && _a !== void 0 ? _a : 0) >= tipoAbilitazione;
92
+ });
93
+ const requireAll = ((_e = options === null || options === void 0 ? void 0 : options.tipoControllo) !== null && _e !== void 0 ? _e : "AND") === "AND";
94
+ const hasAbil = requireAll
95
+ ? requisiti.every((r) => hasMenu(r.codiceMenu, r.tipoAbilitazione))
96
+ : requisiti.some((r) => hasMenu(r.codiceMenu, r.tipoAbilitazione));
97
+ if (!hasAbil)
98
+ return res.sendStatus(401);
99
+ req.userGrants = grantsResult;
100
+ }
101
+ req.data = decoded;
102
+ return next();
103
+ }
104
+ catch (error) {
105
+ return res.sendStatus(401);
106
+ }
107
+ });
108
+ }
109
+ exports.authenticateGen = authorizeAccessi;
110
+ //# sourceMappingURL=authenticateGen.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authenticateGen.js","sourceRoot":"","sources":["../../../src/accessi-module/middleware/authenticateGen.ts"],"names":[],"mappings":"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","sourcesContent":["import { NextFunction, Request, Response } from \"express\";\nimport * as jwt from \"jsonwebtoken\";\nimport { AccessiOptions } from \"../AccessiModule\";\nimport { PermissionService } from \"../Services/PermissionService/PermissionService\";\n\nexport type AccessiAuthorizationOptions = {\n requisiti: { codiceMenu: string; tipoAbilitazione: number }[];\n tipoControllo?: \"AND\" | \"OR\";\n};\n\nlet accessiOptionsRef: AccessiOptions | null = null;\n\nexport function setAccessiAuthOptions(options: AccessiOptions) {\n accessiOptionsRef = options;\n}\n\nfunction resolveCodiceUtente(decoded: any): number | undefined {\n return (\n decoded?.userData?.utente?.codiceUtente ??\n decoded?.utente?.codiceUtente ??\n decoded?.codiceUtente\n );\n}\n\nexport async function authorizeAccessi(\n req: Request,\n res: Response,\n next: NextFunction,\n options?: AccessiAuthorizationOptions\n) {\n try {\n const authHeader = req.headers.authorization;\n if (!authHeader) return res.sendStatus(401);\n\n const token = authHeader.split(\" \")[1];\n if (!token) return res.sendStatus(401);\n\n const secret =\n accessiOptionsRef?.jwtOptions?.secret ?? process.env.ACC_JWT_SECRET;\n if (!secret) return res.sendStatus(500);\n\n let decoded: any;\n try {\n decoded = jwt.verify(token, secret);\n } catch (error) {\n return res.sendStatus(401);\n }\n\n const codiceUtente = resolveCodiceUtente(decoded);\n if (!codiceUtente) return res.sendStatus(401);\n\n const requisiti = options?.requisiti ?? [];\n if (requisiti.length > 0) {\n if (!accessiOptionsRef?.databaseOptions) return res.sendStatus(500);\n const permissionService = new PermissionService(accessiOptionsRef);\n const grantsResult = await permissionService.getUserRolesAndGrants(\n codiceUtente\n );\n\n const grants = grantsResult.grants ?? [];\n const hasMenu = (codiceMenu: string, tipoAbilitazione: number) =>\n grants.some(\n (g) =>\n g.codiceMenu == codiceMenu &&\n Number(g.tipoAbilitazione ?? 0) >= tipoAbilitazione\n );\n const requireAll = (options?.tipoControllo ?? \"AND\") === \"AND\";\n const hasAbil = requireAll\n ? requisiti.every((r) => hasMenu(r.codiceMenu, r.tipoAbilitazione))\n : requisiti.some((r) => hasMenu(r.codiceMenu, r.tipoAbilitazione));\n\n if (!hasAbil) return res.sendStatus(401);\n (req as any).userGrants = grantsResult;\n }\n\n (req as any).data = decoded;\n return next();\n } catch (error) {\n return res.sendStatus(401);\n }\n}\n\nexport const authenticateGen = authorizeAccessi;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emilsoftware-utilities",
3
- "version": "1.6.10",
3
+ "version": "1.7.0",
4
4
  "description": "Utilities for EmilSoftware",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",