@noony-serverless/core 0.1.0 → 0.1.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.
Files changed (33) hide show
  1. package/build/middlewares/guards/RouteGuards.d.ts +255 -0
  2. package/build/middlewares/guards/RouteGuards.js +500 -0
  3. package/build/middlewares/guards/cache/CacheAdapter.d.ts +132 -0
  4. package/build/middlewares/guards/cache/CacheAdapter.js +86 -0
  5. package/build/middlewares/guards/cache/ConservativeCacheInvalidation.d.ts +191 -0
  6. package/build/middlewares/guards/cache/ConservativeCacheInvalidation.js +510 -0
  7. package/build/middlewares/guards/cache/MemoryCacheAdapter.d.ts +119 -0
  8. package/build/middlewares/guards/cache/MemoryCacheAdapter.js +294 -0
  9. package/build/middlewares/guards/cache/NoopCacheAdapter.d.ts +95 -0
  10. package/build/middlewares/guards/cache/NoopCacheAdapter.js +131 -0
  11. package/build/middlewares/guards/config/GuardConfiguration.d.ts +112 -0
  12. package/build/middlewares/guards/config/GuardConfiguration.js +137 -0
  13. package/build/middlewares/guards/guards/FastAuthGuard.d.ts +201 -0
  14. package/build/middlewares/guards/guards/FastAuthGuard.js +460 -0
  15. package/build/middlewares/guards/guards/PermissionGuardFactory.d.ts +202 -0
  16. package/build/middlewares/guards/guards/PermissionGuardFactory.js +563 -0
  17. package/build/middlewares/guards/index.d.ts +67 -0
  18. package/build/middlewares/guards/index.js +192 -0
  19. package/build/middlewares/guards/registry/PermissionRegistry.d.ts +188 -0
  20. package/build/middlewares/guards/registry/PermissionRegistry.js +425 -0
  21. package/build/middlewares/guards/resolvers/ExpressionPermissionResolver.d.ts +129 -0
  22. package/build/middlewares/guards/resolvers/ExpressionPermissionResolver.js +451 -0
  23. package/build/middlewares/guards/resolvers/PermissionResolver.d.ts +155 -0
  24. package/build/middlewares/guards/resolvers/PermissionResolver.js +176 -0
  25. package/build/middlewares/guards/resolvers/PlainPermissionResolver.d.ts +101 -0
  26. package/build/middlewares/guards/resolvers/PlainPermissionResolver.js +248 -0
  27. package/build/middlewares/guards/resolvers/WildcardPermissionResolver.d.ts +146 -0
  28. package/build/middlewares/guards/resolvers/WildcardPermissionResolver.js +377 -0
  29. package/build/middlewares/guards/services/FastUserContextService.d.ts +216 -0
  30. package/build/middlewares/guards/services/FastUserContextService.js +434 -0
  31. package/build/middlewares/index.d.ts +1 -0
  32. package/build/middlewares/index.js +1 -0
  33. package/package.json +2 -2
