-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (65 loc) · 2.61 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
'use strict';
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var passport = require('passport');
var jwt = require('jsonwebtoken');
module.exports = function(config, seneca_instance) {
var seneca = seneca_instance || require('seneca')();
config.auth.strategyFolder = config.auth.strategyFolder || path.join(process.cwd(), 'strategies');
_.each(fs.readdirSync(config.auth.strategyFolder), function(file) {
if (file[0] == '.') { return; }
let fileNoJs = _.initial(file.split('.')).join('.');
require(`${config.auth.strategyFolder}/${file}`)(
passport,
_.extend(_.cloneDeep(config.auth.common), config.auth[fileNoJs])
);
});
passport.framework(require('./passport-fw-seneca-json'));
var mapArgsToAuth = function(args) {
var newSession = {};
const params = {
session: ['request_token', 'oauth_token_secret'],
query : ['oauth_token', 'oauth_verifier', 'code', 'client_id']
};
newSession['oauth:' + args.strategy] = _.pick(args.session, params.session);
return {
session: newSession,
query : _.pick(args, params.query)
}
};
seneca.addAsync({system: 'auth', action: 'auth'}, function (args) {
let auth = passport.authenticate(args.strategy, config.auth[args.strategy].options);
var session = {};
return auth(mapArgsToAuth(args, session))
.then(response => handlers.get(response.result)(response, session, args.strategy))
});
var handlers = {
redirect: function(response, session, strategy) {
response.oauth_token_secret = _.get(session, `['oauth:${strategy}'].oauth_token_secret`);
return response;
},
success: function(response) {
if (config.auth.autoLogin) {
return seneca.actAsync({ system: 'user', action: 'login', query: response })
.then(function(response) {
var token = jwt.sign(
{ detail: { name: response.user.name } },
config.jwt.secret,
{ expiresInMinutes: config.auth.expiry, subject: response.user.id }
);
return { success: true, result: 'success', user: response.user, jwt: token };
});
} else {
return { success: true, result: 'success', auth: response };
}
},
get: function(name) {
return this[name] || function() { return Promise.reject(`Unknown response ${name}`) }
}
};
return {
koa: function() { return require('./seneca-auth-koa')(seneca); }
};
};
module.exports.koa = function(seneca) { return require('./seneca-auth-koa')(seneca); };