summaryrefslogtreecommitdiff
path: root/modules/timetrack2/timetrack2.js
blob: cb22132a6d1c3ff6e476e77f047dc58580509c7a (plain)
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
"use strict";

var cmn = require("../$common.js"),
    persist = require("node-persist"),
    crypto = require("crypto"),
    basicAuth = require("basic-auth"),
    fs = require("fs");

var moddir = null;

var ROOT_ENDPOINT = "/timetrack2";

persist = persist.create({
	dir: cmn.persistdir + "/timetrack2",
	continuous: false,
	interval: false
});
persist.initSync();

//accounts: {
//  "user": {
//    "list": [{id: Int, sheet: String, text: String, indate: Date, outdate: Date}],
//    "current": {sheet: String, text: String, indate: Date}/null,
//    "pwhash": hash (String)
//  }
//}
var nextid = persist.getItemSync("nextid");
if (nextid == null) {
	nextid = 1;
	persist.setItemSync("nextid", nextid);
}
var accounts = persist.getItemSync("accounts");
var naccounts = 0;
(function() {
	if (accounts == null) {
		accounts = {};
		persist.setItemSync("accounts", accounts);
	} else {
		for (var user in accounts) {
			naccounts++;
			for (var ev of accounts[user].list) {
				ev.indate = new Date(ev.indate);
				ev.outdate = new Date(ev.outdate);
				if(nextid <= ev.id) nextid = ev.id+1;
			}
			if (accounts[user].current != null) {
				accounts[user].current.indate = new Date(accounts[user].current.indate);
			}
		}
		persist.setItemSync("nextid", nextid);
	}
})();


function scryptHash(password, cb) {
	crypto.randomBytes(16, function(err, salt){
		if (err) {
			cb(err, null);
			return;
		}
		crypto.scrypt(password, salt, 32, function(err, key){
			if (err) cb(err,null);
			else cb(null, salt.toString("hex") + "$" + key.toString("hex"));
		});
	});
}

function scryptCompare(password, hash, cb){
	hash = hash.split("$");
	if (hash.length != 2) {
		cb(new Error("Invalid hash in database"), null);
		return;
	}
	var salt = Buffer.from(hash[0], "hex"), shash = hash[1];
	crypto.scrypt(password, salt, 32, function(err, key){
		if (err) cb(err, null);
		else if(key.toString("hex") == shash) cb(null, true);
		else cb(null, false);
	});
}


function sendUnauth(res) {
	res.set("WWW-Authenticate", "Basic realm=Authorization required");
	return res.sendStatus(401);
}

function unknownUserHandler(req, res, next){
	res.sendFile(moddir + "/unknownuser.html");
}

function authMiddleware(req, res, next){
	var user = basicAuth(req);
	req.authuser = null;
	if (!user || !user.name) {
		sendUnauth(res);
		return;
	}
	req.authuser = user.name;
	if (accounts[req.authuser]) {
		scryptCompare(user.pass, accounts[req.authuser].pwhash, function(err, ok){
			if (ok) next();
			else sendUnauth(res);
		});
	} else {
		unknownUserHandler(req, res, next);
	}
}

function asciiValid(str) {
	var i, c;
	for(i = 0; i < str.length; i++) {
		c = str.charCodeAt(i);
		if (c < 32 || c >= 127) return false;
	}
	return true;
}


