-
Notifications
You must be signed in to change notification settings - Fork 47
/
post.js
143 lines (129 loc) · 4.88 KB
/
post.js
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
140
141
142
143
const { RequestHandlers } = require('@openintegrationhub/webhooks');
const { DEFAULT_HMAC_HEADER_KEY = 'x-hmac', DEFAULT_HMAC_ALGORITHM = 'sha265', WEBHOOK_EXECUTE_PERMISSION = 'webhooks.execute' } = process.env;
const fetch = require('node-fetch');
const iamUtils = require('@openintegrationhub/iam-utils');
class PostRequestHandler extends RequestHandlers.Post {
constructor(req, res, messagePublisher, config) {
super(req, res, messagePublisher);
this.logger = this.getLogger();
this._config = config;
}
/* async handle() {
await this.authorize();
super.handle();
} */
async authorize() {
// Has the first node of the flow been configured to enforce authentication?
const flow = this.getFlow();
const flowSettings = flow.getFlowSettings();
const flowUser = flow.getFlowUser();
// Default to false if field doesn't exist
const { requireWebhookAuth = false, hmacHeaderKey = DEFAULT_HMAC_HEADER_KEY, hmacAuthSecret, hmacAlgorithm = DEFAULT_HMAC_ALGORITHM, allTenantUsers=false } = flowSettings;
if (!requireWebhookAuth) {
this.logger.debug('Not authenticating the flow. requireAuthorization is not enabled');
return;
}
const hmacHeaderValue = this._req.header(hmacHeaderKey);
if (hmacHeaderValue) {
const success = await this.authenticateHmac(hmacAuthSecret,hmacHeaderValue,hmacAlgorithm,flowUser,this._req.rawBody);
if (success) {
return;
} else {
this.sendPermissionsError();
}
}
const authHeader = this._req.header('authorization');
if (authHeader) {
const headerArray = authHeader.split(' ');
let hasPermissions = false;
switch (headerArray[0]) {
case 'Bearer': {
hasPermissions = await this.checkPermissions({ token:headerArray[1], flow, allTenantUsers });
break;
}
case 'Basic': {
// Decode the user/pass combination and login to the system to check user permissions
const authString = new Buffer.from(headerArray[1], 'base64').toString();
const splitIndex = authString.indexOf(':');
const user = authString.substring(0,splitIndex);
const pass = authString.substring(splitIndex+1);
const token = await this.login(user,pass);
hasPermissions = await this.checkPermissions({ token, flow, allTenantUsers });
break;
}
default:
break;
}
if (!hasPermissions) {
this.sendPermissionsError();
} else {
return true;
}
}
this.sendPermissionsError();
}
async checkPermissions({ token, flow, allTenantUsers }) {
try {
const user = await iamUtils.getUserData({ token, introspectType: this._req.headers['x-auth-type'] });
return (iamUtils.hasAll({ user, requiredPermissions:WEBHOOK_EXECUTE_PERMISSION }) &&
iamUtils.isOwnerOf({entity: flow, user, allTenantUsers }));
}
catch (e) {
this.logger.info('Error authorizing webhook: ',e)
return false;
}
}
sendPermissionsError() {
const err = new Error('Unauthorized');
err.statusCode = 403;
throw err;
}
async login( username, password) {
const body = {
username,
password,
};
const response = await fetch(
`${this._config.get('IAM_BASE_URL')}/login`,
{
method: 'post',
body: JSON.stringify(body),
headers: {'Content-Type': 'application/json'},
},
);
const data = await response.json();
return data.token;
}
async authenticateHmac(secretId, hmacHeader, hmacAlgo, userId, rawBody) {
const iamClient = iamUtils.createClient({
iamToken: this._config.get('IAM_TOKEN'),
baseUrl: this._config.get('IAM_BASE_URL')
});
const { id: tokenId, token } = await iamClient.createToken({
accountId: userId,
expiresIn: '5m',
description: 'Created to test HMAC for webhook',
forceNew: true,
});
// get user token, then use that token in the fetch
const response = await fetch(
`${this._config.get('SECRET_SERVICE_URL')}/secrets/${secretId}/validateHmac`,
{
headers: {
'x-auth-type': 'basic',
authorization: `Bearer ${token}`,
},
body: {
hmacHeader,
hmacAlgo,
rawBody,
}
},
);
// This *should* be an await statement, but we don't care about the return
iamClient.deleteTokenById(tokenId);
const body = await response.json();
return body.data;
}
}
module.exports.PostRequestHandler = PostRequestHandler;