@nocobase/database 2.0.60 → 2.0.62
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.
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
import { DataTypes } from 'sequelize';
|
|
10
|
+
import type { UpdateOptions } from 'sequelize';
|
|
11
|
+
import { Model } from '../model';
|
|
10
12
|
import { BaseColumnFieldOptions, Field } from './field';
|
|
11
13
|
export interface PasswordFieldOptions extends BaseColumnFieldOptions {
|
|
12
14
|
type: 'password';
|
|
@@ -19,11 +21,17 @@ export interface PasswordFieldOptions extends BaseColumnFieldOptions {
|
|
|
19
21
|
*/
|
|
20
22
|
randomBytesSize?: number;
|
|
21
23
|
}
|
|
24
|
+
type PasswordValue = string | number;
|
|
25
|
+
type BulkUpdateOptions = UpdateOptions<Record<string, unknown>> & {
|
|
26
|
+
attributes?: Record<string, unknown>;
|
|
27
|
+
};
|
|
22
28
|
export declare class PasswordField extends Field {
|
|
23
29
|
get dataType(): DataTypes.StringDataTypeConstructor;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
listener: (instances: Model | Model[]) => Promise<void>;
|
|
31
|
+
bulkUpdateListener: (options: BulkUpdateOptions) => Promise<void>;
|
|
32
|
+
verify(password: PasswordValue, hash: string): Promise<boolean>;
|
|
33
|
+
hash(password: PasswordValue): Promise<string>;
|
|
27
34
|
bind(): void;
|
|
28
35
|
unbind(): void;
|
|
29
36
|
}
|
|
37
|
+
export {};
|
|
@@ -47,58 +47,78 @@ const _PasswordField = class _PasswordField extends import_field.Field {
|
|
|
47
47
|
get dataType() {
|
|
48
48
|
return import_sequelize.DataTypes.STRING;
|
|
49
49
|
}
|
|
50
|
+
listener = /* @__PURE__ */ __name(async (instances) => {
|
|
51
|
+
const { name } = this.options;
|
|
52
|
+
const fieldName = name;
|
|
53
|
+
instances = Array.isArray(instances) ? instances : [instances];
|
|
54
|
+
for (const instance of instances) {
|
|
55
|
+
if (!instance.changed(fieldName)) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const value = instance.get(fieldName);
|
|
59
|
+
if (value !== null && value !== void 0 && value !== "") {
|
|
60
|
+
const password = typeof value === "number" ? value : String(value);
|
|
61
|
+
const hash = await this.hash(password);
|
|
62
|
+
instance.set(fieldName, hash);
|
|
63
|
+
} else {
|
|
64
|
+
instance.set(fieldName, instance.previous(fieldName));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}, "listener");
|
|
68
|
+
bulkUpdateListener = /* @__PURE__ */ __name(async (options) => {
|
|
69
|
+
var _a;
|
|
70
|
+
const { name } = this.options;
|
|
71
|
+
const fieldName = name;
|
|
72
|
+
const { attributes } = options;
|
|
73
|
+
if (!attributes || !Object.prototype.hasOwnProperty.call(attributes, fieldName)) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const value = attributes[fieldName];
|
|
77
|
+
if (value !== null && value !== void 0 && value !== "") {
|
|
78
|
+
const password = typeof value === "number" ? value : String(value);
|
|
79
|
+
attributes[fieldName] = await this.hash(password);
|
|
80
|
+
} else {
|
|
81
|
+
delete attributes[fieldName];
|
|
82
|
+
options.fields = (_a = options.fields) == null ? void 0 : _a.filter((field) => field !== fieldName);
|
|
83
|
+
}
|
|
84
|
+
}, "bulkUpdateListener");
|
|
50
85
|
async verify(password, hash) {
|
|
51
|
-
|
|
86
|
+
const passwordString = password === null || password === void 0 ? "" : String(password);
|
|
52
87
|
hash = hash || "";
|
|
53
88
|
const { length = 64, randomBytesSize = 8 } = this.options;
|
|
54
89
|
return new Promise((resolve, reject) => {
|
|
55
90
|
const salt = hash.substring(0, randomBytesSize * 2);
|
|
56
91
|
const key = hash.substring(randomBytesSize * 2);
|
|
57
|
-
import_crypto.default.scrypt(
|
|
92
|
+
import_crypto.default.scrypt(passwordString, salt, length / 2 - randomBytesSize, (err, derivedKey) => {
|
|
58
93
|
if (err) reject(err);
|
|
59
94
|
resolve(key == derivedKey.toString("hex"));
|
|
60
95
|
});
|
|
61
96
|
});
|
|
62
97
|
}
|
|
63
98
|
async hash(password) {
|
|
99
|
+
const passwordString = String(password);
|
|
64
100
|
const { length = 64, randomBytesSize = 8 } = this.options;
|
|
65
101
|
return new Promise((resolve, reject) => {
|
|
66
102
|
const salt = import_crypto.default.randomBytes(randomBytesSize).toString("hex");
|
|
67
|
-
import_crypto.default.scrypt(
|
|
103
|
+
import_crypto.default.scrypt(passwordString, salt, length / 2 - randomBytesSize, (err, derivedKey) => {
|
|
68
104
|
if (err) reject(err);
|
|
69
105
|
resolve(salt + derivedKey.toString("hex"));
|
|
70
106
|
});
|
|
71
107
|
});
|
|
72
108
|
}
|
|
73
|
-
init() {
|
|
74
|
-
const { name } = this.options;
|
|
75
|
-
this.listener = async (instances) => {
|
|
76
|
-
instances = Array.isArray(instances) ? instances : [instances];
|
|
77
|
-
for (const instance of instances) {
|
|
78
|
-
if (!instance.changed(name)) {
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
const value = instance.get(name);
|
|
82
|
-
if (value) {
|
|
83
|
-
const hash = await this.hash(value);
|
|
84
|
-
instance.set(name, hash);
|
|
85
|
-
} else {
|
|
86
|
-
instance.set(name, instance.previous(name));
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
109
|
bind() {
|
|
92
110
|
super.bind();
|
|
93
111
|
this.on("beforeCreate", this.listener);
|
|
94
112
|
this.on("beforeBulkCreate", this.listener);
|
|
95
113
|
this.on("beforeUpdate", this.listener);
|
|
114
|
+
this.on("beforeBulkUpdate", this.bulkUpdateListener);
|
|
96
115
|
}
|
|
97
116
|
unbind() {
|
|
98
117
|
super.unbind();
|
|
99
118
|
this.off("beforeCreate", this.listener);
|
|
100
119
|
this.off("beforeBulkCreate", this.listener);
|
|
101
120
|
this.off("beforeUpdate", this.listener);
|
|
121
|
+
this.off("beforeBulkUpdate", this.bulkUpdateListener);
|
|
102
122
|
}
|
|
103
123
|
};
|
|
104
124
|
__name(_PasswordField, "PasswordField");
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/database",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.62",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@nocobase/logger": "2.0.
|
|
10
|
-
"@nocobase/utils": "2.0.
|
|
9
|
+
"@nocobase/logger": "2.0.62",
|
|
10
|
+
"@nocobase/utils": "2.0.62",
|
|
11
11
|
"async-mutex": "^0.3.2",
|
|
12
12
|
"chalk": "^4.1.1",
|
|
13
13
|
"cron-parser": "4.4.0",
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
39
39
|
"directory": "packages/database"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "b21b0a5111d188ba495eb579fdae674c5680fff2"
|
|
42
42
|
}
|