-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-component.js
More file actions
109 lines (88 loc) · 3.48 KB
/
auth-component.js
File metadata and controls
109 lines (88 loc) · 3.48 KB
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
'use strict';
var _ = require('lodash'),
_cloneDeep = require('lodash/lang/cloneDeep'),
passport = require('passport'),
error = function(message) {
throw new Error('[auth-component] ' + message);
},
push = function(array, items) {
var args = [].slice.call(arguments, 1);
if(args.length > 0) {
[].push.apply(array, args);
}
};
module.exports = function(app, Component) {
var webuser_model = app.models.webuser,
auth = Component({
name: 'auth'
}),
_initialize_strategy = function(name, config, process) {
var module_name = 'passport-' + name,
module = require(module_name),
strategy,
default_passport_strategy = 'Strategy',
custom_passport_strategy = config.passport_strategy || config.passportStrategy;
strategy = module[custom_passport_strategy || default_passport_strategy];
if(!strategy) {
error('Cannot find passport Strategy for [' + module_name + ']');
}
/**
*
* Configuration is deep freezed but some passport modules redefine configuration. So there is deep cloning
*/
passport.use(new strategy(_cloneDeep(config), process));
},
_initialize_passport = function(webuser_strategies) {
var supported_strategies = this.config,
supported_strategies_names = Object.keys(supported_strategies),
name, process,
i;
passport.serializeUser(webuser_model.serialize);
passport.deserializeUser(webuser_model.deserialize);
for(i = supported_strategies_names.length; i--; ) {
name = supported_strategies_names[i];
process = webuser_strategies[name];
if(!process) {
error('Cannot find Strategy handler for [' + name + ']');
}
_initialize_strategy.call(this, name, supported_strategies[name], process);
}
};
auth.initialize = function() {
var self = this,
webuser_strategies = webuser_model.strategy,
webuser_get_role = webuser_model.get_role || webuser_model.getRole,
auth_roles;
this.default_roles = {
all: '*',
guest: '?',
authenticated: '@'
};
this.roles = auth_roles = _.values(this.default_roles);
if(typeof webuser_get_role === 'function') {
this.get_role = webuser_get_role.bind(webuser_model);
}
if(typeof webuser_model.roles === 'function') {
webuser_model.roles(function(err, roles) {
if(err) {
roles = [];
}
self.user_role_field = webuser_model.user_role_field || webuser_model.userRoleField;
push(auth_roles, roles);
_initialize_passport.call(self, webuser_strategies);
});
} else {
push(auth_roles, webuser_model.roles);
_initialize_passport.call(this, webuser_strategies);
}
return this;
};
auth.authenticate = function(strategy, options, cb) {
return passport.authenticate(strategy, options, cb);
};
auth.attach = function(listener) {
listener.use(passport.initialize(passport));
listener.use(passport.session(passport));
return this;
};
};