forked from domharrington/secure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess-control-list.js
201 lines (169 loc) · 4.91 KB
/
access-control-list.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
var assignIn = require('lodash/assignIn')
;
module.exports = function(logger) {
var acl = {
'*': {
actions: {
'*': []
}
}
};
// Provides the console as a default logger
logger = logger || {
debug: console.log
};
/**
* Clears all grants attached to the ACL
*
* @api public
*/
function clearGrants() {
Object.keys(acl).forEach(function(resource) {
Object.keys(acl[resource].actions).forEach(function(action) {
acl[resource].actions[action] = [];
});
});
}
/**
* Adds a resource to the ACL. Throws an error if actions option is not
* an Array.
*
* Action list defaults to: create, read, update, delete and *.
*
* @param {String} resource to add
* @param {Object} resource options: action list & description
* @api public
*/
function addResource(resource, resourceOptions) {
if (acl[resource] === undefined) {
var options = {
actions: ['create', 'read', 'update', 'delete', '*']
};
assignIn(options, resourceOptions);
if (!Array.isArray(options.actions)) {
throw new TypeError('actionList is excepted to be an array of action ' +
'names that can be performed on the resource \'' + resource + '\'');
}
var actions = {};
// Create a blank array for each action
options.actions.forEach(function(action) {
actions[action] = [];
});
logger.debug('Adding resource \'' + resource + '\' to access control list');
// Adding the resource to the ACL
acl[resource] = {
actions: actions
};
if (options.description) {
acl[resource].description = options.description;
}
} else {
logger.debug('Resource \'' + resource + '\' already added');
}
}
/**
* Helper function to throw a RangeError if a given resource doesnt exist
*
* @param {String} resource
* @api private
*/
function throwIfResourceDoesntExist(resource) {
// Ensure the resource has been added
if (acl[resource] === undefined) {
throw new RangeError('Unknown resource: ' + resource);
}
}
/**
* Grant a given target permission to perform the given action a resource
*
* @param {String} target
* @param {String} resource
* @param {String} action
* @api public
*/
function grant(target, resource, action) {
throwIfResourceDoesntExist(resource);
// if (acl[resource][action] === undefined) {
// throw new RangeError('Unknown action: ' + action);
// }
if (acl[resource].actions[action] === undefined) {
acl[resource].actions[action] = [target];
} else if (acl[resource].actions[action].indexOf(target) === -1) {
acl[resource].actions[action].push(target);
}
}
/**
* Revoke a given target permission to access the given resource
*
* @param {String} target
* @param {String} resource
* @param {String} action
* @api public
*/
function revoke(target, resource, action) {
throwIfResourceDoesntExist(resource);
var targets = acl[resource].actions[action]
;
if (targets === undefined) {
throw new RangeError('Unknown action: ' + action);
}
targets.some(function(eachTarget, index) {
if (target === eachTarget) {
targets.splice(index, 1);
return true;
}
});
}
/**
* Checks if a given target is allowed access. Firstly checks if the acl has
* a given resource, then checks that the resource has the given action,
* finally checks if the target is in the granted targets array or if the
* target is in the '*' group to be allowed all access.
*
* @param {String} target
* @param {String} resource
* @param {String} action
* @api private
*/
function targetAllowed(target, resource, action) {
return acl[resource]
&& acl[resource].actions[action]
&& ((acl[resource].actions[action].indexOf(target) !== -1)
|| (acl[resource].actions['*'] && acl[resource].actions['*'].indexOf(target) !== -1));
}
/**
* Check if a target is allowed access to a given resource/action.
*
* @param {Array} targets array of potential targets
* @param {String} resource
* @param {String} action
* @api public
*/
function allowed(targets, resource, action) {
var target;
if (!Array.isArray(targets)) {
targets = [targets];
}
for (var i = 0; i < targets.length; i++) {
target = targets[i];
// Allow wildcard resource. This allows you to create a target with
// grant('root', '*', '*')
// Who will always have access
if (acl['*'].actions['*'].indexOf(target) !== -1) {
return true;
}
if (targetAllowed(target, resource, action)) {
return true;
}
}
return false;
}
return {
get acl() { return acl; },
addResource: addResource,
clearGrants: clearGrants,
grant: grant,
revoke: revoke,
allowed: allowed
};
};