namirasoft-node 1.4.20 → 1.4.22

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,12 +7,21 @@ export declare class OTPOperation {
7
7
  static OTP_VER_MAX_TRY_COUNT: number;
8
8
  static generate(length?: number, digit?: number): string;
9
9
  static getWaitTime(x_otp_req_tried_count: number): number;
10
- static onSafeRequest(x_otp_time: Date | null, x_otp_req_tried_count: number, handler: () => Promise<void>): Promise<{
10
+ static onSafeRequest(x_otp: string | null, x_otp_time: Date | null, x_otp_req_tried_count: number, handler: (row: {
11
+ x_otp: string;
12
+ x_otp_time: Date;
13
+ x_otp_req_tried_count: number;
14
+ x_otp_ver_tried_count: number;
15
+ }) => Promise<void>): Promise<{
11
16
  error: string;
12
17
  next_time: number;
13
18
  } | {
14
19
  next_time: number;
15
20
  error?: undefined;
16
21
  }>;
17
- static onSafeVerify(x_otp: string, x_otp_time: Date, x_otp_ver_tried_count: number, code: string, handler: () => Promise<void>, errorHandler: () => Promise<void>): Promise<void>;
22
+ static onSafeVerify(x_otp: string | null, x_otp_time: Date | null, x_otp_ver_tried_count: number, code: string, handler: (row: {
23
+ x_otp: string | null;
24
+ x_otp_time: Date | null;
25
+ x_approved: boolean;
26
+ }) => Promise<void>, errorHandler: () => Promise<void>): Promise<void>;
18
27
  }
@@ -32,7 +32,7 @@ class OTPOperation {
32
32
  wait_time = Math.min(Math.pow(OTPOperation.INCREASE_POWER_BASE, extra_attempt) * 60, OTPOperation.OTP_MAX_WAIT_TIME);
33
33
  return parseInt(wait_time + "");
34
34
  }
35
- static onSafeRequest(x_otp_time, x_otp_req_tried_count, handler) {
35
+ static onSafeRequest(x_otp, x_otp_time, x_otp_req_tried_count, handler) {
36
36
  return __awaiter(this, void 0, void 0, function* () {
37
37
  let wait_time = this.getWaitTime(x_otp_req_tried_count);
38
38
  let wait_date = namirasoft_core_1.TimeOperation.minutesAgo(wait_time, new Date());
@@ -43,16 +43,22 @@ class OTPOperation {
43
43
  next_time
44
44
  };
45
45
  }
46
- yield handler();
47
- wait_time = this.getWaitTime(x_otp_req_tried_count + 1);
46
+ let x_otp_ver_tried_count = 0;
47
+ if (!x_otp)
48
+ x_otp_req_tried_count = 0;
49
+ x_otp = OTPOperation.generate(6, 3);
50
+ x_otp_time = new Date();
51
+ x_otp_req_tried_count += 1;
52
+ yield handler({ x_otp, x_otp_time, x_otp_req_tried_count, x_otp_ver_tried_count });
53
+ wait_time = this.getWaitTime(x_otp_req_tried_count);
48
54
  next_time = wait_time * 60;
49
55
  return { next_time };
50
56
  });
51
57
  }
52
58
  static onSafeVerify(x_otp, x_otp_time, x_otp_ver_tried_count, code, handler, errorHandler) {
53
59
  return __awaiter(this, void 0, void 0, function* () {
54
- if (!x_otp)
55
- namirasoft_core_1.ErrorOperation.throwHTTP(403, "The OTP not generated yet. Please request first.");
60
+ if (!x_otp || !x_otp_time)
61
+ throw namirasoft_core_1.ErrorOperation.getHTTP(403, "The OTP not generated yet. Please request first.");
56
62
  if (x_otp_time < namirasoft_core_1.TimeOperation.minutesAgo(OTPOperation.OTP_EXPIRE_TIME, new Date()))
57
63
  namirasoft_core_1.ErrorOperation.throwHTTP(403, "The OTP code expired. Please request again.");
58
64
  if (x_otp_ver_tried_count > OTPOperation.OTP_VER_MAX_TRY_COUNT)
@@ -61,7 +67,11 @@ class OTPOperation {
61
67
  yield errorHandler();
62
68
  namirasoft_core_1.ErrorOperation.throwHTTP(403, "Wrong code.");
63
69
  }
64
- yield handler();
70
+ yield handler({
71
+ x_otp: null,
72
+ x_otp_time: null,
73
+ x_approved: true,
74
+ });
65
75
  });
66
76
  }
67
77
  }
