-
Notifications
You must be signed in to change notification settings - Fork 2
/
token.js
44 lines (37 loc) · 942 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
38
39
40
41
42
43
44
/*
Generate, stores and verifies tokens
*/
var returnError = require('./error');
var tokenTable = [];
// Private function to store token
var _storeToken = function (token, cookie) {
tokenTable.push({
token: token,
cookie: cookie
});
};
// Generate a 5 char randmo token
// TODO: Check if token hasn't been already attributed
exports._generateToken = function (cookie, cb) {
require('crypto').randomBytes(3, function(ex, buf) {
var token = buf.toString('hex');
_storeToken(token, cookie);
cb('', token);
});
};
// Private function to verify token validity
exports._verifyToken = function (token, cb) {
if(!token) {
return cb(returnError.missingToken);
}
var i = 0;
var found = 0;
for (i = 0; i < tokenTable.length; i++) {
if(tokenTable[i].token === token) {
found = 1;
return cb('', tokenTable[i].cookie);
}
}
if(found === 0)
return cb(returnError.invalidToken);
};