mvc-common-toolkit 1.43.9 → 1.43.11

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.
Files changed (201) hide show
  1. package/dist/src/constants.d.ts +42 -0
  2. package/dist/src/constants.js +50 -0
  3. package/dist/src/constants.js.map +1 -0
  4. package/dist/src/gateways/alibaba-cloud-gateway.d.ts +30 -0
  5. package/dist/src/gateways/alibaba-cloud-gateway.js +120 -0
  6. package/dist/src/gateways/alibaba-cloud-gateway.js.map +1 -0
  7. package/dist/src/gateways/http-audit-gateway.d.ts +20 -0
  8. package/dist/src/gateways/http-audit-gateway.js +76 -0
  9. package/dist/src/gateways/http-audit-gateway.js.map +1 -0
  10. package/dist/src/gateways/index.d.ts +5 -0
  11. package/dist/src/gateways/index.js +22 -0
  12. package/dist/src/gateways/index.js.map +1 -0
  13. package/dist/src/gateways/internal-auth-gateway.d.ts +8 -0
  14. package/dist/src/gateways/internal-auth-gateway.js +40 -0
  15. package/dist/src/gateways/internal-auth-gateway.js.map +1 -0
  16. package/dist/src/gateways/stdout-audit-gateway.d.ts +7 -0
  17. package/dist/src/gateways/stdout-audit-gateway.js +25 -0
  18. package/dist/src/gateways/stdout-audit-gateway.js.map +1 -0
  19. package/dist/src/gateways/webhook-audit-gateway.d.ts +14 -0
  20. package/dist/src/gateways/webhook-audit-gateway.js +27 -0
  21. package/dist/src/gateways/webhook-audit-gateway.js.map +1 -0
  22. package/dist/src/interfaces.d.ts +218 -0
  23. package/dist/src/interfaces.js +3 -0
  24. package/dist/src/interfaces.js.map +1 -0
  25. package/dist/src/models/audit-log.d.ts +77 -0
  26. package/dist/src/models/audit-log.js +92 -0
  27. package/dist/src/models/audit-log.js.map +1 -0
  28. package/dist/src/models/index.d.ts +1 -0
  29. package/dist/src/models/index.js +18 -0
  30. package/dist/src/models/index.js.map +1 -0
  31. package/dist/src/pkg/array-helper.d.ts +1 -0
  32. package/dist/src/pkg/array-helper.js +12 -0
  33. package/dist/src/pkg/array-helper.js.map +1 -0
  34. package/dist/src/pkg/bcrypt-helper.d.ts +2 -0
  35. package/dist/src/pkg/bcrypt-helper.js +36 -0
  36. package/dist/src/pkg/bcrypt-helper.js.map +1 -0
  37. package/dist/src/pkg/crypto-helper.d.ts +2 -0
  38. package/dist/src/pkg/crypto-helper.js +16 -0
  39. package/dist/src/pkg/crypto-helper.js.map +1 -0
  40. package/dist/src/pkg/encryption-helper.d.ts +18 -0
  41. package/dist/src/pkg/encryption-helper.js +89 -0
  42. package/dist/src/pkg/encryption-helper.js.map +1 -0
  43. package/dist/src/pkg/encryption-helper.spec.d.ts +1 -0
  44. package/dist/src/pkg/encryption-helper.spec.js +238 -0
  45. package/dist/src/pkg/encryption-helper.spec.js.map +1 -0
  46. package/dist/src/pkg/filter-helper.d.ts +2 -0
  47. package/dist/src/pkg/filter-helper.js +102 -0
  48. package/dist/src/pkg/filter-helper.js.map +1 -0
  49. package/dist/src/pkg/filter-helper.spec.d.ts +1 -0
  50. package/dist/src/pkg/filter-helper.spec.js +94 -0
  51. package/dist/src/pkg/filter-helper.spec.js.map +1 -0
  52. package/dist/src/pkg/geoip-helper.d.ts +2 -0
  53. package/dist/src/pkg/geoip-helper.js +32 -0
  54. package/dist/src/pkg/geoip-helper.js.map +1 -0
  55. package/dist/src/pkg/hash-helper.d.ts +1 -0
  56. package/dist/src/pkg/hash-helper.js +37 -0
  57. package/dist/src/pkg/hash-helper.js.map +1 -0
  58. package/dist/src/pkg/http-request-utils.d.ts +4 -0
  59. package/dist/src/pkg/http-request-utils.js +55 -0
  60. package/dist/src/pkg/http-request-utils.js.map +1 -0
  61. package/dist/src/pkg/index.d.ts +18 -0
  62. package/dist/src/pkg/index.js +45 -0
  63. package/dist/src/pkg/index.js.map +1 -0
  64. package/dist/src/pkg/key-helper.d.ts +2 -0
  65. package/dist/src/pkg/key-helper.js +20 -0
  66. package/dist/src/pkg/key-helper.js.map +1 -0
  67. package/dist/src/pkg/logger.d.ts +9 -0
  68. package/dist/src/pkg/logger.js +23 -0
  69. package/dist/src/pkg/logger.js.map +1 -0
  70. package/dist/src/pkg/object-helper.d.ts +2 -0
  71. package/dist/src/pkg/object-helper.js +37 -0
  72. package/dist/src/pkg/object-helper.js.map +1 -0
  73. package/dist/src/pkg/paginated-cache-registry.d.ts +8 -0
  74. package/dist/src/pkg/paginated-cache-registry.js +23 -0
  75. package/dist/src/pkg/paginated-cache-registry.js.map +1 -0
  76. package/dist/src/pkg/query-helper.d.ts +3 -0
  77. package/dist/src/pkg/query-helper.js +60 -0
  78. package/dist/src/pkg/query-helper.js.map +1 -0
  79. package/dist/src/pkg/referral-tree-utils.d.ts +33 -0
  80. package/dist/src/pkg/referral-tree-utils.js +71 -0
  81. package/dist/src/pkg/referral-tree-utils.js.map +1 -0
  82. package/dist/src/pkg/scripts/index.d.ts +1 -0
  83. package/dist/src/pkg/scripts/index.js +28 -0
  84. package/dist/src/pkg/scripts/index.js.map +1 -0
  85. package/dist/src/pkg/scripts/lua.d.ts +10 -0
  86. package/dist/src/pkg/scripts/lua.js +109 -0
  87. package/dist/src/pkg/scripts/lua.js.map +1 -0
  88. package/dist/src/pkg/sort-helper.d.ts +3 -0
  89. package/dist/src/pkg/sort-helper.js +18 -0
  90. package/dist/src/pkg/sort-helper.js.map +1 -0
  91. package/dist/src/pkg/string-utils.d.ts +10 -0
  92. package/dist/src/pkg/string-utils.js +79 -0
  93. package/dist/src/pkg/string-utils.js.map +1 -0
  94. package/dist/src/pkg/task-helper.d.ts +2 -0
  95. package/dist/src/pkg/task-helper.js +30 -0
  96. package/dist/src/pkg/task-helper.js.map +1 -0
  97. package/dist/src/pkg/workflow/delayed-task-registry.d.ts +10 -0
  98. package/dist/src/pkg/workflow/delayed-task-registry.js +67 -0
  99. package/dist/src/pkg/workflow/delayed-task-registry.js.map +1 -0
  100. package/dist/src/pkg/workflow/delayed-task.d.ts +18 -0
  101. package/dist/src/pkg/workflow/delayed-task.js +95 -0
  102. package/dist/src/pkg/workflow/delayed-task.js.map +1 -0
  103. package/dist/src/pkg/workflow/index.d.ts +5 -0
  104. package/dist/src/pkg/workflow/index.js +22 -0
  105. package/dist/src/pkg/workflow/index.js.map +1 -0
  106. package/dist/src/pkg/workflow/processing-milestone.d.ts +18 -0
  107. package/dist/src/pkg/workflow/processing-milestone.js +39 -0
  108. package/dist/src/pkg/workflow/processing-milestone.js.map +1 -0
  109. package/dist/src/pkg/workflow/retry-task.d.ts +24 -0
  110. package/dist/src/pkg/workflow/retry-task.js +89 -0
  111. package/dist/src/pkg/workflow/retry-task.js.map +1 -0
  112. package/dist/src/pkg/workflow/retry-task.spec.d.ts +1 -0
  113. package/dist/src/pkg/workflow/retry-task.spec.js +145 -0
  114. package/dist/src/pkg/workflow/retry-task.spec.js.map +1 -0
  115. package/dist/src/pkg/workflow/sync-taskqueue.d.ts +32 -0
  116. package/dist/src/pkg/workflow/sync-taskqueue.js +108 -0
  117. package/dist/src/pkg/workflow/sync-taskqueue.js.map +1 -0
  118. package/dist/src/pkg/worksheet.utils.d.ts +27 -0
  119. package/dist/src/pkg/worksheet.utils.js +116 -0
  120. package/dist/src/pkg/worksheet.utils.js.map +1 -0
  121. package/dist/src/services/audit-service.d.ts +7 -0
  122. package/dist/src/services/audit-service.js +32 -0
  123. package/dist/src/services/audit-service.js.map +1 -0
  124. package/dist/src/services/excel.service.d.ts +25 -0
  125. package/dist/src/services/excel.service.js +95 -0
  126. package/dist/src/services/excel.service.js.map +1 -0
  127. package/dist/src/services/http-service.d.ts +7 -0
  128. package/dist/src/services/http-service.js +67 -0
  129. package/dist/src/services/http-service.js.map +1 -0
  130. package/dist/src/services/index.d.ts +8 -0
  131. package/dist/src/services/index.js +25 -0
  132. package/dist/src/services/index.js.map +1 -0
  133. package/dist/src/services/kafka-service.d.ts +15 -0
  134. package/dist/src/services/kafka-service.js +68 -0
  135. package/dist/src/services/kafka-service.js.map +1 -0
  136. package/dist/src/services/mailer-service.d.ts +15 -0
  137. package/dist/src/services/mailer-service.js +44 -0
  138. package/dist/src/services/mailer-service.js.map +1 -0
  139. package/dist/src/services/paginated-cache.d.ts +16 -0
  140. package/dist/src/services/paginated-cache.js +115 -0
  141. package/dist/src/services/paginated-cache.js.map +1 -0
  142. package/dist/src/services/paginated-cache.spec.d.ts +1 -0
  143. package/dist/src/services/paginated-cache.spec.js +284 -0
  144. package/dist/src/services/paginated-cache.spec.js.map +1 -0
  145. package/dist/src/services/redis-service.d.ts +33 -0
  146. package/dist/src/services/redis-service.js +230 -0
  147. package/dist/src/services/redis-service.js.map +1 -0
  148. package/dist/src/services/security-service.d.ts +11 -0
  149. package/dist/src/services/security-service.js +68 -0
  150. package/dist/src/services/security-service.js.map +1 -0
  151. package/package.json +1 -1
  152. package/src/constants.ts +47 -0
  153. package/src/gateways/alibaba-cloud-gateway.ts +127 -0
  154. package/src/gateways/http-audit-gateway.ts +104 -0
  155. package/src/gateways/index.ts +5 -0
  156. package/src/gateways/internal-auth-gateway.ts +42 -0
  157. package/src/gateways/stdout-audit-gateway.ts +23 -0
  158. package/src/gateways/webhook-audit-gateway.ts +33 -0
  159. package/src/interfaces.ts +304 -0
  160. package/src/models/audit-log.ts +126 -0
  161. package/src/models/index.ts +1 -0
  162. package/src/pkg/array-helper.ts +7 -0
  163. package/src/pkg/bcrypt-helper.ts +9 -0
  164. package/src/pkg/crypto-helper.ts +18 -0
  165. package/src/pkg/encryption-helper.spec.ts +423 -0
  166. package/src/pkg/encryption-helper.ts +155 -0
  167. package/src/pkg/filter-helper.spec.ts +105 -0
  168. package/src/pkg/filter-helper.ts +139 -0
  169. package/src/pkg/geoip-helper.ts +5 -0
  170. package/src/pkg/hash-helper.ts +12 -0
  171. package/src/pkg/http-request-utils.ts +75 -0
  172. package/src/pkg/index.ts +18 -0
  173. package/src/pkg/key-helper.ts +20 -0
  174. package/src/pkg/logger.ts +23 -0
  175. package/src/pkg/object-helper.ts +42 -0
  176. package/src/pkg/paginated-cache-registry.ts +25 -0
  177. package/src/pkg/query-helper.ts +79 -0
  178. package/src/pkg/referral-tree-utils.ts +165 -0
  179. package/src/pkg/scripts/index.ts +1 -0
  180. package/src/pkg/scripts/lua.ts +112 -0
  181. package/src/pkg/sort-helper.ts +19 -0
  182. package/src/pkg/string-utils.ts +104 -0
  183. package/src/pkg/task-helper.ts +25 -0
  184. package/src/pkg/workflow/delayed-task-registry.ts +54 -0
  185. package/src/pkg/workflow/delayed-task.ts +106 -0
  186. package/src/pkg/workflow/index.ts +5 -0
  187. package/src/pkg/workflow/processing-milestone.ts +54 -0
  188. package/src/pkg/workflow/retry-task.spec.ts +194 -0
  189. package/src/pkg/workflow/retry-task.ts +119 -0
  190. package/src/pkg/workflow/sync-taskqueue.ts +118 -0
  191. package/src/pkg/worksheet.utils.ts +178 -0
  192. package/src/services/audit-service.ts +22 -0
  193. package/src/services/excel.service.ts +103 -0
  194. package/src/services/http-service.ts +71 -0
  195. package/src/services/index.ts +8 -0
  196. package/src/services/kafka-service.ts +81 -0
  197. package/src/services/mailer-service.ts +43 -0
  198. package/src/services/paginated-cache.spec.ts +519 -0
  199. package/src/services/paginated-cache.ts +122 -0
  200. package/src/services/redis-service.ts +238 -0
  201. package/src/services/security-service.ts +80 -0
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.SecurityService = void 0;
13
+ class SecurityService {
14
+ constructor(cacheService, userService) {
15
+ this.cacheService = cacheService;
16
+ this.userService = userService;
17
+ }
18
+ static genLockUserCacheKey(userId) {
19
+ return `user:${userId}:lock`;
20
+ }
21
+ addFailureCount(userId, cacheKey) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const finalCacheKey = cacheKey || SecurityService.genLockUserCacheKey(userId);
24
+ yield this.cacheService.incrBy(finalCacheKey);
25
+ return this.cacheService.get(finalCacheKey);
26
+ });
27
+ }
28
+ clearFailureCount(userId, cacheKey) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ const finalCacheKey = cacheKey || SecurityService.genLockUserCacheKey(userId);
31
+ yield this.cacheService.del(finalCacheKey);
32
+ });
33
+ }
34
+ shouldLockUser(userId, cacheKey) {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ try {
37
+ const incrementedAttemptsCount = yield this.addFailureCount(userId, cacheKey);
38
+ if (incrementedAttemptsCount >= SecurityService.LIMIT_TO_LOCK_USER) {
39
+ yield this.clearFailureCount(userId);
40
+ return {
41
+ success: true,
42
+ data: {
43
+ isLocked: true,
44
+ attemptsLeft: 0,
45
+ },
46
+ };
47
+ }
48
+ return {
49
+ success: true,
50
+ data: {
51
+ isLocked: false,
52
+ attemptsLeft: Math.max(0, SecurityService.LIMIT_TO_LOCK_USER - incrementedAttemptsCount),
53
+ },
54
+ };
55
+ }
56
+ catch (error) {
57
+ return {
58
+ success: false,
59
+ message: error.message,
60
+ metadata: error.stack,
61
+ };
62
+ }
63
+ });
64
+ }
65
+ }
66
+ SecurityService.LIMIT_TO_LOCK_USER = 5;
67
+ exports.SecurityService = SecurityService;
68
+ //# sourceMappingURL=security-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"security-service.js","sourceRoot":"","sources":["../../../src/services/security-service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,MAAa,eAAe;IAC1B,YACY,YAA0B,EAC1B,WAAwB;QADxB,iBAAY,GAAZ,YAAY,CAAc;QAC1B,gBAAW,GAAX,WAAW,CAAa;IACjC,CAAC;IAIG,MAAM,CAAC,mBAAmB,CAAC,MAAc;QAC9C,OAAO,QAAQ,MAAM,OAAO,CAAC;IAC/B,CAAC;IAEY,eAAe,CAC1B,MAAc,EACd,QAAiB;;YAEjB,MAAM,aAAa,GACjB,QAAQ,IAAI,eAAe,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC;YAE1D,MAAM,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;YAE9C,OAAO,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAC9C,CAAC;KAAA;IAEY,iBAAiB,CAAC,MAAc,EAAE,QAAiB;;YAC9D,MAAM,aAAa,GACjB,QAAQ,IAAI,eAAe,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC;YAE1D,MAAM,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAC7C,CAAC;KAAA;IAEK,cAAc,CAClB,MAAc,EACd,QAAiB;;YAEjB,IAAI;gBACF,MAAM,wBAAwB,GAAG,MAAM,IAAI,CAAC,eAAe,CACzD,MAAM,EACN,QAAQ,CACT,CAAC;gBAGF,IAAI,wBAAwB,IAAI,eAAe,CAAC,kBAAkB,EAAE;oBAClE,MAAM,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;oBAErC,OAAO;wBACL,OAAO,EAAE,IAAI;wBACb,IAAI,EAAE;4BACJ,QAAQ,EAAE,IAAI;4BACd,YAAY,EAAE,CAAC;yBAChB;qBACF,CAAC;iBACH;gBAED,OAAO;oBACL,OAAO,EAAE,IAAI;oBACb,IAAI,EAAE;wBACJ,QAAQ,EAAE,KAAK;wBACf,YAAY,EAAE,IAAI,CAAC,GAAG,CACpB,CAAC,EACD,eAAe,CAAC,kBAAkB,GAAG,wBAAwB,CAC9D;qBACF;iBACF,CAAC;aACH;YAAC,OAAO,KAAU,EAAE;gBACnB,OAAO;oBACL,OAAO,EAAE,KAAK;oBACd,OAAO,EAAE,KAAK,CAAC,OAAO;oBACtB,QAAQ,EAAE,KAAK,CAAC,KAAK;iBACtB,CAAC;aACH;QACH,CAAC;KAAA;;AAjEa,kCAAkB,GAAG,CAAC,CAAC;AAN1B,0CAAe"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mvc-common-toolkit",
3
- "version": "1.43.9",
3
+ "version": "1.43.11",
4
4
  "description": "This package contains common toolkits like query string parser, filter parser,... and other reusable patterns for any web projects",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -0,0 +1,47 @@
