@ruiapp/rapid-core 0.3.5 → 0.3.6

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/dist/index.d.ts CHANGED
@@ -9,6 +9,7 @@ export * from "./utilities/accessControlUtility";
9
9
  export * from "./utilities/entityUtility";
10
10
  export * from "./utilities/jwtUtility";
11
11
  export * from "./utilities/timeUtility";
12
+ export * from "./utilities/passwordUtility";
12
13
  export * from "./helpers/licenseHelper";
13
14
  export { mapDbRowToEntity } from "./dataAccess/entityMapper";
14
15
  export * as bootstrapApplicationConfig from "./bootstrapApplicationConfig";
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ var qs = require('qs');
9
9
  var dayjs = require('dayjs');
10
10
  var jsonwebtoken = require('jsonwebtoken');
11
11
  var crypto = require('crypto');
12
- var bcrypt = require('bcrypt');
12
+ var bcrypt = require('bcryptjs');
13
13
  var path = require('path');
14
14
  var fs = require('fs');
15
15
  var uuid = require('uuid');
@@ -4361,6 +4361,30 @@ async function generateJwtSecretKey() {
4361
4361
  return encode(exportedKey);
4362
4362
  }
4363
4363
 
4364
+ /**
4365
+ * Generates password hash.
4366
+ * @param password
4367
+ * @param salt
4368
+ * @returns
4369
+ */
4370
+ async function generatePasswordHash(password, salt) {
4371
+ if (!salt) {
4372
+ salt = 10;
4373
+ }
4374
+ const passwordHash = await bcrypt__default["default"].hash(password, salt);
4375
+ return passwordHash;
4376
+ }
4377
+ /**
4378
+ * Validates the password against the hash.
4379
+ * @param password
4380
+ * @param passwordHash
4381
+ * @returns
4382
+ */
4383
+ async function validatePassword(password, passwordHash) {
4384
+ const isMatch = await bcrypt__default["default"].compare(password, passwordHash);
4385
+ return isMatch;
4386
+ }
4387
+
4364
4388
  function validateLicense(server) {
4365
4389
  const licenseService = server.getService("licenseService");
4366
4390
  const license = licenseService.getLicense();
@@ -6047,12 +6071,11 @@ async function handler$e(plugin, ctx, options) {
6047
6071
  if (!user) {
6048
6072
  throw new Error("User not found.");
6049
6073
  }
6050
- const isMatch = await bcrypt__default["default"].compare(oldPassword, user.password);
6074
+ const isMatch = await validatePassword(oldPassword, user.password);
6051
6075
  if (!isMatch) {
6052
6076
  throw new Error("旧密码错误。");
6053
6077
  }
6054
- const saltRounds = 10;
6055
- const passwordHash = await bcrypt__default["default"].hash(newPassword, saltRounds);
6078
+ const passwordHash = await generatePasswordHash(newPassword);
6056
6079
  await userDataAccessor.updateById(user.id, {
6057
6080
  password: passwordHash,
6058
6081
  });
@@ -6086,7 +6109,10 @@ async function handler$d(plugin, ctx, options) {
6086
6109
  if (!user) {
6087
6110
  throw new Error("用户名或密码错误。");
6088
6111
  }
6089
- const isMatch = await bcrypt__default["default"].compare(password, user.password);
6112
+ if (user.state !== "enabled") {
6113
+ throw new Error("用户已被禁用,不允许登录。");
6114
+ }
6115
+ const isMatch = await validatePassword(password, user.password);
6090
6116
  if (!isMatch) {
6091
6117
  throw new Error("用户名或密码错误。");
6092
6118
  }
@@ -6186,8 +6212,7 @@ async function handler$a(plugin, ctx, options) {
6186
6212
  if (!user) {
6187
6213
  throw new Error("User not found.");
6188
6214
  }
6189
- const saltRounds = 10;
6190
- const passwordHash = await bcrypt__default["default"].hash(password, saltRounds);
6215
+ const passwordHash = await generatePasswordHash(password);
6191
6216
  await userDataAccessor.updateById(user.id, {
6192
6217
  password: passwordHash,
6193
6218
  });
@@ -8316,6 +8341,7 @@ exports.bootstrapApplicationConfig = bootstrapApplicationConfig$1;
8316
8341
  exports.createJwt = createJwt;
8317
8342
  exports.decodeJwt = decodeJwt;
8318
8343
  exports.generateJwtSecretKey = generateJwtSecretKey;
8344
+ exports.generatePasswordHash = generatePasswordHash;
8319
8345
  exports.getEntityRelationTargetId = getEntityRelationTargetId;
8320
8346
  exports.getNowString = getNowString;
8321
8347
  exports.getNowStringWithTimezone = getNowStringWithTimezone;
@@ -8323,4 +8349,5 @@ exports.isAccessAllowed = isAccessAllowed;
8323
8349
  exports.mapDbRowToEntity = mapDbRowToEntity;
8324
8350
  exports.tryValidateLicense = tryValidateLicense;
8325
8351
  exports.validateLicense = validateLicense;
8352
+ exports.validatePassword = validatePassword;
8326
8353
  exports.verifyJwt = verifyJwt;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Generates password hash.
3
+ * @param password
4
+ * @param salt
5
+ * @returns
6
+ */
7
+ export declare function generatePasswordHash(password: string, salt?: number | string): Promise<string>;
8
+ /**
9
+ * Validates the password against the hash.
10
+ * @param password
11
+ * @param passwordHash
12
+ * @returns
13
+ */
14
+ export declare function validatePassword(password: string, passwordHash: string): Promise<boolean>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,7 +19,7 @@
19
19
  "typescript": "^4.8.4"
20
20
  },
21
21
  "dependencies": {
22
- "bcrypt": "^5.1.1",
22
+ "bcryptjs": "^3.0.2",
23
23
  "cron": "^3.1.7",
24
24
  "dayjs": "^1.11.7",
25
25
  "jsonwebtoken": "^9.0.2",
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ export * from "./utilities/accessControlUtility";
14
14
  export * from "./utilities/entityUtility";
15
15
  export * from "./utilities/jwtUtility";
16
16
  export * from "./utilities/timeUtility";
17
+ export * from "./utilities/passwordUtility";
17
18
 
18
19
  export * from "./helpers/licenseHelper";
19
20
 
@@ -1,6 +1,6 @@
1
- import bcrypt from "bcrypt";
2
1
  import { ActionHandlerContext } from "~/core/actionHandler";
3
2
  import { RapidPlugin } from "~/core/server";
3
+ import { generatePasswordHash, validatePassword } from "~/utilities/passwordUtility";
4
4
 
5
5
  export const code = "changePassword";
6
6
 
@@ -38,13 +38,12 @@ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, op
38
38
  throw new Error("User not found.");
39
39
  }
40
40
 
41
- const isMatch = await bcrypt.compare(oldPassword, user.password);
41
+ const isMatch = await validatePassword(oldPassword, user.password);
42
42
  if (!isMatch) {
43
43
  throw new Error("旧密码错误。");
44
44
  }
45
45
 
46
- const saltRounds = 10;
47
- const passwordHash = await bcrypt.hash(newPassword, saltRounds);
46
+ const passwordHash = await generatePasswordHash(newPassword);
48
47
 
49
48
  await userDataAccessor.updateById(user.id, {
50
49
  password: passwordHash,
@@ -1,9 +1,9 @@
1
- import bcrypt from "bcrypt";
2
1
  import { setCookie } from "~/deno-std/http/cookie";
3
2
  import { createJwt } from "~/utilities/jwtUtility";
4
3
  import { ActionHandlerContext } from "~/core/actionHandler";
5
4
  import { RapidPlugin } from "~/core/server";
6
5
  import { validateLicense } from "~/helpers/licenseHelper";
6
+ import { validatePassword } from "~/utilities/passwordUtility";
7
7
 
8
8
  export interface UserAccessToken {
9
9
  sub: "userAccessToken";
@@ -37,7 +37,11 @@ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, op
37
37
  throw new Error("用户名或密码错误。");
38
38
  }
39
39
 
40
- const isMatch = await bcrypt.compare(password, user.password);
40
+ if (user.state !== "enabled") {
41
+ throw new Error("用户已被禁用,不允许登录。");
42
+ }
43
+
44
+ const isMatch = await validatePassword(password, user.password);
41
45
  if (!isMatch) {
42
46
  throw new Error("用户名或密码错误。");
43
47
  }
@@ -1,6 +1,6 @@
1
- import bcrypt from "bcrypt";
2
1
  import { ActionHandlerContext } from "~/core/actionHandler";
3
2
  import { RapidPlugin } from "~/core/server";
3
+ import { generatePasswordHash } from "~/utilities/passwordUtility";
4
4
 
5
5
  export const code = "resetPassword";
6
6
 
@@ -27,8 +27,7 @@ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, op
27
27
  throw new Error("User not found.");
28
28
  }
29
29
 
30
- const saltRounds = 10;
31
- const passwordHash = await bcrypt.hash(password, saltRounds);
30
+ const passwordHash = await generatePasswordHash(password);
32
31
 
33
32
  await userDataAccessor.updateById(user.id, {
34
33
  password: passwordHash,
@@ -0,0 +1,26 @@
1
+ import bcrypt from "bcryptjs";
2
+
3
+ /**
4
+ * Generates password hash.
5
+ * @param password
6
+ * @param salt
7
+ * @returns
8
+ */
9
+ export async function generatePasswordHash(password: string, salt?: number | string): Promise<string> {
10
+ if (!salt) {
11
+ salt = 10;
12
+ }
13
+ const passwordHash = await bcrypt.hash(password, salt);
14
+ return passwordHash;
15
+ }
16
+
17
+ /**
18
+ * Validates the password against the hash.
19
+ * @param password
20
+ * @param passwordHash
21
+ * @returns
22
+ */
23
+ export async function validatePassword(password: string, passwordHash: string): Promise<boolean> {
24
+ const isMatch = await bcrypt.compare(password, passwordHash);
25
+ return isMatch;
26
+ }