-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.ts
285 lines (247 loc) · 9.27 KB
/
mongodb.ts
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const uuidv4 = require('uuid/v4');
const utils = require('../server/utils');
import { Request, Response, NextFunction } from 'express';
import mongoose = require('mongoose');
import { Nonce } from '../models/nonce';
import { User } from '../models/user';
import { runtimeCfg } from '../server/getconfig';
// https://scotch.io/tutorials/using-mongoosejs-in-node-js-and-mongodb-applications
mongoose.connect(runtimeCfg.connectionString)
.then((val) => {
console.log("Successfully connected to the MongoDB");
})
.catch((error) => {
console.error("Could not connect to the MongoDB:");
console.error(error);
throw ('Could not connect to the MongoDB!')
});
export const serveNonce = (req: Request, res: Response) => {
const serverNonce = uuidv4();
const serverEpoch = + new Date();
Nonce.findOne({ serverNonce: serverNonce })
.then((nonce) => {
if (nonce === null) {
return uuidv4();
}
})
.catch((err) => {
return serverNonce;
})
.then((newServerNonce: string) => {
const nonce = new Nonce({
serverNonce: newServerNonce,
serverEpoch: serverEpoch
});
nonce.save()
.then((value) => {
res.send({
serverNonce: newServerNonce,
serverEpoch: serverEpoch
});
})
.catch((err) => {
console.log(err);
});
});
}
export const serveSignup = (req: Request, res: Response) => {
const postData = req.body;
const rawObj = postData.rawObj;
const signature = postData.signature;
console.log(postData);
// Check timeout: 0 <= Current Epoch - rawObj.serverEpoch <= TIME_OUT_LIMIT
Nonce.findOne({ serverNonce: rawObj.serverNonce })
.then((nonce) => {
if (nonce === null || nonce === undefined) {
const errMsg = `The server nonce is not found: ${rawObj.serverNonce}`;
console.error(errMsg);
throw errMsg;
}
const serverEpoch = parseInt(nonce.serverEpoch);
if (serverEpoch !== rawObj.serverEpoch) {
throw `The server Epoch is not matching: Original (${serverEpoch}) vs Client (${rawObj.serverEpoch})`;
}
// Validate the schema of the input JSON
// Verify the signature
// Insert to the DB if not present
const email = rawObj.email.trim().toLowerCase();
User.findOne({ email: email })
.then((user) => {
console.error(user);
if (user !== undefined && user !== null) {
console.log(user);
console.log(`Duplicate email found: ${email}`);
res.send({
success: false,
errCode: 'SIGNUP_ERROR_EMAIL_DUP',
errMsg: 'The email is in use!'
});
return false;
} else {
// ECDSA
const acctHex = postData.acct.trim().toLowerCase();
const signatureHex = postData.signature.trim().toLowerCase().substring(2);
const theMessage = rawObj.netId +
rawObj.domain +
rawObj.intent +
rawObj.serverNonce +
rawObj.serverEpoch +
rawObj.clientNonce +
rawObj.clientEpoch +
rawObj.email +
rawObj.fullName;
const recoveryResult = utils.recoverPubKey(theMessage, signatureHex);
console.log(recoveryResult);
if (acctHex !== recoveryResult.acct) {
throw `Mismatch between recovered account & claimed account: Recovered(${recoveryResult.acct}) vs Client(${acctHex})`;
}
// Enroll the user
const user = new User({ email: email, addr: acctHex });
user.save()
.then((value) => {
console.log(`Successfully enrolled (${email}, ${acctHex})`);
res.send({
success: true,
errCode: 'SIGNUP_SUCCESS',
errMsg: 'Your are signed up!'
});
})
.catch((err) => {
console.error(`Error occured when saving (${email}, ${acctHex})`);
res.send({
success: false,
errCode: 'SIGNUP_ERROR_NOT_SAVED',
errMsg: 'Could not sign you up!'
});
});
return true;
}
})
.catch((err) => {
console.error(`Error occured when searching for the email: ${email}`);
res.send({
success: false,
errCode: 'SIGNUP_ERROR',
errMsg: 'Could not sign you up!'
});
});
// Clean the DB
Nonce.findOneAndRemove({ serverNonce: rawObj.serverNonce })
.then((value) => {
console.log(value);
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
// Clean the DB
Nonce.findOneAndRemove({ serverNonce: rawObj.serverNonce })
.catch((err) => {
console.error(err);
});
res.send({
success: false,
errCode: 'SIGNUP_ERROR',
errMsg: 'Could not sign you up!'
});
});
}
export const serveLogin = (req: Request, res: Response) => {
const postData = req.body;
const rawObj = postData.rawObj;
const signature = postData.signature;
console.log(postData);
// Check timeout: 0 <= Current Epoch - rawObj.serverEpoch <= TIME_OUT_LIMIT
Nonce.findOne({ serverNonce: rawObj.serverNonce })
.then((nonce) => {
if (nonce === null || nonce === undefined) {
const errMsg = `The server nonce is not found: ${rawObj.serverNonce}`;
console.error(errMsg);
throw errMsg;
}
const serverEpoch = parseInt(nonce.serverEpoch);
if (serverEpoch !== rawObj.serverEpoch) {
throw `The server Epoch is not matching: Original (${serverEpoch}) vs Client (${rawObj.serverEpoch})`;
}
// Validate the schema of the input JSON
// Verify the signature
// Insert to the DB if not present
const email = rawObj.email.trim().toLowerCase();
User.findOne({ email: email })
.then((user) => {
if (user === undefined || user === null) {
console.log(`Email not found: ${email}`);
res.send({
success: false,
errCode: 'LOGIN_ERROR_NO_EMAIL',
errMsg: 'Could not login!'
});
return false;
}
// ECDSA
const acctHex = postData.acct.trim().toLowerCase();
const signatureHex = postData.signature.trim().toLowerCase().substring(2);
const theMessage = rawObj.netId +
rawObj.domain +
rawObj.intent +
rawObj.serverNonce +
rawObj.serverEpoch +
rawObj.clientNonce +
rawObj.clientEpoch +
rawObj.email;
const recoveryResult = utils.recoverPubKey(theMessage, signatureHex);
console.log(recoveryResult);
if (acctHex !== recoveryResult.acct) {
const errMsg = `Mismatch between recovered account & claimed account: Recovered(${recoveryResult.acct}) vs Client(${acctHex})`;
console.log(errMsg);
throw errMsg;
}
if (acctHex !== user.addr) {
const errMsg = `The claimed account has not been saved yet: Saved(${user.addr}) vs Client(${acctHex})`;
console.log(errMsg);
throw errMsg;
}
// TODO: Record user login with IP, time, cookie...
res.send({
success: true,
errCode: 'LOGIN_SUCCESS',
errMsg: 'Successful login!'
});
return true;
})
.catch((err) => {
console.log(`Error occured when searching for the email: ${email}`);
res.send({
success: false,
errCode: 'LOGIN_ERROR',
errMsg: 'Could not login!'
});
});
// Clean the DB
Nonce.findOneAndRemove({ serverNonce: rawObj.serverNonce })
.then((value) => {
console.log(value);
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
// Clean the DB
Nonce.findOneAndRemove({ serverNonce: rawObj.serverNonce })
.then((value) => {
console.log(value);
})
.catch((err) => {
console.log(err);
});
res.send({
success: false,
errCode: 'LOGIN_ERROR',
errMsg: 'Could not login!'
});
});
}