@@ -0,0 +1,255 @@
1
+ /**
2
+ * Route Guards Facade
3
+ *
4
+ * Main entry point for the guard system providing a clean, NestJS-inspired API
5
+ * for protecting routes with authentication and authorization. This facade
6
+ * orchestrates all guard components to provide three distinct protection methods
7
+ * optimized for different use cases.
8
+ *
9
+ * Three Protection Methods:
10
+ * 1. `requirePermissions()` - Simple permission list checks (fastest)
11
+ * 2. `requireWildcardPermissions()` - Hierarchical wildcard patterns
12
+ * 3. `requireComplexPermissions()` - Boolean expression evaluation
13
+ *
14
+ * Key Features:
15
+ * - Automatic resolver selection for optimal performance
16
+ * - Intelligent caching strategies per protection method
17
+ * - Conservative security approach with automatic cache invalidation
18
+ * - Built-in authentication with cached user context loading
19
+ * - Comprehensive monitoring and audit trails
20
+ * - Framework-agnostic middleware integration
21
+ *
22
+ * Usage Examples:
23
+ * ```typescript
24
+ * // Simple permissions (fastest)
25
+ * .use(RouteGuards.requirePermissions(['user:read', 'user:update']))
26
+ *
27
+ * // Wildcard patterns (hierarchical)
28
+ * .use(RouteGuards.requireWildcardPermissions(['admin.*', 'org.reports.*']))
29
+ *
30
+ * // Complex expressions (boolean logic)
31
+ * .use(RouteGuards.requireComplexPermissions({
32
+ * or: [
33
+ * { permission: 'admin.users' },
34
+ * { and: [
35
+ * { permission: 'moderator.content' },
36
+ * { permission: 'org.reports.view' }
37
+ * ]}
38
+ * ]
39
+ * }))
40
+ * ```
41
+ *
42
+ * @author Noony Framework Team
43
+ * @version 1.0.0
44
+ */
45
+ import { BaseMiddleware } from '../../core/handler';
46
+ import { GuardConfiguration, GuardEnvironmentProfile } from './config/GuardConfiguration';
47
+ import { CacheAdapter } from './cache/CacheAdapter';
48
+ import { FastUserContextService, UserPermissionSource } from './services/FastUserContextService';
49
+ import { ConservativeCacheInvalidation } from './cache/ConservativeCacheInvalidation';
50
+ import { FastAuthGuard, AuthGuardConfig, TokenValidator } from './guards/FastAuthGuard';
51
+ import { PermissionGuardFactory } from './guards/PermissionGuardFactory';
52
+ import { PermissionRegistry } from './registry/PermissionRegistry';
53
+ import { PermissionExpression } from './resolvers/PermissionResolver';
54
+ /**
55
+ * Route guard configuration for the facade
56
+ */
57
+ export interface RouteGuardOptions {
58
+ /** Enable authentication requirement (default: true) */
59
+ requireAuth?: boolean;
60
+ /** Enable permission result caching (default: true) */
61
+ cacheResults?: boolean;
62
+ /** Enable detailed audit logging (default: false) */
63
+ auditTrail?: boolean;
64
+ /** Custom error message for access denials */
65
+ errorMessage?: string;
66
+ /** Cache TTL in milliseconds (overrides global config) */
67
+ cacheTtlMs?: number;
68
+ }
69
+ /**
70
+ * Guard system statistics
71
+ */
72
+ export interface GuardSystemStats {
73
+ authentication: Record<string, unknown>;
74
+ userContextService: Record<string, unknown>;
75
+ permissionGuardFactory: Record<string, unknown>;
76
+ cacheInvalidation: Record<string, unknown>;
77
+ cacheAdapter: Record<string, unknown>;
78
+ systemHealth: {
79
+ totalGuardChecks: number;
80
+ averageResponseTime: number;
81
+ errorRate: number;
82
+ cacheEfficiency: number;
83
+ uptime: number;
84
+ };
85
+ }
86
+ /**
87
+ * Route Guards Facade Implementation
88
+ *
89
+ * This class provides the main API for the guard system and handles
90
+ * the orchestration of all guard components. It follows the facade pattern
91
+ * to simplify the complex underlying guard architecture.
92
+ */
93
+ export declare class RouteGuards {
94
+ private static instance;
95
+ private static isConfigured;
96
+ private readonly _config;
97
+ private readonly cache;
98
+ private readonly userContextService;
99
+ private readonly cacheInvalidation;
100
+ private readonly authGuard;
101
+ private readonly guardFactory;
102
+ private readonly _permissionRegistry;
103
+ private systemStartTime;
104
+ private totalGuardChecks;
105
+ private totalErrors;
106
+ private totalResponseTime;
107
+ constructor(config: GuardConfiguration, cache: CacheAdapter, userContextService: FastUserContextService, cacheInvalidation: ConservativeCacheInvalidation, authGuard: FastAuthGuard, guardFactory: PermissionGuardFactory, permissionRegistry: PermissionRegistry);
108
+ /**
109
+ * Configure the guard system with environment-specific settings
110
+ *
111
+ * This method must be called once before using any guard methods.
112
+ * It sets up all guard components with optimal configurations for
113
+ * the target environment (development, production, serverless).
114
+ *
115
+ * @param profile - Environment profile with guard configurations
116
+ * @param permissionSource - User permission data source
117
+ * @param tokenValidator - JWT token validation service
118
+ * @param authConfig - Authentication guard configuration
119
+ * @returns Promise resolving when configuration is complete
120
+ */
121
+ static configure(profile: GuardEnvironmentProfile, permissionSource: UserPermissionSource, tokenValidator: TokenValidator, authConfig: AuthGuardConfig): Promise<void>;
122
+ /**
123
+ * Get the configured RouteGuards instance
124
+ *
125
+ * @returns Configured RouteGuards instance
126
+ * @throws Error if not configured
127
+ */
128
+ static getInstance(): RouteGuards;
129
+ /**
130
+ * Create middleware for simple permission list checks
131
+ *
132
+ * This is the fastest protection method using direct O(1) set membership
133
+ * checks. Ideal for high-traffic endpoints with straightforward permission
134
+ * requirements.
135
+ *
136
+ * Performance: ~0.1ms cached, ~1-2ms uncached
137
+ *
138
+ * @param permissions - Array of required permissions (OR logic)
139
+ * @param options - Optional guard configuration
140
+ * @returns Middleware instance for permission checking
141
+ */
142
+ static requirePermissions(permissions: string[], options?: RouteGuardOptions): BaseMiddleware;
143
+ /**
144
+ * Create middleware for wildcard permission pattern checks
145
+ *
146
+ * Supports hierarchical permission patterns with wildcards for flexible
147
+ * permission management. Uses configurable pre-expansion or on-demand
148
+ * matching strategies.
149
+ *
150
+ * Performance: ~0.2ms cached (pre-expansion), ~2-5ms cached (on-demand)
151
+ *
152
+ * @param wildcardPatterns - Array of wildcard patterns
153
+ * @param options - Optional guard configuration
154
+ * @returns Middleware instance for wildcard permission checking
155
+ */
156
+ static requireWildcardPermissions(wildcardPatterns: string[], options?: RouteGuardOptions): BaseMiddleware;
157
+ /**
158
+ * Create middleware for complex boolean expression checks
159
+ *
160
+ * Supports advanced permission logic with AND, OR, and NOT operations.
161
+ * Includes expression caching and complexity tracking for performance
162
+ * optimization.
163
+ *
164
+ * Performance: ~0.5ms cached, ~5-15ms uncached (depends on complexity)
165
+ *
166
+ * @param expression - Permission expression with boolean logic
167
+ * @param options - Optional guard configuration
168
+ * @returns Middleware instance for expression permission checking
169
+ */
170
+ static requireComplexPermissions(expression: PermissionExpression, options?: RouteGuardOptions): BaseMiddleware;
171
+ /**
172
+ * Create middleware with automatic resolver selection
173
+ *
174
+ * Analyzes permission requirements and automatically selects the optimal
175
+ * resolution strategy for best performance. Useful when you want the
176
+ * system to choose the best approach.
177
+ *
178
+ * @param permissions - Any type of permission requirement
179
+ * @param options - Optional guard configuration
180
+ * @returns Optimally configured middleware instance
181
+ */
182
+ static requireAny(permissions: string[] | PermissionExpression, options?: RouteGuardOptions): BaseMiddleware;
183
+ /**
184
+ * Get authentication-only middleware
185
+ *
186
+ * Provides user authentication without permission checking.
187
+ * Useful for endpoints that only need to verify user identity.
188
+ *
189
+ * @param options - Optional guard configuration
190
+ * @returns Authentication-only middleware
191
+ */
192
+ static requireAuth(_options?: RouteGuardOptions): BaseMiddleware;
193
+ /**
194
+ * Invalidate user permissions cache
195
+ *
196
+ * Use when user permissions change to ensure fresh permission checks.
197
+ * Implements conservative invalidation strategy for security.
198
+ *
199
+ * @param userId - User ID to invalidate
200
+ * @param reason - Reason for invalidation (for audit)
201
+ * @returns Promise resolving when invalidation is complete
202
+ */
203
+ static invalidateUserPermissions(userId: string, reason: string): Promise<void>;
204
+ /**
205
+ * System-wide cache invalidation
206
+ *
207
+ * Nuclear option for clearing all permission-related caches.
208
+ * Use for major system updates or security incidents.
209
+ *
210
+ * @param reason - Reason for system-wide invalidation
211
+ * @returns Promise resolving when invalidation is complete
212
+ */
213
+ static invalidateAllPermissions(reason: string): Promise<void>;
214
+ /**
215
+ * Emergency security invalidation
216
+ *
217
+ * Immediate cache clearing for security incidents.
218
+ * Bypasses backup creation for maximum speed.
219
+ *
220
+ * @param reason - Security incident description
221
+ * @returns Promise resolving when emergency invalidation is complete
222
+ */
223
+ static emergencyInvalidation(reason: string): Promise<void>;
224
+ /**
225
+ * Get comprehensive system statistics
226
+ *
227
+ * @returns Complete guard system performance and health metrics
228
+ */
229
+ static getSystemStats(): GuardSystemStats;
230
+ /**
231
+ * Reset all system statistics
232
+ */
233
+ static resetSystemStats(): void;
234
+ /**
235
+ * Health check for the guard system
236
+ *
237
+ * @returns Health status with key metrics
238
+ */
239
+ static healthCheck(): Promise<{
240
+ status: 'healthy' | 'degraded' | 'unhealthy';
241
+ details: Record<string, unknown>;
242
+ timestamp: string;
243
+ }>;
244
+ private createPlainPermissionGuard;
245
+ private createWildcardPermissionGuard;
246
+ private createExpressionPermissionGuard;
247
+ private createAutoPermissionGuard;
248
+ private wrapGuardWithStats;
249
+ private trackGuardCreation;
250
+ private getSystemStats;
251
+ private resetSystemStats;
252
+ private performHealthCheck;
253
+ private getHealthRecommendations;
254
+ }
255
+ //# sourceMappingURL=RouteGuards.d.ts.map