c2-http 1.0.143 → 1.0.145
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.
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from "express";
|
|
2
|
+
declare class MiddlewareCheckTokenFlow {
|
|
3
|
+
execute(): Promise<(request: Request, response: Response, next: NextFunction) => Promise<void>>;
|
|
4
|
+
config: (request: Request, response: Response, next: NextFunction) => Promise<void>;
|
|
5
|
+
checkToken(): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
declare const _default: MiddlewareCheckTokenFlow;
|
|
8
|
+
export default _default;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const http_status_1 = require("http-status");
|
|
7
|
+
const __1 = require("../..");
|
|
8
|
+
const AddInContextFlowItem_1 = __importDefault(require("./item/AddInContextFlowItem"));
|
|
9
|
+
const GetTokenFlowItem_1 = __importDefault(require("./item/GetTokenFlowItem"));
|
|
10
|
+
const ValidateTokenFlowItem_1 = __importDefault(require("./item/ValidateTokenFlowItem"));
|
|
11
|
+
const c2_util_1 = require("c2-util");
|
|
12
|
+
class MiddlewareCheckTokenFlow {
|
|
13
|
+
async execute() {
|
|
14
|
+
return async (request, response, next) => {
|
|
15
|
+
(0, c2_util_1.log)("LOG_CHECK_TOKEN", "Checking token");
|
|
16
|
+
try {
|
|
17
|
+
if ((request.path === "/metrics" ||
|
|
18
|
+
request.path.startsWith("/metrics") ||
|
|
19
|
+
request.path?.includes("/docs"))
|
|
20
|
+
&& request.method === "GET") {
|
|
21
|
+
next();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (request.headers["x-api-key"]) {
|
|
25
|
+
next();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
await this.checkToken();
|
|
29
|
+
(0, c2_util_1.log)("LOG_CHECK_TOKEN", "Token checked");
|
|
30
|
+
return next();
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
(0, c2_util_1.log)("LOG_ERROR_CHECK_TOKEN", error);
|
|
34
|
+
response
|
|
35
|
+
.status(error.status || http_status_1.INTERNAL_SERVER_ERROR)
|
|
36
|
+
.json({ message: error.message, detail: error.detail?.data });
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
config = async (request, response, next) => {
|
|
41
|
+
(0, c2_util_1.log)("LOG_CHECK_TOKEN", "Checking token");
|
|
42
|
+
try {
|
|
43
|
+
if ((request.path === "/metrics" ||
|
|
44
|
+
request.path.startsWith("/metrics") ||
|
|
45
|
+
request.path?.includes("/docs"))
|
|
46
|
+
&& request.method === "GET") {
|
|
47
|
+
next();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (request.headers["x-api-key"]) {
|
|
51
|
+
next();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
await this.checkToken();
|
|
55
|
+
(0, c2_util_1.log)("LOG_CHECK_TOKEN", "Token checked");
|
|
56
|
+
return next();
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
(0, c2_util_1.log)("LOG_ERROR_CHECK_TOKEN", error);
|
|
60
|
+
response
|
|
61
|
+
.status(error.status || http_status_1.INTERNAL_SERVER_ERROR)
|
|
62
|
+
.json({ message: error.message, detail: error.detail?.data });
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
async checkToken() {
|
|
66
|
+
let token = "";
|
|
67
|
+
try {
|
|
68
|
+
token = GetTokenFlowItem_1.default.get();
|
|
69
|
+
const tokenDecoded = await ValidateTokenFlowItem_1.default.exec(token);
|
|
70
|
+
AddInContextFlowItem_1.default.exec(tokenDecoded, token);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
(0, c2_util_1.log)("LOG_ERROR_CHECK_TOKEN_CHECK_TOKEN", error);
|
|
74
|
+
throw (0, __1.convertErrorToHttpError)(error);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.default = new MiddlewareCheckTokenFlow;
|
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const express_http_context_1 = __importDefault(require("express-http-context"));
|
|
7
|
+
const c2_util_1 = require("c2-util");
|
|
7
8
|
class AddInContextFlowItem {
|
|
8
9
|
exec(decode, token) {
|
|
9
10
|
express_http_context_1.default.set("token", token);
|
|
@@ -16,6 +17,17 @@ class AddInContextFlowItem {
|
|
|
16
17
|
express_http_context_1.default.set("accountBlockDate", decode.accountBlockDate);
|
|
17
18
|
express_http_context_1.default.set("accountExpireDate", decode.accountExpireDate);
|
|
18
19
|
express_http_context_1.default.set("scopes", decode.scopes);
|
|
20
|
+
(0, c2_util_1.log)("LOG_ADD_IN_CONTEXT", "Adding in context");
|
|
21
|
+
(0, c2_util_1.log)("LOG_ADD_IN_CONTEXT_TOKEN", express_http_context_1.default.get("token"));
|
|
22
|
+
(0, c2_util_1.log)("LOG_ADD_IN_CONTEXT_USER_ID", express_http_context_1.default.get("userId"));
|
|
23
|
+
(0, c2_util_1.log)("LOG_ADD_IN_CONTEXT_USER_PROFILE_ID", express_http_context_1.default.get("userProfileId"));
|
|
24
|
+
(0, c2_util_1.log)("LOG_ADD_IN_CONTEXT_USER_IS_ADMIN", express_http_context_1.default.get("userIsAdmin"));
|
|
25
|
+
(0, c2_util_1.log)("LOG_ADD_IN_CONTEXT_USER_EMAIL", express_http_context_1.default.get("userEmail"));
|
|
26
|
+
(0, c2_util_1.log)("LOG_ADD_IN_CONTEXT_USER_NAME", express_http_context_1.default.get("userName"));
|
|
27
|
+
(0, c2_util_1.log)("LOG_ADD_IN_CONTEXT_ACCOUNT_ID", express_http_context_1.default.get("accountId"));
|
|
28
|
+
(0, c2_util_1.log)("LOG_ADD_IN_CONTEXT_ACCOUNT_BLOCK_DATE", express_http_context_1.default.get("accountBlockDate"));
|
|
29
|
+
(0, c2_util_1.log)("LOG_ADD_IN_CONTEXT_ACCOUNT_EXPIRE_DATE", express_http_context_1.default.get("accountExpireDate"));
|
|
30
|
+
(0, c2_util_1.log)("LOG_ADD_IN_CONTEXT_SCOPES", express_http_context_1.default.get("scopes"));
|
|
19
31
|
}
|
|
20
32
|
}
|
|
21
33
|
exports.default = new AddInContextFlowItem;
|
package/dist/model/Server.js
CHANGED
|
@@ -9,6 +9,7 @@ const express_1 = __importDefault(require("express"));
|
|
|
9
9
|
const express_prom_bundle_1 = __importDefault(require("express-prom-bundle"));
|
|
10
10
|
const i18n_1 = require("../i18n");
|
|
11
11
|
const MiddlewareGlobals_1 = __importDefault(require("../middleware/global-middleware/MiddlewareGlobals"));
|
|
12
|
+
const MiddlewareCheckTokenFlow_1 = __importDefault(require("../middleware/jwt-middlware/MiddlewareCheckTokenFlow"));
|
|
12
13
|
class Server {
|
|
13
14
|
app;
|
|
14
15
|
config;
|
|
@@ -39,7 +40,7 @@ class Server {
|
|
|
39
40
|
// this.app.use("/docs", swaggerUI.serve, swaggerUI.setup((global as any).OPEN_API_DOC))
|
|
40
41
|
(0, c2_util_1.log)("LOG", "Rotas abertas OK");
|
|
41
42
|
//middlewares de checagem de token obrigatorio
|
|
42
|
-
|
|
43
|
+
this.app.use(MiddlewareCheckTokenFlow_1.default.config);
|
|
43
44
|
//middlewares de checagem de token opcional
|
|
44
45
|
// this.config.middlewaresCloseControllers?.forEach((middl: RequestHandler) => {
|
|
45
46
|
// this.app.use(middl)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "c2-http",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.145",
|
|
4
4
|
"description": "Biblioteca Typescript para API NodeJS",
|
|
5
5
|
"repository": "https://cabralsilva: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@github.com/cabralsilva/c2-http.git",
|
|
6
6
|
"author": "Daniel Cabral <cabralconsultoriaemsoftware@gmail.com>",
|