This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
102 lines (87 loc) · 2.21 KB
/
users.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
/*
* This provides function for getting and adding users.
* It synchronizes users to the './users.xml' file.
*/
const utils = require("./utils");
const fs = require("fs");
const crypto = require('crypto');
const xml = require("xml2js");
const xmlBuilder = new xml.Builder();
const USERS_FILE = "users.xml";
let users = [];
class User {
constructor(username, password, token) {
this.username = username;
this.password = password;
this.token = token;
}
}
exports.User = User;
function writeUsers() {
try {
let u = xmlBuilder.buildObject({ "users": { "user": users } });
u = utils.injectDTD(u, "users", "users.dtd");
fs.writeFileSync(USERS_FILE, u, null);
} catch (e) {
console.error(e);
}
}
exports.readUsersFromFile = function() {
try {
xml.parseString(fs.readFileSync(USERS_FILE), (err, result) => {
if (err != null) {
console.error(err);
return;
}
for (let user of result.users.user) {
if (user.token === undefined) {
user.token = [ "" ]
}
users.push(new User(user.username[0], user.password[0], user.token[0]));
}
});
} catch(err) {
console.error(err);
}
console.log("Read users");
};
exports.getUsers = function() {
return users;
};
exports.existsUser = function(username) {
let user = users.find(el => el.username === username);
if (user) {
return true;
} else {
return false;
}
};
exports.addUser = function(username, password) {
const hash = crypto.createHash("md5").update(password).digest("hex");
users.push(new User(username, hash, ""));
writeUsers();
};
exports.checkCredentials = function(username, password) {
const hash = crypto.createHash("md5").update(password).digest("hex");
let user = users.find(el => el.username === username);
if (user !== undefined && user.password === hash) {
let randString = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
user.token = randString;
writeUsers();
return username + ":" + randString;
} else {
return "";
}
}
exports.checkToken = function(username, token) {
let user = users.find(el => el.username === username);
if (user) {
if (user.token !== "" && user.token === token) {
return true;
} else {
return false;
}
} else {
return false;
}
}