-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-controller.js
More file actions
128 lines (103 loc) · 3.92 KB
/
auth-controller.js
File metadata and controls
128 lines (103 loc) · 3.92 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
var debug = require('debug')('ifnode-auth:controller'),
_ = require('lodash');
module.exports = function(app, Controller) {
var auth = app.auth.attach(app.listener),
user_role_field = auth.user_role_field,
default_roles = auth.default_roles,
auth_middleware;
Controller.fn.roles = auth.roles;
Controller.fn.default_role = default_roles.all;
Controller.fn._page_access_denied = function(request, response) {
response.unauthorized();
};
Controller.process_config(function(controller_config) {
var process = function(options, default_options) {
if(!options) {
options = default_options;
} else if(!Array.isArray(options)) {
options = [options];
}
return options;
};
controller_config.access = process(controller_config.access, [this.default_role]);
controller_config.only = process(controller_config.only, [this.default_role]);
return controller_config;
});
auth_middleware = function(self, request, options, can_callback, cannot_callback, error_callback) {
var is_authenticated = request.isAuthenticated(),
get_role = function(callback) {
var auth_get_role = auth.get_role,
user_role;
if(typeof auth_get_role === 'function') {
return auth_get_role(request.user, callback);
}
if(user_role_field) {
user_role = request.user[user_role_field];
if(typeof user_role === 'function') {
user_role = user_role();
}
} else {
user_role = default_roles.authenticated;
}
callback(null, user_role);
};
if(_.contains(options, self.default_role)) {
return can_callback();
}
if(is_authenticated) {
get_role(function(err, user_role) {
if(err) { return error_callback(err); }
if(
!_.contains(options, default_roles.authenticated) &&
!_.contains(options, user_role)
) {
return cannot_callback();
}
can_callback();
});
return;
} else if(!_.contains(options, default_roles.guest)) {
return cannot_callback();
}
can_callback();
};
Controller.populate(function(request, response, next, next_router) {
var self = this;
response.access_denied = response.accessDenied = function() {
self._page_access_denied.call(self, request, response);
};
next();
});
Controller.middleware(
function only_middleware(options) {
var self = this,
only_options = options.only;
return function only_middleware(request, response, next_handler, next_route) {
auth_middleware(self, request, only_options,
next_handler,
next_route,
next_route
);
}
},
function access_middleware(options) {
var self = this,
access_options = options.access;
return function access_middleware(request, response, next_handler, next_route) {
var args = [].slice.call(arguments);
auth_middleware(self, request, access_options,
next_handler,
function() {
self._page_access_denied.apply(self, args);
},
next_route
);
};
}
);
Controller.fn.access_denied = Controller.fn.accessDenied = function(callback) {
this._page_access_denied = callback;
return this;
};
};