-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconstants.ts
65 lines (52 loc) · 1.48 KB
/
constants.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { UnauthorizedException } from '@nestjs/common';
export const jwtConstants = {
secret: process.env['JM_JWT_SECRET'],
expirationTime: '12h',
};
export const resetPasswordConstants = {
expirationTimeSeconds: 3600,
};
export const rtConstants = {
secret: process.env['JM_REFRESH_SECRET'],
expirationTime: '5 days',
};
export const orchestratorConstants = {
clientId: 'jobs-manager',
brokers: [process.env['KAFKA_URI']],
topics: {
jobRequests: 'stalker.jobs.requests',
findings: 'stalker.jobs.findings',
jobLogs: 'stalker.jobs.logs',
jobModels: 'stalker.jobs.models',
jobManagement: 'stalker.jobs.management',
},
};
export enum Role {
User = 'user',
UserResetPassword = 'user-reset-password',
Admin = 'admin',
ReadOnly = 'read-only',
}
export function roleIsAuthorized(
userRole: string,
requiredRole: string,
): boolean {
const allowedRoles = expandRole(userRole);
if (!allowedRoles.includes(requiredRole)) throw new UnauthorizedException();
return true;
}
/** Returns all the roles allowed by the given role. */
function expandRole(role: string): string[] {
switch (role) {
case Role.Admin:
return [Role.Admin, Role.User, Role.ReadOnly, Role.UserResetPassword];
case Role.User:
return [Role.User, Role.ReadOnly, Role.UserResetPassword];
case Role.ReadOnly:
return [Role.ReadOnly, Role.UserResetPassword];
case Role.UserResetPassword:
return [Role.UserResetPassword];
default:
return [];
}
}