-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (60 loc) · 1.82 KB
/
index.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
const { createHmac } = require("crypto");
const axios = require("axios");
// Credentials and Endpoint URL
const clientId = "<client id here>";
const secret = "<client secret here>";
const tokenEndpointUrl =
"<SAT token endpoint here. See SAT Playground code examples to easily copy this URL>";
function getSignature() {
const timestamp = Math.trunc(new Date().getTime() / 1000);
const toSign = timestamp + clientId;
const signature = createHmac("sha256", secret).update(toSign).digest("hex");
return { clientId, timestamp, signature };
}
// Retrieves a Secure Anonymous Token (JWT).
async function getToken() {
const { clientId, timestamp, signature } = getSignature();
// Include optional params such as first_name, last_name, email_domain, email, or metadata.
const data = {
first_name: "Jason",
last_name: "Thompson",
email: "[email protected]",
metadata: {
exampleId: 123456,
language: "EN",
timezone: "PT",
},
};
const result = await axios({
method: "post",
url: tokenEndpointUrl,
headers: {
"X-Frame-ClientId": clientId,
"X-Frame-Timestamp": timestamp,
"X-Frame-Signature": signature,
},
data: data,
});
return result.data;
}
// Retrieves metadata or "assertions" using the generated SAT.
async function getUserAssertions(token) {
const result = await axios({
url:
"https://cpanel-backend-prod.frame.nutanix.com/api/rest/v1/me/assertions",
headers: { Authorization: `Bearer ${token}` },
});
return result.data;
}
// Run the script.
(async () => {
console.log("Retrieving token");
try {
const token = await getToken();
console.log(`Frame SAT: ${token}`);
const metadata = await getUserAssertions(token);
console.log(`Retrieved assertions: ${metadata}`);
} catch (err) {
console.error(err);
}
})();