-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (76 loc) · 2.06 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import QRCode from 'qrcode';
import axios from "axios";
import path from 'path';
import * as fs from 'fs';
// Usernames, you want to generate qrcodes for:
let usernames = ['pwinter', 'tschroeder'];
// your confluence-url
let serverUrl = 'https://demonstration.linchpin-intranet.com';
// your admin authentication
let adminname = "";
let adminpassword = "";
// how long is the qrcode valid (hours)
const qrcodeValidFor = 24;
// timeout after each request in ms
const TIMEOUT_MS=100;
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
async function createToken (username) {
let response = await axios.put(
serverUrl + '/rest/linchpin-mobile/1.0/firescope/user/qrcode',
{"userName": username, "expiry": 1, "qrcodetype": qrcodeValidFor},
{
auth: {
username: adminname,
password: adminpassword
},
}
);
return response.data;
}
function checkTargetFolder () {
return new Promise((resolve, reject) => {
if (!fs.existsSync('qrcodes')) {
console.log("not exists");
fs.mkdir("qrcodes", {}, (error) => {
if (!error) {
resolve();
} else {
reject();
}
});
} else {
resolve();
}
})
}
async function run () {
await checkTargetFolder();
for (let username of usernames) {
let token;
await new Promise(r => setTimeout(r, TIMEOUT_MS));
console.log("---------------------");
console.log("working on user 👤", username);
try {
token = await createToken(username);
console.log("✓ Succeeded to get a token from server for user 👤", username);
} catch (e) {
if (e.response && e.response.status) {
console.error("error getting token, server return status ", e.response.status, e.response.statusText);
} else {
console.error("✗ could not get token from server", e.message);
}
}
if (token) {
let savepath = path.resolve('qrcodes', username + '.png');
await QRCode.toFile(savepath, JSON.stringify(token));
console.log("✓ code for " + username + " saved to qrcodes dir " + savepath);
}
}
}
try {
(async () => {
await run()
})();
} catch (error) {
console.log("error", error);
}