c2-http 1.0.95 → 1.0.97

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.
@@ -9,6 +9,10 @@ class MiddlewareContext {
9
9
  expressApplication.use(express_http_context_1.default.middleware);
10
10
  expressApplication.use((request, response, next) => {
11
11
  express_http_context_1.default.set("headers", request.headers);
12
+ express_http_context_1.default.set("requestProtocol", request.protocol);
13
+ express_http_context_1.default.set("requestHost", request.hostname);
14
+ express_http_context_1.default.set("requestOriginalUrl", request.originalUrl);
15
+ express_http_context_1.default.set("request", request);
12
16
  next();
13
17
  });
14
18
  }
@@ -27,7 +27,7 @@ class MiddlewareCheckTokenFlow {
27
27
  let token = "";
28
28
  try {
29
29
  token = GetTokenFlowItem_1.default.get();
30
- const tokenDecoded = ValidateTokenFlowItem_1.default.exec(token);
30
+ const tokenDecoded = await ValidateTokenFlowItem_1.default.exec(token);
31
31
  AddInContextFlowItem_1.default.exec(tokenDecoded);
32
32
  next();
33
33
  }
@@ -1,6 +1,6 @@
1
1
  import { IJWTPayload } from "../../../dto/IJWTPayload";
2
2
  declare class ValidateTokenFlowItem {
3
- exec(token: string): IJWTPayload;
3
+ exec(token: string): Promise<IJWTPayload>;
4
4
  }
5
5
  declare const _default: ValidateTokenFlowItem;
6
6
  export default _default;
@@ -22,15 +22,40 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
25
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
+ /* eslint-disable @typescript-eslint/no-unused-vars */
26
30
  const http_status_1 = require("http-status");
27
31
  const jwt = __importStar(require("jsonwebtoken"));
28
32
  const i18n_1 = require("../../../i18n");
29
33
  const HttpError_1 = require("../../../model/HttpError");
34
+ const express_http_context_1 = __importDefault(require("express-http-context"));
35
+ const axios_1 = __importDefault(require("axios"));
30
36
  class ValidateTokenFlowItem {
31
- exec(token) {
37
+ async exec(token) {
32
38
  try {
33
- return jwt.verify(token, process.env.JWT_SECRET ?? "");
39
+ // if the origin api has JWT_SECRET, use it to verify the token
40
+ if (process.env.JWT_SECRET) {
41
+ return jwt.verify(token, process.env.JWT_SECRET ?? "");
42
+ }
43
+ // if the origin api has API_ACCOUNT, use it to validate the token
44
+ if (process.env.API_ACCOUNT) {
45
+ const res = await axios_1.default.get(`${process.env.API_ACCOUNT}/valid-token`, { headers: { Authorization: `Bearer ${token}` } });
46
+ if (res.status !== http_status_1.OK)
47
+ throw new Error("Invalid token or expired");
48
+ return res.data;
49
+ }
50
+ // otherwise, construct the URL to validate the token based on the request context
51
+ const originUrl = `${express_http_context_1.default.get("requestProtocol")}://${express_http_context_1.default.get("requestHost")}${express_http_context_1.default.get("requestOriginalUrl")}`;
52
+ const newUrl = originUrl.replace(/\/api-([^\/]+)/, (_match, p1) => {
53
+ return "/api-account";
54
+ });
55
+ const res = await axios_1.default.get(`${newUrl}/valid-token`, { headers: { Authorization: `Bearer ${token}` } });
56
+ if (res.status !== http_status_1.OK)
57
+ throw new Error("Invalid token or expired");
58
+ return res.data;
34
59
  }
35
60
  catch (error) {
36
61
  throw new HttpError_1.HttpError(http_status_1.FORBIDDEN, (0, i18n_1.getMessage)("message.authentication.authenticateIsRequired"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-http",
3
- "version": "1.0.95",
3
+ "version": "1.0.97",
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>",
@@ -34,7 +34,6 @@
34
34
  "http-status": "^1.7.4",
35
35
  "jsonwebtoken": "^9.0.0",
36
36
  "moment": "^2.30.1",
37
- "mongoose": "^8.3.4",
38
37
  "qs": "^6.13.0",
39
38
  "swagger-autogen": "^2.23.7",
40
39
  "swagger-ui-express": "^5.0.0",