-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure_config.js
75 lines (62 loc) · 2.03 KB
/
azure_config.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
const axios = require("axios");
module.exports = function (RED) {
function AzureAdConfigNode(config) {
RED.nodes.createNode(this, config);
// Validate if the required JSON field is present
if (!this.credentials || !this.credentials.tenantId) {
this.error("Tenant Id is missing");
return;
}
if (!this.credentials || !this.credentials.clientId) {
this.error("Client id is missing");
return;
}
if (!this.credentials || !this.credentials.clientSecret) {
this.error("Client secret is missing");
return;
}
this.name = config.name;
this.tenantId = this.credentials.tenantId;
this.clientId = this.credentials.clientId;
this.clientSecret = this.credentials.clientSecret;
this.has_credentials = this.tenantId && this.clientId && this.clientSecret;
async function generateToken() {
const formUrlEncoded = (x) =>
Object.keys(x).reduce(
(p, c) => p + `&${c}=${encodeURIComponent(x[c])}`,
""
);
try {
const clientId = this.clientId;
const clientSecret = this.clientSecret;
const tenantId = this.tenantId;
const tokenEndpoint =
"https://login.microsoftonline.com/" +
tenantId +
"/oauth2/v2.0/token";
const bodyFormData = {
grant_type: "client_credentials",
client_id: clientId,
client_secret: clientSecret,
scope: "https://graph.microsoft.com/.default",
};
const response = await axios({
url: tokenEndpoint,
headers: { "Content-Type": "application/x-www-form-urlencoded" },
data: formUrlEncoded(bodyFormData),
});
return response.data.access_token;
} catch (error) {
console.error(error);
}
};
this.get_access_token = generateToken;
}
RED.nodes.registerType("azure_config", AzureAdConfigNode, {
credentials: {
tenantId: { type: "text" },
clientId: { type: "text" },
clientSecret: { type: "text" },
},
});
};