-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbuiltinOperators.ts
373 lines (319 loc) · 10.3 KB
/
builtinOperators.ts
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright 2017 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as rbac from '../rbac';
import { ip } from './ip';
// regexMatch determines whether key1 matches the pattern of key2 in regular expression.
function regexMatch(key1: string, key2: string): boolean {
return new RegExp(key2).test(key1);
}
// KeyGet returns the matched part
// For example, "/foo/bar/foo" matches "/foo/*"
// "bar/foo" will been returned
function keyGet(key1: string, key2: string): string {
const pos: number = key2.indexOf('*');
if (pos === -1) {
return '';
}
if (key1.length > pos) {
if (key1.slice(0, pos) === key2.slice(0, pos)) {
return key1.slice(pos, key1.length);
}
}
return '';
}
// keyGetFunc is the wrapper for keyGet.
function keyGetFunc(...args: any[]): string {
const [arg0, arg1] = args;
const name1: string = (arg0 || '').toString();
const name2: string = (arg1 || '').toString();
return keyGet(name1, name2);
}
// KeyGet2 returns value matched pattern
// For example, "/resource1" matches "/:resource"
// if the pathVar == "resource", then "resource1" will be returned
function keyGet2(key1: string, key2: string, pathVar: string): string {
if (keyMatch2(key1, key2)) {
const re = new RegExp('[^/]+', 'g');
const keys = key2.match(re);
const values = key1.match(re);
if (!keys || !values) {
return '';
}
const index = keys.indexOf(`:${pathVar}`);
if (index === -1) {
return '';
}
return values[index];
} else {
return '';
}
}
function keyGet2Func(...args: any[]): string {
const [arg0, arg1, arg2] = args;
const name1: string = (arg0 || '').toString();
const name2: string = (arg1 || '').toString();
const name3: string = (arg2 || '').toString();
return keyGet2(name1, name2, name3);
}
// keyMatch determines whether key1 matches the pattern of key2 (similar to RESTful path),
// key2 can contain a *.
// For example, '/foo/bar' matches '/foo/*'
function keyMatch(key1: string, key2: string): boolean {
const pos: number = key2.indexOf('*');
if (pos === -1) {
return key1 === key2;
}
if (key1.length > pos) {
return key1.slice(0, pos) === key2.slice(0, pos);
}
return key1 === key2.slice(0, pos);
}
// keyMatchFunc is the wrapper for keyMatch.
function keyMatchFunc(...args: any[]): boolean {
const [arg0, arg1] = args;
const name1: string = (arg0 || '').toString();
const name2: string = (arg1 || '').toString();
return keyMatch(name1, name2);
}
// keyMatch2 determines whether key1 matches the pattern of key2 (similar to RESTful path),
// key2 can contain a *.
// For example, '/foo/bar' matches '/foo/*', '/resource1' matches '/:resource'
function keyMatch2(key1: string, key2: string): boolean {
key2 = key2.replace(/\/\*/g, '/.*');
const regexp = new RegExp(/(.*):[^/]+(.*)/g);
for (;;) {
if (!key2.includes('/:')) {
break;
}
key2 = key2.replace(regexp, '$1[^/]+$2');
}
if (key2 === '*') {
key2 = '(.*)';
}
return regexMatch(key1, '^' + key2 + '$');
}
// keyMatch2Func is the wrapper for keyMatch2.
function keyMatch2Func(...args: any[]): boolean {
const [arg0, arg1] = args;
const name1: string = (arg0 || '').toString();
const name2: string = (arg1 || '').toString();
return keyMatch2(name1, name2);
}
// keyMatch3 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
// For example, '/foo/bar' matches '/foo/*', '/resource1' matches '/{resource}'
function keyMatch3(key1: string, key2: string): boolean {
key2 = key2.replace(/\/\*/g, '/.*');
const regexp = new RegExp(/(.*){[^/]+}(.*)/g);
for (;;) {
if (!key2.includes('/{')) {
break;
}
key2 = key2.replace(regexp, '$1[^/]+$2');
}
return regexMatch(key1, '^' + key2 + '$');
}
// keyMatch3Func is the wrapper for keyMatch3.
function keyMatch3Func(...args: any[]): boolean {
const [arg0, arg1] = args;
const name1: string = (arg0 || '').toString();
const name2: string = (arg1 || '').toString();
return keyMatch3(name1, name2);
}
// keyMatch4 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
// Besides what keyMatch3 does, keyMatch4 can also match repeated patterns:
// "/parent/123/child/123" matches "/parent/{id}/child/{id}"
// "/parent/123/child/456" does not match "/parent/{id}/child/{id}"
// But keyMatch3 will match both.
function keyMatch4(key1: string, key2: string): boolean {
key2 = key2.replace(/\/\*/g, '/.*');
const tokens: string[] = [];
let j = -1;
for (let i = 0; i < key2.length; i++) {
const c = key2.charAt(i);
if (c === '{') {
j = i;
} else if (c === '}') {
tokens.push(key2.substring(j, i + 1));
}
}
let regexp = new RegExp(/(.*){[^/]+}(.*)/g);
for (;;) {
if (!key2.includes('/{')) {
break;
}
key2 = key2.replace(regexp, '$1([^/]+)$2');
}
regexp = new RegExp('^' + key2 + '$');
let values: RegExpExecArray | null | string[] = regexp.exec(key1);
if (!values) {
return false;
}
values = values.slice(1);
if (tokens.length !== values.length) {
throw new Error('KeyMatch4: number of tokens is not equal to number of values');
}
const m = new Map<string, string[]>();
tokens.forEach((n, index) => {
const key = tokens[index];
let v = m.get(key);
if (!v) {
v = [];
}
if (values) {
v.push(values[index]);
}
m.set(key, v);
});
for (const value of m.values()) {
if (value.length > 1) {
for (let i = 1; i < values.length; i++) {
if (values[i] !== values[0]) {
return false;
}
}
}
}
return true;
}
// keyMatch4Func is the wrapper for keyMatch4.
function keyMatch4Func(...args: any[]): boolean {
const [arg0, arg1] = args;
const name1: string = (arg0 || '').toString();
const name2: string = (arg1 || '').toString();
return keyMatch4(name1, name2);
}
// regexMatchFunc is the wrapper for regexMatch.
function regexMatchFunc(...args: any[]): boolean {
const [arg0, arg1] = args;
const name1: string = (arg0 || '').toString();
const name2: string = (arg1 || '').toString();
return regexMatch(name1, name2);
}
// ipMatch determines whether IP address ip1 matches the pattern of IP address ip2,
// ip2 can be an IP address or a CIDR pattern.
// For example, '192.168.2.123' matches '192.168.2.0/24'
function ipMatch(ip1: string, ip2: string): boolean {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('buffer');
} catch {
throw new Error('Please add buffer to your dependency.');
}
// check ip1
if (!(ip.isV4Format(ip1) || ip.isV6Format(ip1))) {
throw new Error('invalid argument: ip1 in ipMatch() function is not an IP address.');
}
// check ip2
const cidrParts: string[] = ip2.split('/');
if (cidrParts.length === 2) {
return ip.cidrSubnet(ip2).contains(ip1);
} else {
if (!(ip.isV4Format(ip2) || ip.isV6Format(ip2))) {
console.log(ip2);
throw new Error('invalid argument: ip2 in ipMatch() function is not an IP address.');
}
return ip.isEqual(ip1, ip2);
}
}
// ipMatchFunc is the wrapper for ipMatch.
function ipMatchFunc(...args: any[]): boolean {
const [arg0, arg1] = args;
const ip1: string = (arg0 || '').toString();
const ip2: string = (arg1 || '').toString();
return ipMatch(ip1, ip2);
}
/**
* Returns true if the specified `string` matches the given glob `pattern`.
*
* @param string String to match
* @param pattern Glob pattern to use for matching.
* @returns Returns true if the string matches the glob pattern.
*
* @example
* ```javascript
* globMatch("abc.conf", "*.conf") => true
* ```
*/
function globMatch(string: string, pattern: string): boolean {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const picomatch = require('picomatch');
return picomatch(pattern)(string);
} catch {
throw new Error('Please add picomatch to your dependency.');
}
}
export type gFunction = (...args: string[]) => Promise<boolean>;
export type syncGFuntion = (...args: string[]) => boolean;
// generateGFunction is the factory method of the g(_, _) function.
function generateGFunction(rm: rbac.RoleManager): gFunction {
const memorized = new Map<string, boolean>();
return async function func(...args: any[]): Promise<boolean> {
const key = args.toString();
let value = memorized.get(key);
if (value) {
return value;
}
const [arg0, arg1] = args;
const name1: string = (arg0 || '').toString();
const name2: string = (arg1 || '').toString();
if (!rm) {
value = name1 === name2;
} else if (args.length === 2) {
value = await rm.hasLink(name1, name2);
} else {
const domain: string = args[2].toString();
value = await rm.hasLink(name1, name2, domain);
}
memorized.set(key, value);
return value;
};
}
// generateGFunction is the factory method of the sync g(_, _) function.
function generateSyncGFunction(rm: rbac.RoleManager): syncGFuntion {
const memorized = new Map<string, boolean>();
return function func(...args: any[]): boolean {
const key = args.toString();
let value = memorized.get(key);
if (value) {
return value;
}
const [arg0, arg1] = args;
const name1: string = (arg0 || '').toString();
const name2: string = (arg1 || '').toString();
if (!rm) {
value = name1 === name2;
} else if (args.length === 2) {
value = rm.hasLink(name1, name2) as boolean;
} else {
const domain: string = args[2].toString();
value = rm.hasLink(name1, name2, domain) as boolean;
}
memorized.set(key, value);
return value;
};
}
export {
keyGetFunc,
keyGet2Func,
keyMatchFunc,
keyMatch2Func,
keyMatch3Func,
regexMatchFunc,
ipMatchFunc,
generateGFunction,
generateSyncGFunction,
keyMatch4Func,
globMatch,
};