namirasoft-node 1.1.4 → 1.1.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.
@@ -1,39 +1,39 @@
1
- import * as express from 'express';
2
- import { getClientIp } from '@supercharge/request-ip';
3
- import { RequestHeaderService } from './RequestHeaderService';
4
- import { ErrorOperation } from 'namirasoft-core';
5
-
6
- export class IPOperation
7
- {
8
- static ERROR_MESSAGE_IP_IS_NOT_WHITELIST = `Ip does not match the whitelisted IP address: {0}`;
9
- static getIP(req: express.Request): string
10
- {
11
- let ip = new RequestHeaderService(req, 'cf-connecting-ip').getString();
12
- if (!ip)
13
- ip = new RequestHeaderService(req, 'x-forwarded-for').getString();
14
- if (!ip)
15
- ip = getClientIp(req) ?? "";
16
- ip = ip.split(',')[0];
17
- return ip;
18
- }
19
- static isWhitelist(req: express.Request, whitelist: string[])
20
- {
21
- let ip = this.getIP(req);
22
- if (!whitelist)
23
- return true;
24
- if (whitelist.length === 0)
25
- return true;
26
- if (whitelist.includes(ip))
27
- return true;
28
- for (let item of whitelist)
29
- if (ip.substring(0, item.length) === item)
30
- return true;
31
- return false;
32
- }
33
- static checkWhitelist(req: express.Request, whitelist: string[])
34
- {
35
- let valid = this.isWhitelist(req, whitelist);
36
- if (!valid)
37
- ErrorOperation.throwHTTP(403, this.ERROR_MESSAGE_IP_IS_NOT_WHITELIST, this.getIP(req));
38
- }
1
+ import * as express from 'express';
2
+ import { getClientIp } from '@supercharge/request-ip';
3
+ import { RequestHeaderService } from './RequestHeaderService';
4
+ import { ErrorOperation } from 'namirasoft-core';
5
+
6
+ export class IPOperation
7
+ {
8
+ static ERROR_MESSAGE_IP_IS_NOT_WHITELIST = `Ip does not match the whitelisted IP address: {0}`;
9
+ static getIP(req: express.Request): string
10
+ {
11
+ let ip = new RequestHeaderService(req, 'cf-connecting-ip').getString();
12
+ if (!ip)
13
+ ip = new RequestHeaderService(req, 'x-forwarded-for').getString();
14
+ if (!ip)
15
+ ip = getClientIp(req) ?? "";
16
+ ip = ip.split(',')[0];
17
+ return ip;
18
+ }
19
+ static isWhitelist(req: express.Request, whitelist: string[])
20
+ {
21
+ let ip = this.getIP(req);
22
+ if (!whitelist)
23
+ return true;
24
+ if (whitelist.length === 0)
25
+ return true;
26
+ if (whitelist.includes(ip))
27
+ return true;
28
+ for (let item of whitelist)
29
+ if (ip.substring(0, item.length) === item)
30
+ return true;
31
+ return false;
32
+ }
33
+ static checkWhitelist(req: express.Request, whitelist: string[])
34
+ {
35
+ let valid = this.isWhitelist(req, whitelist);
36
+ if (!valid)
37
+ ErrorOperation.throwHTTP(403, this.ERROR_MESSAGE_IP_IS_NOT_WHITELIST, this.getIP(req));
38
+ }
39
39
  }
package/src/Meta.ts CHANGED
@@ -1,35 +1,35 @@
1
- import * as express from 'express';
2
- import { IncomingHttpHeaders } from "http";
3
- import { IPOperation } from './IPOperation';
4
-
5
- export class Meta
6
- {
7
- ip: string;
8
- method: string;
9
- url: string;
10
- headers: IncomingHttpHeaders;
11
- body: any;
12
- start_time: Date | null = null;
13
- end_time: Date | null = null;
14
- duration: number | null = null;
15
- code: number = 200;
16
- message: string = "Success";
17
- error: Error | null = null;
18
- constructor(req: express.Request)
19
- {
20
- this.ip = IPOperation.getIP(req);
21
- this.method = req.method;
22
- this.url = req.originalUrl;
23
- this.headers = req.headers;
24
- this.body = req.body;
25
- }
26
- onStart()
27
- {
28
- this.start_time = new Date();
29
- }
30
- onFinish()
31
- {
32
- this.end_time = new Date();
33
- this.duration = (this.end_time?.getTime() ?? 0) - (this.start_time?.getTime() ?? 0);
34
- }
1
+ import * as express from 'express';
2
+ import { IncomingHttpHeaders } from "http";
3
+ import { IPOperation } from './IPOperation';
4
+
5
+ export class Meta
6
+ {
7
+ ip: string;
8
+ method: string;
9
+ url: string;
10
+ headers: IncomingHttpHeaders;
11
+ body: any;
12
+ start_time: Date | null = null;
13
+ end_time: Date | null = null;
14
+ duration: number | null = null;
15
+ code: number = 200;
16
+ message: string = "Success";
17
+ error: Error | null = null;
18
+ constructor(req: express.Request)
19
+ {
20
+ this.ip = IPOperation.getIP(req);
21
+ this.method = req.method;
22
+ this.url = req.originalUrl;
23
+ this.headers = req.headers;
24
+ this.body = req.body;
25
+ }
26
+ onStart()
27
+ {
28
+ this.start_time = new Date();
29
+ }
30
+ onFinish()
31
+ {
32
+ this.end_time = new Date();
33
+ this.duration = (this.end_time?.getTime() ?? 0) - (this.start_time?.getTime() ?? 0);
34
+ }
35
35
  }
@@ -1,65 +1,65 @@
1
- import { ErrorOperation, TimeOperation } from "namirasoft-core";
2
-
3
- export class OTPOperation
4
- {
5
- static generate(length: number = 6, digit: number = 3)
6
- {
7
- if (!length)
8
- length = 6;
9
- if (!digit)
10
- digit = 3;
11
- let dig = [];
12
- for (let i = 0; i < digit; i++)
13
- dig[i] = parseInt((Math.random() * 9 + 1) + "");
14
- let ans = '';
15
- for (let i = 0; i < length; i++)
16
- ans = ans + '' + dig[parseInt((Math.random() * dig.length) + "")];
17
- return ans;
18
- }
19
- static getWaitTime(min_wait_time: number, max_wait_time: number, min_attempt: number, user_attemptted: number, increase_power_base: number = 2): number
20
- {
21
- let wait_time = min_wait_time;
22
- let extra_attempt = user_attemptted - min_attempt;
23
- if (extra_attempt > 0)
24
- wait_time = Math.min(Math.pow(increase_power_base, extra_attempt) * 60, max_wait_time);
25
- return parseInt(wait_time + "");
26
- }
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
- {
29
- // check wait time
30
- let wait_time = this.getWaitTime(min_wait_time, max_wait_time, min_try_count, user_tried_count, increase_power_base);
31
- let wait_date = TimeOperation.minutesAgo(wait_time, new Date());
32
- let next_time = TimeOperation.diffInSecond(user_tried_time, wait_date, false);
33
- if (next_time > 0)
34
- {
35
- return {
36
- error: 'Too many request, please try again in ' + next_time + ' seconds.',
37
- next_time
38
- };
39
- }
40
- await handler();
41
- wait_time = this.getWaitTime(min_wait_time, max_wait_time, min_try_count, user_tried_count + 1, increase_power_base);
42
- next_time = wait_time * 60;
43
- return { next_time };
44
- }
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
- {
49
- if (!entered_otp)
50
- ErrorOperation.throwHTTP(403, "The OTP not generated yet.");
51
-
52
- if (valid_otp_time < TimeOperation.minutesAgo(otp_expire_time, new Date()))
53
- ErrorOperation.throwHTTP(403, "The OTP code expired. Please request again.");
54
-
55
- if (user_tried_count > max_try_count)
56
- ErrorOperation.throwHTTP(403, "The try limit attempt exceeded. Please request again.");
57
-
58
- if (valid_otp !== entered_otp)
59
- {
60
- await errorHandler();
61
- ErrorOperation.throwHTTP(403, "Wrong code.");
62
- }
63
- await handler();
64
- }
1
+ import { ErrorOperation, TimeOperation } from "namirasoft-core";
2
+
3
+ export class OTPOperation
4
+ {
5
+ static generate(length: number = 6, digit: number = 3)
6
+ {
7
+ if (!length)
8
+ length = 6;
9
+ if (!digit)
10
+ digit = 3;
11
+ let dig = [];
12
+ for (let i = 0; i < digit; i++)
13
+ dig[i] = parseInt((Math.random() * 9 + 1) + "");
14
+ let ans = '';
15
+ for (let i = 0; i < length; i++)
16
+ ans = ans + '' + dig[parseInt((Math.random() * dig.length) + "")];
17
+ return ans;
18
+ }
19
+ static getWaitTime(min_wait_time: number, max_wait_time: number, min_attempt: number, user_attemptted: number, increase_power_base: number = 2): number
20
+ {
21
+ let wait_time = min_wait_time;
22
+ let extra_attempt = user_attemptted - min_attempt;
23
+ if (extra_attempt > 0)
24
+ wait_time = Math.min(Math.pow(increase_power_base, extra_attempt) * 60, max_wait_time);
25
+ return parseInt(wait_time + "");
26
+ }
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
+ {
29
+ // check wait time
30
+ let wait_time = this.getWaitTime(min_wait_time, max_wait_time, min_try_count, user_tried_count, increase_power_base);
31
+ let wait_date = TimeOperation.minutesAgo(wait_time, new Date());
32
+ let next_time = TimeOperation.diffInSecond(user_tried_time, wait_date, false);
33
+ if (next_time > 0)
34
+ {
35
+ return {
36
+ error: 'Too many request, please try again in ' + next_time + ' seconds.',
37
+ next_time
38
+ };
39
+ }
40
+ await handler();
41
+ wait_time = this.getWaitTime(min_wait_time, max_wait_time, min_try_count, user_tried_count + 1, increase_power_base);
42
+ next_time = wait_time * 60;
43
+ return { next_time };
44
+ }
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
+ {
49
+ if (!entered_otp)
50
+ ErrorOperation.throwHTTP(403, "The OTP not generated yet.");
51
+
52
+ if (valid_otp_time < TimeOperation.minutesAgo(otp_expire_time, new Date()))
53
+ ErrorOperation.throwHTTP(403, "The OTP code expired. Please request again.");
54
+
55
+ if (user_tried_count > max_try_count)
56
+ ErrorOperation.throwHTTP(403, "The try limit attempt exceeded. Please request again.");
57
+
58
+ if (valid_otp !== entered_otp)
59
+ {
60
+ await errorHandler();
61
+ ErrorOperation.throwHTTP(403, "Wrong code.");
62
+ }
63
+ await handler();
64
+ }
65
65
  }
@@ -1,23 +1,23 @@
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
- let item = this.req.headers[this.name];
17
- return new ObjectService(item).getNullString();
18
- }
19
- protected override onMandatoryError(): void
20
- {
21
- throw new Error(`Request Header value was not provided: ${this.name}`);
22
- }
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
+ let item = this.req.headers[this.name];
17
+ return new ObjectService(item).getNullString();
18
+ }
19
+ protected override onMandatoryError(): void
20
+ {
21
+ throw new Error(`Request Header value was not provided: ${this.name}`);
22
+ }
23
23
  }
