opticore-webapp-core 1.0.20 → 1.0.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.
Files changed (43) hide show
  1. package/dist/index.d.cts +8 -4
  2. package/dist/index.d.ts +8 -4
  3. package/package.json +7 -8
  4. package/src/application/middlewares/bodyParser.middleware.ts +74 -0
  5. package/src/application/services/asymmetricCryptionDataWithPrivateRSAKey.service.ts +194 -0
  6. package/src/application/services/asymmetricCryptionDataWithPublicRSAKey.service.ts +188 -0
  7. package/src/application/services/loggerFileConfiguration.service.ts +6 -0
  8. package/src/core/config/database/connexion.config.database.ts +116 -0
  9. package/src/core/config/database/middleware/postgresChecker.database.ts +35 -0
  10. package/src/core/config/loaders/localLanguage.loader.ts +31 -0
  11. package/src/core/config/loaders/translateLanguage.loader.ts +27 -0
  12. package/src/core/config/logger/logger.config.ts +33 -0
  13. package/src/core/constants/signRSA/algorithm.constant.ts +54 -0
  14. package/src/core/constants/signRSA/encodingFormat.constant.ts +14 -0
  15. package/src/core/constants/signRSA/keyType.constant.ts +4 -0
  16. package/src/core/constants/signRSA/outputFormat.constant.ts +6 -0
  17. package/src/core/constants/signRSAKeyComponent.constant.ts +11 -0
  18. package/src/core/errors/databaseConnexion.error.ts +107 -0
  19. package/src/core/events/pathModuleVerifier.event.ts +58 -0
  20. package/src/domains/constants/logLevel.constant.ts +6 -0
  21. package/src/index.ts +56 -0
  22. package/src/interfaces/bodyParserOptions.interface.ts +4 -0
  23. package/src/interfaces/responseBodyEndFromResponseEvent.interface.ts +7 -0
  24. package/src/interfaces/responseBodyWriteFromResponseEvent.interface.ts +4 -0
  25. package/src/interfaces/wrappingBodyResponse.interface.ts +4 -0
  26. package/src/types/json.type.ts +18 -0
  27. package/src/types/logLevel.type.ts +3 -0
  28. package/src/types/originalWriteEncoding.type.ts +1 -0
  29. package/src/types/parseFunction.type.ts +1 -0
  30. package/src/types/raw.type.ts +23 -0
  31. package/src/types/text.type.ts +18 -0
  32. package/src/types/urlencoded.type.ts +19 -0
  33. package/src/utils/cryptography/decryption/rsaKey.decryption.ts +19 -0
  34. package/src/utils/cryptography/encryption/rsaKey.encryption.ts +25 -0
  35. package/src/utils/dateTimeFormatted.utils.ts +6 -0
  36. package/src/utils/environment.utils.ts +11 -0
  37. package/src/utils/logMessage.utils.ts +20 -0
  38. package/src/utils/parseUrlencoded.utils.ts +17 -0
  39. package/src/utils/parsing/parsingYaml.utils.ts +81 -0
  40. package/src/utils/utility.utils.ts +154 -0
  41. package/tsup.config.ts +11 -0
  42. /package/{dist → src}/utils/translations/message.translation.en.json +0 -0
  43. /package/{dist → src}/utils/translations/message.translation.fr.json +0 -0
package/dist/index.d.cts CHANGED
@@ -1,5 +1,8 @@
1
1
  import { StackTraceError } from 'opticore-catch-exception-error';
2
2
  import { ServerResponse, IncomingMessage } from 'node:http';
3
+ import * as express from 'express';
4
+ import * as qs from 'qs';
5
+ import * as express_serve_static_core from 'express-serve-static-core';
3
6
 
