exguard-backend 1.0.7 → 1.0.8
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/package.json +1 -1
- package/scripts/setup-nestjs.cjs +48 -24
package/package.json
CHANGED
package/scripts/setup-nestjs.cjs
CHANGED
|
@@ -125,12 +125,12 @@ import { Guard } from 'exguard-backend';
|
|
|
125
125
|
apiUrl: process.env.EXGUARD_API_URL || 'http://localhost:3000',
|
|
126
126
|
cache: {
|
|
127
127
|
enabled: process.env.EXGUARD_CACHE_ENABLED !== 'false',
|
|
128
|
-
ttl: parseInt(process.env.EXGUARD_CACHE_TTL
|
|
128
|
+
ttl: parseInt(process.env.EXGUARD_CACHE_TTL || '300000'), // 5 minutes
|
|
129
129
|
},
|
|
130
130
|
realtime: {
|
|
131
131
|
enabled: process.env.EXGUARD_REALTIME_ENABLED === 'true',
|
|
132
|
-
url: process.env.EXGUARD_REALTIME_URL,
|
|
133
|
-
token: process.env.EXGUARD_SERVICE_TOKEN,
|
|
132
|
+
url: process.env.EXGUARD_REALTIME_URL || undefined,
|
|
133
|
+
token: process.env.EXGUARD_SERVICE_TOKEN || undefined,
|
|
134
134
|
},
|
|
135
135
|
}),
|
|
136
136
|
},
|
|
@@ -160,7 +160,7 @@ import { Guard, GuardContext } from 'exguard-backend';
|
|
|
160
160
|
|
|
161
161
|
@Injectable()
|
|
162
162
|
export class ExGuardNestGuard implements CanActivate {
|
|
163
|
-
constructor(
|
|
163
|
+
constructor(public exGuard: Guard) {}
|
|
164
164
|
|
|
165
165
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
166
166
|
const request = context.switchToHttp().getRequest();
|
|
@@ -185,11 +185,11 @@ export class ExGuardNestGuard implements CanActivate {
|
|
|
185
185
|
return true;
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
|
|
188
|
+
public async checkPermissions(context: GuardContext) {
|
|
189
189
|
return this.exGuard.authenticate(context);
|
|
190
190
|
}
|
|
191
191
|
|
|
192
|
-
|
|
192
|
+
public extractToken(request: any): string | null {
|
|
193
193
|
const authHeader = request.headers?.authorization;
|
|
194
194
|
if (authHeader?.startsWith('Bearer ')) {
|
|
195
195
|
return authHeader.substring(7);
|
|
@@ -200,14 +200,14 @@ export class ExGuardNestGuard implements CanActivate {
|
|
|
200
200
|
|
|
201
201
|
@Injectable()
|
|
202
202
|
export class ExGuardPermissionGuard extends ExGuardNestGuard {
|
|
203
|
-
|
|
203
|
+
public async checkPermissions(context: GuardContext) {
|
|
204
204
|
return this.exGuard.requirePermissions(context, ['read']);
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
207
|
|
|
208
208
|
@Injectable()
|
|
209
209
|
export class ExGuardRoleGuard extends ExGuardNestGuard {
|
|
210
|
-
|
|
210
|
+
public async checkPermissions(context: GuardContext) {
|
|
211
211
|
return this.exGuard.requireRoles(context, ['Admin']);
|
|
212
212
|
}
|
|
213
213
|
}
|
|
@@ -215,7 +215,7 @@ export class ExGuardRoleGuard extends ExGuardNestGuard {
|
|
|
215
215
|
// Factory functions for dynamic guards
|
|
216
216
|
export function createPermissionGuard(permissions: string[], requireAll = false) {
|
|
217
217
|
return class extends ExGuardNestGuard {
|
|
218
|
-
|
|
218
|
+
public async checkPermissions(context: GuardContext) {
|
|
219
219
|
return this.exGuard.requirePermissions(context, permissions, { requireAll });
|
|
220
220
|
}
|
|
221
221
|
};
|
|
@@ -223,7 +223,7 @@ export function createPermissionGuard(permissions: string[], requireAll = false)
|
|
|
223
223
|
|
|
224
224
|
export function createRoleGuard(roles: string[], requireAll = false) {
|
|
225
225
|
return class extends ExGuardNestGuard {
|
|
226
|
-
|
|
226
|
+
public async checkPermissions(context: GuardContext) {
|
|
227
227
|
return this.exGuard.requireRoles(context, roles, { requireAll });
|
|
228
228
|
}
|
|
229
229
|
};
|
|
@@ -231,7 +231,7 @@ export function createRoleGuard(roles: string[], requireAll = false) {
|
|
|
231
231
|
|
|
232
232
|
export function createModuleGuard(modules: string[], requireAll = false) {
|
|
233
233
|
return class extends ExGuardNestGuard {
|
|
234
|
-
|
|
234
|
+
public async checkPermissions(context: GuardContext) {
|
|
235
235
|
return this.exGuard.requireModules(context, modules, { requireAll });
|
|
236
236
|
}
|
|
237
237
|
};
|
|
@@ -421,26 +421,50 @@ function updateAppModule() {
|
|
|
421
421
|
return;
|
|
422
422
|
}
|
|
423
423
|
|
|
424
|
-
// Add import at the top
|
|
425
|
-
|
|
424
|
+
// Add import at the top - find the last import statement
|
|
425
|
+
const importRegex = /import\s+.*\s+from\s+['"][^'"]*['"];?\s*/g;
|
|
426
|
+
const imports = appModuleContent.match(importRegex);
|
|
427
|
+
|
|
428
|
+
if (imports && imports.length > 0) {
|
|
429
|
+
// Add after the last import
|
|
430
|
+
const lastImport = imports[imports.length - 1];
|
|
431
|
+
const lastImportIndex = appModuleContent.lastIndexOf(lastImport);
|
|
432
|
+
const afterLastImport = appModuleContent.substring(lastImportIndex + lastImport.length);
|
|
433
|
+
|
|
434
|
+
appModuleContent = appModuleContent.substring(0, lastImportIndex + lastImport.length) +
|
|
435
|
+
`import { ExGuardModule } from './exguard/exguard.module';\n` +
|
|
436
|
+
afterLastImport;
|
|
437
|
+
} else {
|
|
438
|
+
// Add at the very top
|
|
426
439
|
appModuleContent = `import { ExGuardModule } from './exguard/exguard.module';\n${appModuleContent}`;
|
|
427
440
|
}
|
|
428
441
|
|
|
429
|
-
//
|
|
430
|
-
const
|
|
431
|
-
if (
|
|
432
|
-
const moduleContent =
|
|
442
|
+
// Find and update the @Module decorator
|
|
443
|
+
const moduleDecoratorMatch = appModuleContent.match(/@Module\s*\(\s*\{([^}]*)\}/s);
|
|
444
|
+
if (moduleDecoratorMatch) {
|
|
445
|
+
const moduleContent = moduleDecoratorMatch[1];
|
|
433
446
|
|
|
434
447
|
// Check if imports array exists
|
|
435
|
-
const
|
|
436
|
-
if (
|
|
437
|
-
|
|
448
|
+
const importsArrayMatch = moduleContent.match(/imports:\s*\[([^\]]*)\]/s);
|
|
449
|
+
if (importsArrayMatch) {
|
|
450
|
+
// Add to existing imports array
|
|
451
|
+
const importsContent = importsArrayMatch[1];
|
|
438
452
|
const newImportsContent = importsContent.trim() + ',\n ExGuardModule';
|
|
439
|
-
appModuleContent = appModuleContent.replace(
|
|
453
|
+
appModuleContent = appModuleContent.replace(
|
|
454
|
+
importsArrayMatch[0],
|
|
455
|
+
`imports: [${newImportsContent}]`
|
|
456
|
+
);
|
|
440
457
|
} else {
|
|
441
|
-
// Add imports array
|
|
442
|
-
const
|
|
443
|
-
|
|
458
|
+
// Add imports array after @Module({
|
|
459
|
+
const moduleStartMatch = appModuleContent.match(/@Module\s*\(\s*\{/);
|
|
460
|
+
if (moduleStartMatch) {
|
|
461
|
+
const moduleStartIndex = appModuleContent.indexOf(moduleStartMatch[0]);
|
|
462
|
+
const afterModuleStart = appModuleContent.substring(moduleStartIndex + moduleStartMatch[0].length);
|
|
463
|
+
|
|
464
|
+
appModuleContent = appModuleContent.substring(0, moduleStartIndex + moduleStartMatch[0].length) +
|
|
465
|
+
`\n imports: [ExGuardModule],` +
|
|
466
|
+
afterModuleStart;
|
|
467
|
+
}
|
|
444
468
|
}
|
|
445
469
|
}
|
|
446
470
|
|