@@ -1,24 +1,24 @@
1
- import * as express from 'express';
2
- import { ErrorOperation, SignOperation } 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 SignOperation.isValid(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, SignOperation } 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 SignOperation.isValid(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 "./BaseDatabase";
6
- export * from "./BaseMySqlDatabase";
7
- export * from "./BaseSequelizeDatabase";
8
- export * from "./BaseSequelizeModel";
9
- export * from "./BaseSequelizeTable";
10
- export * from "./BaseTable";
11
- export * from "./EmailService";
12
- export * from "./EnvService";
13
- export * from "./IPOperation";
14
- export * from "./Meta";
15
- export * from "./OTPOperation";
16
- export * from "./RequestHeaderService";
1
+ export * from "./AnomalyDetector";
2
+ export * from "./BaseApplication";
3
+ export * from "./BaseApplicationLink";
4
+ export * from "./BaseController";
5
+ export * from "./BaseDatabase";
6
+ export * from "./BaseMySqlDatabase";
7
+ export * from "./BaseSequelizeDatabase";
8
+ export * from "./BaseSequelizeModel";
9
+ export * from "./BaseSequelizeTable";
10
+ export * from "./BaseTable";
11
+ export * from "./EmailService";
12
+ export * from "./EnvService";
13
+ export * from "./IPOperation";
14
+ export * from "./Meta";
15
+ export * from "./OTPOperation";
16
+ export * from "./RequestHeaderService";
17
17
  export * from "./ServerToServerOperation";
package/tsconfig.json CHANGED
@@ -1,30 +1,30 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES6",
4
- "module": "CommonJS",
5
- "rootDir": "./src",
6
- "outDir": "./dist",
7
- "lib": [
8
- "es6",
9
- "dom"
10
- ],
11
- "allowJs": false,
12
- "checkJs": false,
13
- "strict": true,
14
- "esModuleInterop": true,
15
- "sourceMap": true,
16
- "resolveJsonModule": true,
17
- "declaration": true,
18
- "noUnusedLocals": true,
19
- "noUnusedParameters": true,
20
- "noImplicitAny": true,
21
- "noImplicitOverride": true,
22
- "noImplicitReturns": true,
23
- "noImplicitThis": true,
24
- "skipLibCheck": false,
25
- "allowSyntheticDefaultImports": true,
26
- "forceConsistentCasingInFileNames": true,
27
- "noFallthroughCasesInSwitch": true,
28
- "isolatedModules": false,
29
- }
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES6",
4
+ "module": "CommonJS",
5
+ "rootDir": "./src",
6
+ "outDir": "./dist",
7
+ "lib": [
8
+ "es6",
9
+ "dom"
10
+ ],
11
+ "allowJs": false,
12
+ "checkJs": false,
13
+ "strict": true,
14
+ "esModuleInterop": true,
15
+ "sourceMap": true,
16
+ "resolveJsonModule": true,
17
+ "declaration": true,
18
+ "noUnusedLocals": true,
19
+ "noUnusedParameters": true,
20
+ "noImplicitAny": true,
21
+ "noImplicitOverride": true,
22
+ "noImplicitReturns": true,
23
+ "noImplicitThis": true,
24
+ "skipLibCheck": false,
25
+ "allowSyntheticDefaultImports": true,
26
+ "forceConsistentCasingInFileNames": true,
27
+ "noFallthroughCasesInSwitch": true,
28
+ "isolatedModules": false,
29
+ }
30
30
  }