-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
204 lines (181 loc) · 5.7 KB
/
main.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
const { app, protocol, BrowserWindow, ipcMain, dialog } = require('electron');
const url = require('url');
const request = require('request');
const qrcode = require('qrcode');
const { base32, base64 } = require('rfc4648');
const path = require("node:path");
let win
let client_id='g2c2pjLUThOaBumECqbf'
let token
function setStatus(stat) {
win.webContents.send("status", stat);
}
function setDetail(detail) {
win.webContents.send("detail", detail);
}
function setError(headline, detail) {
setStatus("ERROR: " + headline);
setDetail(detail);
}
function showCodeForm(on) {
if (on) {
win.webContents.send("showform", "block");
} else {
win.webContents.send("showform", "none");
}
}
function verifyCode(code) {
const options = {
method: 'POST',
url: 'https://api.my.gov.au/authbiz-ext-sec/api/v1/authclients/g2c2pjLUThOaBumECqbf/totpverify.json',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: {
password: code,
},
json: true,
};
request(options, function (error, response, body) {
if (error) {
setError("Code verification failed, try again", error);
console.log("error: " + error)
return;
}
if (body == null) { // lol
setStatus("Enrolment complete! DON'T LOSE THE CODE");
showCodeForm(false);
return;
} else if (body.error) {
setError("Code verification failed, try again", body.error + ' - ' + body.error_description);
console.log("body error: " + body.error)
return;
} else {
console.log("body?? " + body)
console.dir(body)
msg = "myGov error: " + body.info[0].code + " " + body.info[0].message
msg += "\n Make sure you're using an OTP tool that supports SHA1 keys!"
dialog.showMessageBox({
"type": "error",
"title": "myGov error",
"message": msg,
"buttons": ["OK"],
})
}
});
}
function makeQR(secret) {
secret32 = base32.stringify(base64.parse(secret))
secret32 = secret32.replace(/=+$/, "")
totp_uri = 'otpauth://totp/myGov?secret=' + secret32 + '&algorithm=SHA512'
qrcode.toDataURL(totp_uri)
.then(url => {
setStatus("Enroll this secret in your TOTP client and enter the current code");
setDetail(totp_uri + '<p><img src="' + url + '"/>');
showCodeForm(true);
})
.catch(err => {
setError("QR encoding error", err);
})
}
function requestSecret(token) {
setStatus("Requesting OAuth secret...");
const options = {
method: 'POST',
url: 'https://api.my.gov.au/authbiz-ext-sec/api/v1/authclients/g2c2pjLUThOaBumECqbf/totpcredential.json',
headers: {'Authorization': 'Bearer ' + token},
};
request(options, function (error, response, body) {
if (error) {
setError("Secret request failed", error);
return;
}
body = JSON.parse(body)
if (body.error) {
setError("Secret request failed",
body.error + ' - ' + body.error_description);
return;
}
secret = body.secret;
makeQR(secret);
});
}
function requestToken(code) {
setStatus("Requesting OAuth token...");
const options = {
method: 'POST',
url: 'https://auth.my.gov.au/mga/sps/oauth/oauth20/token',
form: {
client_id: client_id,
redirect_uri: 'au.gov.my://app',
grant_type: 'authorization_code',
code: code,
},
};
request(options, function (error, response, body) {
if (error) {
setError("Token request failed", error);
return;
}
body = JSON.parse(body)
if (body.error) {
setError("Token request failed",
body.error + ' - ' + body.error_description);
return;
}
token = body.access_token;
requestSecret(token);
});
}
function createWindow () {
protocol.registerFileProtocol('au.gov.my', (req, callback) => {
console.log(req.url)
query = url.parse(req.url, true).query
if ("code" in query) {
callback({ path: `${__dirname}/ui.html` })
requestToken(query.code)
} else {
if ("error_code" in query) {
console.log("set error")
callback({ path: `${__dirname}/instructions.html` })
dialog.showMessageBox({
"type": "error",
"title": "myGov error",
"message": "myGov error: " + query.error_code,
"buttons": ["OK"],
})
}
}
}, (error) => {
if (error) console.error('Failed to register protocol')
})
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
preload: path.join(__dirname, "preload.js")
}
})
win.loadFile(path.join(__dirname, "instructions.html"));
ipcMain.on('code', (event, arg) => {
verifyCode(arg);
});
win.on('closed', () => {
win = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (win === null) {
createWindow()
}
})