This repository has been archived by the owner on Jan 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken.js
94 lines (79 loc) · 1.69 KB
/
token.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
const nanoid = require("nanoid");
const filehandler = require('./filehandler');
let list;
const refresh = () => {
try {
delete require.cache[require.resolve('./data/token.json')];
} catch (e) {
}
try {
list = new Map(require('./data/token.json'));
} catch (e) {
list = new Map();
}
return list;
};
const save = () => {
filehandler.saveFile('token.json', JSON.stringify([...list]));
};
exports.get = () => refresh();
exports.generate = (name) => {
refresh();
let token = {};
token.publicToken = nanoid();
token.privateToken = nanoid();
list.set(name, token);
save();
return token.publicToken;
};
exports.revoke = (name) => {
refresh();
if (list.has(name)) {
list.delete(name);
save();
return true;
} else {
save();
return false;
}
};
exports.getNames = () => {
refresh();
list.keys();
};
exports.getTokens = () => {
refresh();
list.values();
};
exports.checkHasToken = (name) => {
refresh();
return list.has(name);
};
exports.doCheckPublicToken = (name, pub) => {
refresh();
if (exports.checkHasToken(name)) {
return list.get(name).publicToken === pub;
} else {
return false;
}
};
exports.getPublicToken = (name) => {
refresh();
if (exports.checkHasToken(name)) {
return list.get(name).publicToken;
} else {
return null;
}
};
exports.getPrivateToken = (name, pub) => {
refresh();
if (exports.doCheckPublicToken(name, pub)) {
return list[name].privateToken;
} else {
return null;
}
};
exports.resetTokenList = () => {
list = new Map();
save();
};