@@ -1 +1 @@
1
- {"version":3,"file":"OTPOperation.js","sourceRoot":"","sources":["../src/OTPOperation.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAgE;AAEhE,MAAa,YAAY;IAUrB,MAAM,CAAC,QAAQ,CAAC,SAAiB,CAAC,EAAE,QAAgB,CAAC;QA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EAAE,CAAC;QACzB,CAAC;KAAA;IACD,MAAM,CAAO,YAAY,CAAC,KAAoB,EAAE,UAAuB,EAAE,qBAA6B,EAAE,IAAY,EAChH,OAImB,EACnB,YAAiC;;YAEjC,IAAI,CAAC,KAAK,IAAI,CAAC,UAAU;gBACrB,MAAM,gCAAc,CAAC,OAAO,CAAC,GAAG,EAAE,kDAAkD,CAAC,CAAC;YAE1F,IAAI,UAAU,GAAG,+BAAa,CAAC,UAAU,CAAC,YAAY,CAAC,eAAe,EAAE,IAAI,IAAI,EAAE,CAAC;gBAC/E,gCAAc,CAAC,SAAS,CAAC,GAAG,EAAE,6CAA6C,CAAC,CAAC;YAEjF,IAAI,qBAAqB,GAAG,YAAY,CAAC,qBAAqB;gBAC1D,gCAAc,CAAC,SAAS,CAAC,GAAG,EAAE,uDAAuD,CAAC,CAAC;YAE3F,IAAI,KAAK,KAAK,IAAI,EAClB,CAAC;gBACG,MAAM,YAAY,EAAE,CAAC;gBACrB,gCAAc,CAAC,SAAS,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC;YACjD,CAAC;YACD,MAAM,OAAO,CAAC;gBACV,KAAK,EAAE,IAAI;gBACX,UAAU,EAAE,IAAI;gBAChB,UAAU,EAAE,IAAI;aACnB,CAAC,CAAC;QACP,CAAC;KAAA;;AAvFL,oCAwFC;AAtFiB,8BAAiB,GAAW,CAAC,CAAC;AAC9B,8BAAiB,GAAW,IAAI,CAAC;AACjC,4BAAe,GAAW,CAAC,CAAC;AAC5B,gCAAmB,GAAW,CAAC,CAAC;AAEhC,kCAAqB,GAAW,CAAC,CAAC;AAClC,kCAAqB,GAAW,CAAC,CAAC"}
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "framework": "npm",
9
9
  "application": "package",
10
10
  "private": false,
11
- "version": "1.4.20",
11
+ "version": "1.4.22",
12
12
  "author": "Amir Abolhasani",
13
13
  "license": "MIT",
14
14
  "main": "./dist/index.js",
@@ -32,7 +32,8 @@ export class OTPOperation
32
32
  wait_time = Math.min(Math.pow(OTPOperation.INCREASE_POWER_BASE, extra_attempt) * 60, OTPOperation.OTP_MAX_WAIT_TIME);
33
33
  return parseInt(wait_time + "");
34
34
  }
35
- static async onSafeRequest(x_otp_time: Date | null, x_otp_req_tried_count: number, handler: () => Promise<void>)
35
+ static async onSafeRequest(x_otp: string | null, x_otp_time: Date | null, x_otp_req_tried_count: number,
36
+ handler: (row: { x_otp: string, x_otp_time: Date, x_otp_req_tried_count: number, x_otp_ver_tried_count: number }) => Promise<void>)
36
37
  {
37
38
  // check wait time
38
39
  let wait_time = this.getWaitTime(x_otp_req_tried_count);
@@ -45,16 +46,30 @@ export class OTPOperation
45
46
  next_time
46
47
  };
47
48
  }
48
- await handler();
49
- wait_time = this.getWaitTime(x_otp_req_tried_count + 1);
49
+
50
+ let x_otp_ver_tried_count = 0;
51
+ if (!x_otp)
52
+ x_otp_req_tried_count = 0;
53
+ x_otp = OTPOperation.generate(6, 3);
54
+ x_otp_time = new Date();
55
+ x_otp_req_tried_count += 1;
56
+
57
+ await handler({ x_otp, x_otp_time, x_otp_req_tried_count, x_otp_ver_tried_count });
58
+
59
+ wait_time = this.getWaitTime(x_otp_req_tried_count);
50
60
  next_time = wait_time * 60;
51
61
  return { next_time };
52
62
  }
53
- static async onSafeVerify(x_otp: string, x_otp_time: Date, x_otp_ver_tried_count: number, code: string,
54
- handler: () => Promise<void>, errorHandler: () => Promise<void>)
63
+ static async onSafeVerify(x_otp: string | null, x_otp_time: Date | null, x_otp_ver_tried_count: number, code: string,
64
+ handler: (row: {
65
+ x_otp: string | null,
66
+ x_otp_time: Date | null,
67
+ x_approved: boolean,
68
+ }) => Promise<void>,
69
+ errorHandler: () => Promise<void>)
55
70
  {
56
- if (!x_otp)
57
- ErrorOperation.throwHTTP(403, "The OTP not generated yet. Please request first.");
71
+ if (!x_otp || !x_otp_time)
72
+ throw ErrorOperation.getHTTP(403, "The OTP not generated yet. Please request first.");
58
73
 
59
74
  if (x_otp_time < TimeOperation.minutesAgo(OTPOperation.OTP_EXPIRE_TIME, new Date()))
60
75
  ErrorOperation.throwHTTP(403, "The OTP code expired. Please request again.");
@@ -67,6 +82,10 @@ export class OTPOperation
67
82
  await errorHandler();
68
83
  ErrorOperation.throwHTTP(403, "Wrong code.");
69
84
  }
70
- await handler();
85
+ await handler({
86
+ x_otp: null,
87
+ x_otp_time: null,
88
+ x_approved: true,
89
+ });
71
90
  }
72
91
  }