-
Notifications
You must be signed in to change notification settings - Fork 11
/
koa-rbac.js
202 lines (162 loc) · 5.47 KB
/
koa-rbac.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
const RBAC = require('rbac-a');
/**
Define custom error type
*/
class InvalidOptionException extends Error {}
/**
Koa middleware.
Options
- rbac {RBAC} (optional) the RBAC-A instance (will create one if omitted)
- identify {Function} (optional) the identity function. By default, a function returning ctx.user will be used.
@param options {Object}
@return {Function} the middleware function
*/
function middleware (options) {
options = options || {};
if (('rbac' in options) && !(options.rbac instanceof RBAC)) {
throw new InvalidOptionException('Invalid RBAC instance');
}
if (!('identity' in options)) {
options.identity = defaultIdentity;
} else if (typeof options.identity !== 'function') {
throw new InvalidOptionException('Invalid identity function');
}
if (('restrictionHandler' in options) && (typeof options.restrictionHandler !== 'function')) {
throw new InvalidOptionException('Invalid restriction handler');
}
return (ctx, next) => {
Object.defineProperty(ctx, 'rbac', {
enumerable: true,
writable: false,
value: Object.create(options.rbac || {}, {
// current allowed priority
check: {
enumerable: true,
writable: false,
value: function (permissions, params) {
return options.rbac && Promise.resolve().then(function () {
return options.identity(ctx);
}).then(function (user) {
return user && options.rbac.check(user, permissions, params) || NaN;
}) || Promise.resolve(NaN);
}
},
_restrict: {
enumerable: false,
writable: false,
value: function restrict(permissions, redirectUrl) {
if (options.restrictionHandler) {
return options.restrictionHandler(ctx, permissions, redirectUrl);
} else if (redirectUrl) {
return ctx.redirect(redirectUrl);
} else {
ctx.status = 403;
ctx.body = 'Forbidden';
}
}
}
})
});
return next();
};
}
/**
Default identity function.
Try to return most common used user property.
@param ctx the request context
@return mixed
*/
function defaultIdentity(ctx) {
return ctx && ctx.user;
}
/**
Return a middleware to allow only for the given permissions.
@param permissions {string} the required permissions
@param params {object} (optional) params sent to the RBAC-A instance
@param redirectUrl {string} (optional) if not allowed, try to redirect
*/
function allowMiddleware(permissions, params, redirectUrl) {
if (arguments.length < 3 && typeof params === 'string') {
redirectUrl = params;
params = undefined;
}
return async (ctx, next) => {
const rbac = ctx.rbac;
if (rbac) {
const allowedPriority = await rbac.check(permissions, params);
if (!allowedPriority) {
return rbac._restrict(permissions, redirectUrl);
}
}
return next();
};
}
/**
Return a middleware to deny any with the given permissions.
@param permissions {string} the restricted permissions
@param params {object} (optional) params sent to the RBAC-A instance
@param redirectUrl {string} (optional) if not allowed, try to redirect
*/
function denyMiddleware(permissions, params, redirectUrl) {
if (arguments.length < 3 && typeof params === 'string') {
redirectUrl = params;
params = undefined;
}
return async (ctx, next) => {
const rbac = ctx.rbac;
if (rbac) {
const deniedPriority = await rbac.check(permissions, params);
if (deniedPriority) {
return rbac._restrict(permissions, redirectUrl);
}
} else {
ctx.status = 403;
ctx.body = 'Forbidden';
return;
}
return next();
};
}
/**
Return a middleware to allow and not deny any with the given permissions.
@param permissions {object} (optional) permissions sent to the RBAC-A instance
@param permissions.allowPermissions {string|Array<string>} the required permissions
@param permissions.deniedPermissions {string|Array<string>} the restricted permissions
@param params {object} (optional) params sent to the RBAC-A instance
@param redirectUrl {string} (optional) if not allowed, try to redirect
*/
function checkMiddleware(permissions, params, redirectUrl) {
if (arguments.length < 3 && typeof params === 'string') {
redirectUrl = params;
params = undefined;
}
return async (ctx, next) => {
const rbac = ctx.rbac;
if (rbac) {
let allowedPriority;
let deniedPriority;
permissions = permissions || {};
if ('allow' in permissions) {
allowedPriority = await rbac.check(permissions['allow'], params);
} else {
allowedPriority = Infinity;
}
if ('deny' in permissions) {
deniedPriority = await rbac.check(permissions['deny'], params);
} else {
deniedPriority = Infinity;
}
if (isNaN(allowedPriority) || (!isNaN(deniedPriority) && (deniedPriority <= allowedPriority))) {
return rbac._restrict(permissions, redirectUrl);
}
}
return next();
};
}
module.exports = middleware;
module.exports.middleware = middleware;
module.exports.allow = allowMiddleware;
module.exports.deny = denyMiddleware;
module.exports.check = checkMiddleware;
module.exports.InvalidOptionException = InvalidOptionException;
module.exports.RBAC = RBAC;