-
Notifications
You must be signed in to change notification settings - Fork 76
/
ygopro-auth.js
192 lines (170 loc) · 5.08 KB
/
ygopro-auth.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
// Generated by CoffeeScript 2.6.1
(function() {
/*
Main script of new dashboard account system.
The account list file is stored at `./config/admin_user.json`. The users are stored at `users`.
The key is the username. The `permissions` field could be a string, using a permission set from the example, or an object, to define a specific set of permissions.
eg. An account for a judge could be as follows, to use the default permission of judges,
"username": {
"password": "123456",
"enabled": true,
"permissions": "judge"
},
or as follows, to use a specific set of permissions.
"username": {
"password": "123456",
"enabled": true,
"permissions": {
"get_rooms": true,
"duel_log": true,
"download_replay": true,
"deck_dashboard_read": true,
"deck_dashboard_write": true,
"shout": true,
"kick_user": true,
"start_death": true
}
},
*/
var add_log, bunyan, check_permission, default_data, fs, loadJSON, loadJSONPromise, log, moment, reload, save, setting_save, users, util;
fs = require('fs');
loadJSON = require('load-json-file').sync;
loadJSONPromise = require('load-json-file');
moment = require('moment');
moment.updateLocale('zh-cn', {
relativeTime: {
future: '%s内',
past: '%s前',
s: '%d秒',
m: '1分钟',
mm: '%d分钟',
h: '1小时',
hh: '%d小时',
d: '1天',
dd: '%d天',
M: '1个月',
MM: '%d个月',
y: '1年',
yy: '%d年'
}
});
bunyan = require('bunyan');
log = bunyan.createLogger({
name: "auth"
});
util = require('util');
if (!fs.existsSync('./logs')) {
fs.mkdirSync('./logs');
}
add_log = async function(message) {
var mt, res, text;
mt = moment();
log.info(message);
text = mt.format('YYYY-MM-DD HH:mm:ss') + " --> " + message + "\n";
res = false;
try {
await fs.promises.appendFile("./logs/" + mt.format('YYYY-MM-DD') + ".log", text);
res = true;
} catch (error) {
res = false;
}
return res;
};
default_data = loadJSON('./data/default_data.json');
setting_save = async function(settings) {
var e;
try {
await fs.promises.writeFile(settings.file, JSON.stringify(settings, null, 2));
} catch (error) {
e = error;
add_log("save fail");
}
};
default_data = loadJSON('./data/default_data.json');
try {
users = loadJSON('./config/admin_user.json');
} catch (error) {
users = default_data.users;
setting_save(users);
}
save = async function() {
return (await setting_save(users));
};
reload = async function() {
var user_backup;
user_backup = users;
try {
users = (await loadJSONPromise('./config/admin_user.json'));
} catch (error) {
users = user_backup;
await add_log("Invalid user data JSON");
}
};
check_permission = async function(user, permission_required) {
var _permission, permission;
_permission = user.permissions;
permission = _permission;
if (typeof permission !== 'object') {
permission = users.permission_examples[_permission];
}
if (!permission) {
await add_log("Permision not set:" + _permission);
return false;
}
return permission[permission_required];
};
this.auth = async function(name, pass, permission_required, action = 'unknown', no_log) {
var user;
await reload();
user = users.users[name];
if (!user) {
await add_log("Unknown user login. User: " + name + ", Permission needed: " + permission_required + ", Action: " + action);
return false;
}
if (user.password !== pass) {
await add_log("Unauthorized user login. User: " + name + ", Permission needed: " + permission_required + ", Action: " + action);
return false;
}
if (!user.enabled) {
await add_log("Disabled user login. User: " + name + ", Permission needed: " + permission_required + ", Action: " + action);
return false;
}
if (!(await check_permission(user, permission_required))) {
await add_log("Permission denied. User: " + name + ", Permission needed: " + permission_required + ", Action: " + action);
return false;
}
if (!no_log) {
await add_log("Operation success. User: " + name + ", Permission needed: " + permission_required + ", Action: " + action);
}
return true;
};
this.add_user = async function(name, pass, enabled, permissions) {
await reload();
if (users.users[name]) {
return false;
}
users.users[name] = {
"password": pass,
"enabled": enabled,
"permissions": permissions
};
await save();
return true;
};
this.delete_user = async function(name) {
await reload();
if (!users.users[name]) {
return false;
}
delete users.users[name];
await save();
};
this.update_user = async function(name, key, value) {
await reload();
if (!users.users[name]) {
return false;
}
users.users[name][key] = value;
await save();
};
}).call(this);