-
Notifications
You must be signed in to change notification settings - Fork 484
/
Copy pathenvironment.controller.ts
139 lines (113 loc) · 4.86 KB
/
environment.controller.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { accountService, environmentService, errorManager, hmacService } from '@nangohq/shared';
import { NANGO_ADMIN_UUID } from './account.controller.js';
import type { RequestLocals } from '../utils/express.js';
import type { NextFunction, Request, Response } from 'express';
class EnvironmentController {
getHmacDigest(req: Request, res: Response<any, Required<RequestLocals>>, next: NextFunction) {
try {
const { environment } = res.locals;
const { provider_config_key: providerConfigKey, connection_id: connectionId } = req.query;
if (!providerConfigKey) {
errorManager.errRes(res, 'missing_provider_config_key');
return;
}
if (!connectionId) {
errorManager.errRes(res, 'missing_connection_id');
return;
}
if (environment.hmac_enabled && environment.hmac_key) {
const digest = hmacService.computeDigest({ key: environment.hmac_key, values: [providerConfigKey as string, connectionId as string] });
res.status(200).send({ hmac_digest: digest });
} else {
res.status(200).send({ hmac_digest: null });
}
} catch (err) {
next(err);
}
}
async getAdminAuthInfo(req: Request, res: Response<any, Required<RequestLocals>>, next: NextFunction) {
try {
const { connection_id: connectionId } = req.query;
if (!connectionId) {
errorManager.errRes(res, 'missing_connection_id');
return;
}
const integration_key = process.env['NANGO_SLACK_INTEGRATION_KEY'] || 'slack';
const nangoAdminUUID = NANGO_ADMIN_UUID;
const env = process.env['NANGO_ADMIN_PROD_ENV'] || 'prod';
const info = await accountService.getAccountAndEnvironmentIdByUUID(nangoAdminUUID as string, env);
if (!info) {
errorManager.errRes(res, 'account_not_found');
return;
}
const environment = await environmentService.getById(info.environmentId);
if (!environment) {
errorManager.errRes(res, 'account_not_found');
return;
}
const digest = hmacService.computeDigest({ key: environment.hmac_key!, values: [integration_key, connectionId as string] });
res.status(200).send({ hmac_digest: digest, public_key: environment.public_key, integration_key });
} catch (err) {
next(err);
}
}
async getEnvironmentVariables(_req: Request, res: Response<any, Required<RequestLocals>>, next: NextFunction) {
try {
const environmentId = res.locals['environment'].id;
const environmentVariables = await environmentService.getEnvironmentVariables(environmentId);
if (!environmentVariables) {
res.status(200).send([]);
return;
}
const envs = environmentVariables.map((env) => {
return {
name: env.name,
value: env.value
};
});
res.status(200).send(envs);
} catch (err) {
next(err);
}
}
async rotateKey(req: Request, res: Response<any, Required<RequestLocals>>, next: NextFunction) {
try {
if (!req.body.type) {
res.status(400).send({ error: 'The type of key to rotate is required' });
return;
}
const { environment } = res.locals;
const newKey = await environmentService.rotateKey(environment.id, req.body.type);
res.status(200).send({ key: newKey });
} catch (err) {
next(err);
}
}
async revertKey(req: Request, res: Response<any, Required<RequestLocals>>, next: NextFunction) {
try {
if (!req.body.type) {
res.status(400).send({ error: 'The type of key to rotate is required' });
return;
}
const { environment } = res.locals;
const newKey = await environmentService.revertKey(environment.id, req.body.type);
res.status(200).send({ key: newKey });
} catch (err) {
next(err);
}
}
async activateKey(req: Request, res: Response<any, Required<RequestLocals>>, next: NextFunction) {
try {
if (!req.body.type) {
res.status(400).send({ error: 'The type of key to activate is required' });
return;
}
const { environment } = res.locals;
await environmentService.activateKey(environment.id, req.body.type);
res.status(200).send();
} catch (err) {
next(err);
}
}
}
export default new EnvironmentController();