powr-sdk-api 4.21.1 → 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.
- package/dist/routes/auth.js +46 -7
- package/dist/routes/auth.otp.test.js +66 -6
- package/dist/services/otp.js +7 -0
- package/dist/services/otp.test.js +17 -0
- package/package.json +1 -1
package/dist/routes/auth.js
CHANGED
|
@@ -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,
|
|
@@ -314,6 +315,38 @@ router.post("/register", async (req, res) => {
|
|
|
314
315
|
});
|
|
315
316
|
}
|
|
316
317
|
});
|
|
318
|
+
router.post("/login-options", async (req, res) => {
|
|
319
|
+
var _req$body, _req$body2;
|
|
320
|
+
const phoneOrEmail = String(((_req$body = req.body) === null || _req$body === void 0 ? void 0 : _req$body.phoneOrEmail) || ((_req$body2 = req.body) === null || _req$body2 === void 0 ? void 0 : _req$body2.mobile) || "").trim();
|
|
321
|
+
try {
|
|
322
|
+
const q = buildIdentifierQuery({
|
|
323
|
+
phoneOrEmail
|
|
324
|
+
});
|
|
325
|
+
if (!q) {
|
|
326
|
+
return res.status(400).json({
|
|
327
|
+
success: false,
|
|
328
|
+
message: "Valid phone is required"
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
const db = await getDb();
|
|
332
|
+
const user = await db.collection("users").findOne(q, {
|
|
333
|
+
projection: {
|
|
334
|
+
password: 1
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
return res.status(200).json({
|
|
338
|
+
success: true,
|
|
339
|
+
exists: Boolean(user),
|
|
340
|
+
hasPassword: Boolean(user && user.password)
|
|
341
|
+
});
|
|
342
|
+
} catch (error) {
|
|
343
|
+
console.error("Error in login-options:", error);
|
|
344
|
+
return res.status(500).json({
|
|
345
|
+
success: false,
|
|
346
|
+
message: "Could not check login."
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
});
|
|
317
350
|
|
|
318
351
|
// Login User
|
|
319
352
|
router.post("/login", async (req, res) => {
|
|
@@ -644,9 +677,9 @@ router.post("/reset-password", async (req, res) => {
|
|
|
644
677
|
// Admin views another account on this project. Access token only.
|
|
645
678
|
// Does not change the target password or refresh token.
|
|
646
679
|
router.post("/impersonate", verifyToken, async (req, res) => {
|
|
647
|
-
var _req$
|
|
680
|
+
var _req$body3;
|
|
648
681
|
const projectId = req.projectId;
|
|
649
|
-
const phoneOrEmail = String(((_req$
|
|
682
|
+
const phoneOrEmail = String(((_req$body3 = req.body) === null || _req$body3 === void 0 ? void 0 : _req$body3.phoneOrEmail) || "").trim();
|
|
650
683
|
try {
|
|
651
684
|
var _req$user;
|
|
652
685
|
if (!phoneOrEmail) {
|
|
@@ -750,8 +783,8 @@ router.post("/impersonate/exit", verifyToken, async (req, res) => {
|
|
|
750
783
|
}
|
|
751
784
|
});
|
|
752
785
|
router.post("/send-otp", async (req, res) => {
|
|
753
|
-
var _req$
|
|
754
|
-
const mobile = tenDigitMobile(((_req$
|
|
786
|
+
var _req$body4, _req$body5;
|
|
787
|
+
const mobile = tenDigitMobile(((_req$body4 = req.body) === null || _req$body4 === void 0 ? void 0 : _req$body4.mobile) || ((_req$body5 = req.body) === null || _req$body5 === void 0 ? void 0 : _req$body5.phoneOrEmail));
|
|
755
788
|
if (mobile.length !== 10) {
|
|
756
789
|
return res.status(400).json({
|
|
757
790
|
success: false,
|
|
@@ -759,6 +792,12 @@ router.post("/send-otp", async (req, res) => {
|
|
|
759
792
|
});
|
|
760
793
|
}
|
|
761
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
|
+
}
|
|
762
801
|
try {
|
|
763
802
|
const db = await getDb();
|
|
764
803
|
const template = await findTemplate(db, app);
|
|
@@ -804,9 +843,9 @@ router.post("/send-otp", async (req, res) => {
|
|
|
804
843
|
}
|
|
805
844
|
});
|
|
806
845
|
router.post("/verify-otp", async (req, res) => {
|
|
807
|
-
var _req$
|
|
808
|
-
const mobile = tenDigitMobile(((_req$
|
|
809
|
-
const otp = String(((_req$
|
|
846
|
+
var _req$body6, _req$body7, _req$body8;
|
|
847
|
+
const mobile = tenDigitMobile(((_req$body6 = req.body) === null || _req$body6 === void 0 ? void 0 : _req$body6.mobile) || ((_req$body7 = req.body) === null || _req$body7 === void 0 ? void 0 : _req$body7.phoneOrEmail));
|
|
848
|
+
const otp = String(((_req$body8 = req.body) === null || _req$body8 === void 0 ? void 0 : _req$body8.otp) || "").replace(/\D/g, "");
|
|
810
849
|
if (mobile.length !== 10 || !otp) {
|
|
811
850
|
return res.status(400).json({
|
|
812
851
|
success: false,
|
|
@@ -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
|
|
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: "
|
|
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({}, "
|
|
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: "
|
|
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
|
};
|
|
@@ -203,4 +219,48 @@ describe("auth OTP routes", () => {
|
|
|
203
219
|
expect(decoded.userId).toBeUndefined();
|
|
204
220
|
expect(decoded.mobile).toBe("7903658469");
|
|
205
221
|
});
|
|
222
|
+
});
|
|
223
|
+
describe("auth login-options", () => {
|
|
224
|
+
it("reports exists and hasPassword", async () => {
|
|
225
|
+
const findOne = jest.fn().mockResolvedValue({
|
|
226
|
+
password: "hash"
|
|
227
|
+
});
|
|
228
|
+
getDb.mockResolvedValue({
|
|
229
|
+
collection: () => ({
|
|
230
|
+
findOne
|
|
231
|
+
})
|
|
232
|
+
});
|
|
233
|
+
const req = {
|
|
234
|
+
body: {
|
|
235
|
+
mobile: "7903658469"
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
const res = mockRes();
|
|
239
|
+
await findRoute("post", "/login-options")(req, res);
|
|
240
|
+
expect(res.status).toHaveBeenCalledWith(200);
|
|
241
|
+
expect(res.json.mock.calls[0][0]).toEqual({
|
|
242
|
+
success: true,
|
|
243
|
+
exists: true,
|
|
244
|
+
hasPassword: true
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
it("reports no user", async () => {
|
|
248
|
+
getDb.mockResolvedValue({
|
|
249
|
+
collection: () => ({
|
|
250
|
+
findOne: jest.fn().mockResolvedValue(null)
|
|
251
|
+
})
|
|
252
|
+
});
|
|
253
|
+
const req = {
|
|
254
|
+
body: {
|
|
255
|
+
phoneOrEmail: "+917903658469"
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
const res = mockRes();
|
|
259
|
+
await findRoute("post", "/login-options")(req, res);
|
|
260
|
+
expect(res.json.mock.calls[0][0]).toEqual({
|
|
261
|
+
success: true,
|
|
262
|
+
exists: false,
|
|
263
|
+
hasPassword: false
|
|
264
|
+
});
|
|
265
|
+
});
|
|
206
266
|
});
|
package/dist/services/otp.js
CHANGED
|
@@ -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.
|
|
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",
|