4
7
  declare class SAsymmetricCryptionDataWithPrivateRSAKey {
5
8
  private readonly log;
@@ -337,6 +340,7 @@ declare class LocalLanguageLoader {
337
340
 
338
341
  interface IBodyParserOptions {
339
342
  type: string;
343
+ limit?: number;
340
344
  }
341
345
 
342
346
  interface IResponseBodyEndFromResponseEvent {
@@ -372,7 +376,7 @@ type TParseFunction = (rawBody: string) => any;
372
376
  * in express, it can be used like this: app.use(TJSONBodyParser({ type: 'application/vnd.custom-type' }));
373
377
  *
374
378
  */
375
- declare function TJSONBodyParser(options: IBodyParserOptions, localLanguage: string, environnementPath: any): void;
379
+ declare function TJSONBodyParser(options: IBodyParserOptions, localLanguage: string, environnementPath: any): (req: express.Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>>, res: express.Response<any, Record<string, any>>, next: express.NextFunction) => void;
376
380
 
377
381
  /**
378
382
  *
@@ -385,7 +389,7 @@ declare function TJSONBodyParser(options: IBodyParserOptions, localLanguage: str
385
389
  * in express, it can be used like this: app.use(rawBodyParserType({ type: 'application/vnd.custom-type' }));
386
390
  *
387
391
  */
388
- declare function TRawBodyParser(options: IBodyParserOptions, localLanguage: string, environnementPath: any): void;
392
+ declare function TRawBodyParser(options: IBodyParserOptions, localLanguage: string, environnementPath: any): (req: express.Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>>, res: express.Response<any, Record<string, any>>, next: express.NextFunction) => void;
389
393
 
390
394
  /**
391
395
  *
@@ -398,7 +402,7 @@ declare function TRawBodyParser(options: IBodyParserOptions, localLanguage: stri
398
402
  * in express, it can be used like this: app.use(textBodyParserType({ type: 'text/html' }));
399
403
  *
400
404
  */
401
- declare function TTextBodyParser(options: IBodyParserOptions, localLanguage: string, environmentPath: any): void;
405
+ declare function TTextBodyParser(options: IBodyParserOptions, localLanguage: string, environmentPath: any): (req: express.Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>>, res: express.Response<any, Record<string, any>>, next: express.NextFunction) => void;
402
406
 
403
407
  /**
404
408
  *
@@ -411,6 +415,6 @@ declare function TTextBodyParser(options: IBodyParserOptions, localLanguage: str
411
415
  * in express, it can be used like this: app.use(urlencodedBodyParserType({ type: 'application/x-www-form-urlencoded' }));
412
416
  *
413
417
  */
414
- declare function TUrlencodedBodyParser(options: IBodyParserOptions, localLanguage: string, environmentPath: any): void;
418
+ declare function TUrlencodedBodyParser(options: IBodyParserOptions, localLanguage: string, environmentPath: any): (req: express.Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>>, res: express.Response<any, Record<string, any>>, next: express.NextFunction) => void;
415
419
 
416
420
  export { CLogLevel, CSignRSAKeyComponent, Environment, type IBodyParserOptions, type IResponseBodyEndFromResponseEvent, type IResponseBodyWriteFromResponseEvent, type IWrappingBodyResponse, LocalLanguageLoader, PathModuleVerifier, RSAKeyDecryption, RSAKeyEncryption, SAsymmetricCryptionDataWithPrivateRSAKey, SAsymmetricCryptionDataWithPublicRSAKey, TJSONBodyParser, type TLogLevel, type TOriginalWriteEncoding, type TParseFunction, TRawBodyParser, TTextBodyParser, TUrlencodedBodyParser, Utility, YamlParsing, dateTimeFormatted, loggerConfig };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  import { StackTraceError } from 'opticore-catch-exception-error';
2
2
  import { ServerResponse, IncomingMessage } from 'node:http';
3
+ import * as express from 'express';
4
+ import * as qs from 'qs';
5
+ import * as express_serve_static_core from 'express-serve-static-core';
3
6
 
4
7
  declare class SAsymmetricCryptionDataWithPrivateRSAKey {
5
8
  private readonly log;
@@ -337,6 +340,7 @@ declare class LocalLanguageLoader {
337
340
 
338
341
  interface IBodyParserOptions {
339
342
  type: string;
343
+ limit?: number;
340
344
  }
341
345
 
342
346
  interface IResponseBodyEndFromResponseEvent {
@@ -372,7 +376,7 @@ type TParseFunction = (rawBody: string) => any;
372
376
  * in express, it can be used like this: app.use(TJSONBodyParser({ type: 'application/vnd.custom-type' }));
373
377
  *
374
378
  */
375
- declare function TJSONBodyParser(options: IBodyParserOptions, localLanguage: string, environnementPath: any): void;
379
+ declare function TJSONBodyParser(options: IBodyParserOptions, localLanguage: string, environnementPath: any): (req: express.Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>>, res: express.Response<any, Record<string, any>>, next: express.NextFunction) => void;
376
380
 
377
381
  /**
378
382
  *
@@ -385,7 +389,7 @@ declare function TJSONBodyParser(options: IBodyParserOptions, localLanguage: str
385
389
  * in express, it can be used like this: app.use(rawBodyParserType({ type: 'application/vnd.custom-type' }));
386
390
  *
387
391
  */
388
- declare function TRawBodyParser(options: IBodyParserOptions, localLanguage: string, environnementPath: any): void;
392
+ declare function TRawBodyParser(options: IBodyParserOptions, localLanguage: string, environnementPath: any): (req: express.Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>>, res: express.Response<any, Record<string, any>>, next: express.NextFunction) => void;
389
393
 
390
394
  /**
391
395
  *
@@ -398,7 +402,7 @@ declare function TRawBodyParser(options: IBodyParserOptions, localLanguage: stri
398
402
  * in express, it can be used like this: app.use(textBodyParserType({ type: 'text/html' }));
399
403
  *
400
404
  */
401
- declare function TTextBodyParser(options: IBodyParserOptions, localLanguage: string, environmentPath: any): void;
405
+ declare function TTextBodyParser(options: IBodyParserOptions, localLanguage: string, environmentPath: any): (req: express.Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>>, res: express.Response<any, Record<string, any>>, next: express.NextFunction) => void;
402
406
 
403
407
  /**
404
408
  *
@@ -411,6 +415,6 @@ declare function TTextBodyParser(options: IBodyParserOptions, localLanguage: str
411
415
  * in express, it can be used like this: app.use(urlencodedBodyParserType({ type: 'application/x-www-form-urlencoded' }));
412
416
  *
413
417
  */
414
- declare function TUrlencodedBodyParser(options: IBodyParserOptions, localLanguage: string, environmentPath: any): void;
418
+ declare function TUrlencodedBodyParser(options: IBodyParserOptions, localLanguage: string, environmentPath: any): (req: express.Request<express_serve_static_core.ParamsDictionary, any, any, qs.ParsedQs, Record<string, any>>, res: express.Response<any, Record<string, any>>, next: express.NextFunction) => void;
415
419
 
416
420
  export { CLogLevel, CSignRSAKeyComponent, Environment, type IBodyParserOptions, type IResponseBodyEndFromResponseEvent, type IResponseBodyWriteFromResponseEvent, type IWrappingBodyResponse, LocalLanguageLoader, PathModuleVerifier, RSAKeyDecryption, RSAKeyEncryption, SAsymmetricCryptionDataWithPrivateRSAKey, SAsymmetricCryptionDataWithPublicRSAKey, TJSONBodyParser, type TLogLevel, type TOriginalWriteEncoding, type TParseFunction, TRawBodyParser, TTextBodyParser, TUrlencodedBodyParser, Utility, YamlParsing, dateTimeFormatted, loggerConfig };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opticore-webapp-core",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "description": "opticore Web Application core module",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -30,13 +30,12 @@
30
30
  "dotenv": "^16.5.0",
31
31
  "mongodb": "^6.6.1",
32
32
  "mysql": "^2.18.1",
33
- "opticore-catch-exception-error": "^1.0.25",
34
- "opticore-env-access": "^1.0.12",
35
- "opticore-express": "^1.0.7",
36
- "opticore-http-response": "^1.0.9",
37
- "opticore-inject-dependency": "^1.0.12",
38
- "opticore-logger": "^1.0.17",
39
- "opticore-translator": "^1.0.10",
33
+ "opticore-catch-exception-error": "^1.0.30",
34
+ "opticore-env-access": "^1.0.25",
35
+ "opticore-express": "^1.0.9",
36
+ "opticore-http-response": "^1.0.11",
37
+ "opticore-logger": "^1.0.33",
38
+ "opticore-translator": "^1.0.17",
40
39
  "pg": "^8.15.6"
41
40
  },
42
41
  "devDependencies": {
@@ -0,0 +1,74 @@
1
+ import { express, NextFunction } from "opticore-express";
2
+ import { LoggerCore } from "opticore-logger";
3
+ import { HttpStatusCode } from "opticore-http-response";
4
+ import { TranslationLoader } from "opticore-translator";
5
+
6
+ import { IBodyParserOptions } from "@webAppCore/interfaces/bodyParserOptions.interface";
7
+ import { TParseFunction } from "@webAppCore/types/parseFunction.type";
8
+ import { loggerConfig } from "@webAppCore/core/config/logger/logger.config";
9
+ import { SLoggerFileConfiguration } from "@webAppCore/application/services/loggerFileConfiguration.service";
10
+
11
+
12
+ /**
13
+ *
14
+ * @param localeLanguage
15
+ * @param parseFunction
16
+ * @param options
17
+ * @param environmentPath
18
+ *
19
+ * This is a middleware that allows parsing data
20
+ *
21
+ */
22
+ export function MBodyParser(parseFunction: TParseFunction,
23
+ options: IBodyParserOptions,
24
+ localeLanguage: string,
25
+ environmentPath: any): (req: express.Request,
26
+ res: express.Response,
27
+ next: NextFunction) => void {
28
+ const logger: LoggerCore = SLoggerFileConfiguration(environmentPath);
29
+
30
+ return function (req: express.Request, res: express.Response, next: NextFunction): void {
31
+ if (!req.headers['content-type']?.includes(options.type)) {
32
+ return next();
33
+ }
34
+
35
+ const maxSize: number = options.limit ?? 1024 * 1024;
36
+ let body: Uint8Array[] = [];
37
+ let totalSize: number = 0;
38
+
39
+ req.on("data", (chunk: any): void => {
40
+ totalSize += chunk.length;
41
+ if (totalSize > maxSize) {
42
+ req.destroy();
43
+ res.status(HttpStatusCode.PAYLOAD_TOO_LARGE).json({
44
+ message: TranslationLoader.t("payloadTooLarge", localeLanguage, loggerConfig)
45
+ });
46
+ return;
47
+ }
48
+ body.push(chunk);
49
+ });
50
+
51
+ req.on("end", (): void => {
52
+ const rawBody: string = Buffer.concat(body).toString();
53
+ try {
54
+ req.body = parseFunction(rawBody);
55
+ next();
56
+ } catch (error: any) {
57
+ res.status(HttpStatusCode.BAD_REQUEST).json({
58
+ message: TranslationLoader.t("invalidRequest", localeLanguage, loggerConfig)
59
+ });
60
+ }
61
+ });
62
+
63
+ req.on("error", (err: any): void => {
64
+ logger.error({
65
+ message: err.message,
66
+ title: "Content Type error",
67
+ errorType: TranslationLoader.t("invalidRequest", localeLanguage, loggerConfig),
68
+ stackTrace: err.stack,
69
+ httpCodeValue: HttpStatusCode.BAD_REQUEST
70
+ });
71
+ next(err);
72
+ });
73
+ }
74
+ }
@@ -0,0 +1,194 @@
1
+ import crypto from "crypto";
2
+ import { BinaryToTextEncoding } from "node:crypto";
3
+ import { HttpStatusCode as status } from "opticore-http-response";
4
+ import { LoggerCore } from "opticore-logger";
5
+ import { TranslationLoader } from "opticore-translator";
6
+ import { StackTraceError as ErrorHandler } from "opticore-catch-exception-error";
7
+
8
+ import { RSAKeyEncryption } from "@webAppCore/utils/cryptography/encryption/rsaKey.encryption";
9
+ import { RSAKeyDecryption } from "@webAppCore/utils/cryptography/decryption/rsaKey.decryption";
10
+ import { CSignRSAKeyComponent } from "@webAppCore/core/constants/signRSAKeyComponent.constant";
11
+ import { SLoggerFileConfiguration } from "@webAppCore/application/services/loggerFileConfiguration.service";
12
+
13
+
14
+ export class SAsymmetricCryptionDataWithPrivateRSAKey {
15
+ private readonly log: LoggerCore;
16
+ private readonly localeLanguage: string;
17
+
18
+ constructor(localeLanguage: string, environnementPath: any) {
19
+ this.localeLanguage = localeLanguage;
20
+ this.log = SLoggerFileConfiguration(environnementPath)
21
+ }
22
+
23
+
24
+ /**
25
+ *
26
+ * @param rsaKey
27
+ * @param keyType
28
+ * @protected
29
+ *
30
+ * Return ErrorHandler | string
31
+ */
32
+ protected verifyExistingKey(rsaKey: string, keyType: string): string | ErrorHandler {
33
+ if (!rsaKey) {
34
+ const stackTrace: ErrorHandler = this.traceError(
35
+ keyType +" "+ TranslationLoader.t("rsaKeyNotFound", this.localeLanguage, this.log),
36
+ TranslationLoader.t("erBadDbError", this.localeLanguage, this.log),
37
+ status.NOT_FOUND
38
+ );
39
+
40
+ this.log.error({
41
+ message: TranslationLoader.t("rsaKeyNotFound", this.localeLanguage, this.log),
42
+ title: TranslationLoader.t("verifyExistingKey", this.localeLanguage, this.log),
43
+ errorType: TranslationLoader.t("erBadDbError", this.localeLanguage, this.log),
44
+ stackTrace: stackTrace.stack,
45
+ httpCodeValue: status.NOT_FOUND
46
+ });
47
+ }
48
+
49
+ return rsaKey;
50
+ }
51
+
52
+ /**
53
+ *
54
+ * @param privateKey
55
+ * @param payload
56
+ * @private
57
+ *
58
+ * Return Buffer
59
+ */
60
+ private encryptionWithPrivateKey(privateKey: string, payload: any): Buffer {
61
+ this.verifyExistingKey(privateKey, CSignRSAKeyComponent.keyType.private);
62
+
63
+ try {
64
+ const bufferedData: Buffer = Buffer.from(payload, Number(CSignRSAKeyComponent.encodingFormat));
65
+ return RSAKeyEncryption.privateEncrypt(privateKey, bufferedData);
66
+ } catch (err: any) {
67
+ const stackTrace: ErrorHandler = this.traceError(
68
+ TranslationLoader.t("encryptionWithPrivateKeyFailed", this.localeLanguage, this.log),
69
+ TranslationLoader.t("encryptionFailed", this.localeLanguage, this.log),
70
+ status.NOT_FOUND
71
+ );
72
+
73
+ this.log.error({
74
+ message: err.message,
75
+ title: TranslationLoader.t("encryptionFailed", this.localeLanguage, this.log),
76
+ errorType: TranslationLoader.t("encryptionWithPrivateKeyFailed", this.localeLanguage, this.log),
77
+ stackTrace: stackTrace.stack,
78
+ httpCodeValue: status.NOT_FOUND
79
+ });
80
+ process.exit();
81
+ }
82
+ }
83
+
84
+ /**
85
+ *
86
+ * @param privateKey
87
+ * @param publicKey
88
+ * @param payload
89
+ * @private
90
+ */
91
+ private decryptionWithPublicKey(privateKey: string, publicKey: string, payload: any): Buffer {
92
+ this.verifyExistingKey(publicKey, CSignRSAKeyComponent.keyType.public);
93
+
94
+ try {
95
+ const encryptedPayload: Buffer = this.encryptionWithPrivateKey(privateKey, payload);
96
+
97
+ return RSAKeyDecryption.publicDecrypt(publicKey, encryptedPayload);
98
+ } catch (error: any) {
99
+ const stackTrace: ErrorHandler = this.traceError(
100
+ TranslationLoader.t("errorDecryption", this.localeLanguage, this.log),
101
+ TranslationLoader.t("decryptionFailed", this.localeLanguage, this.log),
102
+ status.NOT_ACCEPTABLE
103
+ );
104
+
105
+ this.log.error({
106
+ message: TranslationLoader.t("decryptionWithPublicKeyFailed", this.localeLanguage, this.log),
107
+ title: TranslationLoader.t("decryptionFailed", this.localeLanguage, this.log),
108
+ errorType: TranslationLoader.t("errorDecryption", this.localeLanguage, this.log),
109
+ stackTrace: stackTrace.stack,
110
+ httpCodeValue: status.NOT_ACCEPTABLE
111
+ });
112
+ process.exit();
113
+ }
114
+ }
115
+
116
+ /**
117
+ *
118
+ * @param privateKey
119
+ * @param payload
120
+ * @private
121
+ */
122
+ private signWithRSAKey(privateKey: string, payload: any): string {
123
+ this.verifyExistingKey(privateKey, CSignRSAKeyComponent.keyType.private);
124
+
125
+ const sign: crypto.Sign = crypto.createSign(CSignRSAKeyComponent.algorithm.sha256);
126
+ sign.update(payload);
127
+
128
+ return sign.sign(privateKey, CSignRSAKeyComponent.outputFormat.base64 as BinaryToTextEncoding);
129
+ }
130
+
131
+ /**
132
+ *
133
+ * @param privateKey
134
+ * @param publicKey
135
+ * @param payload
136
+ */
137
+ public verifyRSAKey(privateKey: string, publicKey: string, payload: any): string | Error {
138
+ try {
139
+ const verify: crypto.Verify = crypto.createVerify(CSignRSAKeyComponent.algorithm.sha256);
140
+ verify.update(payload);
141
+
142
+ const signature: string = this.signWithRSAKey(privateKey, payload);
143
+ const isVerified: boolean = verify.verify(publicKey, signature, CSignRSAKeyComponent.outputFormat.base64 as BinaryToTextEncoding);
144
+
145
+ if (isVerified) {
146
+ const decryptedData: Buffer = this.decryptionWithPublicKey(privateKey, publicKey, payload);
147
+
148
+ return decryptedData.toString(CSignRSAKeyComponent.encodingFormat.utf_8 as BufferEncoding);
149
+ } else {
150
+ const stackTrace: ErrorHandler = this.traceError(
151
+ TranslationLoader.t("signatureRSAKeyFailed", this.localeLanguage, this.log),
152
+ TranslationLoader.t("verifyRSAKeyFailed", this.localeLanguage, this.log),
153
+ status.NOT_ACCEPTABLE
154
+ );
155
+
156
+ this.log.error({
157
+ message: TranslationLoader.t("verifyRSAKey", this.localeLanguage, this.log),
158
+ title: TranslationLoader.t("verifyRSAKeyFailed", this.localeLanguage, this.log),
159
+ errorType: TranslationLoader.t("signatureRSAKeyFailed", this.localeLanguage, this.log),
160
+ stackTrace: stackTrace.stack,
161
+ httpCodeValue: status.NOT_FOUND
162
+ });
163
+ process.exit();
164
+ }
165
+
166
+ } catch (err: any) {
167
+ const stackTrace: ErrorHandler = this.traceError(
168
+ err.name,
169
+ TranslationLoader.t("signatureRSAKeysError", this.localeLanguage, this.log),
170
+ status.NOT_ACCEPTABLE
171
+ );
172
+
173
+ this.log.error({
174
+ message: TranslationLoader.t("signatureRSAKeysError", this.localeLanguage, this.log),
175
+ title: TranslationLoader.t("errorNameNotVerifyingRSAKey", this.localeLanguage, this.log),
176
+ errorType: err.name,
177
+ stackTrace: stackTrace.stack,
178
+ httpCodeValue: status.NOT_FOUND
179
+ });
180
+ process.exit();
181
+ }
182
+ }
183
+
184
+ /**
185
+ *
186
+ * @param props
187
+ * @param name
188
+ * @param status
189
+ * @private
190
+ */
191
+ private traceError(props: string, name: string, status: number): ErrorHandler {
192
+ return new ErrorHandler(props, name, status, true);
193
+ }
194
+ }
@@ -0,0 +1,188 @@
1
+ import crypto from "crypto";
2
+ import { BinaryToTextEncoding } from "node:crypto";
3
+ import { LoggerCore } from "opticore-logger";
4
+ import { TranslationLoader } from "opticore-translator";
5
+ import { HttpStatusCode } from "opticore-http-response";
6
+
7
+ import { StackTraceError } from "opticore-catch-exception-error";
8
+ import { RSAKeyEncryption } from "@webAppCore/utils/cryptography/encryption/rsaKey.encryption";
9
+ import { CSignRSAKeyComponent } from "@webAppCore/core/constants/signRSAKeyComponent.constant";
10
+ import { RSAKeyDecryption } from "@webAppCore/utils/cryptography/decryption/rsaKey.decryption";
11
+ import { SLoggerFileConfiguration } from "@webAppCore/application/services/loggerFileConfiguration.service";
12
+
13
+
14
+ export class SAsymmetricCryptionDataWithPublicRSAKey {
15
+ private readonly logger: LoggerCore;
16
+ private readonly localeLanguage: string;
17
+
18
+ constructor(localeLanguage: string, environnementPath: any) {
19
+ this.localeLanguage = localeLanguage;
20
+ this.logger = SLoggerFileConfiguration(environnementPath);
21
+ }
22
+
23
+ /**
24
+ *
25
+ * @param rsaKey
26
+ * @param keyType
27
+ *
28
+ * @protected
29
+ */
30
+ protected verifyExistingKey(rsaKey: string, keyType: string): string | Error {
31
+ if (!rsaKey) {
32
+ const stackTrace: StackTraceError = this.traceError(
33
+ keyType + TranslationLoader.t("verifyExistingKey", this.localeLanguage),
34
+ TranslationLoader.t("verifyExistingKey", this.localeLanguage),
35
+ HttpStatusCode.NOT_FOUND
36
+ );
37
+
38
+ this.logger.error({
39
+ message: TranslationLoader.t("verifyExistingKey", this.localeLanguage),
40
+ title: "key verification",
41
+ errorType: TranslationLoader.t("verifyExistingKeyError", this.localeLanguage),
42
+ stackTrace: stackTrace.stack!,
43
+ httpCodeValue: HttpStatusCode.NOT_FOUND
44
+ });
45
+ }
46
+
47
+ return rsaKey;
48
+ }
49
+
50
+
51
+ /**
52
+ *
53
+ * @param publicKey
54
+ * @param payload
55
+ *
56
+ * @private
57
+ */
58
+ private encryptionWithPublicKey(publicKey: string, payload: any): Buffer {
59
+ this.verifyExistingKey(publicKey, CSignRSAKeyComponent.keyType.public);
60
+
61
+ try {
62
+ const bufferedData: Buffer = Buffer.from(payload, Number(CSignRSAKeyComponent.encodingFormat));
63
+ return RSAKeyEncryption.publicEncrypt(publicKey, bufferedData);
64
+ } catch (err: any) {
65
+ const stackTrace: StackTraceError = this.traceError(
66
+ err.message,
67
+ TranslationLoader.t("encryptionWithPublicKey", this.localeLanguage),
68
+ HttpStatusCode.NOT_ACCEPTABLE
69
+ );
70
+
71
+ this.logger.error({
72
+ message: TranslationLoader.t("errorEncryptionPublicKey", this.localeLanguage),
73
+ title: TranslationLoader.t("encryptionWithPublicKey", this.localeLanguage),
74
+ errorType: err.code,
75
+ stackTrace: stackTrace.stack,
76
+ httpCodeValue: HttpStatusCode.NOT_ACCEPTABLE
77
+ });
78
+ process.exit();
79
+ }
80
+ }
81
+
82
+
83
+ /**
84
+ *
85
+ * @param privateKey
86
+ * @param publicKey
87
+ * @param payload
88
+ *
89
+ * @private
90
+ */
91
+ private decryptionWithPrivateKey(privateKey: string, publicKey: string, payload: any): Buffer {
92
+ this.verifyExistingKey(publicKey, CSignRSAKeyComponent.keyType.public);
93
+
94
+ try {
95
+ const encryptedPayload: Buffer = this.encryptionWithPublicKey(publicKey, payload);
96
+ return RSAKeyDecryption.privateDecrypt(privateKey, encryptedPayload);
97
+ } catch (err: any) {
98
+ const stackTrace: StackTraceError = this.traceError(
99
+ err.code,
100
+ TranslationLoader.t("errorDecryption", this.localeLanguage),
101
+ HttpStatusCode.NOT_ACCEPTABLE
102
+ );
103
+
104
+ this.logger.error({
105
+ message: TranslationLoader.t("errorDecryptionWithPrivateKey", this.localeLanguage),
106
+ title: TranslationLoader.t("errorDecryption", this.localeLanguage),
107
+ errorType: err.message,
108
+ stackTrace: stackTrace.stack,
109
+ httpCodeValue: HttpStatusCode.NOT_ACCEPTABLE
110
+ });
111
+ process.exit();
112
+ }
113
+ }
114
+
115
+
116
+ /**
117
+ *
118
+ * @param publicKey
119
+ * @param payload
120
+ *
121
+ * @private
122
+ */
123
+ private signWithPublicRSAKey(publicKey: string, payload: any): string {
124
+ this.verifyExistingKey(publicKey, CSignRSAKeyComponent.keyType.private);
125
+
126
+ const sign: crypto.Sign = crypto.createSign(CSignRSAKeyComponent.algorithm.sha256);
127
+ sign.update(payload);
128
+
129
+ return sign.sign(publicKey, CSignRSAKeyComponent.outputFormat.base64 as BinaryToTextEncoding);
130
+ }
131
+
132
+
133
+ /**
134
+ *
135
+ * @param privateKey
136
+ * @param publicKey
137
+ * @param payload
138
+ *
139
+ */
140
+ public verifyPublicRSAKey(privateKey: string, publicKey: string, payload: any): string | StackTraceError {
141
+ try {
142
+ const verify: crypto.Verify = crypto.createVerify(CSignRSAKeyComponent.algorithm.sha256);
143
+ verify.update(payload);
144
+
145
+ const signature: string = this.signWithPublicRSAKey(publicKey, payload);
146
+ const isVerified: boolean = verify.verify(publicKey, signature, CSignRSAKeyComponent.outputFormat.base64 as BinaryToTextEncoding);
147
+
148
+ if (isVerified) {
149
+ const decryptedData: Buffer = this.decryptionWithPrivateKey(privateKey, publicKey, payload);
150
+ return decryptedData.toString(CSignRSAKeyComponent.encodingFormat.utf_8 as BufferEncoding | undefined);
151
+ } else {
152
+ const stackTrace: StackTraceError = this.traceError(
153
+ TranslationLoader.t("verifyPublicRSAKeyError", this.localeLanguage),
154
+ TranslationLoader.t("signatureRSAKeyFailed", this.localeLanguage),
155
+ HttpStatusCode.NOT_FOUND
156
+ );
157
+ this.logger.error({
158
+ message: TranslationLoader.t("verifyPublicRSAKey", this.localeLanguage),
159
+ title: TranslationLoader.t("signatureRSAKeyFailed", this.localeLanguage),
160
+ errorType: TranslationLoader.t("verifyPublicRSAKeyError", this.localeLanguage),
161
+ stackTrace: stackTrace.stack,
162
+ httpCodeValue: HttpStatusCode.NOT_FOUND
163
+ });
164
+
165
+ return stackTrace;
166
+ }
167
+ } catch (err: any) {
168
+ const stackTrace: StackTraceError = this.traceError(
169
+ err.code,
170
+ TranslationLoader.t("verifyPublicRSAKey", this.localeLanguage),
171
+ HttpStatusCode.NOT_ACCEPTABLE
172
+ );
173
+
174
+ this.logger.error({
175
+ message: TranslationLoader.t("verifyPublicRSAKey", this.localeLanguage),
176
+ title: TranslationLoader.t("errorDecryption", this.localeLanguage),
177
+ errorType: err.message,
178
+ stackTrace: stackTrace.stack,
179
+ httpCodeValue: HttpStatusCode.NOT_ACCEPTABLE
180
+ });
181
+ process.exit();
182
+ }
183
+ }
184
+
185
+ private traceError(props: string, name: string, HttpStatusCode: number): StackTraceError {
186
+ return new StackTraceError(props, name, HttpStatusCode, true);
187
+ }
188
+ }
@@ -0,0 +1,6 @@
1
+ import { ILoggerConfig, LoggerCore } from "opticore-logger";
2
+ import { loggerConfig } from "@webAppCore/core/config/logger/logger.config";
3
+
4
+ export const SLoggerFileConfiguration: (environmentPath: any) => LoggerCore = (environmentPath: any): LoggerCore => {
5
+ return new LoggerCore(loggerConfig(environmentPath) as ILoggerConfig);
6
+ }