namirasoft-node 1.4.21 → 1.4.23
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/BaseApplication.js +7 -17
- package/dist/BaseApplication.js.map +1 -1
- package/dist/OTPOperation.d.ts +11 -2
- package/dist/OTPOperation.js +14 -4
- package/dist/OTPOperation.js.map +1 -1
- package/package.json +1 -1
- package/src/AnomalyDetector.ts +84 -84
- package/src/BaseApplication.ts +432 -432
- package/src/BaseApplicationLink.ts +6 -6
- package/src/BaseController.ts +193 -193
- package/src/BaseCron.ts +54 -54
- package/src/BaseDatabase.ts +199 -199
- package/src/BaseEmailService.ts +38 -38
- package/src/BaseTable.ts +107 -107
- package/src/CommandOperation.ts +32 -32
- package/src/EmptyDatabase.ts +11 -11
- package/src/GmailService.ts +22 -22
- package/src/IPOperation.ts +38 -38
- package/src/Meta.ts +40 -40
- package/src/OTPOperation.ts +90 -71
- package/src/RequestHeaderService.ts +27 -27
- package/src/SMTPService.ts +26 -26
- package/src/ServerToServerOperation.ts +23 -23
- package/src/index.ts +16 -16
package/src/OTPOperation.ts
CHANGED
|
@@ -1,72 +1,91 @@
|
|
|
1
|
-
import { ErrorOperation, TimeOperation } from "namirasoft-core";
|
|
2
|
-
|
|
3
|
-
export class OTPOperation
|
|
4
|
-
{
|
|
5
|
-
public static OTP_MIN_WAIT_TIME: number = 1; // minutes
|
|
6
|
-
public static OTP_MAX_WAIT_TIME: number = 1440; // minutes
|
|
7
|
-
public static OTP_EXPIRE_TIME: number = 5; // minutes
|
|
8
|
-
public static INCREASE_POWER_BASE: number = 2;
|
|
9
|
-
|
|
10
|
-
public static OTP_REQ_MAX_TRY_COUNT: number = 3;
|
|
11
|
-
public static OTP_VER_MAX_TRY_COUNT: number = 3;
|
|
12
|
-
|
|
13
|
-
static generate(length: number = 6, digit: number = 3)
|
|
14
|
-
{
|
|
15
|
-
if (!length)
|
|
16
|
-
length = 6;
|
|
17
|
-
if (!digit)
|
|
18
|
-
digit = 3;
|
|
19
|
-
let dig = [];
|
|
20
|
-
for (let i = 0; i < digit; i++)
|
|
21
|
-
dig[i] = parseInt((Math.random() * 9 + 1) + "");
|
|
22
|
-
let ans = '';
|
|
23
|
-
for (let i = 0; i < length; i++)
|
|
24
|
-
ans = ans + '' + dig[parseInt((Math.random() * dig.length) + "")];
|
|
25
|
-
return ans;
|
|
26
|
-
}
|
|
27
|
-
static getWaitTime(x_otp_req_tried_count: number): number
|
|
28
|
-
{
|
|
29
|
-
let wait_time = OTPOperation.OTP_MIN_WAIT_TIME;
|
|
30
|
-
let extra_attempt = x_otp_req_tried_count - OTPOperation.OTP_REQ_MAX_TRY_COUNT;
|
|
31
|
-
if (extra_attempt >= 0)
|
|
32
|
-
wait_time = Math.min(Math.pow(OTPOperation.INCREASE_POWER_BASE, extra_attempt) * 60, OTPOperation.OTP_MAX_WAIT_TIME);
|
|
33
|
-
return parseInt(wait_time + "");
|
|
34
|
-
}
|
|
35
|
-
static async onSafeRequest(x_otp_time: Date | null, x_otp_req_tried_count: number,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
let
|
|
40
|
-
let
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
next_time
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
1
|
+
import { ErrorOperation, TimeOperation } from "namirasoft-core";
|
|
2
|
+
|
|
3
|
+
export class OTPOperation
|
|
4
|
+
{
|
|
5
|
+
public static OTP_MIN_WAIT_TIME: number = 1; // minutes
|
|
6
|
+
public static OTP_MAX_WAIT_TIME: number = 1440; // minutes
|
|
7
|
+
public static OTP_EXPIRE_TIME: number = 5; // minutes
|
|
8
|
+
public static INCREASE_POWER_BASE: number = 2;
|
|
9
|
+
|
|
10
|
+
public static OTP_REQ_MAX_TRY_COUNT: number = 3;
|
|
11
|
+
public static OTP_VER_MAX_TRY_COUNT: number = 3;
|
|
12
|
+
|
|
13
|
+
static generate(length: number = 6, digit: number = 3)
|
|
14
|
+
{
|
|
15
|
+
if (!length)
|
|
16
|
+
length = 6;
|
|
17
|
+
if (!digit)
|
|
18
|
+
digit = 3;
|
|
19
|
+
let dig = [];
|
|
20
|
+
for (let i = 0; i < digit; i++)
|
|
21
|
+
dig[i] = parseInt((Math.random() * 9 + 1) + "");
|
|
22
|
+
let ans = '';
|
|
23
|
+
for (let i = 0; i < length; i++)
|
|
24
|
+
ans = ans + '' + dig[parseInt((Math.random() * dig.length) + "")];
|
|
25
|
+
return ans;
|
|
26
|
+
}
|
|
27
|
+
static getWaitTime(x_otp_req_tried_count: number): number
|
|
28
|
+
{
|
|
29
|
+
let wait_time = OTPOperation.OTP_MIN_WAIT_TIME;
|
|
30
|
+
let extra_attempt = x_otp_req_tried_count - OTPOperation.OTP_REQ_MAX_TRY_COUNT;
|
|
31
|
+
if (extra_attempt >= 0)
|
|
32
|
+
wait_time = Math.min(Math.pow(OTPOperation.INCREASE_POWER_BASE, extra_attempt) * 60, OTPOperation.OTP_MAX_WAIT_TIME);
|
|
33
|
+
return parseInt(wait_time + "");
|
|
34
|
+
}
|
|
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>)
|
|
37
|
+
{
|
|
38
|
+
// check wait time
|
|
39
|
+
let wait_time = this.getWaitTime(x_otp_req_tried_count);
|
|
40
|
+
let wait_date = TimeOperation.minutesAgo(wait_time, new Date());
|
|
41
|
+
let next_time = TimeOperation.diffInSecond(x_otp_time ?? new Date("0001-01-01"), wait_date, false);
|
|
42
|
+
if (next_time > 0)
|
|
43
|
+
{
|
|
44
|
+
return {
|
|
45
|
+
error: 'Too many request, please try again in ' + next_time + ' seconds.',
|
|
46
|
+
next_time
|
|
47
|
+
};
|
|
48
|
+
}
|
|
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);
|
|
60
|
+
next_time = wait_time * 60;
|
|
61
|
+
return { next_time };
|
|
62
|
+
}
|
|
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_verified: boolean,
|
|
68
|
+
}) => Promise<void>,
|
|
69
|
+
errorHandler: () => Promise<void>)
|
|
70
|
+
{
|
|
71
|
+
if (!x_otp || !x_otp_time)
|
|
72
|
+
throw ErrorOperation.getHTTP(403, "The OTP not generated yet. Please request first.");
|
|
73
|
+
|
|
74
|
+
if (x_otp_time < TimeOperation.minutesAgo(OTPOperation.OTP_EXPIRE_TIME, new Date()))
|
|
75
|
+
ErrorOperation.throwHTTP(403, "The OTP code expired. Please request again.");
|
|
76
|
+
|
|
77
|
+
if (x_otp_ver_tried_count > OTPOperation.OTP_VER_MAX_TRY_COUNT)
|
|
78
|
+
ErrorOperation.throwHTTP(403, "The try limit attempt exceeded. Please request again.");
|
|
79
|
+
|
|
80
|
+
if (x_otp !== code)
|
|
81
|
+
{
|
|
82
|
+
await errorHandler();
|
|
83
|
+
ErrorOperation.throwHTTP(403, "Wrong code.");
|
|
84
|
+
}
|
|
85
|
+
await handler({
|
|
86
|
+
x_otp: null,
|
|
87
|
+
x_otp_time: null,
|
|
88
|
+
x_verified: true,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
72
91
|
}
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import * as express from 'express';
|
|
2
|
-
import { ConvertService, ObjectService } from 'namirasoft-core';
|
|
3
|
-
|
|
4
|
-
export class RequestHeaderService extends ConvertService
|
|
5
|
-
{
|
|
6
|
-
private req: express.Request;
|
|
7
|
-
private name: string;
|
|
8
|
-
constructor(req: express.Request, name: string, mandatory: boolean = false)
|
|
9
|
-
{
|
|
10
|
-
super(mandatory);
|
|
11
|
-
this.req = req;
|
|
12
|
-
this.name = name;
|
|
13
|
-
}
|
|
14
|
-
override getNullString()
|
|
15
|
-
{
|
|
16
|
-
if (this.req)
|
|
17
|
-
if (this.req.headers)
|
|
18
|
-
{
|
|
19
|
-
let item = this.req.headers[this.name];
|
|
20
|
-
return new ObjectService(item).getNullString();
|
|
21
|
-
}
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
protected override onMandatoryError(): void
|
|
25
|
-
{
|
|
26
|
-
throw new Error(`Request Header value was not provided: ${this.name}`);
|
|
27
|
-
}
|
|
1
|
+
import * as express from 'express';
|
|
2
|
+
import { ConvertService, ObjectService } from 'namirasoft-core';
|
|
3
|
+
|
|
4
|
+
export class RequestHeaderService extends ConvertService
|
|
5
|
+
{
|
|
6
|
+
private req: express.Request;
|
|
7
|
+
private name: string;
|
|
8
|
+
constructor(req: express.Request, name: string, mandatory: boolean = false)
|
|
9
|
+
{
|
|
10
|
+
super(mandatory);
|
|
11
|
+
this.req = req;
|
|
12
|
+
this.name = name;
|
|
13
|
+
}
|
|
14
|
+
override getNullString()
|
|
15
|
+
{
|
|
16
|
+
if (this.req)
|
|
17
|
+
if (this.req.headers)
|
|
18
|
+
{
|
|
19
|
+
let item = this.req.headers[this.name];
|
|
20
|
+
return new ObjectService(item).getNullString();
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
protected override onMandatoryError(): void
|
|
25
|
+
{
|
|
26
|
+
throw new Error(`Request Header value was not provided: ${this.name}`);
|
|
27
|
+
}
|
|
28
28
|
}
|
package/src/SMTPService.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { BaseEmailService } from './BaseEmailService';
|
|
2
|
-
|
|
3
|
-
export class SMTPService extends BaseEmailService
|
|
4
|
-
{
|
|
5
|
-
host: string;
|
|
6
|
-
port: number;
|
|
7
|
-
password: string;
|
|
8
|
-
constructor(host: string, port: number, username: string, password: string)
|
|
9
|
-
{
|
|
10
|
-
super(username);
|
|
11
|
-
this.host = host;
|
|
12
|
-
this.port = port;
|
|
13
|
-
this.password = password;
|
|
14
|
-
}
|
|
15
|
-
protected override getTransform(): any
|
|
16
|
-
{
|
|
17
|
-
return {
|
|
18
|
-
host: this.host,
|
|
19
|
-
port: this.port,
|
|
20
|
-
secure: true,
|
|
21
|
-
auth: {
|
|
22
|
-
user: this.username,
|
|
23
|
-
pass: this.password
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
}
|
|
1
|
+
import { BaseEmailService } from './BaseEmailService';
|
|
2
|
+
|
|
3
|
+
export class SMTPService extends BaseEmailService
|
|
4
|
+
{
|
|
5
|
+
host: string;
|
|
6
|
+
port: number;
|
|
7
|
+
password: string;
|
|
8
|
+
constructor(host: string, port: number, username: string, password: string)
|
|
9
|
+
{
|
|
10
|
+
super(username);
|
|
11
|
+
this.host = host;
|
|
12
|
+
this.port = port;
|
|
13
|
+
this.password = password;
|
|
14
|
+
}
|
|
15
|
+
protected override getTransform(): any
|
|
16
|
+
{
|
|
17
|
+
return {
|
|
18
|
+
host: this.host,
|
|
19
|
+
port: this.port,
|
|
20
|
+
secure: true,
|
|
21
|
+
auth: {
|
|
22
|
+
user: this.username,
|
|
23
|
+
pass: this.password
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
27
|
}
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import * as express from 'express';
|
|
2
|
-
import { ErrorOperation, HashOperation } from "namirasoft-core";
|
|
3
|
-
import { RequestHeaderService } from './RequestHeaderService';
|
|
4
|
-
|
|
5
|
-
export class ServerToServerOperation
|
|
6
|
-
{
|
|
7
|
-
static isValid(sign_key: string, data: any, req: express.Request, sign_header: string): boolean
|
|
8
|
-
{
|
|
9
|
-
let signature = new RequestHeaderService(req, sign_header).getString();
|
|
10
|
-
return HashOperation.isValidSHA256Secret(sign_key, data, signature);
|
|
11
|
-
}
|
|
12
|
-
static check(sign_key: string, data: any, req: express.Request, sign_header: string): void
|
|
13
|
-
{
|
|
14
|
-
if (!sign_key)
|
|
15
|
-
ErrorOperation.throwHTTP(401, "Invlid signature - No sign key.");
|
|
16
|
-
if (!sign_header)
|
|
17
|
-
ErrorOperation.throwHTTP(401, "Invlid signature - No sign header name.");
|
|
18
|
-
let signature = new RequestHeaderService(req, sign_header).getString();
|
|
19
|
-
if (!signature)
|
|
20
|
-
ErrorOperation.throwHTTP(401, "Invlid signature - No signature.");
|
|
21
|
-
if (!this.isValid(sign_key, data, req, sign_header))
|
|
22
|
-
ErrorOperation.throwHTTP(401, "Invlid signature.");
|
|
23
|
-
}
|
|
1
|
+
import * as express from 'express';
|
|
2
|
+
import { ErrorOperation, HashOperation } from "namirasoft-core";
|
|
3
|
+
import { RequestHeaderService } from './RequestHeaderService';
|
|
4
|
+
|
|
5
|
+
export class ServerToServerOperation
|
|
6
|
+
{
|
|
7
|
+
static isValid(sign_key: string, data: any, req: express.Request, sign_header: string): boolean
|
|
8
|
+
{
|
|
9
|
+
let signature = new RequestHeaderService(req, sign_header).getString();
|
|
10
|
+
return HashOperation.isValidSHA256Secret(sign_key, data, signature);
|
|
11
|
+
}
|
|
12
|
+
static check(sign_key: string, data: any, req: express.Request, sign_header: string): void
|
|
13
|
+
{
|
|
14
|
+
if (!sign_key)
|
|
15
|
+
ErrorOperation.throwHTTP(401, "Invlid signature - No sign key.");
|
|
16
|
+
if (!sign_header)
|
|
17
|
+
ErrorOperation.throwHTTP(401, "Invlid signature - No sign header name.");
|
|
18
|
+
let signature = new RequestHeaderService(req, sign_header).getString();
|
|
19
|
+
if (!signature)
|
|
20
|
+
ErrorOperation.throwHTTP(401, "Invlid signature - No signature.");
|
|
21
|
+
if (!this.isValid(sign_key, data, req, sign_header))
|
|
22
|
+
ErrorOperation.throwHTTP(401, "Invlid signature.");
|
|
23
|
+
}
|
|
24
24
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
export * from "./AnomalyDetector";
|
|
2
|
-
export * from "./BaseApplication";
|
|
3
|
-
export * from "./BaseApplicationLink";
|
|
4
|
-
export * from "./BaseController";
|
|
5
|
-
export * from "./BaseCron";
|
|
6
|
-
export * from "./BaseDatabase";
|
|
7
|
-
export * from "./BaseEmailService";
|
|
8
|
-
export * from "./BaseTable";
|
|
9
|
-
export * from "./CommandOperation";
|
|
10
|
-
export * from "./EmptyDatabase";
|
|
11
|
-
export * from "./GmailService";
|
|
12
|
-
export * from "./IPOperation";
|
|
13
|
-
export * from "./Meta";
|
|
14
|
-
export * from "./OTPOperation";
|
|
15
|
-
export * from "./RequestHeaderService";
|
|
16
|
-
export * from "./ServerToServerOperation";
|
|
1
|
+
export * from "./AnomalyDetector";
|
|
2
|
+
export * from "./BaseApplication";
|
|
3
|
+
export * from "./BaseApplicationLink";
|
|
4
|
+
export * from "./BaseController";
|
|
5
|
+
export * from "./BaseCron";
|
|
6
|
+
export * from "./BaseDatabase";
|
|
7
|
+
export * from "./BaseEmailService";
|
|
8
|
+
export * from "./BaseTable";
|
|
9
|
+
export * from "./CommandOperation";
|
|
10
|
+
export * from "./EmptyDatabase";
|
|
11
|
+
export * from "./GmailService";
|
|
12
|
+
export * from "./IPOperation";
|
|
13
|
+
export * from "./Meta";
|
|
14
|
+
export * from "./OTPOperation";
|
|
15
|
+
export * from "./RequestHeaderService";
|
|
16
|
+
export * from "./ServerToServerOperation";
|
|
17
17
|
export * from "./SMTPService";
|