-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (70 loc) · 2.31 KB
/
index.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
'use strict';
const Hashids = require('hashids/cjs');
const NODE_ENV = process.env.NODE_ENV;
const isProduction = (NODE_ENV === 'production');
const IGNORE_AUTH_PATH_REGEX = process.env.IGNORE_AUTH_PATH_REGEX || '';
function isIgnoredPath(path) {
return IGNORE_AUTH_PATH_REGEX !== '' && path.search(IGNORE_AUTH_PATH_REGEX) >= 0;
}
function genHashId(idHashids, id = 141236) {
const time = Date.now(); // Timestamp
const idHash = idHashids.encode([id, time]); // encode id and time to fake hex
return idHash;
}
function decHashId(idHashids, encrypted) {
const decoded = idHashids.decode(encrypted);
return decoded;
}
function hashKeyMiddlewareWrapper (options = {
salt: process.env.SALT || 'my salt',
hashkeyAliveTime: process.env.HASHKEY_ALIVE_TIME || 15,
isMainModule: false,
}) {
const hashSalt = options.salt;
// Hashkey alive time (minutes)
const HASHKEY_ALIVE_TIME = options.hashkeyAliveTime * 60 * 1000;
const idHashids = new Hashids(hashSalt, 32, '0123456789abcdef');
if (options.isMainModule === true) {
const hashid = genHashId(idHashids, process.argv[2]);
let decoded = decHashId(idHashids, process.argv[2]);
decoded = { id: decoded[0], time: new Date(decoded[1]) };
return {
hashid,
decoded: decoded || decHashId(idHashids, hashid),
};
}
return function hashKeyMiddleware(req, res, next) {
if (isIgnoredPath(req.path)) {
return next();
}
const hashKey = req.query.hashkey || req.query.access_token;
const decodedKey = decHashId(idHashids, hashKey);
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const err = new Error('E_ACCESS_DENIED');
err.name = 'E_ACCESS_DENIED';
err.statusCode = 403;
res.locals.ip = ip;
if (decodedKey.length < 2) {
next(err);
return;
}
const now = Date.now();
const timeFromHash = decodedKey[1];
if (isProduction && (HASHKEY_ALIVE_TIME > 0) && (timeFromHash + (HASHKEY_ALIVE_TIME) < now)) {
next(err);
return;
}
res.locals.userId = decodedKey[0];
res.locals.hashKey = hashKey;
next();
};
};
if (require.main === module) {
const hashid = hashKeyMiddlewareWrapper({
salt: process.env.SALT,
isMainModule: true,
});
console.log(hashid);
} else {
module.exports = hashKeyMiddlewareWrapper;
}