-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
121 lines (101 loc) · 3.09 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
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
const debug = require('debug')('authtoken:main');
const redis = require('redis');
const shortid = require('shortid');
const crypto = require('crypto');
const sentinel = require('redis-sentinel');
const mongojs = require('mongojs');
const Keys = require('./lib/Keys');
/**
* authtoken main function.
* @returns {Function|*}
*/
function authtoken(params = {}) {
this.mws = (req, res, next) => {
debug('do request: ', req.path);
if (this.ready) {
if (req.headers.tokenservice && req.headers.tokenservice === 'login') {
debug('login');
return this.login(req.headers.apikey || '', req.headers.secret || '', res)
.then((secretToken) => {
res.set('secret-token', secretToken);
this.send(res, 'Login OK');
})
.catch((err) => {
debug('catch KLKTR43: ', err.toString());
return this.sendError(res, err);
});
}
return this.check(req, res)
.then((razon) => {
debug('pass, next called: ', razon);
next();
return true;
})
.catch((err) => {
debug('Catch Check: ', err.toString());
this.sendError(res, err);
return err;
});
}
return res.end(this.params.startupMessage);
};
return this.run(params);
}
require('./prototypes')(authtoken);
authtoken.prototype.run = function run(params) {
debug('run called: ', params);
this.params = Object.assign({}, {
mongodb: 'authtoken',
startupMessage: 'Waiting for AUTH Service...',
redis: '',
refreshKeys: 60,
base: '/',
excludes: [],
forcelogin: false,
}, params);
this.collections = params.collections || ['tokens', 'keys'];
this.context = {
mongodb: mongojs(this.params.mongodb, this.collections),
redis: (() => {
if (Object.prototype.toString.call(this.params.redis) === '[object Array]') {
return sentinel.createClient(this.params.redis, null, null);
}
return redis.createClient(this.params.redis);
})(),
};
this.context.redis.on('error', err => debug('Redis.error: ', err));
this.context.redis.on('connect', () => debug('Redis.connect '));
this.Keys = new Keys(this.context);
this.ready = false;
const init = () => {
this.loadKeys()
.then(() => {
debug('Keys loaded');
this.ready = true;
})
.catch((err) => {
debug('Reject Starting: ', err);
this.params.startupMessage = 'AUTH Error: starting faild.';
});
};// end init
this.interval = setInterval(() => {
this.loadKeys()
.catch(() => {
this.ready = false;
});
}, this.params.refreshKeys * 1000);
init();
return this.mws;
};
authtoken.prototype.sendError = (res, err) => res.status(401).end(JSON.stringify({
Error: true,
msg: err,
timestamp: new Date().getTime(),
}));
authtoken.prototype.send = (res, msg) => res.end(JSON.stringify({
Error: null,
msg,
timestamp: new Date().getTime(),
}));
authtoken.prototype.generateSecretToken = () => `${crypto.randomBytes(20).toString('hex')}-${shortid.generate()}`;
module.exports = authtoken;