-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.js
37 lines (31 loc) · 906 Bytes
/
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
class CSTFToken {
constructor() {
this.token_prefix = 'cstf_token';
this.token = new Map();
this.tokenLength = 32;
}
generateToken (sessionId) {
const token = this.randomString();
this.token.set(sessionId, token);
return token;
}
validateToken(sessionId, token) {
if (!this.token.has(sessionId)) return false;
if (this.token.get(sessionId) && this.token.get(sessionId) === token) {
this.token.delete(sessionId);
return true;
}
return false;
}
randomString() {
let result = '';
const chars = 'skdskdjskdjskd38748738473483874u';
for (let i = 0; i < this.tokenLength; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
getTokenName() {
return this.token;
}
}