namirasoft-node 1.0.16 → 1.0.17
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/OTPOperation.d.ts +2 -2
- package/dist/OTPOperation.js +8 -8
- package/dist/OTPOperation.js.map +1 -1
- package/package.json +1 -1
- package/src/OTPOperation.ts +10 -10
package/dist/OTPOperation.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
export declare class OTPOperation {
|
|
2
2
|
static generate(length?: number, digit?: number): string;
|
|
3
3
|
static getWaitTime(min_wait_time: number, max_wait_time: number, min_attempt: number, user_attemptted: number, increase_power_base?: number): number;
|
|
4
|
-
static onSafeRequest(handler: () => Promise<void>,
|
|
4
|
+
static onSafeRequest(handler: () => Promise<void>, min_wait_time: number, max_wait_time: number, min_try_count: number, user_tried_time: Date, user_tried_count: number, increase_power_base?: number): Promise<{
|
|
5
5
|
error: string;
|
|
6
6
|
next_time: number;
|
|
7
7
|
} | {
|
|
8
8
|
next_time: number;
|
|
9
9
|
error?: undefined;
|
|
10
10
|
}>;
|
|
11
|
-
static onSafeVerify(
|
|
11
|
+
static onSafeVerify(handler: () => Promise<void>, errorHandler: () => Promise<void>, valid_otp: string, valid_otp_time: Date, otp_expire_time: number, max_try_count: number, entered_otp: string, user_tried_count: number): Promise<void>;
|
|
12
12
|
}
|
package/dist/OTPOperation.js
CHANGED
|
@@ -32,12 +32,12 @@ class OTPOperation {
|
|
|
32
32
|
wait_time = Math.min(Math.pow(increase_power_base, extra_attempt) * 60, max_wait_time);
|
|
33
33
|
return parseInt(wait_time + "");
|
|
34
34
|
}
|
|
35
|
-
static onSafeRequest(handler,
|
|
35
|
+
static onSafeRequest(handler, min_wait_time, max_wait_time, min_try_count, user_tried_time, user_tried_count, increase_power_base = 2) {
|
|
36
36
|
return __awaiter(this, void 0, void 0, function* () {
|
|
37
37
|
// check wait time
|
|
38
|
-
let wait_time = this.getWaitTime(min_wait_time, max_wait_time,
|
|
38
|
+
let wait_time = this.getWaitTime(min_wait_time, max_wait_time, min_try_count, user_tried_count, increase_power_base);
|
|
39
39
|
let wait_date = namirasoft_core_1.TimeOperation.minutesAgo(wait_time, new Date());
|
|
40
|
-
let next_time = namirasoft_core_1.TimeOperation.diffInSecond(
|
|
40
|
+
let next_time = namirasoft_core_1.TimeOperation.diffInSecond(user_tried_time, wait_date, false);
|
|
41
41
|
if (next_time > 0) {
|
|
42
42
|
return {
|
|
43
43
|
error: 'Too many request, please try again in ' + next_time + ' seconds.',
|
|
@@ -45,20 +45,20 @@ class OTPOperation {
|
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
47
|
yield handler();
|
|
48
|
-
wait_time = this.getWaitTime(min_wait_time, max_wait_time,
|
|
48
|
+
wait_time = this.getWaitTime(min_wait_time, max_wait_time, min_try_count, user_tried_count + 1, increase_power_base);
|
|
49
49
|
next_time = wait_time * 60;
|
|
50
50
|
return { next_time };
|
|
51
51
|
});
|
|
52
52
|
}
|
|
53
|
-
static onSafeVerify(
|
|
53
|
+
static onSafeVerify(handler, errorHandler, valid_otp, valid_otp_time, otp_expire_time, max_try_count, entered_otp, user_tried_count) {
|
|
54
54
|
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
-
if (!
|
|
55
|
+
if (!entered_otp)
|
|
56
56
|
namirasoft_core_1.ErrorOperation.throwHTTP(403, "The OTP not generated yet.");
|
|
57
|
-
if (
|
|
57
|
+
if (valid_otp_time < namirasoft_core_1.TimeOperation.minutesAgo(otp_expire_time, new Date()))
|
|
58
58
|
namirasoft_core_1.ErrorOperation.throwHTTP(403, "The OTP code expired. Please request again.");
|
|
59
59
|
if (user_tried_count > max_try_count)
|
|
60
60
|
namirasoft_core_1.ErrorOperation.throwHTTP(403, "The try limit attempt exceeded. Please request again.");
|
|
61
|
-
if (
|
|
61
|
+
if (valid_otp !== entered_otp) {
|
|
62
62
|
yield errorHandler();
|
|
63
63
|
namirasoft_core_1.ErrorOperation.throwHTTP(403, "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,cAAc,GAAG,+BAAa,CAAC,UAAU,CAAC,eAAe,EAAE,IAAI,IAAI,EAAE,CAAC;gBACtE,gCAAc,CAAC,SAAS,CAAC,GAAG,EAAE,6CAA6C,CAAC,CAAC;YAEjF,IAAI,gBAAgB,GAAG,aAAa;gBAChC,gCAAc,CAAC,SAAS,CAAC,GAAG,EAAE,uDAAuD,CAAC,CAAC;YAE3F,IAAI,SAAS,KAAK,WAAW,EAC7B;gBACI,MAAM,YAAY,EAAE,CAAC;gBACrB,gCAAc,CAAC,SAAS,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC;aAChD;YACD,MAAM,OAAO,EAAE,CAAC;QACpB,CAAC;KAAA;CACJ;AA9DD,oCA8DC"}
|
package/package.json
CHANGED
package/src/OTPOperation.ts
CHANGED
|
@@ -24,12 +24,12 @@ export class OTPOperation
|
|
|
24
24
|
wait_time = Math.min(Math.pow(increase_power_base, extra_attempt) * 60, max_wait_time);
|
|
25
25
|
return parseInt(wait_time + "");
|
|
26
26
|
}
|
|
27
|
-
static async onSafeRequest(handler: () => Promise<void>,
|
|
27
|
+
static async onSafeRequest(handler: () => Promise<void>, min_wait_time: number, max_wait_time: number, min_try_count: number, user_tried_time: Date, user_tried_count: number, increase_power_base: number = 2)
|
|
28
28
|
{
|
|
29
29
|
// check wait time
|
|
30
|
-
let wait_time = this.getWaitTime(min_wait_time, max_wait_time,
|
|
30
|
+
let wait_time = this.getWaitTime(min_wait_time, max_wait_time, min_try_count, user_tried_count, increase_power_base);
|
|
31
31
|
let wait_date = TimeOperation.minutesAgo(wait_time, new Date());
|
|
32
|
-
let next_time = TimeOperation.diffInSecond(
|
|
32
|
+
let next_time = TimeOperation.diffInSecond(user_tried_time, wait_date, false);
|
|
33
33
|
if (next_time > 0)
|
|
34
34
|
{
|
|
35
35
|
return {
|
|
@@ -38,24 +38,24 @@ export class OTPOperation
|
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
await handler();
|
|
41
|
-
wait_time = this.getWaitTime(min_wait_time, max_wait_time,
|
|
41
|
+
wait_time = this.getWaitTime(min_wait_time, max_wait_time, min_try_count, user_tried_count + 1, increase_power_base);
|
|
42
42
|
next_time = wait_time * 60;
|
|
43
43
|
return { next_time };
|
|
44
44
|
}
|
|
45
|
-
static async onSafeVerify(
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
static async onSafeVerify(handler: () => Promise<void>, errorHandler: () => Promise<void>,
|
|
46
|
+
valid_otp: string, valid_otp_time: Date, otp_expire_time: number, max_try_count: number,
|
|
47
|
+
entered_otp: string, user_tried_count: number)
|
|
48
48
|
{
|
|
49
|
-
if (!
|
|
49
|
+
if (!entered_otp)
|
|
50
50
|
ErrorOperation.throwHTTP(403, "The OTP not generated yet.");
|
|
51
51
|
|
|
52
|
-
if (
|
|
52
|
+
if (valid_otp_time < TimeOperation.minutesAgo(otp_expire_time, new Date()))
|
|
53
53
|
ErrorOperation.throwHTTP(403, "The OTP code expired. Please request again.");
|
|
54
54
|
|
|
55
55
|
if (user_tried_count > max_try_count)
|
|
56
56
|
ErrorOperation.throwHTTP(403, "The try limit attempt exceeded. Please request again.");
|
|
57
57
|
|
|
58
|
-
if (
|
|
58
|
+
if (valid_otp !== entered_otp)
|
|
59
59
|
{
|
|
60
60
|
await errorHandler();
|
|
61
61
|
ErrorOperation.throwHTTP(403, "Wrong code.");
|