-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
208 lines (191 loc) · 5.5 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
require("dotenv").config();
const axios = require("axios");
const express = require("express");
const path = require("path");
const { Auth } = require("@vonage/auth");
const { Vonage } = require("@vonage/server-sdk");
const { Channels } = require("@vonage/verify2");
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
const vonage = new Vonage(
new Auth({
apiKey: process.env.VONAGE_API_KEY,
apiSecret: process.env.VONAGE_API_SECRET,
applicationId: process.env.VONAGE_APPLICATION_ID,
privateKey: process.env.VONAGE_PRIVATE_KEY,
})
);
const scope = "dpv:FraudPreventionAndDetection#check-sim-swap";
const authReqUrl = "https://api-eu.vonage.com/oauth2/bc-authorize";
const tokenUrl = "https://api-eu.vonage.com/oauth2/token";
const simSwapApiUrl = "https://api-eu.vonage.com/camara/sim-swap/v040/check";
let phoneNumber = null;
let verifyRequestId = null;
let pwd = "123"; // Initial password
// In-memory store for user data
const users = {
user1: {
username: "user1",
password: "123",
},
};
// Serve the index.html file
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "views/index.html"));
});
// Serve the main.html file
app.get("/main", (req, res) => {
res.sendFile(path.join(__dirname, "views/main.html"));
});
// Authenticate function for SIM Swap API
async function authenticate(phone, scope) {
try {
const authReqResponse = await axios.post(
authReqUrl,
{
login_hint: phone,
scope: scope,
},
{
headers: {
Authorization: `Bearer ${process.env.JWT}`,
"Content-Type": "application/x-www-form-urlencoded",
},
}
);
const authReqId = authReqResponse.data.auth_req_id;
const tokenResponse = await axios.post(
tokenUrl,
{
auth_req_id: authReqId,
grant_type: "urn:openid:params:grant-type:ciba",
},
{
headers: {
Authorization: `Bearer ${process.env.JWT}`,
"Content-Type": "application/x-www-form-urlencoded",
},
}
);
return tokenResponse.data.access_token;
} catch (error) {
console.error(
"Error during authentication:",
error.response?.data || error.message
);
throw error;
}
}
// Check if the phone number has been recently swapped
async function checkSim() {
try {
const accessToken = await authenticate(phoneNumber, scope);
const response = await axios.post(
simSwapApiUrl,
{
phoneNumber: phoneNumber,
maxAge: process.env.MAX_AGE,
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
}
);
return response.data.swapped;
} catch (error) {
console.error(
"Error checking SIM swap:",
error.response?.data || error.message
);
throw error;
}
}
// Route to send a verification code via Verify2 API using Channels
app.post("/sendcode", async (req, res) => {
const phone = process.env.RECIPIENT_NUMBER ? process.env.RECIPIENT_NUMBER : req.body.phone;
try {
const response = await vonage.verify2.newRequest({
brand: "Vonage Bank",
workflow: [
{
channel: Channels.SMS, // Using SMS as the channel
to: phone,
},
],
});
verifyRequestId = response.requestId;
res.json({
message: "Verification code sent.",
verifycode: true,
request_id: verifyRequestId,
});
} catch (error) {
console.error("Error during verification:", error);
res.status(500).json({ message: "Error processing request.", verifycode: false });
}
});
// Handle SIM swap check
app.post("/simswap", async (req, res) => {
phoneNumber = req.body.phone;
try {
const simSwapped = await checkSim();
res.json({
swapped: simSwapped,
});
} catch (error) {
console.error("Error checking SIM swap:", error);
res.status(500).json({ message: "Error processing request." });
}
});
// Handle PIN verification
app.post("/verify", (req, res) => {
const { pin, newPass } = req.body;
vonage.verify2
.checkCode(verifyRequestId, pin)
.then((status) => {
if (status === "completed") {
// Update the password in the in-memory store
const user = Object.values(users).find(user => user.username === "user1");
if (user) {
user.password = newPass;
} else {
res.status(404).json({ message: "User not found." });
return;
}
res.json({ message: "Success" });
} else {
res.json({ message: "Invalid verification code. Please try again." });
}
})
.catch((err) => {
console.error("Error during PIN verification:", err);
res.status(500).json({ message: "Error during PIN verification." });
});
});
// Update password
app.post("/update", (req, res) => {
const { newPass } = req.body;
const user = Object.values(users).find(user => user.username === "user1"); // assuming user1
if (user) {
user.password = newPass;
res.json({ message: "Success" });
} else {
res.status(404).json({ message: "User not found." });
}
});
// Handle login request
app.post("/login", (req, res) => {
const { username, password } = req.body;
const user = users[username];
if (user && user.password === password) {
res.json({ message: "Success" });
} else {
res.status(401).json({ message: "Invalid user and password" });
}
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});