-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
349 lines (255 loc) · 8.42 KB
/
preload.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
const fs = require('fs');
const https = require('https');
const express = require('express');
const basicAuth = require('express-basic-auth');
const forge = require('node-forge');
const URL = require('url').URL;
const { BrowserWindow, shell } = require('electron').remote;
const Config = require('./config');
const { startServerWithCrypto, getPasswordFromOSKeyStore } = require('./authFactory');
const rest = express();
const port = 33457;
var intervalObj = null;
var issueQueueCount = null;
var previousIssueQueueCount = null;
var win = null;
var DEV_MODE = false;
var DEV_INTERVAL = DEV_MODE ? 1 : 60;
var config = new Config();
rest.use(express.json());
function loadConfig(callback) {
getPasswordFromOSKeyStore().then((pw) => {
config.load(pw, (config) => {
callback && callback();
});
});
}
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
loadConfig(() => {
config.setFormValues();
enableInterval();
});
document.getElementById("save").addEventListener('click', () => {
config.getFormValues();
getPasswordFromOSKeyStore().then((pw) => {
config.save(pw, () => {
alert("Config has been saved.");
});
});
});
$('#installChromeExtension').click(function () {
const chromeExtensionUrl = 'https://chrome.google.com/webstore/detail/oaihfnoiihagifdgcdagfkecnafncefh';
shell.openExternal(chromeExtensionUrl);
//shell.openItem('./extension/cookieextractor.crx');
});
$('#enableFunctionality').change(function () {
if ($(this).is(':checked')) {
enableInterval();
} else {
clearInterval(intervalObj);
}
});
function enableInterval() {
intervalObj = setInterval(() => {
var headers = {
'Content-Type': 'application/json',
'X-ExperimentalApi': 'opt-in'
};
if (config.crowdTokenKey) {
var cookie = `crowd.token_key=${config.crowdTokenKey}`;
headers['Cookie'] = cookie;
} else {
headers["Authorization"] = "Basic " + btoa(config.username + ":" + config.password);
}
var hostname = new URL(config.baseurl).hostname;
const options = {
hostname: hostname,
port: 443,
path: `/rest/servicedeskapi/servicedesk/${config.serviceDeskId}/queue/${config.queueId}?includeCount=true`,
method: 'GET',
headers: headers
};
var bodyBuffer = [];
const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
bodyBuffer = bodyBuffer.concat(d);
});
res.on('end', () => {
var statusCode = res.statusCode;
if (statusCode !== 200 && statusCode !== 201) {
alert("Error occured: " + bodyBuffer.toString());
} else {
var body = JSON.parse(bodyBuffer.toString());
console.log(body);
console.log('No more data in response.');
processResponse(body.issueCount);
}
})
});
req.on('error', (e) => {
alert("Error occured: " + JSON.stringify(e));
});
req.end();
}, config.interval * DEV_INTERVAL * 1000);
}
})
function processResponse(issueQueueCount) {
if (previousIssueQueueCount != null && issueQueueCount > previousIssueQueueCount) {
if (win == null) {
openDialog();
} else {
console.log("Windows already open.");
}
}
previousIssueQueueCount = issueQueueCount;
}
function openDialog() {
//https://www.christianengvall.se/electron-show-messagebox/
win = new BrowserWindow({
width: 540,
height: 200,
center: true,
alwaysOnTop: true,
webPreferences: {
preload: `${__dirname}/dialogpreload.js`
}
});
win.on('closed', () => {
win = null
})
win.loadURL(`file://${__dirname}/dialog.html`)
}
rest.post('/setcrowdtokenkey', function (req, res) {
if (!config.chromeExtensionPassword) {
throw new Error("Chrome extension not authorized. Aborted.");
}
if (req.body && req.body.crowdtokenkey) {
const token = req.body.crowdtokenkey;
getPasswordFromOSKeyStore().then((pw) => {
config.load(pw, (config) => {
config.crowdTokenKey = token;
config.save(pw, function() {
config.setFormValues();
console.log("Config has been saved.");
res.send({ message: "OK" });
});
});
});
} else {
throw new Error("Token missing in payload.");
}
});
// TODO
// https://github.com/jaredhanson/oauth2orize
// https://developer.chrome.com/apps/app_identity#non
// https://oauth.net/code/nodejs/
// https://developer.okta.com/blog/2019/08/22/okta-authjs-pkce/?utm_campaign=text_website_all_multiple_dev_dev_oauth-pkce_null&utm_source=oauthio&utm_medium=cpc
var newsalt = '';
rest.post('/authorizechromeextension', function (req, res) {
newsalt = '';
if (req.body && req.body.salt) {
console.log(req.body.salt);
var md = forge.md.sha256.create();
newsalt = forge.util.bytesToHex(forge.random.getBytesSync(10)).toUpperCase();
var digest = md.update(req.body.salt).digest().toHex();
var stringtocompare = forge.util.bytesToHex(forge.random.getBytesSync(4)).toUpperCase();
res.send({
newsalt: newsalt,
digest: digest,
stringtocompare: stringtocompare
});
alert(stringtocompare.split('').join(' '));
}
});
rest.post('/authorizechromeextensiontoken', function (req, res) {
if (req.body) {
var data = req.body;
var respdigest = data.digest;
var md = forge.md.sha256.create();
var newdigest = md.update(newsalt).digest().toHex();
var salt = data.salt;
if (respdigest === newdigest) {
var newpassword = forge.util.bytesToHex(forge.random.getBytesSync(16)).toUpperCase();
var md2 = forge.md.sha256.create();
var prepayload = { salt: salt, password: newpassword };
var digest = md2.update(prepayload).digest().toHex();
var payload = {
salt: prepayload.salt,
password: prepayload.password,
digest: digest
};
getPasswordFromOSKeyStore().then((pw) => {
config.load(pw, (config) => {
config.chromeExtensionPassword = newpassword;
config.save(pw, (config) => {
rest.use(basicAuth({
users: {
'chromeext': config.chromeExtensionPassword
},
challenge: true
}));
newsalt = '';
res.send(payload);
});
});
});
} else {
newsalt = '';
res.send({ message: "Digests not matching. Aborted." });
}
} else {
newsalt = '';
res.send({ message: "Please send payload. Aborted." });
}
});
rest.get('/test', function (req, res) {
var stringtocompare = forge.util.bytesToHex(forge.random.getBytesSync(4)).toUpperCase().split('').join(' ');
res.send('Test request over HTTPS was successful. You can compare your verification code with you application: ' + stringtocompare + '. You can close this window.');
alert('Verification code: ' + stringtocompare);
});
rest.use('/extension', express.static(__dirname + '/extension'));
// const filehosting = express();
// filehosting.use('/extension', express.static(__dirname + '/extension'));
/*
filehosting.get('/cookieextractor.crx', function (req, res) {
res.download('./cookieextractor.crx', 'cookieextractor.crx');
});
*/
// http://localhost:33458/cookieextractor/updates.xml
// filehosting.listen(filehostingport, () => console.log(`Chrome extension hosting listening on port ${filehostingport}!`))
loadConfig(function () {
if (config.chromeExtensionPassword) {
rest.use(basicAuth({
users: {
'chromeext': config.chromeExtensionPassword
},
challenge: true
}));
} else {
rest.use(basicAuth({
users: {
'authorize': 'supersecret'
},
challenge: true
}));
}
startServerWithCrypto(
(encKey, cert, password) => https.createServer({
key: encKey,
cert: cert,
passphrase: password
}, rest)
.listen(port, () => console.log(`Chrome extension connector listening on port ${port}!`))
);
});