-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
241 lines (215 loc) · 6.31 KB
/
index.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
const express = require("express");
const path = require("path");
const { Client } = require("pg");
var bodyParser = require("body-parser");
const fetch = require("node-fetch");
var expressSession = require("express-session");
var expressValidator = require("express-validator");
const port = process.env.PORT || 5000;
const crypto = require("crypto");
const app = express();
var server = app.listen(port, function() {
console.log(`Listening on ${port}`);
});
var socket = require("socket.io");
io = socket(server);
io.on("connection", socket => {
console.log(socket.id);
socket.on("SEND_MESSAGE", function(data) {
io.emit("RECEIVE_MESSAGE", data);
});
});
const { OAuth2Client } = require("google-auth-library");
const OAUTHCID =
"656090020496-s4loae0qcgon2psaacomcfftu57jdomk.apps.googleusercontent.com";
const Oauthclient = new OAuth2Client(OAUTHCID);
const client = new Client({
connectionString:
process.env.DATABASE_URL,
ssl: true
});
client.connect();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator());
app.use(bodyParser.json({ type: "application/json" }));
app.use(bodyParser.json({ type: "html/text" }));
// Serve static files from the React app
app.use(express.static(path.join(__dirname, "client/build")));
app.use(
expressSession({ secret: "max", saveUninitialized: false, resave: false })
);
// Put all API endpoints under '/api'
// Put all API endpoints under '/api'
app.post("/api/signup", async (req, res) => {
let { username, password } = req.body;
let userCount;
try {
userCount = await client.query(
"SELECT COUNT(user_id) from account where username = $1",
[username]
);
} catch (e) {
res.writeHead(401);
res.end();
return;
}
if (userCount.rows[0].count > 0) {
console.log("On olemas");
res.writeHead(400);
res.end("User already exists");
return;
}
// create new user
if (userCount.rows[0].count == 0) {
try {
// SELECT COUNT(user_id) from account where username = 'as'
await client.query(
`INSERT INTO account(username,password) VALUES('${username}' , '${password}')`
);
} catch (e) {
res.writeHead(401);
res.end();
return;
}
}
res.end();
});
app.post("/api/login", async (req, res) => {
let { username, password } = req.body;
let userCount;
try {
userCount = await client.query(
"SELECT email, username, user_id from account where username = $1 and password = $2 LIMIT 1",
[username, password]
);
} catch (e) {
res.writeHead(401);
res.end();
return;
}
if (userCount.rowCount > 0) {
req.session.isLoggedIn = true;
try {
res.contentType("application/json");
res.json({ isLoggedIn: Boolean(req.session.isLoggedIn) });
} catch (e) {}
} else {
req.session.isLoggedIn = false;
try {
res.contentType("application/json");
res.json({ isLoggedIn: Boolean(req.session.isLoggedIn) });
} catch (e) {}
}
res.end();
});
app.post("/api/googleLogin", async (req, res) => {
let token = await verify(req.headers.authorization).catch(console.error);
let userCount;
try {
userCount = await client.query(
`SELECT email, username, user_id from account where username = '${token["sub"]}' and password = '${token["sub"]}'`
);
} catch (e) {
res.writeHead(401);
res.end();
return;
}
if (userCount.rowCount > 0) {
console.log("jey");
req.session.isLoggedIn = true;
try {
res.contentType("application/json");
res.json({ isLoggedIn: Boolean(req.session.isLoggedIn) });
} catch (e) {}
} else {
req.session.isLoggedIn = true;
try {
await client.query(
`INSERT INTO account(username,password) VALUES('${token["sub"]}' , '${
token["sub"]
}')`
);
res.contentType("application/json");
res.json({ isLoggedIn: Boolean(req.session.isLoggedIn) });
} catch (e) {}
}
res.end();
});
app.post("/api/isLoggedIn", async (req, res) => {
res.contentType("application/json");
res.json({ isLoggedIn: Boolean(req.session.isLoggedIn) });
});
app.post("/api/logout", async (req, res) => {
req.session.isLoggedIn = false;
res.json({ isLoggedIn: false });
});
app.post("/api/smartid", async (req, res) => {
let { isikukood, riik } = req.body;
const buf = crypto.randomBytes(32);
const hash = crypto.createHash("sha256");
hash.update(buf);
hashString = hash.digest("base64");
fetch(
`https://sid.demo.sk.ee/smart-id-rp/v1/authentication/pno/${riik}/${isikukood}`,
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
relyingPartyUUID: "00000000-0000-0000-0000-000000000000",
relyingPartyName: "DEMO",
certificateLevel: "QUALIFIED",
hashType: "SHA256",
hash: hashString
})
}
)
.then(result => {
if (result.status === 200) {
return result.json();
} else {
console.log("Error");
throw new Error("Error");
}
})
.then(result => {
const sessionID = result.sessionID;
fetch(`https://sid.demo.sk.ee/smart-id-rp/v1/session/${sessionID}`, {
method: "GET"
})
.then(result => {
if (result.status === 200) {
return result.json();
} else {
console.log("Error");
throw new Error("Error");
}
})
.then(result => {
if (result.state === "COMPLETE" && result.result.endResult === "OK") {
req.session.isLoggedIn = true;
res.json({ isLoggedIn: Boolean(req.session.isLoggedIn) });
}
});
})
.catch(err => {
console.log(err);
req.session.isLoggedIn = false;
res.json({ isLoggedIn: Boolean(req.session.isLoggedIn) });
});
});
// The "catchall" handler: for any request that doesn't
// match one above, send back React's index.html file.
async function verify(token) {
const ticket = await Oauthclient.verifyIdToken({
idToken: token,
audience: OAUTHCID // Specify the CLIENT_ID of the app that accesses the backend
// Or, if multiple clients access the backend:
//[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]
});
const payload = await ticket.getPayload();
return payload;
}
console.log(`Password generator listening on ${port}`);