This repository has been archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
314 lines (300 loc) · 10.2 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
const express = require('express');
const app = express();
const NodeCache = require('node-cache');
const {v4: uuidv4} = require('uuid');
const {checkSignature} = require('./ether');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const fetch = require('node-fetch');
app.use(cors());
app.listen(8000, () => {
console.log('Server started!')
});
const sessionCache = new NodeCache({
stdTTL: 60 * 5,
checkperiod: 60,
});
const SESSION_COOKIE_TIME = 5 * 60 * 1000;
const AUTH_COOKIE_TIME = 8 * 60 * 60 * 1000;
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(cookieParser());
const IDENA_SESSION_TOKEN_COOKIE = 'IDENA_SESSION_TOKEN';
const IDENA_AUTH_COOKIE = 'IDENA_AUTH';
app.route('/auth/v1/logout').post(async (req, res) => {
const session = req.cookies[IDENA_AUTH_COOKIE];
if (session) {
let headers = {
'Authorization': 'cfd8d2ae-a31b-4f5e-87a9-a0743d180b24',
'Content-Type': 'application/json'
};
fe = await fetch(`http://idenapoll.com:3000/sessions?address=${session.address}`, {
method: 'GET',
headers: headers
});
const sessions = await fe.json();
if (sessions) {
for (let ses of sessions) {
fe = await fetch(`http://idenapoll.com:3000/sessions/${ses.id}`, {
method: 'DELETE',
headers: headers
});
const json = await fe.json();
}
}
res.clearCookie(IDENA_AUTH_COOKIE);
return res.json()
}
});
app.route('/auth/v1/new-token').get((_, res) => {
const token = uuidv4();
res.cookie(IDENA_SESSION_TOKEN_COOKIE, token, {
maxAge: SESSION_COOKIE_TIME,
httpOnly: true,
});
res.set('Access-Control-Allow-Origin', [_.header('origin')]);
res.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.append('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Allow-Credentials', 'true');
return res.json({token})
});
app.route('/recheck').get(async (_, res) => {
let fe = await fetch(`http://idenapoll.com:3000/polls?endsAt_lte=${new Date().getTime()}&status=active`, {method: 'GET'});
const polljs = await fe.json();
let headers = {
'Authorization': 'cfd8d2ae-a31b-4f5e-87a9-a0743d180b24',
'Content-Type': 'application/json'
};
if (polljs)
for (let poll of polljs) {
poll.status = "ended";
fe = await fetch(`http://idenapoll.com:3000/polls/${poll.id}`, {
method: 'PATCH',
body: JSON.stringify(poll),
headers: headers
});
const json = await fe.json();
}
return res.json()
});
app.route('/auth/v1/start-session').post((req, res) => {
const {token, address} = req.body;
const nonce = `signin-${uuidv4()}`;
sessionCache.set(token, {address, nonce});
return res.json({success: true, data: {nonce}})
});
app.route('/auth/v1/authenticate').post((req, res) => {
const {token, signature} = req.body;
const cacheValue = sessionCache.get(token);
if (!cacheValue) {
return res.json({
success: false,
data: {
authenticated: false,
},
})
}
const address = checkSignature(cacheValue.nonce, signature);
if (address.toLowerCase() !== cacheValue.address.toLowerCase()) {
return res.json({
success: false,
data: {
authenticated: false,
},
})
}
sessionCache.set(token, {...cacheValue, authenticated: true});
return res.json({
success: true,
data: {
authenticated: true,
},
})
});
app.route('/auth/v1/session').get(async (req, res) => {
res.set('Access-Control-Allow-Origin', [req.header('origin')]);
res.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.append('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Allow-Credentials', 'true');
const session = req.cookies[IDENA_AUTH_COOKIE];
if (session) {
return res.json({authenticated: true, address: session.address})
}
if (req.query.onlyCheck) {
return res.sendStatus(403)
}
const sessionToken = req.cookies[IDENA_SESSION_TOKEN_COOKIE];
if (sessionToken) {
const data = sessionCache.get(sessionToken);
if (data) {
const {address, authenticated} = data;
if (authenticated) {
res.clearCookie(IDENA_SESSION_TOKEN_COOKIE);
res.cookie(
IDENA_AUTH_COOKIE,
{authenticated: true, address},
{
maxAge: AUTH_COOKIE_TIME,
httpOnly: true,
}
);
let session = {
address: address,
expiresAt: new Date().getTime() + AUTH_COOKIE_TIME
};
let headers = {
'Authorization': 'cfd8d2ae-a31b-4f5e-87a9-a0743d180b24',
'Content-Type': 'application/json'
};
let fe = await fetch(`http://idenapoll.com:3000/sessions?address=${address}`, {
method: 'GET',
headers: headers
});
const sessions = await fe.json();
if (sessions)
for (let session of sessions) {
fe = await fetch(`http://idenapoll.com:3000/sessions/${session.id}`, {
method: 'DELETE',
headers: headers
});
const json = await fe.json();
}
fe = await fetch("http://idenapoll.com:3000/sessions", {
method: 'POST',
body: JSON.stringify(session),
headers: headers
});
const json = await fe.json();
return res.status(200).json({authenticated: true, address})
}
}
}
return res.sendStatus(403)
});
app.route("/vote").post(async (req, res) => {
res.set('Access-Control-Allow-Origin', [req.header('origin')]);
res.append('Access-Control-Allow-Methods', 'POST');
res.append('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Allow-Credentials', 'true');
const body = JSON.parse(req.body);
const {poll, option, voter, status, age} = body;
let headers = {
'Authorization': 'cfd8d2ae-a31b-4f5e-87a9-a0743d180b24',
'Content-Type': 'application/json'
};
if (req.cookies[IDENA_AUTH_COOKIE]) {
let fe = await fetch(`http://idenapoll.com:3000/sessions?address=${voter}`, {
method: 'GET',
headers: headers
});
const sessions = await fe.json();
if (sessions[0] && sessions[0].expiresAt > new Date().getTime()) {
let statusc = {status: "ok"};
fe = await fetch(`http://idenapoll.com:3000/polls/${poll}`, {method: 'GET'});
const polljs = await fe.json();
let found;
for (o of polljs.options) {
let search = o.votes.find((v) => {
return v.address === voter
});
found = search !== undefined ? search : found;
}
if (found == null) {
if (polljs.settings.ageRequirement === "" || (polljs.settings.ageRequirement !== "" && polljs.settings.ageRequirement <= age)) {
if (polljs.settings.statusRequirement === "" || (polljs.settings.statusRequirement !== "" && ((polljs.settings.statusRequirement === "NEWBIE" && (status === "NEWBIE" || status === "VERIFIED" || status === "HUMAN")) || (polljs.settings.statusRequirement === "VERIFIED" && (status === "VERIFIED" || status === "HUMAN")) || (polljs.settings.statusRequirement === "HUMAN" && status === "HUMAN")))) {
polljs.options[option].votes.push({
address: voter,
status: status,
age: age
});
const fe = await fetch(`http://idenapoll.com:3000/polls/${poll}`, {
method: 'PATCH',
body: JSON.stringify(polljs),
headers: headers
});
const jsonres = await fe.json();
} else {
statusc = {status: "noStatus"}
}
} else {
statusc = {status: "noAge"}
}
} else {
statusc = {status: "dup"}
}
return res.status(200).json(statusc);
} else
return res.sendStatus(403);
} else
return res.sendStatus(403);
});
app.route("/create").post(async (req, res, next) => {
res.set('Access-Control-Allow-Origin', [req.header('origin')]);
res.append('Access-Control-Allow-Methods', 'POST');
res.append('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Allow-Credentials', 'true');
const poll = JSON.parse(req.body);
if (req.cookies[IDENA_AUTH_COOKIE]) {
const voter = req.cookies[IDENA_AUTH_COOKIE].address;
let headers = {
'Authorization': 'cfd8d2ae-a31b-4f5e-87a9-a0743d180b24',
'Content-Type': 'application/json',
'Accept': 'application/json',
'Connection': 'keep-alive'
};
let fe = await fetch(`http://idenapoll.com:3000/sessions?address=${voter}`, {
method: 'GET',
headers: headers,
body: null
});
poll.endsAt = new Date(poll.endsAt).getTime();
const sessions = await fe.json();
if (sessions[0].expiresAt > new Date().getTime()) {
headers = {
'Authorization': 'cfd8d2ae-a31b-4f5e-87a9-a0743d180b24',
'Content-Type': 'application/json'
};
const fe = await fetch("http://idenapoll.com:3000/polls", {
method: 'POST',
body: JSON.stringify(poll),
headers: headers
});
const json = await fe.json();
return res.status(200).json(json);
} else
return res.sendStatus(403)
} else
return res.sendStatus(403)
});
setInterval(async () => {
let fe = await fetch(`http://idenapoll.com:3000/polls?endsAt_lte=${new Date().getTime()}&status=active`, {method: 'GET'});
const polljs = await fe.json();
let headers = {
'Authorization': 'cfd8d2ae-a31b-4f5e-87a9-a0743d180b24',
'Content-Type': 'application/json'
};
if (polljs)
for (let poll of polljs) {
poll.status = "ended";
fe = await fetch(`http://idenapoll.com:3000/polls/${poll.id}`, {
method: 'PATCH',
body: JSON.stringify(poll),
headers: headers
});
const json = await fe.json();
}
fe = await fetch(`http://idenapoll.com:3000/sessions?expiresAt_lte=${new Date().getTime()}`, {
method: 'GET',
headers: headers
});
const sessions = await fe.json();
if (sessions)
for (let session of sessions) {
fe = await fetch(`http://idenapoll.com:3000/sessions/${session.id}`, {
method: 'DELETE',
headers: headers
});
const json = await fe.json();
}
}, 600000);