@nestia/core 0.1.0
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/LICENSE +21 -0
- package/lib/decorators/EncryptedBody.d.ts +22 -0
- package/lib/decorators/EncryptedBody.js +125 -0
- package/lib/decorators/EncryptedBody.js.map +1 -0
- package/lib/decorators/EncryptedController.d.ts +31 -0
- package/lib/decorators/EncryptedController.js +42 -0
- package/lib/decorators/EncryptedController.js.map +1 -0
- package/lib/decorators/EncryptedModule.d.ts +48 -0
- package/lib/decorators/EncryptedModule.js +270 -0
- package/lib/decorators/EncryptedModule.js.map +1 -0
- package/lib/decorators/EncryptedRoute.d.ts +78 -0
- package/lib/decorators/EncryptedRoute.js +191 -0
- package/lib/decorators/EncryptedRoute.js.map +1 -0
- package/lib/decorators/PlainBody.d.ts +22 -0
- package/lib/decorators/PlainBody.js +83 -0
- package/lib/decorators/PlainBody.js.map +1 -0
- package/lib/decorators/TypedBody.d.ts +14 -0
- package/lib/decorators/TypedBody.js +93 -0
- package/lib/decorators/TypedBody.js.map +1 -0
- package/lib/decorators/TypedParam.d.ts +25 -0
- package/lib/decorators/TypedParam.js +62 -0
- package/lib/decorators/TypedParam.js.map +1 -0
- package/lib/decorators/TypedRoute.d.ts +71 -0
- package/lib/decorators/TypedRoute.js +159 -0
- package/lib/decorators/TypedRoute.js.map +1 -0
- package/lib/decorators/internal/EncryptedConstant.d.ts +1 -0
- package/lib/decorators/internal/EncryptedConstant.js +8 -0
- package/lib/decorators/internal/EncryptedConstant.js.map +1 -0
- package/lib/decorators/internal/get_path_and_stringify.d.ts +1 -0
- package/lib/decorators/internal/get_path_and_stringify.js +75 -0
- package/lib/decorators/internal/get_path_and_stringify.js.map +1 -0
- package/lib/decorators/internal/headers_to_object.d.ts +1 -0
- package/lib/decorators/internal/headers_to_object.js +53 -0
- package/lib/decorators/internal/headers_to_object.js.map +1 -0
- package/lib/decorators/internal/route_error.d.ts +1 -0
- package/lib/decorators/internal/route_error.js +90 -0
- package/lib/decorators/internal/route_error.js.map +1 -0
- package/lib/decorators/internal/validate_request_body.d.ts +2 -0
- package/lib/decorators/internal/validate_request_body.js +56 -0
- package/lib/decorators/internal/validate_request_body.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +32 -0
- package/lib/index.js.map +1 -0
- package/lib/module.d.ts +9 -0
- package/lib/module.js +26 -0
- package/lib/module.js.map +1 -0
- package/lib/options/INestiaTransformOptions.d.ts +5 -0
- package/lib/options/INestiaTransformOptions.js +3 -0
- package/lib/options/INestiaTransformOptions.js.map +1 -0
- package/lib/options/INestiaTransformProject.d.ts +5 -0
- package/lib/options/INestiaTransformProject.js +3 -0
- package/lib/options/INestiaTransformProject.js.map +1 -0
- package/lib/options/IRequestBodyValidator.d.ts +16 -0
- package/lib/options/IRequestBodyValidator.js +3 -0
- package/lib/options/IRequestBodyValidator.js.map +1 -0
- package/lib/options/IResponseBodyStringifier.d.ts +20 -0
- package/lib/options/IResponseBodyStringifier.js +3 -0
- package/lib/options/IResponseBodyStringifier.js.map +1 -0
- package/lib/transform.d.ts +3 -0
- package/lib/transform.js +21 -0
- package/lib/transform.js.map +1 -0
- package/lib/transformers/BodyTransformer.d.ts +5 -0
- package/lib/transformers/BodyTransformer.js +69 -0
- package/lib/transformers/BodyTransformer.js.map +1 -0
- package/lib/transformers/FileTransformer.d.ts +5 -0
- package/lib/transformers/FileTransformer.js +33 -0
- package/lib/transformers/FileTransformer.js.map +1 -0
- package/lib/transformers/MethodTransformer.d.ts +5 -0
- package/lib/transformers/MethodTransformer.js +55 -0
- package/lib/transformers/MethodTransformer.js.map +1 -0
- package/lib/transformers/NodeTransformer.d.ts +5 -0
- package/lib/transformers/NodeTransformer.js +21 -0
- package/lib/transformers/NodeTransformer.js.map +1 -0
- package/lib/transformers/ParameterTransformer.d.ts +5 -0
- package/lib/transformers/ParameterTransformer.js +31 -0
- package/lib/transformers/ParameterTransformer.js.map +1 -0
- package/lib/transformers/RouteTransformer.d.ts +5 -0
- package/lib/transformers/RouteTransformer.js +115 -0
- package/lib/transformers/RouteTransformer.js.map +1 -0
- package/lib/typings/Creator.d.ts +3 -0
- package/lib/typings/Creator.js +3 -0
- package/lib/typings/Creator.js.map +1 -0
- package/lib/utils/ExceptionManager.d.ts +64 -0
- package/lib/utils/ExceptionManager.js +113 -0
- package/lib/utils/ExceptionManager.js.map +1 -0
- package/lib/utils/Singleton.d.ts +1 -0
- package/lib/utils/Singleton.js +24 -0
- package/lib/utils/Singleton.js.map +1 -0
- package/package.json +66 -0
- package/src/decorators/EncryptedBody.ts +102 -0
- package/src/decorators/EncryptedController.ts +43 -0
- package/src/decorators/EncryptedModule.ts +127 -0
- package/src/decorators/EncryptedRoute.ts +200 -0
- package/src/decorators/PlainBody.ts +38 -0
- package/src/decorators/TypedBody.ts +49 -0
- package/src/decorators/TypedParam.ts +70 -0
- package/src/decorators/TypedRoute.ts +149 -0
- package/src/decorators/internal/EncryptedConstant.ts +4 -0
- package/src/decorators/internal/get_path_and_stringify.ts +76 -0
- package/src/decorators/internal/headers_to_object.ts +13 -0
- package/src/decorators/internal/route_error.ts +41 -0
- package/src/decorators/internal/validate_request_body.ts +58 -0
- package/src/index.ts +5 -0
- package/src/module.ts +9 -0
- package/src/options/INestiaTransformOptions.ts +6 -0
- package/src/options/INestiaTransformProject.ts +6 -0
- package/src/options/IRequestBodyValidator.ts +20 -0
- package/src/options/IResponseBodyStringifier.ts +25 -0
- package/src/transform.ts +20 -0
- package/src/transformers/BodyTransformer.ts +106 -0
- package/src/transformers/FileTransformer.ts +49 -0
- package/src/transformers/MethodTransformer.ts +91 -0
- package/src/transformers/NodeTransformer.ts +18 -0
- package/src/transformers/ParameterTransformer.ts +45 -0
- package/src/transformers/RouteTransformer.ts +134 -0
- package/src/typings/Creator.ts +3 -0
- package/src/utils/ExceptionManager.ts +126 -0
- package/src/utils/Singleton.ts +20 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExceptionManager.js","sourceRoot":"","sources":["../../src/utils/ExceptionManager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,2CAA4C;AAC5C,yCAA+C;AAC/C,+BAAuC;AAIvC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,IAAiB,gBAAgB,CAsEhC;AAtED,WAAiB,gBAAgB;IAC7B;;;;;;;OAOG;IACH,SAAgB,MAAM,CAClB,OAAmB,EACnB,OAAmB;QAEnB,IAAM,KAAK,GAAW,iBAAA,MAAM,CAAC,SAAS,CAAC,UAAC,KAAK,IAAK,OAAA,KAAK,CAAC,CAAC,CAAC,KAAK,OAAO,EAApB,CAAoB,CAAC,CAAC;QACxE,IAAI,KAAK,KAAK,CAAC,CAAC;YAAE,iBAAA,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QAE1C,iBAAA,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;QAChC,iBAAA,MAAM,CAAC,IAAI,CAAC,UAAC,EAAG,EAAE,EAAG;gBAAR,KAAA,aAAG,EAAF,CAAC,QAAA;gBAAG,KAAA,aAAG,EAAF,CAAC,QAAA;YAAM,OAAA,CAAC,CAAC,CAAC,SAAS,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAAnC,CAAmC,CAAC,CAAC;IACnE,CAAC;IATe,uBAAM,SASrB,CAAA;IAED;;;;;OAKG;IACH,SAAgB,KAAK,CAAkB,OAAmB;QACtD,IAAM,KAAK,GAAW,iBAAA,MAAM,CAAC,SAAS,CAAC,UAAC,KAAK,IAAK,OAAA,KAAK,CAAC,CAAC,CAAC,KAAK,OAAO,EAApB,CAAoB,CAAC,CAAC;QACxE,IAAI,KAAK,KAAK,CAAC,CAAC;YAAE,OAAO,KAAK,CAAC;QAE/B,iBAAA,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QACxB,OAAO,IAAI,CAAC;IAChB,CAAC;IANe,sBAAK,QAMpB,CAAA;IAED,SAAgB,EAAE,CAAC,OAA4B;QAC3C,iBAAA,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;IAFe,mBAAE,KAEjB,CAAA;IAED,SAAgB,GAAG,CAAC,OAA4B;QAC5C,iBAAA,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;IAFe,oBAAG,MAElB,CAAA;IAqBD;;OAEG;IACU,uBAAM,GAAwC,EAAE,CAAC;IAE9D;;OAEG;IACU,0BAAS,GAA6B,IAAI,GAAG,EAAE,CAAC;AACjE,CAAC,EAtEgB,gBAAgB,GAAhB,wBAAgB,KAAhB,wBAAgB,QAsEhC;AAED,gBAAgB,CAAC,MAAM,CACnB,sBAAc,EACd,UAAC,KAAK;IACF,OAAA,IAAI,sBAAa,CACb;QACI,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,MAAM,EAAE,KAAK,CAAC,OAAO;QACrB,OAAO,EAAE,qDAAqD;KACjE,EACD,GAAG,CACN;AAPD,CAOC,CACR,CAAC;AAEF,gBAAgB,CAAC,MAAM,CACnB,mBAAS,EACT,UAAC,KAAK;IACF,OAAA,IAAI,sBAAa,CACb;QACI,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,OAAO,EAAE,KAAK,CAAC,OAAO;KACzB,EACD,KAAK,CAAC,MAAM,CACf;AAND,CAMC,CACR,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Singleton = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
var Singleton = /** @class */ (function () {
|
|
8
|
+
function Singleton(closure_) {
|
|
9
|
+
this.closure_ = closure_;
|
|
10
|
+
this.value_ = NOT_MOUNTED_YET;
|
|
11
|
+
}
|
|
12
|
+
Singleton.prototype.get = function () {
|
|
13
|
+
if (this.value_ === NOT_MOUNTED_YET)
|
|
14
|
+
this.value_ = this.closure_();
|
|
15
|
+
return this.value_;
|
|
16
|
+
};
|
|
17
|
+
return Singleton;
|
|
18
|
+
}());
|
|
19
|
+
exports.Singleton = Singleton;
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
var NOT_MOUNTED_YET = {};
|
|
24
|
+
//# sourceMappingURL=Singleton.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Singleton.js","sourceRoot":"","sources":["../../src/utils/Singleton.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH;IAGI,mBAAoC,QAAiB;QAAjB,aAAQ,GAAR,QAAQ,CAAS;QACjD,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC;IAClC,CAAC;IAEM,uBAAG,GAAV;QACI,IAAI,IAAI,CAAC,MAAM,KAAK,eAAe;YAAE,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;QACnE,OAAO,IAAI,CAAC,MAAW,CAAC;IAC5B,CAAC;IACL,gBAAC;AAAD,CAAC,AAXD,IAWC;AAXY,8BAAS;AAatB;;GAEG;AACH,IAAM,eAAe,GAAG,EAAE,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nestia/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Super-fast validation decorators of NestJS",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"typings": "lib/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "rimraf lib && tsc",
|
|
9
|
+
"build:test": "rimraf bin && ttsc -p test/tsconfig.json",
|
|
10
|
+
"dev": "npm run build -- --watch",
|
|
11
|
+
"eslint": "eslint ./**/*.ts",
|
|
12
|
+
"eslint:fix": "eslint ./**/*.ts --fix",
|
|
13
|
+
"prettier": "prettier ./**/*.ts --write",
|
|
14
|
+
"test": "node bin/test"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/samchon/nestia"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"nestjs",
|
|
22
|
+
"nestia",
|
|
23
|
+
"typia",
|
|
24
|
+
"validator",
|
|
25
|
+
"decorator",
|
|
26
|
+
"class-validator",
|
|
27
|
+
"class-transformer"
|
|
28
|
+
],
|
|
29
|
+
"author": "Jeongho Nam",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/samchon/nestia/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/samchon/nestia",
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@trivago/prettier-plugin-sort-imports": "^4.0.0",
|
|
37
|
+
"@types/express": "^4.17.15",
|
|
38
|
+
"@typescript-eslint/eslint-plugin": "^5.46.1",
|
|
39
|
+
"@typescript-eslint/parser": "^5.46.1",
|
|
40
|
+
"eslint-plugin-deprecation": "^1.3.3",
|
|
41
|
+
"git-last-commit": "^1.0.1",
|
|
42
|
+
"rimraf": "^3.0.2",
|
|
43
|
+
"ts-node": "^10.9.1",
|
|
44
|
+
"tstl": "^2.5.13",
|
|
45
|
+
"ttypescript": "^1.5.15",
|
|
46
|
+
"typescript": "^4.9.4"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@nestia/fetcher": "^0.1.0",
|
|
50
|
+
"@nestjs/common": "*",
|
|
51
|
+
"@nestjs/core": "*",
|
|
52
|
+
"@nestjs/platform-express": "*",
|
|
53
|
+
"detect-ts-node": "^1.0.5",
|
|
54
|
+
"raw-body": "*",
|
|
55
|
+
"reflect-metadata": "*",
|
|
56
|
+
"rxjs": "*",
|
|
57
|
+
"typia": "^3.4.7"
|
|
58
|
+
},
|
|
59
|
+
"files": [
|
|
60
|
+
"README.md",
|
|
61
|
+
"LICENSE",
|
|
62
|
+
"package.json",
|
|
63
|
+
"lib",
|
|
64
|
+
"src"
|
|
65
|
+
]
|
|
66
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { AesPkcs5, IEncryptionPassword } from "@nestia/fetcher";
|
|
2
|
+
import {
|
|
3
|
+
BadRequestException,
|
|
4
|
+
ExecutionContext,
|
|
5
|
+
createParamDecorator,
|
|
6
|
+
} from "@nestjs/common";
|
|
7
|
+
import type express from "express";
|
|
8
|
+
import raw from "raw-body";
|
|
9
|
+
import { assert, is, validate } from "typia";
|
|
10
|
+
|
|
11
|
+
import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
|
|
12
|
+
import { Singleton } from "../utils/Singleton";
|
|
13
|
+
import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
|
|
14
|
+
import { headers_to_object } from "./internal/headers_to_object";
|
|
15
|
+
import { validate_request_body } from "./internal/validate_request_body";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Encrypted body decorator.
|
|
19
|
+
*
|
|
20
|
+
* `EncryptedBody` is a decorator function getting JSON data from HTTP request who've
|
|
21
|
+
* been encrypted by AES-128/256 algorithm. Also, `EncyrptedBody` validates the JSON
|
|
22
|
+
* data type through
|
|
23
|
+
* [`typia.assert()`](https://github.com/samchon/typia#runtime-type-checkers)
|
|
24
|
+
* function and throws `BadRequestException` error (status code: 400), if the JSON
|
|
25
|
+
* data is not following the promised type.
|
|
26
|
+
*
|
|
27
|
+
* For reference, `EncryptedRoute` decrypts request body usnig those options.
|
|
28
|
+
*
|
|
29
|
+
* - AES-128/256
|
|
30
|
+
* - CBC mode
|
|
31
|
+
* - PKCS #5 Padding
|
|
32
|
+
* - Base64 Encoding
|
|
33
|
+
*
|
|
34
|
+
* @return Parameter decorator
|
|
35
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
36
|
+
*/
|
|
37
|
+
export function EncryptedBody<T>(validator: IRequestBodyValidator<T>) {
|
|
38
|
+
const checker = validate_request_body("EncryptedBody")(validator);
|
|
39
|
+
return createParamDecorator(async function EncryptedBody(
|
|
40
|
+
_unknown: any,
|
|
41
|
+
ctx: ExecutionContext,
|
|
42
|
+
) {
|
|
43
|
+
const request: express.Request = ctx.switchToHttp().getRequest();
|
|
44
|
+
if (request.readable === false)
|
|
45
|
+
throw new BadRequestException(
|
|
46
|
+
"Request body is not the text/plain.",
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const param:
|
|
50
|
+
| IEncryptionPassword
|
|
51
|
+
| IEncryptionPassword.Closure
|
|
52
|
+
| undefined = Reflect.getMetadata(
|
|
53
|
+
ENCRYPTION_METADATA_KEY,
|
|
54
|
+
ctx.getClass(),
|
|
55
|
+
);
|
|
56
|
+
if (!param)
|
|
57
|
+
throw new Error(
|
|
58
|
+
"Error on EncryptedBody(): no encryption password is given.",
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// GET BODY DATA
|
|
62
|
+
const headers: Singleton<Record<string, string>> = new Singleton(() =>
|
|
63
|
+
headers_to_object(request.headers),
|
|
64
|
+
);
|
|
65
|
+
const body: string = (await raw(request, "utf8")).trim();
|
|
66
|
+
const password: IEncryptionPassword =
|
|
67
|
+
typeof param === "function"
|
|
68
|
+
? param({ headers: headers.get(), body }, false)
|
|
69
|
+
: param;
|
|
70
|
+
const disabled: boolean =
|
|
71
|
+
password.disabled === undefined
|
|
72
|
+
? false
|
|
73
|
+
: typeof password.disabled === "function"
|
|
74
|
+
? password.disabled({ headers: headers.get(), body }, true)
|
|
75
|
+
: password.disabled;
|
|
76
|
+
|
|
77
|
+
// PARSE AND VALIDATE DATA
|
|
78
|
+
const data: any = JSON.parse(
|
|
79
|
+
disabled ? body : decrypt(body, password.key, password.iv),
|
|
80
|
+
);
|
|
81
|
+
checker(data);
|
|
82
|
+
return data;
|
|
83
|
+
})();
|
|
84
|
+
}
|
|
85
|
+
Object.assign(EncryptedBody, assert);
|
|
86
|
+
Object.assign(EncryptedBody, is);
|
|
87
|
+
Object.assign(EncryptedBody, validate);
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @internal
|
|
91
|
+
*/
|
|
92
|
+
function decrypt(body: string, key: string, iv: string): string {
|
|
93
|
+
try {
|
|
94
|
+
return AesPkcs5.decrypt(body, key, iv);
|
|
95
|
+
} catch (exp) {
|
|
96
|
+
if (exp instanceof Error)
|
|
97
|
+
throw new BadRequestException(
|
|
98
|
+
"Failed to decrypt the request body. Check your body content or encryption password.",
|
|
99
|
+
);
|
|
100
|
+
else throw exp;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { IEncryptionPassword } from "@nestia/fetcher";
|
|
2
|
+
import { Controller } from "@nestjs/common";
|
|
3
|
+
|
|
4
|
+
import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Encrypted controller.
|
|
8
|
+
*
|
|
9
|
+
* `EncryptedController` is an extension of the {@link nest.Controller} class decorator
|
|
10
|
+
* function who configures encryption password of the AES-128/256 algorithm. The
|
|
11
|
+
* encryption algorithm and password would be used by {@link EncryptedRoute} and
|
|
12
|
+
* {@link EncryptedBody} to encrypt the request and response body of the HTTP protocol.
|
|
13
|
+
*
|
|
14
|
+
* > However, if you've configure the {@link IEncryptionPassword.disabled} to be `true`,
|
|
15
|
+
* > you can disable the encryption and decryption algorithm. Therefore, when the
|
|
16
|
+
* > {@link IEncryptionPassword.disable} becomes the `true`, content like request and
|
|
17
|
+
* > response body would be considered as a plain text instead.
|
|
18
|
+
*
|
|
19
|
+
* By the way, you can configure the encryption password in the global level by using
|
|
20
|
+
* {@link EncryptedModule} instead of the {@link nest.Module} in the module level. In
|
|
21
|
+
* that case, you don't need to use this `EncryptedController` more. Just use the
|
|
22
|
+
* {@link nest.Controller} without duplicated encryption password definitions.
|
|
23
|
+
*
|
|
24
|
+
* Of course, if you want to use different encryption password from the
|
|
25
|
+
* {@link EncryptedModule}, this `EncryptedController` would be useful again. Therefore,
|
|
26
|
+
* I recommend to use this `EncryptedController` decorator function only when you must
|
|
27
|
+
* configure different encryption password from the {@link EncryptedModule}.
|
|
28
|
+
*
|
|
29
|
+
* @param path Path of the HTTP request
|
|
30
|
+
* @param password Encryption password or its getter function
|
|
31
|
+
* @returns Class decorator
|
|
32
|
+
*
|
|
33
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
34
|
+
*/
|
|
35
|
+
export function EncryptedController(
|
|
36
|
+
path: string,
|
|
37
|
+
password: IEncryptionPassword | IEncryptionPassword.Closure,
|
|
38
|
+
): ClassDecorator {
|
|
39
|
+
return function (target: any) {
|
|
40
|
+
Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, target);
|
|
41
|
+
Controller(path)(target);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
|
|
2
|
+
import { Module, ModuleMetadata } from "@nestjs/common";
|
|
3
|
+
import is_ts_node from "detect-ts-node";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
|
|
6
|
+
import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Encrypted module.
|
|
10
|
+
*
|
|
11
|
+
* `EncryptedModule` is an extension of the {@link Module} class decorator function
|
|
12
|
+
* who configures encryption password of the AES-128/256 algorithm. The encryption
|
|
13
|
+
* algorithm and password would be used by {@link EncryptedRoute} and {@link EncryptedBody}
|
|
14
|
+
* to encrypt the request and response bod of the HTTP protocol.
|
|
15
|
+
*
|
|
16
|
+
* By using this `EncryptedModule` decorator function, all of the
|
|
17
|
+
* {@link Controller controllers} configured in the *metadata* would be automatically
|
|
18
|
+
* changed to the {@link EncryptedController} with the *password*. If there're some
|
|
19
|
+
* original {@link EncryptedController} decorated classes in the *metadata*, their
|
|
20
|
+
* encryption password would be kept.
|
|
21
|
+
*
|
|
22
|
+
* Therefore, if you're planning to place original {@link EncryptedController} decorated
|
|
23
|
+
* classes in the *metadata*, I hope them to have different encryption password from the
|
|
24
|
+
* module level. If not, I recommend you use the {@link Controller} decorator
|
|
25
|
+
* function instead.
|
|
26
|
+
*
|
|
27
|
+
* In addition, the `EncryptedModule` supports a convenient dynamic controller importing
|
|
28
|
+
* function, {@link EncryptedModule.dynamic}. If you utilize the function with directory
|
|
29
|
+
* path of the controller classes, it imports and configures the controller classes into
|
|
30
|
+
* the `Module`, automatically.
|
|
31
|
+
*
|
|
32
|
+
* @param metadata Module configuration metadata
|
|
33
|
+
* @param password Encryption password or its getter function
|
|
34
|
+
* @returns Class decorator
|
|
35
|
+
*
|
|
36
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
37
|
+
*/
|
|
38
|
+
export function EncryptedModule(
|
|
39
|
+
metadata: ModuleMetadata,
|
|
40
|
+
password: IEncryptionPassword | IEncryptionPassword.Closure,
|
|
41
|
+
): ClassDecorator {
|
|
42
|
+
return function (target: any) {
|
|
43
|
+
Module(metadata)(target);
|
|
44
|
+
if (metadata.controllers === undefined) return;
|
|
45
|
+
|
|
46
|
+
for (const controller of metadata.controllers)
|
|
47
|
+
if (
|
|
48
|
+
Reflect.hasMetadata(ENCRYPTION_METADATA_KEY, controller) ===
|
|
49
|
+
false
|
|
50
|
+
)
|
|
51
|
+
Reflect.defineMetadata(
|
|
52
|
+
ENCRYPTION_METADATA_KEY,
|
|
53
|
+
password,
|
|
54
|
+
controller,
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export namespace EncryptedModule {
|
|
60
|
+
/**
|
|
61
|
+
* Dynamic encrypted module.
|
|
62
|
+
*
|
|
63
|
+
* `EncryptedModule.dynamic` is an extension of the {@link EncryptedModule} function
|
|
64
|
+
* who configures controller classes by the dynamic importing. By specifying directory
|
|
65
|
+
* path of the controller classes, those controllers would be automatically imported
|
|
66
|
+
* and configured.
|
|
67
|
+
*
|
|
68
|
+
* @param path Directory path of the controller classes
|
|
69
|
+
* @param password Encryption password or its getter function
|
|
70
|
+
* @returns Class decorated module instance
|
|
71
|
+
*/
|
|
72
|
+
export async function dynamic(
|
|
73
|
+
path: string,
|
|
74
|
+
password: IEncryptionPassword | IEncryptionPassword.Closure,
|
|
75
|
+
): Promise<object> {
|
|
76
|
+
// LOAD CONTROLLERS
|
|
77
|
+
const metadata: ModuleMetadata = {
|
|
78
|
+
controllers: await controllers(path, password),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// RETURNS WITH DECORATING
|
|
82
|
+
@EncryptedModule(metadata, password)
|
|
83
|
+
class Module {}
|
|
84
|
+
return Module;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function controllers(
|
|
88
|
+
path: string,
|
|
89
|
+
password: IEncryptionPassword | IEncryptionPassword.Closure,
|
|
90
|
+
): Promise<any[]> {
|
|
91
|
+
const output: any[] = [];
|
|
92
|
+
await iterate(output, path);
|
|
93
|
+
|
|
94
|
+
for (const controller of output)
|
|
95
|
+
if (
|
|
96
|
+
Reflect.hasMetadata(ENCRYPTION_METADATA_KEY, controller) ===
|
|
97
|
+
false
|
|
98
|
+
)
|
|
99
|
+
Reflect.defineMetadata(
|
|
100
|
+
ENCRYPTION_METADATA_KEY,
|
|
101
|
+
password,
|
|
102
|
+
controller,
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
return output;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function iterate(controllers: object[], path: string): Promise<void> {
|
|
109
|
+
const directory: string[] = await fs.promises.readdir(path);
|
|
110
|
+
for (const file of directory) {
|
|
111
|
+
const current: string = `${path}/${file}`;
|
|
112
|
+
const stats: fs.Stats = await fs.promises.lstat(current);
|
|
113
|
+
|
|
114
|
+
if (stats.isDirectory() === true)
|
|
115
|
+
await iterate(controllers, current);
|
|
116
|
+
else if (file.substring(file.length - 3) === `.${EXTENSION}`) {
|
|
117
|
+
const external: any = await import(current);
|
|
118
|
+
for (const key in external) {
|
|
119
|
+
const instance: object = external[key];
|
|
120
|
+
if (Reflect.getMetadata("path", instance) !== undefined)
|
|
121
|
+
controllers.push(instance);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const EXTENSION = is_ts_node ? "ts" : "js";
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { AesPkcs5, IEncryptionPassword } from "@nestia/fetcher";
|
|
2
|
+
import {
|
|
3
|
+
CallHandler,
|
|
4
|
+
Delete,
|
|
5
|
+
ExecutionContext,
|
|
6
|
+
Get,
|
|
7
|
+
NestInterceptor,
|
|
8
|
+
Patch,
|
|
9
|
+
Post,
|
|
10
|
+
Put,
|
|
11
|
+
UseInterceptors,
|
|
12
|
+
applyDecorators,
|
|
13
|
+
} from "@nestjs/common";
|
|
14
|
+
import { HttpArgumentsHost } from "@nestjs/common/interfaces";
|
|
15
|
+
import express from "express";
|
|
16
|
+
import { Observable, catchError, map } from "rxjs";
|
|
17
|
+
import {
|
|
18
|
+
assertStringify,
|
|
19
|
+
isStringify,
|
|
20
|
+
stringify,
|
|
21
|
+
validateStringify,
|
|
22
|
+
} from "typia";
|
|
23
|
+
|
|
24
|
+
import { Singleton } from "../utils/Singleton";
|
|
25
|
+
import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
|
|
26
|
+
import { get_path_and_stringify } from "./internal/get_path_and_stringify";
|
|
27
|
+
import { headers_to_object } from "./internal/headers_to_object";
|
|
28
|
+
import { route_error } from "./internal/route_error";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Encrypted router decorator functions.
|
|
32
|
+
*
|
|
33
|
+
* `EncryptedRoute` is a module containing router decorator functions which encrypts
|
|
34
|
+
* response body data through AES-128/250 encryption. Also, those decorator functions
|
|
35
|
+
* can boost up JSON string conversion speed about 5x times faster, through
|
|
36
|
+
* [`typia.stringify()`](https://github.com/samchon/typia#fastest-json-string-conversion).
|
|
37
|
+
*
|
|
38
|
+
* For reference, `EncryptedRoute` encrypts response body usnig those options.
|
|
39
|
+
*
|
|
40
|
+
* - AES-128/256
|
|
41
|
+
* - CBC mode
|
|
42
|
+
* - PKCS #5 Padding
|
|
43
|
+
* - Base64 Encoding
|
|
44
|
+
*
|
|
45
|
+
* Also, router functions in `EncryptedRoute` can convert custom error classes to the
|
|
46
|
+
* regular {@link nest.HttpException} class automatically, through
|
|
47
|
+
* {@link ExceptionManager}.
|
|
48
|
+
*
|
|
49
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
50
|
+
*/
|
|
51
|
+
export namespace EncryptedRoute {
|
|
52
|
+
/**
|
|
53
|
+
* Encrypted router decorator function for the GET method.
|
|
54
|
+
*
|
|
55
|
+
* @param paths Path(s) of the HTTP request
|
|
56
|
+
* @returns Method decorator
|
|
57
|
+
*/
|
|
58
|
+
export const Get = Generator("Get");
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Encrypted router decorator function for the GET method.
|
|
62
|
+
*
|
|
63
|
+
* @param paths Path(s) of the HTTP request
|
|
64
|
+
* @returns Method decorator
|
|
65
|
+
*/
|
|
66
|
+
export const Post = Generator("Post");
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Encrypted router decorator function for the PATCH method.
|
|
70
|
+
*
|
|
71
|
+
* @param path Path of the HTTP request
|
|
72
|
+
* @returns Method decorator
|
|
73
|
+
*/
|
|
74
|
+
export const Patch = Generator("Patch");
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Encrypted router decorator function for the PUT method.
|
|
78
|
+
*
|
|
79
|
+
* @param path Path of the HTTP request
|
|
80
|
+
* @returns Method decorator
|
|
81
|
+
*/
|
|
82
|
+
export const Put = Generator("Put");
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Encrypted router decorator function for the DELETE method.
|
|
86
|
+
*
|
|
87
|
+
* @param path Path of the HTTP request
|
|
88
|
+
* @returns Method decorator
|
|
89
|
+
*/
|
|
90
|
+
export const Delete = Generator("Delete");
|
|
91
|
+
|
|
92
|
+
function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
|
|
93
|
+
function route(path?: string | string[]): MethodDecorator;
|
|
94
|
+
function route(stringify?: (input: any) => string): MethodDecorator;
|
|
95
|
+
function route(
|
|
96
|
+
path: string | string[],
|
|
97
|
+
stringify: (input: any) => string,
|
|
98
|
+
): MethodDecorator;
|
|
99
|
+
|
|
100
|
+
function route(...args: any[]): MethodDecorator {
|
|
101
|
+
const [path, stringify] = get_path_and_stringify(
|
|
102
|
+
`EncryptedRoute.${method}`,
|
|
103
|
+
)(...args);
|
|
104
|
+
return applyDecorators(
|
|
105
|
+
ROUTERS[method](path),
|
|
106
|
+
UseInterceptors(
|
|
107
|
+
new EncryptedRouteInterceptor(method, stringify),
|
|
108
|
+
),
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
return route;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (const method of [
|
|
116
|
+
assertStringify,
|
|
117
|
+
isStringify,
|
|
118
|
+
stringify,
|
|
119
|
+
validateStringify,
|
|
120
|
+
]) {
|
|
121
|
+
Object.assign(EncryptedRoute.Get, method);
|
|
122
|
+
Object.assign(EncryptedRoute.Delete, method);
|
|
123
|
+
Object.assign(EncryptedRoute.Post, method);
|
|
124
|
+
Object.assign(EncryptedRoute.Put, method);
|
|
125
|
+
Object.assign(EncryptedRoute.Patch, method);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @internal
|
|
130
|
+
*/
|
|
131
|
+
class EncryptedRouteInterceptor implements NestInterceptor {
|
|
132
|
+
public constructor(
|
|
133
|
+
private readonly method: string,
|
|
134
|
+
private readonly stringify: (input: any) => string,
|
|
135
|
+
) {}
|
|
136
|
+
|
|
137
|
+
public intercept(
|
|
138
|
+
context: ExecutionContext,
|
|
139
|
+
next: CallHandler,
|
|
140
|
+
): Observable<any> {
|
|
141
|
+
const http: HttpArgumentsHost = context.switchToHttp();
|
|
142
|
+
return next.handle().pipe(
|
|
143
|
+
map((value) => {
|
|
144
|
+
const param:
|
|
145
|
+
| IEncryptionPassword
|
|
146
|
+
| IEncryptionPassword.Closure
|
|
147
|
+
| undefined = Reflect.getMetadata(
|
|
148
|
+
ENCRYPTION_METADATA_KEY,
|
|
149
|
+
context.getClass(),
|
|
150
|
+
);
|
|
151
|
+
if (!param)
|
|
152
|
+
throw new Error(
|
|
153
|
+
`Error on EncryptedRoute.${this.method}(): no encryption password is given.`,
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const headers: Singleton<Record<string, string>> =
|
|
157
|
+
new Singleton(() => {
|
|
158
|
+
const request: express.Request = http.getRequest();
|
|
159
|
+
return headers_to_object(request.headers);
|
|
160
|
+
});
|
|
161
|
+
const body: string | undefined = this.stringify(value);
|
|
162
|
+
const password: IEncryptionPassword =
|
|
163
|
+
typeof param === "function"
|
|
164
|
+
? param({ headers: headers.get(), body }, false)
|
|
165
|
+
: param;
|
|
166
|
+
const disabled: boolean =
|
|
167
|
+
password.disabled === undefined
|
|
168
|
+
? false
|
|
169
|
+
: typeof password.disabled === "function"
|
|
170
|
+
? password.disabled(
|
|
171
|
+
{ headers: headers.get(), body },
|
|
172
|
+
false,
|
|
173
|
+
)
|
|
174
|
+
: password.disabled;
|
|
175
|
+
|
|
176
|
+
const response: express.Response = http.getResponse();
|
|
177
|
+
response.header(
|
|
178
|
+
"Content-Type",
|
|
179
|
+
disabled ? "application/json" : "text/plain",
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
if (disabled === true) return body;
|
|
183
|
+
else if (body === undefined) return body;
|
|
184
|
+
return AesPkcs5.encrypt(body, password.key, password.iv);
|
|
185
|
+
}),
|
|
186
|
+
catchError((err) => route_error(http.getRequest(), err)),
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* @internal
|
|
193
|
+
*/
|
|
194
|
+
const ROUTERS = {
|
|
195
|
+
Get,
|
|
196
|
+
Post,
|
|
197
|
+
Put,
|
|
198
|
+
Patch,
|
|
199
|
+
Delete,
|
|
200
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BadRequestException,
|
|
3
|
+
ExecutionContext,
|
|
4
|
+
createParamDecorator,
|
|
5
|
+
} from "@nestjs/common";
|
|
6
|
+
import type express from "express";
|
|
7
|
+
import raw from "raw-body";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Plain body decorator.
|
|
11
|
+
*
|
|
12
|
+
* `PlainBody` is a decorator function getting full body text from the HTTP request.
|
|
13
|
+
*
|
|
14
|
+
* If you adjust the regular {@link Body} decorator function to the body parameter,
|
|
15
|
+
* you can't get the full body text because the {@link Body} tries to convert the
|
|
16
|
+
* body text to JSON object. Therefore, `nestia-helper` provides this `PlainBody`
|
|
17
|
+
* decorator function to get the full body text.
|
|
18
|
+
*
|
|
19
|
+
* ```typescript
|
|
20
|
+
* \@TypedRoute.Post("memo")
|
|
21
|
+
* public store
|
|
22
|
+
* (
|
|
23
|
+
* \@PlainBody() body: string
|
|
24
|
+
* ): void;
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @return Parameter decorator
|
|
28
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
29
|
+
*/
|
|
30
|
+
export const PlainBody: () => ParameterDecorator = createParamDecorator(
|
|
31
|
+
async function PlainBody(_data: any, context: ExecutionContext) {
|
|
32
|
+
const request: express.Request = context.switchToHttp().getRequest();
|
|
33
|
+
if (!request.readable) throw new BadRequestException("Invalid body");
|
|
34
|
+
|
|
35
|
+
const body: string = (await raw(request)).toString("utf8").trim();
|
|
36
|
+
return body;
|
|
37
|
+
},
|
|
38
|
+
);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BadRequestException,
|
|
3
|
+
ExecutionContext,
|
|
4
|
+
createParamDecorator,
|
|
5
|
+
} from "@nestjs/common";
|
|
6
|
+
import type express from "express";
|
|
7
|
+
import raw from "raw-body";
|
|
8
|
+
import { assert, is, validate } from "typia";
|
|
9
|
+
|
|
10
|
+
import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
|
|
11
|
+
import { validate_request_body } from "./internal/validate_request_body";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Safe body decorator.
|
|
15
|
+
*
|
|
16
|
+
* `TypedBody` is a decorator function getting JSON data from HTTP request. Also,
|
|
17
|
+
* it validates the JSON data type through
|
|
18
|
+
* [`typia.assert()`](https://github.com/samchon/typia#runtime-type-checkers)
|
|
19
|
+
* function and throws `BadRequestException` error (status code: 400), if the JSON
|
|
20
|
+
* data is not following the promised type.
|
|
21
|
+
*
|
|
22
|
+
* @param validator Custom validator if required. Default is `typia.assert()`
|
|
23
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
24
|
+
*/
|
|
25
|
+
export function TypedBody<T>(
|
|
26
|
+
validator?: IRequestBodyValidator<T>,
|
|
27
|
+
): ParameterDecorator {
|
|
28
|
+
const checker = validate_request_body("TypedBody")(validator);
|
|
29
|
+
return createParamDecorator(async function TypedBody(
|
|
30
|
+
_unknown: any,
|
|
31
|
+
context: ExecutionContext,
|
|
32
|
+
) {
|
|
33
|
+
const request: express.Request = context.switchToHttp().getRequest();
|
|
34
|
+
if (request.headers["content-type"] !== "application/json") {
|
|
35
|
+
throw new BadRequestException(
|
|
36
|
+
"Request body is not the application/json.",
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const data: any = request.body
|
|
40
|
+
? request.body
|
|
41
|
+
: JSON.parse((await raw(request, "utf8")).trim());
|
|
42
|
+
checker(data);
|
|
43
|
+
return data;
|
|
44
|
+
})();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
Object.assign(TypedBody, assert);
|
|
48
|
+
Object.assign(TypedBody, is);
|
|
49
|
+
Object.assign(TypedBody, validate);
|