mm_validator 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +32 -6
- package/package.json +33 -34
- package/tool/abac/index.js +78 -5
- package/tool/basic/index.js +71 -5
- package/tool/code/index.js +460 -5
- package/tool/enum/index.js +20 -5
- package/tool/json/index.js +179 -5
- package/tool/jwt/index.js +107 -5
- package/tool/length/index.js +59 -5
- package/tool/oauth/index.js +168 -5
- package/tool/params/index.js +97 -5
- package/tool/pattern/index.js +56 -5
- package/tool/range/index.js +110 -5
- package/tool/rbac/index.js +90 -5
- package/tool/required/index.js +35 -5
- package/tool/sanitize/index.js +122 -5
- package/tool/string/index.js +73 -5
- package/tool/type/index.js +19 -5
package/index.js
CHANGED
|
@@ -1,9 +1,35 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
|
-
const
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const TOOL_DIR = path.join(__dirname, 'tool');
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
/**
|
|
8
|
+
* 列出含 tool.json 的工具目录名
|
|
9
|
+
* @param {string} tool_dir 工具根目录绝对路径
|
|
10
|
+
* @returns {Array<string>} 工具名数组
|
|
11
|
+
*/
|
|
12
|
+
function scan_tool_names(tool_dir) {
|
|
13
|
+
if (!fs.existsSync(tool_dir)) return [];
|
|
14
|
+
return fs.readdirSync(tool_dir, { withFileTypes: true })
|
|
15
|
+
.filter((e) => e.isDirectory() && fs.existsSync(path.join(tool_dir, e.name, 'tool.json')))
|
|
16
|
+
.map((e) => e.name);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 读取指定工具的契约
|
|
21
|
+
* @param {string} tool_dir 工具根目录绝对路径
|
|
22
|
+
* @param {string} name 工具名
|
|
23
|
+
* @returns {object|null} tool.json 内容,不存在返回 null
|
|
24
|
+
*/
|
|
25
|
+
function read_tool_config(tool_dir, name) {
|
|
26
|
+
const file = path.join(tool_dir, name, 'tool.json');
|
|
27
|
+
return fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, 'utf8')) : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 工具契约查询:模块根直接导出函数 */
|
|
31
|
+
module.exports = {
|
|
32
|
+
TOOL_DIR,
|
|
33
|
+
listTools: function () { return scan_tool_names(TOOL_DIR); },
|
|
34
|
+
getToolConfig: function (name) { return read_tool_config(TOOL_DIR, name); }
|
|
35
|
+
};
|
package/package.json
CHANGED
|
@@ -1,36 +1,35 @@
|
|
|
1
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
|
-
}
|
|
2
|
+
"name": "mm_validator",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "mm_workflow 验证器工具包:ABAC、基础、验证码、枚举、JSON、JWT、长度、OAuth、参数、模式、范围、RBAC、必填、清洗、字符串、类型 等 16 个验证工具模块",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_validator.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"mm_mod",
|
|
13
|
+
"operator",
|
|
14
|
+
"validator",
|
|
15
|
+
"超级美眉",
|
|
16
|
+
"工作流工具"
|
|
17
|
+
],
|
|
18
|
+
"layer": "util",
|
|
19
|
+
"group": "operator",
|
|
20
|
+
"operator": "validator",
|
|
21
|
+
"author": "邱文武",
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"files": [
|
|
24
|
+
"index.js",
|
|
25
|
+
"lib",
|
|
26
|
+
"tool"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=16"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"test": "node -e \"const p=require('path');const{TOOL_DIR,listTools}=require('./index.js');const n=listTools();n.forEach(x=>require(p.join(TOOL_DIR,x)));console.log(n.length+' tools loaded')\""
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {}
|
|
36
35
|
}
|
package/tool/abac/index.js
CHANGED
|
@@ -1,7 +1,80 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* ABAC权限验证器
|
|
3
|
+
* 基于属性的访问控制:根据 subject/resource/operation/environment 属性评估策略
|
|
4
|
+
* 策略配置在 this.config.options.policies 中
|
|
6
5
|
*/
|
|
7
|
-
|
|
6
|
+
|
|
7
|
+
async function main(params, ctx, options) {
|
|
8
|
+
var action = params.action || 'evaluate';
|
|
9
|
+
var subject = params.subject || {};
|
|
10
|
+
var resource = params.resource || {};
|
|
11
|
+
var operation = params.operation || '';
|
|
12
|
+
var environment = params.environment || {};
|
|
13
|
+
|
|
14
|
+
// 从 options 中读取策略配置
|
|
15
|
+
var opts = (this && typeof this.getOptions === 'function') ? this.getOptions() : {};
|
|
16
|
+
var policies = opts.policies || [];
|
|
17
|
+
|
|
18
|
+
switch (action) {
|
|
19
|
+
case 'evaluate': {
|
|
20
|
+
var decisions = [];
|
|
21
|
+
var allowed = false;
|
|
22
|
+
|
|
23
|
+
for (var i = 0; i < policies.length; i++) {
|
|
24
|
+
var policy = policies[i];
|
|
25
|
+
// 简单规则匹配:检查 subject.role 和 resource.type
|
|
26
|
+
var subject_match = !policy.subject_role || policy.subject_role === subject.role;
|
|
27
|
+
var resource_match = !policy.resource_type || policy.resource_type === resource.type;
|
|
28
|
+
var operation_match = !policy.operation || policy.operation === operation;
|
|
29
|
+
|
|
30
|
+
if (subject_match && resource_match && operation_match) {
|
|
31
|
+
decisions.push({
|
|
32
|
+
policy: policy.name || 'policy_' + i,
|
|
33
|
+
effect: policy.effect || 'deny'
|
|
34
|
+
});
|
|
35
|
+
if (policy.effect === 'allow') {
|
|
36
|
+
allowed = true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 无策略匹配则默认拒绝
|
|
42
|
+
var tip = allowed ? '参数校验通过' : '1个参数校验失败: 无匹配策略,默认拒绝';
|
|
43
|
+
ctx.output = {
|
|
44
|
+
valid: allowed,
|
|
45
|
+
tip: tip,
|
|
46
|
+
errors: allowed ? [] : ['无匹配策略,默认拒绝'],
|
|
47
|
+
policies: decisions
|
|
48
|
+
};
|
|
49
|
+
return ctx.output;
|
|
50
|
+
}
|
|
51
|
+
case 'add_policy': {
|
|
52
|
+
var new_policy = {
|
|
53
|
+
name: params.policy_name || 'policy_' + policies.length,
|
|
54
|
+
subject_role: params.subject_role || '',
|
|
55
|
+
resource_type: params.resource_type || '',
|
|
56
|
+
operation: params.operation || '',
|
|
57
|
+
effect: params.effect || 'deny'
|
|
58
|
+
};
|
|
59
|
+
policies.push(new_policy);
|
|
60
|
+
ctx.output = {
|
|
61
|
+
valid: true,
|
|
62
|
+
tip: '参数校验通过',
|
|
63
|
+
errors: [],
|
|
64
|
+
policies: policies
|
|
65
|
+
};
|
|
66
|
+
return ctx.output;
|
|
67
|
+
}
|
|
68
|
+
default: {
|
|
69
|
+
ctx.output = {
|
|
70
|
+
valid: false,
|
|
71
|
+
tip: '1个参数校验失败: 不支持的操作类型: ' + action,
|
|
72
|
+
errors: ['不支持的操作类型: ' + action],
|
|
73
|
+
policies: []
|
|
74
|
+
};
|
|
75
|
+
return ctx.output;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports = { main };
|
package/tool/basic/index.js
CHANGED
|
@@ -1,7 +1,73 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* 其它模块可直接 require('mm_validator').tools.basic 复用。
|
|
2
|
+
* Basic验证器
|
|
3
|
+
* 验证HTTP Basic认证
|
|
6
4
|
*/
|
|
7
|
-
|
|
5
|
+
|
|
6
|
+
async function main(params, ctx, options) {
|
|
7
|
+
const action = params.action;
|
|
8
|
+
const username = params.username || '';
|
|
9
|
+
const password = params.password || '';
|
|
10
|
+
const encoded = params.encoded || '';
|
|
11
|
+
|
|
12
|
+
switch (action) {
|
|
13
|
+
case 'encode': {
|
|
14
|
+
const combined = username + ':' + password;
|
|
15
|
+
const base64 = Buffer.from(combined).toString('base64');
|
|
16
|
+
ctx.output = { valid: true, tip: '参数校验通过', errors: [] };
|
|
17
|
+
return ctx.output;
|
|
18
|
+
}
|
|
19
|
+
case 'decode': {
|
|
20
|
+
const decoded = Buffer.from(encoded, 'base64').toString();
|
|
21
|
+
ctx.output = { valid: true, tip: '参数校验通过', errors: [] };
|
|
22
|
+
return ctx.output;
|
|
23
|
+
}
|
|
24
|
+
case 'verify': {
|
|
25
|
+
let verified = false;
|
|
26
|
+
const base64 = params.base64 || '';
|
|
27
|
+
|
|
28
|
+
// 如果提供了base64,解码后比对
|
|
29
|
+
if (base64) {
|
|
30
|
+
let auth_value = base64;
|
|
31
|
+
// 去掉 'Basic ' 前缀
|
|
32
|
+
if (auth_value.indexOf('Basic ') === 0) {
|
|
33
|
+
auth_value = auth_value.substring(6);
|
|
34
|
+
}
|
|
35
|
+
const decoded = Buffer.from(auth_value, 'base64').toString();
|
|
36
|
+
const parts = decoded.split(':');
|
|
37
|
+
const decoded_username = parts[0] || '';
|
|
38
|
+
const decoded_password = parts.slice(1).join(':');
|
|
39
|
+
|
|
40
|
+
if (decoded_username === username && decoded_password === password) {
|
|
41
|
+
verified = true;
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
// 没有base64时,比对expected_password或直接比对password
|
|
45
|
+
const expected_password = params.expected_password;
|
|
46
|
+
if (expected_password !== undefined) {
|
|
47
|
+
verified = password === expected_password;
|
|
48
|
+
} else {
|
|
49
|
+
// 至少有一个凭证就被认为是有效的
|
|
50
|
+
verified = !!(username && password);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
var tip = verified ? '参数校验通过' : '1个参数校验失败: 用户名或密码不匹配';
|
|
55
|
+
ctx.output = {
|
|
56
|
+
valid: verified,
|
|
57
|
+
tip: tip,
|
|
58
|
+
errors: verified ? [] : ['用户名或密码不匹配']
|
|
59
|
+
};
|
|
60
|
+
return ctx.output;
|
|
61
|
+
}
|
|
62
|
+
default: {
|
|
63
|
+
ctx.output = {
|
|
64
|
+
valid: false,
|
|
65
|
+
tip: '1个参数校验失败: 不支持的操作类型: ' + action,
|
|
66
|
+
errors: ['不支持的操作类型: ' + action]
|
|
67
|
+
};
|
|
68
|
+
return ctx.output;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = { main };
|