-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
697 lines (568 loc) · 23.9 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
var isNode = require('detect-node'); // detect if this is node (or browser)
var jwt = require('jsonwebtoken'); // json web tokens (jwt)
var cookie = require('cookie-cutter'); // cookie parsing and setting
var cookieBaker = require('cookie').serialize; // generate cookie headers
var extend = require('extend');
var clone = require('clone');
/*
TODO set cookies on server side on login
*/
var defaultTokenName = 'AuthToken';
var defaultTokenHeader = 'authorization';
var defaultTokenExpiration = 336; // in hours (two weeks default)
function unixEpochTime(hours) {
hours = hours || 0;
return Math.floor(new Date((new Date()).getTime() + 1000 * 60 * 60 * hours).getTime() / 1000);
}
function createToken(uniqueID, secret, userData, opts) {
opts = extend({
expiration: defaultTokenExpiration
}, opts || {});
var o = {
id: uniqueID,
created: unixEpochTime(),
expires: unixEpochTime(opts.expiration)
};
if(userData) {
o.userData = userData;
}
return jwt.sign(o, secret);
}
function verifyToken(token, secret, opts, cb) {
opts = extend({
expiration: defaultTokenExpiration, // how long from creation does token expire (hours)
}, opts || {});
jwt.verify(token, secret, function(err, decoded) {
if(err) return cb("Invalid token: " + err);
if(decoded.created < unixEpochTime(-opts.expiration)) {
return cb("Token expired.");
}
return cb(null, decoded.userData);
});
}
function authHTTP(opts) {
opts = extend(true, {
allowCookieToken: defaultTokenName, // name of the cookie or false
allowHeaderToken: defaultTokenHeader, // name of header or false
tokenExpiration: defaultTokenExpiration, // how long from creation does token expire (hours)
cookie: {
setCookie: true, // set cookie on server side
httpOnly: false, // use httpOnly cookies
secure: false, // use secure cookies (https only)
// additional opts:
// domain, path, firstPartyOnly, maxAge
},
secret: null, // must be set to your unique secret
success: function(req, res, match, tokenData) {
if(match && match.next && (typeof match.next == 'function')) {
match = match.next();
if(!match) {
res.statusCode = 404;
res.setHeader("Content-Type", "text/plain");
res.end("No matching routes");
return;
}
match.fn(req, res, match, tokenData);
} else if(match && (typeof match == 'function')) {
return match(null, tokenData);
} else {
throw new Error(module.name + ": Authentication succeeded but no idea what to do now. Third argument should be a function like next() or an object with a .next() function.");
}
},
fail: function(err, req, res, match) {
if(match && (typeof match == 'function')) {
return match(err);
}
res.statusCode = 401;
res.setHeader("Content-Type", "text/plain");
res.end(err);
},
// An optional extra function to run before deciding
// if auth check was successful
// receives args: req, res, match, tokenData, callback
check: undefined
}, opts || {});
if(!opts.secret) {
throw new Error(module.name + ": You must supply a secret!");
}
function authFail(errs, req, res, match, cb) {
var err = errs.join("\n");
if(!err) {
if(!opts.allowCookieToken && !opts.allowHeaderToken) {
err = "Neither cookie nor custom-header authentication allowed so no way to authenticate.";
} else {
err = "Unknown error.";
}
}
return cb("Authentication failed: " + err, req, res, match);
}
function loginFunc(res, uniqueID, userData, callback) {
if(typeof res != 'object') {
callback = userData;
userData = uniqueID;
uniqueID = res;
res = undefined;
}
if(typeof userData == 'function') {
callback = userData;
userData = null;
}
if(!uniqueID || !(typeof uniqueID == 'string' || typeof uniqueID == 'number')) {
return callback(new Error("Missing or invalid ID"));
}
var token = createToken(uniqueID, opts.secret, userData, {
expiration: opts.tokenExpiration
});
if(!opts.allowCookieToken || !res) {
return callback(null, token);
}
var cookieOpts = clone(opts.cookie);
if(cookieOpts.expiration) {
cookieOpts.expires = new Date((new Date).getTime() + 1000 * 60 * 60 * opts.tokenExpiration);
}
if(res && opts.cookie) {
res.setHeader('Set-Cookie', cookieBaker(opts.allowCookieToken, token, cookieOpts));
}
callback(null, token);
}
var authFunc = function(req, res, match) {
if(typeof res == 'function') {
match = res;
res = undefined;
}
var successCallback = opts.success;
var failCallback = opts.fail;
var token = false;
var errs = [];
if(!opts.secret) {
errs.push("Token secret missing. Cannot authenticate.");
return authFail(errs, req, res, match, failCallback);
}
if(opts.allowCookieToken) {
if(!req.headers.cookie) {
errs.push("Request had no token cookie.");
} else {
var cookies = cookie(req.headers.cookie);
token = cookies.get(opts.allowCookieToken);
if(!token) {
errs.push("Request did not have the '"+opts.allowCookieToken+"' cookie set.");
}
}
}
if(!token && opts.allowHeaderToken) {
if(!req.headers[opts.allowHeaderToken]) {
errs.push("Request had no token header.");
} else {
token = req.headers[opts.allowHeaderToken];
if(!token) {
errs.push("Request did not have the '"+opts.allowHeaderToken+"' custom header set.");
}
}
}
if(!token) {
return authFail(errs, req, res, match, failCallback);
}
verifyToken(token, opts.secret, {expiration: opts.tokenExpiration}, function(err, tokenData) {
if(err) {
errs.push(err);
authFail(errs, req, res, match, failCallback);
return;
}
if(opts.check && typeof opts.check == 'function') {
opts.check(req, res, match, tokenData, function(err) {
if(err) {
errs.push(err);
authFail(errs, req, res, match, failCallback);
return;
}
successCallback(req, res, match, tokenData);
});
} else {
successCallback(req, res, match, tokenData);
}
});
};
var inheritFunc = function(newOpts) {
var oldOpts = clone(opts);
newOpts = extend(true, oldOpts, newOpts);
return authHTTP(newOpts);
};
authFunc.login = loginFunc;
authFunc.inherit = inheritFunc;
return authFunc;
}
function authRPC(opts, procs, hookOrNamespace) {
opts = extend({
tokenExpiration: defaultTokenExpiration,
userDataAsFirstArgument: false // if true all functions will receive userData as first argument (undefined if user isn't logged in)
}, opts || {});
if(!opts.secret) throw new Error("Token auth needs a .secret set");
if(!opts.login || typeof opts.login != 'function') throw new Error("Token auth needs a .secret set");
var rpcMethods = {};
// remember (on the server side) that user is logged
function rememberUser(token) {
var decoded = jwt.decode(token);
if(!rpcMethods._rpcMultiAuthData) {
Object.defineProperty(
rpcMethods,
'_rpcMultiAuthData',
{enumerable: false, configurable: true, value: decoded}
);
} else {
rpcMethods._rpcMultiAuthData = decoded;
}
}
// forget (on the server side) user
function forgetUser() {
delete rpcMethods._rpcMultiAuthData;
}
rpcMethods.login = function(loginData, cb) {
opts.login(loginData, function(err, id, userData) {
if(err) return cb("Login failed: " + err);
var token = createToken(id, opts.secret, userData, {
expiration: opts.tokenExpiration
});
rememberUser(token);
cb(null, token, userData);
});
};
rpcMethods.logout = function(cb) {
forgetUser();
cb();
};
// authenticate if client already has token
rpcMethods.authenticate = function(token, cb) {
verifyToken(token, opts.secret, {expiration: opts.tokenExpiration}, function(err, decoded) {
if(err) return cb(err);
rememberUser(token);
cb(null, decoded);
});
};
var reservedMethods = Object.keys(rpcMethods);
function isReserved(methodName) {
if(!methodName) return false;
if(reservedMethods.indexOf(methodName) >= 0 || methodName[0] == '_') {
console.error("rpc method", methodName, "ignored (reserved name)");
return true;
}
return false;
}
function authWrap(method, methodName, namespace, hookOrNamespaceMatch) {
var hook;
var namespaceMatch;
if(typeof hookOrNamespace == 'string') {
namespaceMatch = hookOrNamespaceMatch;
} else if(typeof hookOrNamespaceMatch == 'function') {
hook = hookOrNamespaceMatch;
}
function rpcFail(args, err) {
// if last argument is a function assume
if(args.length > 0 && typeof args[args.length-1] == 'function') {
return args[args.length-1](err);
} else {
console.error("Auth failed for synchronous rpc function. Returning null to client but should be throwing exception. TODO implement client side exceptions.");
return null; // TODO figure out how to throw an exception on the remote side
}
}
function authFail(args, msg) {
return rpcFail(args, "Unauthorized: " + (msg || "Are you sure you are logged in?"));
}
var f = function() {
var userData;
if(rpcMethods && rpcMethods._rpcMultiAuthData) {
userData = rpcMethods._rpcMultiAuthData.userData;
}
if(opts.userDataAsFirstArgument) {
[].splice.call(arguments, 0, 0, userData);
}
// if this is a namespaced function and namespace matching is on
if(namespace && namespaceMatch) {
if(!userData) {
return authFail(arguments, "Not logged in.");
}
if(userData[namespaceMatch] != namespace) {
return authFail(arguments, "You are not allowed to call procedures in the '" + namespace + "' namespace");
}
return method.apply(this, arguments);
} else {
// if user specified a hook function instead of namespace matching
if(hook) {
var args = arguments;
hook(userData, namespace, methodName, function(err) {
if(err) return authFail(args, err);
return method.apply(this, args);
});
} else {
return method.apply(this, arguments);
}
}
};
// Propagate synchronous function tag.
// This tag ensures that the function is recognized
// as the appropriate read/write/duplex stream returning function
if(method._rpcOpts) {
Object.defineProperty(
f,
'_rpcOpts',
{enumerable: false, value: method._rpcOpts}
);
}
return f;
}
var key, innerKey, namespace, wrapped;
for(key in procs) {
if(isReserved(key)) continue;
if(typeof procs[key] == 'function') {
rpcMethods[key] = authWrap(procs[key], key, null, hookOrNamespace);
} else if(typeof procs[key] == 'object') {
namespace = key;
for(innerKey in procs[key]) {
if(typeof procs[key][innerKey] != 'function') continue;
if(isReserved(innerKey)) continue;
rpcMethods[innerKey] = authWrap(procs[key][innerKey], innerKey, namespace, hookOrNamespace);
}
}
}
return rpcMethods;
}
var rpcMultiAuth;
var serverExport = rpcMultiAuth = function() {
// Being initialized for RPC auth
if(typeof arguments[0] == 'object' && typeof arguments[1] == 'object') {
var opts = arguments[0];
var procs = arguments[1]; // remote procedures
var hookOrNamespace = arguments[2];
return authRPC(opts, procs, hookOrNamespace);
// Being initialized for HTTP auth (e.g. as middleware)
} else if((typeof arguments[0] == 'string' || typeof arguments[0] == 'object')) {
var opts;
var callback;
if(typeof arguments[0] == 'object') {
opts = arguments[0];
} else {
opts = {
secret: arguments[0]
};
}
callback = arguments[1];
return authHTTP(opts, callback);
} else {
throw new Exception("Wrong arguments. Read the docs.");
}
};
var store = require('store'); // LocalStorage wrapper
var xhr = require('xhr'); // XMLHttpRequest wrapper
var clientExport = rpcMultiAuth = {
// save token using cookie if opts.setCookie is true (default false)
// and save in LocalStorage if opts.useLocalStorage is true (default true)
saveToken: function(token, opts) {
opts = extend({
tokenName: defaultTokenName, // cookie name and/or localstorage key to use
setCookie: false, // whether to save token in cookie on client side
useLocalStorage: true // true to save token using LocalStorage
}, opts || {});
var saved = false;
if(opts.setCookie) {
var decoded = jwt.decode(token);
if(decoded && decoded.expires) {
document.cookie = cookieBaker(opts.tokenName, token, {
expires: new Date(decoded.expires * 1000)
});
saved = true;
}
}
if(opts.useLocalStorage) {
store.set(opts.tokenName, token);
saved = true;
}
return saved;
},
// do a purely local check to see if we have a token
// and if it is still good (not expired)
isLoggedIn: function(opts) {
var token = rpcMultiAuth.getToken(opts);
if(!token) return false
var decoded = jwt.decode(token);
if(!decoded || !decoded.expires) {
return false;
}
if(decoded.expires > unixEpochTime()) {
return true;
}
return false;
},
// delete the token
delToken: function(opts) {
opts = extend({
tokenName: defaultTokenName, // cookie name and/or localstorage key to use
delCookie: true, // whether to delete token in cookie on client side
useLocalStorage: true // true to delete token using LocalStorage
}, opts || {});
var removed = false;
if(opts.delCookie) {
document.cookie = cookieBaker(opts.tokenName, '', {
expires: new Date(0)
});
removed = true;
}
if(opts.useLocalStorage) {
store.remove(opts.tokenName);
removed = true;
}
return removed;
},
logout: function(opts, callback) {
var remote;
// if this is called with an rpc object that supports logouts
if(typeof opts == 'object' && typeof opts.logout == 'function') {
remote = opts;
rpcMultiAuth.delToken();
remote.logout(callback);
} else {
return rpcMultiAuth.delToken(opts);
}
},
// get token from cookie if available
// otherwise from localstorage if available
getToken: function(opts) {
opts = extend({
tokenName: defaultTokenName, // cookie name and/or localstorage key to use
token: undefined // of opts contains a .token then that will be used
}, opts || {});
if(opts.token) return opts.token;
var token;
var cookies = cookie(document);
if(!token) {
token = cookie.get(opts.tokenName);
}
if(!token) {
token = store.get(opts.tokenName);
}
return token;
},
// check if token is valid / if we are authorized
// remote can be an rpc-multiauth (or other rpc) instance
// or a URL to use for HTTP POST login
// if it is null then the relative url 'login' will be used
authenticate: function(remote, opts, callback) {
if(typeof opts == 'function') {
callback = opts;
opts = null;
}
opts = opts || {};
if(typeof opts == 'string') {
opts = {
token: opts
};
}
opts = extend({
token: undefined, // optionally explicitly pass a token
setCookie: false, // whether to save token in cookie on client side
tokenName: defaultTokenName, // cookie name and/or localstorage key to use
useLocalStorage: true, // true to save token using LocalStorage
httpMethod: 'POST', // which method to use if using http requests
tokenHeader: defaultTokenHeader // header to use for token, set to false to disable sending token in header (e.g. if using cookies)
}, opts);
remote = remote || 'authenticate'; // default URL to use for auth
var token = rpcMultiAuth.getToken(opts);
if(!token) {
return callback(new Error('No token available so could not authenticate. Either explicitly pass a token in the opts argument like so: {token: "my_token"} or ensure that you have a token saved using cookies or LocalAuth using the cookie name / localstorage key "'+opts.tokenName+'".'));
}
if(typeof remote == 'string') {
var o = {
uri: remote,
method: opts.httpMethod.toUpperCase(),
headers: {}
};
if(opts.tokenHeader) {
o.headers[opts.tokenHeader] = token;
}
xhr(o, function(err, resp, body) {
if(err || resp.statusCode < 200 || resp.statusCode >= 300) {
return callback(err || body);
}
if(!body) return callback(new Error("Got empty response from server but no http error status code."));
if(typeof body != 'string') return callback(new Error("Got non-string response from server. Expected userData."));
callback(null, body);
});
} else {
remote.authenticate(token, callback);
}
},
// log in
// remote can be an rpc-multiauth (or other rpc) instance
// or a URL to use for HTTP POST login
// if it is null then the relative url 'login' will be used
login: function(remote, loginData, opts, callback) {
if(typeof opts == 'function') {
callback = opts;
opts = null;
}
opts = extend({
setCookie: false, // whether to save token in cookie on client side
useLocalStorage: true, // whether to save token in LocalStorage
tokenName: defaultTokenName, // cookie name and/or localstorage key to use
tokenHeader: defaultTokenHeader // header to use for token, set to false to disable sending token in header (e.g. if using cookies)
}, opts || {});
remote = remote || 'login'; // default URL to use for login
function doLogin(remote, loginData, opts, callback) {
if(typeof remote == 'string') {
xhr({
uri: remote,
method: 'POST',
body: JSON.stringify(loginData)
}, function(err, resp, token) {
if(err || resp.statusCode < 200 || resp.statusCode >= 300) {
return callback(err || token);
}
if(!token) return callback(new Error("Got empty response from server but no http error status code."));
if(typeof token != 'string') return callback(new Error("Got non-string response from server. Expected token."));
rpcMultiAuth.saveToken(token, opts);
callback(null, token);
});
} else {
remote.login(loginData, function(err, token, userData) {
if(err) return callback(err);
rpcMultiAuth.saveToken(token, opts);
callback(null, token, userData);
});
}
}
doLogin(remote, loginData, opts, callback);
},
// return a wrappted xhr requester
requester: function(popts) {
if(typeof popts == 'string') {
popts = {
token: popts
}
}
popts = extend({
tokenHeader: defaultTokenHeader, // name of header field to use for auth token. if false, disable sending of auth token using its own header field (rely on cookies only)
tokenName: defaultTokenName, // name of cookie and/or localstorage field to look for
token: undefined // explicitly set an auth token to use. if not set then requester will look for token in cookie
}, popts || {});
return function(opts, callback) {
opts.headers = opts.headers || {};
if(popts.tokenHeader) {
var token = opts.token || popts.token;
token = rpcMultiAuth.getToken({token: token});
if(token) {
opts.headers[popts.tokenHeader] = token;
}
}
return xhr(opts, callback);
};
}
};
rpcMultiAuth.xhr = xhr;
// For backwards compatibility with older versions of rpc-multiauth
if(isNode) { // server side
serverExport.server = serverExport;
serverExport.client = clientExport;
module.exports = serverExport;
} else { // client side
clientExport.server = serverExport;
clientExport.client = clientExport;
module.exports = clientExport;
}