emilsoftware-utilities 1.6.10 → 1.7.1
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/accessi-module/Controllers/AuthController.js +1 -1
- package/dist/accessi-module/Controllers/AuthController.js.map +1 -1
- package/dist/accessi-module/index.d.ts +1 -0
- package/dist/accessi-module/index.js +7 -1
- package/dist/accessi-module/index.js.map +1 -1
- package/dist/accessi-module/middleware/authenticateGen.d.ts +12 -0
- package/dist/accessi-module/middleware/authenticateGen.js +110 -0
- package/dist/accessi-module/middleware/authenticateGen.js.map +1 -0
- package/dist/emilsoftware-utilities-1.7.1.tgz +0 -0
- package/package.json +1 -1
- package/dist/emilsoftware-utilities-1.6.10.tgz +0 -0
|
@@ -104,7 +104,7 @@ let AuthController = AuthController_1 = class AuthController {
|
|
|
104
104
|
}
|
|
105
105
|
// Generazione del token JWT
|
|
106
106
|
const tokenData = {
|
|
107
|
-
userData,
|
|
107
|
+
utente: userData === null || userData === void 0 ? void 0 : userData.utente,
|
|
108
108
|
};
|
|
109
109
|
userData.token = {
|
|
110
110
|
expiresIn: this.options.jwtOptions.expiresIn,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthController.js","sourceRoot":"","sources":["../../../src/accessi-module/Controllers/AuthController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA4E;AAC5E,6CAAwF;AAExF,kDAAoC;AACpC,+CAAgD;AAEhD,qEAAkE;AAClE,kCAAsD;AACtD,yCAAsC;AAI/B,IAAM,cAAc,sBAApB,MAAM,cAAc;IAGzB,YACmB,WAAwB,EACd,OAAwC;QADlD,gBAAW,GAAX,WAAW,CAAa;QACG,YAAO,GAAP,OAAO,CAAgB;QAHrE,WAAM,GAAW,IAAI,eAAM,CAAC,gBAAc,CAAC,IAAI,CAAC,CAAC;IAI7C,CAAC;IAQC,aAA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,sBAAsB,CAAC,GAAG,CAAC,CAAC;gBACnD,CAAC;gBAED,4BAA4B;gBAC5B,MAAM,SAAS,GAAG;oBAChB,QAAQ;
|
|
1
|
+
{"version":3,"file":"AuthController.js","sourceRoot":"","sources":["../../../src/accessi-module/Controllers/AuthController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA4E;AAC5E,6CAAwF;AAExF,kDAAoC;AACpC,+CAAgD;AAEhD,qEAAkE;AAClE,kCAAsD;AACtD,yCAAsC;AAI/B,IAAM,cAAc,sBAApB,MAAM,cAAc;IAGzB,YACmB,WAAwB,EACd,OAAwC;QADlD,gBAAW,GAAX,WAAW,CAAa;QACG,YAAO,GAAP,OAAO,CAAgB;QAHrE,WAAM,GAAW,IAAI,eAAM,CAAC,gBAAc,CAAC,IAAI,CAAC,CAAC;IAI7C,CAAC;IAQC,aAAa,CAAQ,GAAa,EAAkB,KAAa,EAAuB,WAAmB;;YAC/G,IAAI,CAAC;gBACH,MAAM,IAAI,CAAC,WAAW,CAAC,oBAAoB,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;gBAChE,OAAO,yBAAa,CAAC,aAAa,CAAC,GAAG,EAAE,mCAAmC,CAAC,CAAC;YAC/E,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,yBAAa,CAAC,gBAAgB,CAAC,GAAG,EAAE,KAAK,EAAE,gBAAc,CAAC,IAAI,CAAC,CAAC;YACzE,CAAC;QACH,CAAC;KAAA;IAOK,cAAc,CAAgB,KAAa,EAAS,GAAa;;YACrE,IAAI,CAAC;gBACH,IAAI,CAAC,KAAK;oBAAE,OAAO,yBAAa,CAAC,gBAAgB,CAAC,GAAG,EAAE,mBAAmB,EAAE,gBAAc,CAAC,IAAI,CAAC,CAAC;gBACjG,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,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BAAO,EAAC,MAAM,CAAC;IACf,IAAA,mBAAU,EAAC,cAAc,CAAC;IAMtB,WAAA,IAAA,eAAM,EAAC,iBAAiB,CAAC,CAAA;qCADI,yBAAW;GAJhC,cAAc,CAuG1B","sourcesContent":["import { Body, Controller, Inject, Param, Post, Res } from '@nestjs/common';\nimport { ApiOperation, ApiTags, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';\nimport { Response } from 'express';\nimport * as jwt from 'jsonwebtoken';\nimport { RestUtilities } from '../../Utilities';\nimport { AccessiOptions } from '../AccessiModule';\nimport { AuthService } from '../Services/AuthService/AuthService';\nimport { LoginRequest, LoginResponse } from '../Dtos';\nimport { Logger } from '../../Logger';\n\n@ApiTags('Auth')\n@Controller('accessi/auth')\nexport class AuthController {\n\n logger: Logger = new Logger(AuthController.name);\n constructor(\n private readonly authService: AuthService,\n @Inject('ACCESSI_OPTIONS') private readonly options: AccessiOptions\n ) { }\n\n @ApiOperation({ summary: 'Conferma il reset della password', operationId: \"resetPassword\" })\n @ApiParam({ name: 'token', description: 'Token per il reset della password', required: true })\n @ApiBody({ schema: { properties: { newPassword: { type: 'string', description: 'Nuova password da impostare' } } } })\n @ApiResponse({ status: 200, description: 'Password aggiornata con successo' })\n @ApiResponse({ status: 400, description: 'Errore nella richiesta o token non valido' })\n @Post('confirm-reset-password/:token')\n async resetPassword(@Res() res: Response, @Param('token') token: string, @Body(\"newPassword\") newPassword: string) {\n try {\n await this.authService.confirmResetPassword(token, newPassword);\n return RestUtilities.sendOKMessage(res, 'Password aggiornata con successo!');\n } catch (error) {\n return RestUtilities.sendErrorMessage(res, error, AuthController.name);\n }\n }\n\n @ApiOperation({ summary: 'Recupera le informazioni utente dal token JWT', operationId: \"getUserByToken\" })\n @ApiBody({ schema: { properties: { token: { type: 'string', description: 'JWT dell\\'utente' } } } })\n @ApiResponse({ status: 200, description: 'Informazioni utente recuperate con successo' })\n @ApiResponse({ status: 401, description: 'Token non valido o scaduto' })\n @Post('get-user-by-token')\n async getUserByToken(@Body('token') token: string, @Res() res: Response) {\n try {\n if (!token) return RestUtilities.sendErrorMessage(res, 'Token non fornito', AuthController.name);\n const decoded = jwt.verify(token, this.options.jwtOptions.secret);\n if (!decoded) return RestUtilities.sendUnauthorized(res);\n return RestUtilities.sendBaseResponse(res, { userData: decoded });\n } catch (error) {\n return RestUtilities.sendErrorMessage(res, error, AuthController.name);\n }\n }\n\n @ApiOperation({\n summary: 'Effettua il login utente',\n description: 'Autentica l\\'utente con email e password. Restituisce un token JWT e i dati dell\\'utente se le credenziali sono corrette.',\n operationId: 'login',\n })\n @ApiBody({ type: LoginRequest })\n @ApiResponse({\n status: 200,\n description: 'Login effettuato con successo',\n type: LoginResponse,\n })\n @ApiResponse({\n status: 401,\n description: 'Credenziali non valide',\n })\n @ApiResponse({\n status: 403,\n description: \"Password scaduta, è necessatio aggiornarla. \",\n schema: {\n example: {\n message: {\n severity: 'warning',\n statusCode: 2, // o il valore di StatusCode.Warning\n code: 'PASSWORD_EXPIRED',\n message: 'Password scaduta. È necessario aggiornarla.',\n }\n }\n }\n })\n @Post('login')\n async login(@Body() loginRequest: LoginRequest, @Res() res: Response) {\n try {\n\n const userData = await this.authService.login(loginRequest);\n if (!userData) {\n return RestUtilities.sendInvalidCredentials(res);\n }\n\n // Generazione del token JWT\n const tokenData = {\n utente: userData?.utente,\n }\n\n userData.token = {\n expiresIn: this.options.jwtOptions.expiresIn,\n value: jwt.sign(tokenData, this.options.jwtOptions.secret, {\n expiresIn: this.options.jwtOptions.expiresIn as any,\n }),\n type: 'Bearer',\n };\n\n return RestUtilities.sendBaseResponse(res, userData);\n } catch (error) {\n\n if (error.message === 'PASSWORD_EXPIRED') {\n this.logger.warning('Password scaduta, cambiare password ', error)\n return RestUtilities.sendPasswordExpired(res)\n }\n\n this.logger.error('Errore durante il login', error);\n return RestUtilities.sendInvalidCredentials(res);\n }\n \n }\n}\n\n"]}
|
|
@@ -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":";;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYA,sDAEC;AAUD,4CAwDC;AA/ED,kDAAoC;AAEpC,uFAAoF;AAOpF,IAAI,iBAAiB,GAA0B,IAAI,CAAC;AAEpD,SAAgB,qBAAqB,CAAC,OAAuB;IAC3D,iBAAiB,GAAG,OAAO,CAAC;AAC9B,CAAC;AAED,SAAS,mBAAmB,CAAC,OAAY;;IACvC,OAAO,CACL,MAAA,MAAA,MAAA,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,0CAAE,MAAM,0CAAE,YAAY,mCACvC,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CA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AC;YAC5B,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC;CAAA;AAEY,QAAA,eAAe,GAAG,gBAAgB,CAAC","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"]}
|
|
Binary file
|
package/package.json
CHANGED
|
Binary file
|