c2-http 1.0.143 → 1.0.144

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,7 @@
1
+ import { NextFunction, Request, Response } from "express";
2
+ declare class MiddlewareCheckTokenFlow {
3
+ execute(): Promise<(request: Request, response: Response, next: NextFunction) => Promise<void>>;
4
+ checkToken(): Promise<void>;
5
+ }
6
+ declare const _default: MiddlewareCheckTokenFlow;
7
+ export default _default;
@@ -0,0 +1,79 @@
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
+ // public config = async (request: Request, response: Response, next: NextFunction) => {
41
+ // log("LOG_CHECK_TOKEN", "Checking token");
42
+ // try {
43
+ // if (
44
+ // (
45
+ // request.path === "/metrics" ||
46
+ // request.path.startsWith("/metrics") ||
47
+ // request.path?.includes("/docs")
48
+ // )
49
+ // && request.method === "GET") {
50
+ // next();
51
+ // return;
52
+ // }
53
+ // if (request.headers["x-api-key"]) {
54
+ // next();
55
+ // return;
56
+ // }
57
+ // await this.checkToken()
58
+ // log("LOG_CHECK_TOKEN", "Token checked");
59
+ // return next()
60
+ // } catch (error: any) {
61
+ // log("LOG_ERROR_CHECK_TOKEN", error);
62
+ // response
63
+ // .status(error.status || INTERNAL_SERVER_ERROR)
64
+ // .json({ message: error.message, detail: error.detail?.data });
65
+ // }
66
+ // }
67
+ async checkToken() {
68
+ let token = "";
69
+ try {
70
+ token = GetTokenFlowItem_1.default.get();
71
+ const tokenDecoded = await ValidateTokenFlowItem_1.default.exec(token);
72
+ AddInContextFlowItem_1.default.exec(tokenDecoded, token);
73
+ }
74
+ catch (error) {
75
+ throw (0, __1.convertErrorToHttpError)(error);
76
+ }
77
+ }
78
+ }
79
+ exports.default = new MiddlewareCheckTokenFlow;
@@ -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
- // this.app.use(MiddlewareJwt.config)
43
+ this.app.use(MiddlewareCheckTokenFlow_1.default.execute);
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.143",
3
+ "version": "1.0.144",
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>",