powr-sdk-api 4.22.0 → 4.22.1

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.
@@ -35,6 +35,7 @@ const {
35
35
  const jwt = require("jsonwebtoken");
36
36
  const {
37
37
  otpAppName,
38
+ otpSmsAllowed,
38
39
  tenDigitMobile,
39
40
  isTestMobile,
40
41
  generateOtpCode,
@@ -791,6 +792,12 @@ router.post("/send-otp", async (req, res) => {
791
792
  });
792
793
  }
793
794
  const app = otpAppName(req);
795
+ if (!otpSmsAllowed(app)) {
796
+ return res.status(400).json({
797
+ success: false,
798
+ message: "OTP is not available"
799
+ });
800
+ }
794
801
  try {
795
802
  const db = await getDb();
796
803
  const template = await findTemplate(db, app);
@@ -46,10 +46,24 @@ function findRoute(method, path) {
46
46
  }
47
47
  describe("auth OTP routes", () => {
48
48
  const userId = new ObjectId("507f1f77bcf86cd799439011");
49
- it("send-otp defaults to powr and never echoes the code", async () => {
49
+ it("send-otp without app is powr and does not send SMS", async () => {
50
+ const req = {
51
+ body: {
52
+ mobile: "7903658469"
53
+ },
54
+ headers: {}
55
+ };
56
+ const res = mockRes();
57
+ await findRoute("post", "/send-otp")(req, res);
58
+ expect(res.status).toHaveBeenCalledWith(400);
59
+ expect(res.json.mock.calls[0][0].message).toBe("OTP is not available");
60
+ expect(otp.findTemplate).not.toHaveBeenCalled();
61
+ expect(otp.saveOtp).not.toHaveBeenCalled();
62
+ });
63
+ it("send-otp for urbanstall never echoes the code", async () => {
50
64
  const inserted = [];
51
65
  otp.findTemplate.mockResolvedValue({
52
- app: "powr",
66
+ app: "urbanstall",
53
67
  sender_id: "SID",
54
68
  message_id: "MID"
55
69
  });
@@ -62,13 +76,14 @@ describe("auth OTP routes", () => {
62
76
  getDb.mockResolvedValue({});
63
77
  const req = {
64
78
  body: {
65
- mobile: "7903658469"
79
+ mobile: "7903658469",
80
+ app: "urbanstall"
66
81
  },
67
82
  headers: {}
68
83
  };
69
84
  const res = mockRes();
70
85
  await findRoute("post", "/send-otp")(req, res);
71
- expect(otp.findTemplate).toHaveBeenCalledWith({}, "powr");
86
+ expect(otp.findTemplate).toHaveBeenCalledWith({}, "urbanstall");
72
87
  expect(inserted[0].otp).toBe("12345");
73
88
  expect(res.status).toHaveBeenCalledWith(200);
74
89
  const body = res.json.mock.calls[0][0];
@@ -79,7 +94,7 @@ describe("auth OTP routes", () => {
79
94
  it("send-otp is 503 when the host has not injected a sender", async () => {
80
95
  otp.isTestMobile.mockReturnValue(false);
81
96
  otp.findTemplate.mockResolvedValue({
82
- app: "powr",
97
+ app: "urbanstall",
83
98
  sender_id: "SID",
84
99
  message_id: "MID"
85
100
  });
@@ -90,7 +105,8 @@ describe("auth OTP routes", () => {
90
105
  getDb.mockResolvedValue({});
91
106
  const req = {
92
107
  body: {
93
- mobile: "7903658469"
108
+ mobile: "7903658469",
109
+ app: "urbanstall"
94
110
  },
95
111
  headers: {}
96
112
  };
@@ -15,6 +15,12 @@ function otpAppName(req) {
15
15
  const raw = String(fromBody || fromHeader || "").trim();
16
16
  return raw || "powr";
17
17
  }
18
+ function otpSmsAllowed(app) {
19
+ const name = String(app || "").trim() || "powr";
20
+ if (name !== "powr") return true;
21
+ const on = String(process.env.POWR_OTP_ENABLED || "").trim().toLowerCase();
22
+ return on === "1" || on === "true" || on === "yes";
23
+ }
18
24
  function tenDigitMobile(raw) {
19
25
  const digits = String(raw || "").replace(/\D/g, "");
20
26
  if (digits.length === 10) return digits;
@@ -92,6 +98,7 @@ module.exports = {
92
98
  setSendOtpSms,
93
99
  getSendOtpSms,
94
100
  otpAppName,
101
+ otpSmsAllowed,
95
102
  tenDigitMobile,
96
103
  isTestMobile,
97
104
  generateOtpCode,
@@ -2,6 +2,7 @@
2
2
 
3
3
  const {
4
4
  otpAppName,
5
+ otpSmsAllowed,
5
6
  tenDigitMobile,
6
7
  isTestMobile,
7
8
  generateOtpCode,
@@ -31,6 +32,22 @@ describe("otpAppName", () => {
31
32
  })).toBe("urbanstall");
32
33
  });
33
34
  });
35
+ describe("otpSmsAllowed", () => {
36
+ const previous = process.env.POWR_OTP_ENABLED;
37
+ afterEach(() => {
38
+ if (previous === undefined) delete process.env.POWR_OTP_ENABLED;else process.env.POWR_OTP_ENABLED = previous;
39
+ });
40
+ it("blocks powr until Powr OTP is turned on", () => {
41
+ delete process.env.POWR_OTP_ENABLED;
42
+ expect(otpSmsAllowed("powr")).toBe(false);
43
+ expect(otpSmsAllowed("")).toBe(false);
44
+ expect(otpSmsAllowed("urbanstall")).toBe(true);
45
+ });
46
+ it("allows powr when POWR_OTP_ENABLED is set", () => {
47
+ process.env.POWR_OTP_ENABLED = "1";
48
+ expect(otpSmsAllowed("powr")).toBe(true);
49
+ });
50
+ });
34
51
  describe("tenDigitMobile", () => {
35
52
  it("normalizes +91 and 91 prefixes", () => {
36
53
  expect(tenDigitMobile("+917903658469")).toBe("7903658469");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powr-sdk-api",
3
- "version": "4.22.0",
3
+ "version": "4.22.1",
4
4
  "description": "Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",