@medi-car-bd/mcb-shared-auth 0.0.18 → 0.0.19

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.
@@ -46,6 +46,10 @@ const authProxy = (authServiceUrl, options = {}) => {
46
46
  if (typeof value === "string")
47
47
  headers[name] = value;
48
48
  }
49
+ // The real caller, so the auth service's per-IP limit does not count every
50
+ // user of this service as one client.
51
+ if (req.ip)
52
+ headers["x-forwarded-for"] = req.ip;
49
53
  let upstream;
50
54
  let text;
51
55
  try {
@@ -1 +1,22 @@
1
+ import "../config";
2
+ /**
3
+ * Per-caller request limit for a service. It counts each client address, so a
4
+ * service behind the gateway must also trust the gateway (see
5
+ * trustProxySetting), or every user counts as one caller.
6
+ *
7
+ * RATE_LIMIT_MAX and RATE_LIMIT_WINDOW_MS override the defaults. The default
8
+ * matches the gateway's per-service limit: field apps on mobile networks share
9
+ * addresses, so a low ceiling throttles real users rather than attackers.
10
+ */
1
11
  export declare const limiter: import("express-rate-limit").RateLimitRequestHandler;
12
+ /**
13
+ * The value for Express's "trust proxy": which directly connected addresses may
14
+ * state the real client through X-Forwarded-For. By default that is this
15
+ * machine and private networks, where the gateway and the other services run.
16
+ * A caller connecting from a public address is not trusted, so it cannot fake
17
+ * its address with that header.
18
+ *
19
+ * TRUST_PROXY overrides it: a number of hops, "true", "false", or a
20
+ * comma-separated list of addresses or names such as "loopback, uniquelocal".
21
+ */
22
+ export declare const trustProxySetting: (raw?: string | undefined) => string | number | boolean;
@@ -3,10 +3,52 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.limiter = void 0;
6
+ exports.trustProxySetting = exports.limiter = void 0;
7
7
  const express_rate_limit_1 = __importDefault(require("express-rate-limit"));
8
+ require("../config");
9
+ const DEFAULT_MAX = 600;
10
+ const DEFAULT_WINDOW_MS = 15 * 60 * 1000;
11
+ const positive = (raw, fallback) => {
12
+ const value = Number(raw);
13
+ return raw && Number.isFinite(value) && value > 0 ? value : fallback;
14
+ };
15
+ /**
16
+ * Per-caller request limit for a service. It counts each client address, so a
17
+ * service behind the gateway must also trust the gateway (see
18
+ * trustProxySetting), or every user counts as one caller.
19
+ *
20
+ * RATE_LIMIT_MAX and RATE_LIMIT_WINDOW_MS override the defaults. The default
21
+ * matches the gateway's per-service limit: field apps on mobile networks share
22
+ * addresses, so a low ceiling throttles real users rather than attackers.
23
+ */
8
24
  exports.limiter = (0, express_rate_limit_1.default)({
9
- max: 100, // 100 requests
10
- windowMs: 15 * 60 * 1000, // 15 minutes
11
- message: "Too many requests from this IP, please try again in an hour!", // message to send
25
+ max: positive(process.env.RATE_LIMIT_MAX, DEFAULT_MAX),
26
+ windowMs: positive(process.env.RATE_LIMIT_WINDOW_MS, DEFAULT_WINDOW_MS),
27
+ standardHeaders: true,
28
+ legacyHeaders: false,
29
+ message: {
30
+ state: "fail",
31
+ message: "Too many requests, please try again later.",
32
+ },
12
33
  });
34
+ /**
35
+ * The value for Express's "trust proxy": which directly connected addresses may
36
+ * state the real client through X-Forwarded-For. By default that is this
37
+ * machine and private networks, where the gateway and the other services run.
38
+ * A caller connecting from a public address is not trusted, so it cannot fake
39
+ * its address with that header.
40
+ *
41
+ * TRUST_PROXY overrides it: a number of hops, "true", "false", or a
42
+ * comma-separated list of addresses or names such as "loopback, uniquelocal".
43
+ */
44
+ const trustProxySetting = (raw = process.env.TRUST_PROXY) => {
45
+ const value = raw === null || raw === void 0 ? void 0 : raw.trim();
46
+ if (!value)
47
+ return "loopback, uniquelocal";
48
+ if (value === "true")
49
+ return true;
50
+ if (value === "false")
51
+ return false;
52
+ return /^\d+$/.test(value) ? Number(value) : value;
53
+ };
54
+ exports.trustProxySetting = trustProxySetting;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medi-car-bd/mcb-shared-auth",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "Shared authentication module for MCB applications",
5
5
  "packageManager": "pnpm@10.18.0",
6
6
  "main": "dist/index.js",