-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathokta_does_group_exist.js
66 lines (52 loc) · 1.88 KB
/
okta_does_group_exist.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
const https = require('https');
module.exports = function(RED) {
function okta_does_group_exist_node(config) {
RED.nodes.createNode(this, config);
var node = this;
node.on('input', function(msg) {
node.auth = RED.nodes.getNode(config.auth);
if (!node.auth || !node.auth.has_credentials) {
node.error("auth configuration is missing");
return
}
const {apiKey, oktaDomain} = config.auth;
const groupName = RED.util.evaluateNodeProperty(
config.groupName, config.userNameType, node, msg
)
const url = 'https://' + oktaDomain + '.okta.com/api/v1/groups?search=profile.name+eq+%22' + groupName + '%22';
const options = {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'SSWS ' + apiKey
}
};
const req = https.get(url, options, (res) => {
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
try {
const parsedData = JSON.parse(rawData);
if (parsedData.length > 0) { // Okta doesn't allow 2 groups with the same name so this case isn't taken care of
msg.groupID = parsedData[0].id;
node.send([msg, null]);
}else{
node.send([null, msg]);
}
} catch (error) {
node.error(error);
node.send([null, error]);
}
});
});
req.on('error', (error) => {
node.warn(error);
node.send([null, error]);
});
req.end();
});
}
RED.nodes.registerType("okta_does_group_exist", okta_does_group_exist_node);
};