UNPKG

@brandazm/dynamic-permissions

Version:

A flexible and powerful permissions management system for NestJS applications with built-in security features

178 lines 9.56 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.SecurityConfigPublisherService = void 0; const common_1 = require("@nestjs/common"); const default_security_config_1 = require("../config/default-security.config"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); let SecurityConfigPublisherService = class SecurityConfigPublisherService { constructor() { this.templates = { basic: { name: 'Basic Security', description: 'Basic security configuration with essential features', config: default_security_config_1.defaultSecurityConfig, }, strict: { name: 'Strict Security', description: 'Enhanced security configuration with stricter settings', config: Object.assign(Object.assign({}, default_security_config_1.defaultSecurityConfig), { rateLimit: { enabled: true, windowMs: 15 * 60 * 1000, max: 50, }, cors: { enabled: true, allowedOrigins: [], allowedMethods: ['GET', 'POST'], allowedHeaders: ['Content-Type', 'Authorization'], exposedHeaders: [], credentials: true, }, helmet: Object.assign(Object.assign({}, default_security_config_1.defaultSecurityConfig.helmet), { contentSecurityPolicy: true, crossOriginEmbedderPolicy: true, hsts: true }), requestValidation: { maxBodySize: 5 * 1024 * 1024, requireJsonContent: true, validateContentType: true, } }), }, enterprise: { name: 'Enterprise Security', description: 'Full-featured security configuration for enterprise applications', config: Object.assign(Object.assign({}, default_security_config_1.defaultSecurityConfig), { rateLimit: { enabled: true, windowMs: 5 * 60 * 1000, max: 100, }, cors: { enabled: true, allowedOrigins: [], allowedMethods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], allowedHeaders: ['Content-Type', 'Authorization', 'X-Custom-Header'], exposedHeaders: ['X-Total-Count', 'X-Rate-Limit'], credentials: true, }, helmet: { enabled: true, contentSecurityPolicy: true, crossOriginEmbedderPolicy: true, crossOriginOpenerPolicy: true, crossOriginResourcePolicy: true, dnsPrefetchControl: true, frameguard: true, hidePoweredBy: true, hsts: true, ieNoOpen: true, noSniff: true, referrerPolicy: true, xssFilter: true, }, requestValidation: { maxBodySize: 10 * 1024 * 1024, requireJsonContent: true, validateContentType: true, } }), }, }; } async publishSecurityConfigToProject(projectPath, template = 'basic', customizations) { if (!this.templates[template]) { throw new Error(`Template "${template}" not found`); } const configDir = path.join(projectPath, 'config'); if (!fs.existsSync(configDir)) { fs.mkdirSync(configDir, { recursive: true }); } const baseConfig = this.templates[template].config; const finalConfig = customizations ? this.mergeConfigs(baseConfig, customizations) : baseConfig; const configPath = path.join(configDir, 'security.config.ts'); const configContent = this.generateConfigFile(finalConfig); fs.writeFileSync(configPath, configContent, 'utf8'); } async updateProjectSecurityConfig(projectPath, updates) { const configPath = path.join(projectPath, 'config', 'security.config.ts'); if (!fs.existsSync(configPath)) { throw new Error('Security configuration file not found'); } try { const configContent = fs.readFileSync(configPath, 'utf8'); const currentConfig = this.parseConfigFile(configContent); const updatedConfig = this.mergeConfigs(currentConfig, updates); const newConfigContent = this.generateConfigFile(updatedConfig); fs.writeFileSync(configPath, newConfigContent, 'utf8'); } catch (error) { throw new Error(`Failed to update security config: ${error.message}`); } } getAvailableTemplates() { return Object.values(this.templates); } generateConfigFile(config) { var _a, _b, _c; const fullConfig = { rateLimit: Object.assign({ enabled: true, windowMs: 15 * 60 * 1000, max: 100 }, config.rateLimit), cors: Object.assign({ enabled: true, allowedOrigins: [], allowedMethods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], allowedHeaders: ['Content-Type', 'Authorization'], exposedHeaders: ['X-Total-Count'], credentials: true }, config.cors), helmet: Object.assign({ enabled: true, contentSecurityPolicy: true, crossOriginEmbedderPolicy: true, crossOriginOpenerPolicy: true, crossOriginResourcePolicy: true, dnsPrefetchControl: true, frameguard: true, hidePoweredBy: true, hsts: true, ieNoOpen: true, noSniff: true, referrerPolicy: true, xssFilter: true }, config.helmet), requestValidation: Object.assign({ maxBodySize: 10 * 1024 * 1024, requireJsonContent: true, validateContentType: true }, config.requestValidation), enableCaching: (_a = config.enableCaching) !== null && _a !== void 0 ? _a : true, cacheTimeout: (_b = config.cacheTimeout) !== null && _b !== void 0 ? _b : 3600, enableAuditLog: (_c = config.enableAuditLog) !== null && _c !== void 0 ? _c : true, }; return `export const securityConfig = ${JSON.stringify(fullConfig, null, 2)};\n`; } parseConfigFile(content) { try { const configString = content .replace(/import.*?;/g, '') .replace(/export.*?const.*?=/, '') .replace(/;$/, '') .trim(); return JSON.parse(configString); } catch (error) { throw new Error('Failed to parse security config file'); } } mergeConfigs(base, updates) { return Object.assign(Object.assign(Object.assign({}, base), updates), { rateLimit: Object.assign(Object.assign({}, base.rateLimit), updates.rateLimit), cors: Object.assign(Object.assign({}, base.cors), updates.cors), helmet: Object.assign(Object.assign({}, base.helmet), updates.helmet), requestValidation: Object.assign(Object.assign({}, base.requestValidation), updates.requestValidation) }); } }; exports.SecurityConfigPublisherService = SecurityConfigPublisherService; exports.SecurityConfigPublisherService = SecurityConfigPublisherService = __decorate([ (0, common_1.Injectable)() ], SecurityConfigPublisherService); //# sourceMappingURL=security-config-publisher.service.js.map