-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
247 lines (203 loc) · 6.47 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'use strict';
var url = require('url');
var base32 = require('thirty-two');
var totp = require('notp').totp;
/**
* Prevent users who aren't logged-in from accessing routes.
* Use `login.route` for redirection. Function also remembers the requested url
* and user is redirected after successful login. If `rest` is enabled
* you'll get a `401` response.
*
* @example
`config.js`
exports.login = {
route: '/login'
};
*
* @example
`app.js`
var config = require('./config.js');
app.get('/private', utils.restrict(config), function(req, res) {
res.send('only a logged in user can see this');
})
*
* @param {Object} [config] - Configuration object
* @param {String} [config.login.route='/login'] - Route that handles the login process
*/
exports.restrict = function(config) {
config = config || {};
var route = (config.login && config.login.route) || '/login';
return function(req, res, next) {
if (req.session.loggedIn) {return next(); }
if (config.rest) {return res.send(401); }
res.redirect(route + '?redirect=' + req.originalUrl);
};
};
/**
* Get type of database and database adapter name from connection information.
*
* @example
`config.js (CouchDB)`
exports.db = 'http://127.0.0.1:5984/';
*
* @example
`config.js (all other DBs)`
exports.db = {
url: 'postgres://127.0.0.1:5432/',
name: 'users',
collection: 'my_user_table'
}
*
* @example
`app.js`
var config = require('./config.js');
var db = util.getDatabase(config);
// {
// type: 'couchdb',
// adapter: 'lockit-couchdb-adapter'
// }
*
* @param {Object} config - Configuration object
* @param {String|Object} config.db - Database connection string / object
*
* @returns {Object} Object containing database `type` and `adapter`
*/
exports.getDatabase = function(config) {
var uri = config.db.url || config.db;
var urlObj = url.parse(uri);
var prot = urlObj.protocol;
var res = {};
switch (prot) {
case 'http:':
case 'https:':
res.type = 'couchdb';
res.adapter = 'lockit-couchdb-adapter';
break;
case 'mongodb:':
res.type = 'mongodb';
res.adapter = 'lockit-mongodb-adapter';
break;
case 'postgres:':
res.type = 'postgresql';
res.adapter = 'lockit-sql-adapter';
break;
case 'mysql:':
res.type = 'mysql';
res.adapter = 'lockit-sql-adapter';
break;
case 'sqlite:':
res.type = 'sqlite';
res.adapter = 'lockit-sql-adapter';
break;
}
return res;
};
/**
* Generate link to QR code, uses [Google Charts](https://developers.google.com/chart/infographics/docs/qr_codes).
*
* @example
var config = {
key: 'abcd1234',
email: '[email protected]'
};
var link = util.qr(config);
// https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FLockit%3Amirco.zeiss%40gmail.com%3Fsecret%3DMFRGGZBRGI2DI%3D%3D%3D%26issuer%3DLockit
*
* @param {Object} config - Configuration object
* @param {String} config.key - Individual random key for user
* @param {String} config.email - User email for Google Authenticator app
* @param {String} [config.issuer='Lockit'] - Issuer for Google Authenticator
*
* @returns {String} URL for QR code
*/
exports.qr = function(config) {
var key = config.key;
var email = config.email;
var issuer = config.issuer || 'Lockit';
var label = issuer + ':' + email;
var encoded = base32.encode(key);
var uri = encodeURIComponent('otpauth://totp/' + label + '?secret=' + encoded + '&issuer=' + issuer);
var api = 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=';
return api + uri;
};
/**
* Verify a two-factor authentication token, uses [time-based one-time password algorithm (totp)](http://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm).
* To be used with [Google Authenticator](https://support.google.com/accounts/answer/1066447?hl=en).
*
* @example
var key = 'abcd1234';
var token = '236709';
var valid = util.verify(token, key);
if (valid) {
// continue here
}
*
* @param {String} token - The two-factor token to verify
* @param {String} key - The individual key for the user
* @param {Object} [options] - Options object for
* [notp#totp.verify](https://github.com/guyht/notp#totpverifytoken-key-opt)
* @param {String} [options.window=6] - Allowable margin for counter
* @param {Number} [options.time=30] - Time step of counter in seconds
*
* @returns {Boolean} `true` if token is valid
*/
exports.verify = function(token, key, options) {
options = options || {};
var verified = totp.verify(token, key, options);
return (verified && verified.delta === 0);
};
/**
* Destroy the current session. Works with cookie sessions and session stores.
*
* @example
util.destroy(req, function() {
// user is now logged out
});
*
* @param {Object} req - The default Express request object
* @param {Function} done - Function executed when session is destroyed
*/
exports.destroy = function(req, done) {
if (req.sessionStore) {return req.session.regenerate(done); }
req.session = null;
done();
};
// private helper function
function pipe(source, target) {
var emit = source.emit;
source.emit = function () {
emit.apply(source, arguments);
target.emit.apply(target, arguments);
return source;
};
}
/**
* Pipe events from `source` to `target`. `source` can be a single event
* emitter or an Array of event emitters.
*
* @example
var util = require('util');
var events = require('events');
var utils = require('lockit-utils');
var Child = function() {};
util.inherits(Child, events.EventEmitter);
var Mother = function() {};
util.inherits(Mother, events.EventEmitter);
var child = new Child();
var mother = new Mother();
utils.pipe(child, mother);
mother.on('action', function(action) {
console.log('look the child is ' + action);
});
child.emit('action', 'smiling');
*
* @param {Object|Array} source - Single event emitter or Array of event emitters
* @param {Object} target - Single event emitter
*/
exports.pipe = function(source, target) {
var isArray = Array.isArray(source);
if (!isArray) {return pipe(source, target); }
source.forEach(function(event) {
pipe(event, target);
});
};