module.exports = function(app, io, _moddir){
	moddir = _moddir;

	//first the endpoints that need to bypass authMiddleware
	app.get(ROOT_ENDPOINT + "/authfail", function(req, res){
		sendUnauth(res);
	});
	app.post(ROOT_ENDPOINT + "/createuser", function(req, res){
		var user = basicAuth(req);
		if (!user || !user.name) {
			res.status(400).send("No credentials sent");
			return;
		}
		if (user.name.length < 3 || user.name.length > 32 || !asciiValid(user.name)) {
			res.status(400).send("Invalid username");
			return;
		}
		if (user.pass.length < 3 || user.pass.length > 32 || !asciiValid(user.pass)) {
			res.status(400).send("Invalid password");
			return;
		}
		if (accounts[user.name]) {
			res.status(400).send("User already exists");
			return;
		}
		if (naccounts >= 20) {
			res.status(500).send("Too many accounts created, please contact Tom...");
			return;
		}
		scryptHash(user.pass, function(err, hash){
			if (!hash) {
				res.status(500).send("Something went wrong...");
				console.log(err);
				return;
			}
			accounts[user.name] = {
				list: [],
				current: null,
				pwhash: hash
			};
			naccounts++;
			persist.setItemSync("accounts", accounts);
			res.status(200).end();
		});
	});

	app.all([ROOT_ENDPOINT, ROOT_ENDPOINT+"/*"], authMiddleware); //for all the other endpoints

	app.get(ROOT_ENDPOINT, function(req, res){
		res.sendFile(moddir + "/timetrack.html");
	});
	app.get(ROOT_ENDPOINT + "/list", function(req, res){
		res.json({
			list: accounts[req.authuser].list,
			current: accounts[req.authuser].current,
		});
	});
	app.get(ROOT_ENDPOINT + "/sheets", function(req, res){
		var seen = new Map();
		var list = [];
		for (var i = 0; i < accounts[req.authuser].list.length; i++){
			if (!seen.has(accounts[req.authuser].list[i].sheet)) {
				seen.set(accounts[req.authuser].list[i].sheet, 0);
				list.push(accounts[req.authuser].list[i].sheet);
			}
		}
		res.json(list);
	});
	app.delete(ROOT_ENDPOINT + "/event", function(req, res){
		var id = +req.body;
		var i;
		var fail = false;
		if (id < 0 || ~~id != id || isNaN(id) || !(req.authuser in accounts)) {
			fail = true;
		} else {
			var userevents = accounts[req.authuser].list;
			for (i = 0; i < userevents.length; i++) if (userevents[i].id == id) break;
			if (i == userevents.length) fail = true;
			else {
				userevents.splice(i, 1);
				persist.setItemSync("accounts", accounts);
			}
		}
		if (fail) res.status(404).send("Unknown id");
		else res.status(200).end();
	});
	app.post(ROOT_ENDPOINT + "/checkin", function(req, res){
		var obj;
		try {
			obj = JSON.parse(req.body);
		} catch (e) {
			res.status(400).send("Invalid request");
			return;
		}
		var sheet = obj.sheet + "", text = obj.text + "", date = new Date(obj.date);
		if (sheet.length == 0 || isNaN(date.getTime())) {
			res.status(400).send("Invalid data");
			return;
		}
		if (accounts[req.authuser].list.length >= 4000) {
			res.status(400).send("Isn't 4000 events enough for you?");
			return;
		}
		if (accounts[req.authuser].current != null) {
			res.status(409).send("Already checked in");
			return;
		}
		accounts[req.authuser].current = {
			sheet: sheet,
			text: text,
			indate: date,
		};
		persist.setItemSync("accounts", accounts);
		res.status(200).end();
	});
	app.post(ROOT_ENDPOINT + "/checkout", function(req, res){
		var obj;
		try {
			obj = JSON.parse(req.body);
		} catch (e) {
			res.status(400).send("Invalid request");
			return;
		}
		var date = new Date(obj.date);
		if (isNaN(date.getTime())) {
			res.status(400).send("Invalid data");
			return;
		}
		if (accounts[req.authuser].current == null) {
			res.status(409).send("Not checked in");
			return;
		}
		if (accounts[req.authuser].list.length >= 4000) {
			res.status(400).send("Isn't 4000 events enough for you?");
			return;
		}
		var current = accounts[req.authuser].current;
		accounts[req.authuser].list.push({
			id: nextid++,
			sheet: current.sheet,
			text: current.text,
			indate: current.indate,
			outdate: date,
		});
		accounts[req.authuser].current = null;
		accounts[req.authuser].list.sort(function(a, b) { return a.indate - b.indate; });
		persist.setItemSync("accounts", accounts);
		persist.setItemSync("nextid", nextid);
		res.status(200).end();
	});
};