This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
126 lines (114 loc) · 3.56 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
const _ = require('lodash') // eslint-disable-line no-unused-vars
const { default: magister, getSchools } = require('magister.js')
const electron = require('electron') // eslint-disable-line no-unused-vars
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path')
const url = require('url')
const isDev = require('electron-is-dev')
const request = require('request')
if (isDev) {
require('electron-reload')(__dirname, {
electron: require(`${__dirname}/node_modules/electron`)
})
}
let mainWin
function createWindow () {
mainWin = new BrowserWindow({
width: 1220,
height: 800,
show: false,
frame: false,
icon: path.join(__dirname, 'img/icons/icon@64px.png')
})
mainWin.on('closed', function () {
app.quit()
})
mainWin.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
mainWin.once('ready-to-show', function () {
mainWin.show()
if (process.argv.includes('--debug')) {
mainWin.webContents.openDevTools()
}
})
}
ipcMain.on('validate-creds', (event, creds) => {
// Retrieve authentication code
request('https://magisterauthcode.webwevers.nl/magisterauthcode/code.json', { json: true }, (err, res, code) => {
var authOptions = {
username: creds.username,
password: creds.password
}
// Validate the response
if (!err && res.statusCode === 200) {
// If successful use the authenticationcode we
// just retrieved.
authOptions.authCode = code.code
}
// Retrieve schools
getSchools(creds.school)
// Retrieve the right school.
.then((schools) => schools[0])
// Authenticate.
.then((school) => magister(_.merge({ school: school }, authOptions)))
// Check if authentication was successful.
.then((m, err) => {
if (err) {
mainWin.webContents.send('login-success', false)
} else {
global.m = m
mainWin.webContents.send('login-success', true)
}
})
.catch((err) => {
if (err.name === 'AuthError') {
mainWin.webContents.send('login-success', false)
} else if (!(err instanceof TypeError)) {
console.log(err)
}
// Just call ourselves again, most of the time that fixes the error
// should probably add a limit though.
mainWin.webContents.send('validate-creds', creds)
})
})
})
ipcMain.on('validate-token', (event, creds) => {
// Retrieve authentication code
request('https://raw.githubusercontent.com/simplyGits/magisterjs-authcode/master/code.json', { json: true }, (err, res, code) => {
var authOptions = {
token: creds.token
}
// Validate the response
if (!err && res.statusCode === 200) {
// If successful use the authenticationcode we
// just retrieved.
authOptions.authcode = code
}
// Retrieve schools
getSchools(creds.school)
// Retrieve the right school.
.then((schools) => schools[0])
// Authenticate.
.then((school) => magister(_.merge({ school: school }, authOptions)))
// Check if authentication was successful.
.then((m, err) => {
if (err) {
mainWin.webContents.send('token-success', false)
} else {
global.m = m
mainWin.webContents.send('token-success', true)
}
}).catch((err) => {
if (!(err instanceof TypeError)) {
console.log(err)
}
mainWin.webContents.send('token-success', false)
})
})
})
app.once('ready', function () {
createWindow()
})