-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathokta_users_to_id.js
73 lines (58 loc) · 2.22 KB
/
okta_users_to_id.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
module.exports = function(RED) {
function okta_users_to_id_Node(config) {
RED.nodes.createNode(this, config);
var node = this;
var results = [];
node.on('input', async function(msg) {
const https = require('https');
node.auth = RED.nodes.getNode(config.auth);
if (!node.auth || !node.auth.has_credentials) {
node.error("auth configuration is missing");
return
}
const emails = RED.util.evaluateNodeProperty(
config.emails, config.emailsType, node, msg
)
const {apiKey, oktaDomain} = config.auth;
const options = {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'SSWS ' + apiKey
}
};
try {
for (const email of emails) {
const url = 'https://' + oktaDomain + '.okta.com/api/v1/users?search=profile.email+eq+%22' + email + '%22';
const rawData = await new Promise((resolve, reject) => {
const req = https.get(url, options, (res) => {
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
resolve(rawData);
});
});
req.on('error', (error) => {
reject(error);
});
req.end();
});
const parsedData = JSON.parse(rawData);
if (parsedData.length > 0){
const userId = parsedData[0].id;
results.push(userId);
}else {
throw new Error('The email ' + emails + ' Doesn\'t exist in Okta');
}
}
msg.usersId = results;
node.send(msg);
} catch (error) {
node.error(error);
}
});
}
RED.nodes.registerType("okta_users_to_id", okta_users_to_id_Node);
};