1
+ export enum LOG_LEVEL {
2
+ INFO = "info",
3
+ ERROR = "error",
4
+ DEBUG = "debug",
5
+ FATAL = "fatal",
6
+ }
7
+
8
+ export type HttpMethod = "get" | "post" | "put" | "patch" | "delete";
9
+
10
+ export enum APP_ERROR {
11
+ HTTP_REQ_TIMEOUT = "ECONNABORTED",
12
+ }
13
+
14
+ export enum SET_CACHE_POLICY {
15
+ KEEP_TTL,
16
+ WITH_TTL,
17
+ IF_EXISTS,
18
+ IF_NOT_EXISTS,
19
+ }
20
+
21
+ export enum APP_ENV {
22
+ DEVELOPMENT = "development",
23
+ STAGING = "staging",
24
+ PRODUCTION = "production",
25
+ }
26
+
27
+ export enum FILTER_OPERATOR {
28
+ NOT = "not",
29
+ EQUAL = "eq",
30
+ LIKE = "like",
31
+ INS_LIKE = "ins_like", // case-insensitive
32
+ STARTS_WITH = "starts_with",
33
+ ENDS_WITH = "ends_with",
34
+ INS_STARTS_WITH = "ins_starts_with", // case-insensitive
35
+ INS_ENDS_WITH = "ins_ends_with", // case-insensitive
36
+ LESS_THAN = "lt",
37
+ LESS_THAN_OR_EQUAL = "lte",
38
+ GREATER_THAN = "gt",
39
+ GREATER_THAN_OR_EQUAL = "gte",
40
+ IN = "in",
41
+ NOT_IN = "nin",
42
+ OR = "or",
43
+ NOR = "nor",
44
+ AND = "and",
45
+ IS = "is",
46
+ NOT_EQUAL = "ne",
47
+ }
@@ -0,0 +1,127 @@
1
+ import OSS from "ali-oss";
2
+ import axios, { HttpStatusCode } from "axios";
3
+ import Core from "@alicloud/pop-core";
4
+
5
+ import {
6
+ CloudStorageClient,
7
+ PutObjectOption,
8
+ STSResponse,
9
+ } from "../interfaces";
10
+
11
+ export interface AlibabaCloudGatewayConfig {
12
+ accessKey: string;
13
+ secret: string;
14
+ apiVersion?: string;
15
+ endpoint: string;
16
+ timeout?: number;
17
+ oss: {
18
+ bucketName: string;
19
+ region: string;
20
+ };
21
+ sts: {
22
+ roleArn: string;
23
+ };
24
+ }
25
+
26
+ export class AlibabaCloudGateway implements CloudStorageClient {
27
+ protected coreClient: Core;
28
+ protected ossClient: OSS;
29
+
30
+ constructor(protected config: AlibabaCloudGatewayConfig) {
31
+ this.coreClient = new Core({
32
+ accessKeyId: config.accessKey,
33
+ accessKeySecret: config.secret,
34
+ apiVersion: config.apiVersion || "2015-04-01",
35
+ endpoint: config.endpoint,
36
+ });
37
+
38
+ this.ossClient = new OSS({
39
+ accessKeyId: config.accessKey,
40
+ accessKeySecret: config.secret,
41
+ region: config.oss.region,
42
+ bucket: config.oss.bucketName,
43
+ timeout: config.timeout || 3000,
44
+ });
45
+ }
46
+
47
+ public async generateTmpCredentials(
48
+ sessionID: string
49
+ ): Promise<STSResponse["Credentials"]> {
50
+ const requestResponse: STSResponse = await this.coreClient.request(
51
+ "AssumeRole",
52
+ {
53
+ RoleArn: this.config.sts.roleArn,
54
+ RoleSessionName: sessionID,
55
+ DurationSeconds: this.config.timeout || 900,
56
+ },
57
+ {
58
+ method: "POST",
59
+ contentType: "application/json",
60
+ }
61
+ );
62
+
63
+ return requestResponse.Credentials;
64
+ }
65
+
66
+ public async uploadLocalToBucket(
67
+ fileName: string,
68
+ fileData: Buffer,
69
+ options?: PutObjectOption
70
+ ): Promise<string> {
71
+ const putResult = await this.ossClient.put(fileName, fileData, {
72
+ timeout: options?.timeout || 15000, // 15s
73
+ });
74
+
75
+ if (options?.domain) {
76
+ return `${options.domain}/${fileName}`;
77
+ }
78
+
79
+ if (putResult?.url?.includes("http://")) {
80
+ return putResult.url.replace("http://", "https://");
81
+ }
82
+
83
+ return putResult.url;
84
+ }
85
+
86
+ public async deleteObject(filePath: string): Promise<void> {
87
+ await this.ossClient.delete(filePath);
88
+ }
89
+
90
+ public async uploadRemoteObjectToBucket(
91
+ fileName: string,
92
+ remoteUrl: string,
93
+ options?: PutObjectOption
94
+ ): Promise<string> {
95
+ const response = await axios.get(remoteUrl, { responseType: "stream" });
96
+ if (response.status !== HttpStatusCode.Ok) {
97
+ throw new Error("failed to fetch from remote url");
98
+ }
99
+
100
+ const { data } = response;
101
+
102
+ const putResult = await this.ossClient.put(fileName, data, {
103
+ timeout: options.timeout || 15000, // 15s
104
+ });
105
+
106
+ if (options?.domain) {
107
+ return `${options.domain}/${fileName}`;
108
+ }
109
+
110
+ if (putResult?.url?.includes("http://")) {
111
+ return putResult.url.replace("http://", "https://");
112
+ }
113
+
114
+ return putResult.url;
115
+ }
116
+
117
+ public getObjectReadStream(fileName: string): Promise<OSS.GetStreamResult> {
118
+ return this.ossClient.getStream(fileName);
119
+ }
120
+
121
+ public writeFileToDestination(
122
+ fileName: string,
123
+ destination: string | WritableStream
124
+ ): Promise<OSS.GetObjectResult> {
125
+ return this.ossClient.get(fileName, destination);
126
+ }
127
+ }
@@ -0,0 +1,104 @@
1
+ import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
2
+
3
+ import { LOG_LEVEL } from "../constants";
4
+ import { AuditGateway } from "../interfaces";
5
+ import { maskFn } from "../pkg/string-utils";
6
+
7
+ export interface HttpAuditGatewayConfig {
8
+ baseUrl: string;
9
+ appName?: string;
10
+ auth: {
11
+ username: string;
12
+ password: string;
13
+ };
14
+ }
15
+
16
+ export class HttpAuditGateway implements AuditGateway {
17
+ protected baseURL: string;
18
+ protected access_token: string;
19
+
20
+ constructor(protected config: HttpAuditGatewayConfig) {
21
+ if (!config.baseUrl) {
22
+ throw new Error("Missing base url config");
23
+ }
24
+
25
+ this.baseURL = config.baseUrl;
26
+ }
27
+
28
+ public async publish(
29
+ logId: string,
30
+ level: LOG_LEVEL,
31
+ content: Record<string, any>
32
+ ) {
33
+ if (!this.access_token) {
34
+ await this.auth();
35
+ }
36
+
37
+ const response = await this.send(logId, {
38
+ method: "POST",
39
+ url: "/event/create",
40
+ data: {
41
+ ...content,
42
+ level,
43
+ },
44
+ });
45
+
46
+ return response?.data;
47
+ }
48
+
49
+ private async auth(logId = "system", forceReAuth = false) {
50
+ if (this.access_token || forceReAuth) {
51
+ return;
52
+ }
53
+
54
+ const authResponse = await this.send<{ access_token: string }>(
55
+ logId,
56
+ {
57
+ method: "POST",
58
+ url: "/auth/login",
59
+ data: this.config.auth,
60
+ },
61
+ false,
62
+ false
63
+ ); // not retry, not use token
64
+
65
+ this.access_token = authResponse?.data?.access_token;
66
+
67
+ return !!this.access_token;
68
+ }
69
+
70
+ protected async send<T>(
71
+ logId: string,
72
+ options: AxiosRequestConfig,
73
+ is_retry = false,
74
+ use_access_token = true
75
+ ): Promise<AxiosResponse<T>> {
76
+ console.log(
77
+ `[${logId}] ${
78
+ is_retry ? "Resend" : "Send"
79
+ } to Audit Service: ${JSON.stringify(options, maskFn)}`
80
+ );
81
+
82
+ options.baseURL = this.baseURL;
83
+ if (use_access_token) {
84
+ options.headers = {
85
+ Authorization: `Bearer ${this.access_token}`,
86
+ };
87
+ }
88
+
89
+ const response = await axios.request(options);
90
+
91
+ // use token, status 401, not retry => auth & retry
92
+ if (response?.status == 401 && use_access_token && !is_retry) {
93
+ console.log(`[${logId}] Retrying...`);
94
+
95
+ const isAuthSuccess = await this.auth(logId, true);
96
+
97
+ if (isAuthSuccess) {
98
+ return this.send(logId, options, true);
99
+ }
100
+ }
101
+
102
+ return response;
103
+ }
104
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./alibaba-cloud-gateway";
2
+ export * from "./http-audit-gateway";
3
+ export * from "./stdout-audit-gateway";
4
+ export * from "./internal-auth-gateway";
5
+ export * from "./webhook-audit-gateway";
@@ -0,0 +1,42 @@
1
+ import {
2
+ HttpService,
3
+ InternalAuthGatewayOptions,
4
+ InternalAuthLoginResult,
5
+ InternalServiceAuthResult,
6
+ OperationResult,
7
+ } from "../interfaces";
8
+
9
+ export class InternalAuthGateway {
10
+ constructor(
11
+ protected httpService: HttpService,
12
+ protected options: InternalAuthGatewayOptions
13
+ ) {}
14
+
15
+ public async login(): Promise<OperationResult<InternalAuthLoginResult>> {
16
+ return this.httpService.send(
17
+ "post",
18
+ `${this.options.endpointURL}/actors/login`,
19
+ {
20
+ body: {
21
+ secret: this.options.secret,
22
+ },
23
+ serviceName: this.options.serviceName,
24
+ }
25
+ );
26
+ }
27
+
28
+ public async verify(
29
+ accessToken: string
30
+ ): Promise<OperationResult<InternalServiceAuthResult>> {
31
+ return this.httpService.send(
32
+ "post",
33
+ `${this.options.endpointURL}/actors/verify`,
34
+ {
35
+ body: {
36
+ accessToken,
37
+ },
38
+ serviceName: this.options.serviceName,
39
+ }
40
+ );
41
+ }
42
+ }
@@ -0,0 +1,23 @@
1
+ import { LOG_LEVEL } from "../constants";
2
+ import { loggers } from "../pkg";
3
+ import { AuditGateway, Logger } from "../interfaces";
4
+
5
+ export class StdOutAuditGateway implements AuditGateway {
6
+ protected logger: Logger;
7
+
8
+ constructor() {
9
+ this.logger = new loggers.PinoLogger();
10
+ }
11
+
12
+ public async publish(
13
+ logId: string,
14
+ level: LOG_LEVEL,
15
+ content: any
16
+ ): Promise<any> {
17
+ return this.logger.info(
18
+ `[LogId ${logId}]`,
19
+ `[${level.toUpperCase}]`,
20
+ content
21
+ );
22
+ }
23
+ }
@@ -0,0 +1,33 @@
1
+ import { LOG_LEVEL } from "../constants";
2
+ import { AuditGateway, HttpService } from "../interfaces";
3
+
4
+ interface WebhookAuditGatewayConfig {
5
+ projectName: string;
6
+ }
7
+
8
+ export class WebhookAuditGateway implements AuditGateway {
9
+ public static contentField = "content";
10
+
11
+ constructor(
12
+ protected webhookURL: string,
13
+ protected httpService: HttpService,
14
+ protected config: WebhookAuditGatewayConfig
15
+ ) {}
16
+
17
+ public publish(logId: string, level: LOG_LEVEL, content: any): Promise<any> {
18
+ const logContent =
19
+ content && typeof content === "object"
20
+ ? JSON.stringify(content)
21
+ : content;
22
+ const msg = `[${this.config.projectName}] | [${level}] | [${logId}]: ${logContent}`;
23
+
24
+ return this.httpService.send("post", this.webhookURL, {
25
+ headers: {
26
+ "Content-Type": "application/json",
27
+ },
28
+ body: {
29
+ [WebhookAuditGateway.contentField]: msg,
30
+ },
31
+ });
32
+ }
33
+ }