forked from vcipi/blockly_unix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
532 lines (475 loc) · 15.8 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const crypto = require('crypto');
const fs = require('fs');
// Importing libraries installed with npm
const { exec } = require('child_process');
const https = require('https');
const express = require('express');
const app = express();
const path = require('path');
const bcrypt = require('bcrypt'); // Importing bcrypt library
const passport = require('passport');
const initializePassport = require('./passport-config');
const flash = require('express-flash');
const session = require('express-session');
const jwt = require('jsonwebtoken');
const cookieParser = require('cookie-parser');
const favicon = require('serve-favicon');
const sqlite3 = require('sqlite3').verbose();
app.use(express.json());
const db = new sqlite3.Database('db/blockly_unix_database.db', (err) => {
if (err) {
console.error(err.message);
}
});
const { body, validationResult } = require('express-validator');
initializePassport(
passport,
(username, done) => {
db.get(`SELECT * FROM users WHERE username = ?`, [username], (err, row) => {
if (err) return done(err);
if (!row) return done(null, false);
return done(null, row);
});
},
(id, done) => {
db.get(`SELECT * FROM users WHERE id = ?`, [id], (err, row) => {
if (err) return done(err);
if (!row) return done(null, false);
return done(null, row);
});
}
);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.urlencoded({ extended: false }));
app.use(flash());
app.use(
session({
secret: process.env.SECRET_KEY,
resave: false, // Don't save back to the session store if nothing has changed
saveUninitialized: false // Don't save if there was no data
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(cookieParser());
app.set('view engine', 'ejs');
app.use((req, res, next) => {
res.locals.isAuthenticated = req.isAuthenticated();
next();
});
app.post('/github-webhook', express.json(), (req, res) => {
const secret = process.env.GITHUB_WEBHOOK_SECRET;
// Verify the signature to ensure that the request is actually coming from GitHub
const signature = req.headers['x-hub-signature-256'];
const expectedSignature = `sha256=${crypto
.createHmac('sha256', secret)
.update(JSON.stringify(req.body))
.digest('hex')}`;
if (signature !== expectedSignature) {
return res.status(403).send('Forbidden');
}
// Handle the push event
if (req.body.ref === 'refs/heads/main') {
// Execute a shell command to pull the latest changes from GitHub
const exec = require('child_process').exec;
exec(
'git pull origin main',
{ cwd: '/home/foivpro/blockly_unix' },
(err, stdout, stderr) => {
if (err) {
console.error(`Error pulling changes: ${stderr}`);
return res.status(500).send('Error pulling changes');
}
console.log(`Pulled latest changes: ${stdout}`);
res.status(200).send('Webhook received successfully');
}
);
} else {
res.status(200).send('Not a push to main branch, ignoring...');
}
});
app.listen(4000, () => {
console.log('Listening for GitHub Webhooks on port 4000');
});
// Middleware to add auth token
function addAuthToken(req, res, next) {
if (req.isAuthenticated()) {
const token = jwt.sign({ user: req.user.id }, process.env.SECRET_KEY, {
expiresIn: '30m'
}); // Token expires in 10 seconds for testing. When in production, set to 20 minutes
req.authToken = token;
} else {
req.authToken = null;
}
next();
}
// Configuring the login post functionality
app.post('/login', checkNotAuthenticated, (req, res, next) => {
passport.authenticate('local', (err, user) => {
if (err) {
return next(err);
}
if (!user) {
req.flash('error', 'Invalid username or password.');
return res.redirect('/login');
}
req.logIn(user, (err) => {
if (err) {
return next(err);
}
req.flash('success', 'You have successfully logged in.');
const token = jwt.sign({ user: user.id }, process.env.SECRET_KEY, {
expiresIn: '20m'
});
res.cookie('remember_me', token, { httpOnly: true });
res.redirect('/blockly_unix');
});
})(req, res, next);
});
// Configuring the register post functionality
app.post(
'/register',
checkNotAuthenticated,
body('password')
.isLength({ min: 8 })
.withMessage('Password must be at least 8 characters long')
.matches(/[!@#$%^&*(),.?":{}|<>]/)
.withMessage('Password must contain a special character'),
body('email')
.isEmail()
.withMessage('Must be a valid email address')
.matches(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/),
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
req.flash(
'error',
errors.array().map((error) => error.msg)
);
return res.redirect('/register');
}
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const newUser = {
id: Date.now().toString(),
username: req.body.username,
email: req.body.email,
password: hashedPassword
};
db.run(
`INSERT INTO users (id, username, email, password) VALUES (?, ?, ?, ?)`,
[
Date.now().toString(),
req.body.username,
req.body.email,
hashedPassword
],
function (err) {
if (err) {
req.flash('error', 'Registration failed.');
return res.redirect('/register');
}
req.logIn(newUser, (err) => {
if (err) {
req.flash('error', 'Registration failed.');
return res.redirect('/register');
}
req.flash(
'success',
'You have successfully registered and logged in.'
);
res.redirect('/blockly_unix');
});
}
);
db.run(
`INSERT INTO workspaces (workspaceData, userId, workspaceName) VALUES (?, ?, ?)`,
['{}', newUser.id, '__autosave__'],
function (err) {
if (err) {
req.flash('error', 'Registration failed.');
return res.redirect('/register');
}
}
);
} catch (e) {
req.flash('error', 'Registration failed.');
res.redirect('/register');
}
}
);
// Routes
app.get('/', (req, res) => {
res.render('homePage', { errorMessages: req.flash('error') || [] });
});
app.get('/tutorials', (req, res) => {
res.render('tutorials', { errorMessages: req.flash('error') || [] });
});
app.get('/blockly_unix', addAuthToken, (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'), {
headers: { 'X-Auth-Token': req.authToken || '' }
});
});
app.get('/auth-token', addAuthToken, (req, res) => {
if (req.isAuthenticated()) {
res.json({
authToken: req.authToken || '',
user: {
id: req.user.id,
username: req.user.username,
email: req.user.email
}
});
} else {
res.json({
message: 'User is not authenticated',
authToken: null,
user: null
});
}
});
app.get('/login', checkNotAuthenticated, (req, res) => {
res.render('login', { errorMessages: req.flash('error') || [] });
});
app.get('/register', checkNotAuthenticated, (req, res) => {
res.render('register', { errorMessages: req.flash('error') || [] });
});
app.get('/logout', function (req, res, next) {
if (req.session) {
req.session.destroy(function (err) {
if (err) {
return next(err);
}
res.clearCookie('connect.sid', {
path: '/',
httpOnly: true,
secure: false
});
return res.redirect('/');
});
} else {
res.redirect('/');
}
});
app.post('/saveWorkspace', (req, res) => {
// Retrieve the workspace data and user ID from the request body
const { workspaceData, userId, workspaceName } = req.body;
if (!workspaceData || !userId) {
return res
.status(400)
.json({ error: 'Missing workspace data or user ID.' });
}
// Insert the workspace data and user ID into the database
const query = `INSERT INTO workspaces (workspaceData, userId, workspaceName) VALUES (?, ?, ?)`;
db.run(query, [workspaceData, userId, workspaceName], function (err) {
if (err) {
console.error('Error inserting workspace data:', err.message);
return res.status(500).json({ error: 'Failed to save workspace data.' });
}
res.status(200).json({
message: 'Workspace data saved successfully.',
workspaceId: this.lastID // Return the ID of the inserted workspace
});
});
});
app.post('/saveGuestWorkspace', (req, res) => {
// Retrieve the workspace data and user ID from the request body
const { workspaceData } = req.body;
if (!workspaceData) {
return res.status(400).json({ error: 'Missing workspace data.' });
}
const query = `INSERT INTO guestsWorkspaces (workspaceData) VALUES (?)`;
db.run(query, [workspaceData], function (err) {
if (err) {
console.error('Error inserting workspace data:', err.message);
return res.status(500).json({ error: 'Failed to save workspace data.' });
}
res.status(200).json({
message: 'Workspace data saved successfully.',
workspaceId: this.lastID // Return the ID of the inserted workspace
});
});
});
app.post('/autoSaveWorkspace', (req, res) => {
const { workspaceData, userId } = req.body;
if (!workspaceData || !userId) {
return res
.status(400)
.json({ error: 'Missing workspace data or user ID.' });
}
// Query to update the __autosave__ workspace
const query = `UPDATE workspaces SET workspaceData = ? WHERE userId = ? AND workspaceName = '__autosave__'`;
db.run(query, [workspaceData, userId], function (err) {
if (err) {
console.error('Error auto-saving workspace:', err.message);
return res.status(500).json({ error: 'Failed to auto-save workspace.' });
}
// If no rows were updated, it means the __autosave__ entry doesn't exist. Insert a new one.
if (this.changes === 0) {
const insertQuery = `INSERT INTO workspaces (workspaceData, userId, workspaceName) VALUES (?, ?, '__autosave__')`;
db.run(insertQuery, [workspaceData, userId], function (err) {
if (err) {
console.error('Error inserting __autosave__ workspace:', err.message);
return res
.status(500)
.json({ error: 'Failed to auto-save workspace.' });
}
res.status(200).json({ message: 'Workspace auto-saved successfully.' });
});
} else {
res.status(200).json({ message: 'Workspace auto-saved successfully.' });
}
});
});
app.get('/getUserWorkspaces', (req, res) => {
// Assume that userId is passed as a query parameter or extracted from session
const userId = req.query.userId;
if (!userId) {
return res.status(400).json({ error: 'Missing user ID.' });
}
// Query to select workspaces for the given userId
const query = `SELECT id, workspaceName FROM workspaces WHERE userId = ?`;
db.all(query, [userId], (err, rows) => {
if (err) {
console.error('Error retrieving workspaces:', err.message);
return res.status(500).json({ error: 'Failed to retrieve workspaces.' });
}
res.status(200).json({ workspaces: rows });
});
});
app.get('/getWorkspace', (req, res) => {
const workspaceId = req.query.workspaceId;
if (!workspaceId) {
return res.status(400).json({ error: 'Missing workspace ID.' });
}
const query = `SELECT workspaceData FROM workspaces WHERE id = ?`;
db.get(query, [workspaceId], (err, row) => {
if (err) {
console.error('Error retrieving workspace:', err.message);
return res.status(500).json({ error: 'Failed to retrieve workspace.' });
}
if (!row) {
return res.status(404).json({ error: 'Workspace not found.' });
}
res.status(200).json({ workspaceData: row.workspaceData });
});
});
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return res.redirect('/blockly_unix');
}
next();
}
const GoogleStrategy = require('passport-google-oauth20').Strategy;
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: 'https://ublocks.balab.aueb.gr/auth/google/callback'
},
(accessToken, refreshToken, profile, done) => {
// Check if user with the given Google ID exists
db.get(
'SELECT * FROM users WHERE googleId = ?',
[profile.id],
(err, user) => {
if (err) return done(err);
if (user) {
// User already exists, log them in
return done(null, user);
} else {
// User does not exist, create a new user
db.get(
'SELECT * FROM users WHERE email = ?',
[profile.emails[0].value],
(err, existingUser) => {
if (err) return done(err);
if (existingUser) {
// Update the existing user with the Google ID
db.run(
'UPDATE users SET googleId = ? WHERE email = ?',
[profile.id, profile.emails[0].value],
(err) => {
if (err) return done(err);
// Fetch the updated user
db.get(
'SELECT * FROM users WHERE email = ?',
[profile.emails[0].value],
(err, updatedUser) => {
if (err) return done(err);
done(null, updatedUser);
}
);
}
);
} else {
// Register a new user
const newUser = {
googleId: profile.id,
username: profile.displayName,
email: profile.emails[0].value
};
db.run(
'INSERT INTO users (googleId, username, email) VALUES (?, ?, ?)',
[newUser.googleId, newUser.username, newUser.email],
function (err) {
if (err) return done(err);
// Retrieve the newly created user
db.get(
'SELECT * FROM users WHERE id = ?',
[this.lastID],
(err, createdUser) => {
if (err) return done(err);
// Create default workspace for the new user
db.run(
'INSERT INTO workspaces (workspaceData, userId, workspaceName) VALUES (?, ?, ?)',
['{}', createdUser.id, '__autosave__'],
(err) => {
if (err) return done(err);
done(null, createdUser);
}
);
}
);
}
);
}
}
);
}
}
);
}
)
);
// Google login route
app.get(
'/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] })
);
// Google login callback
app.get(
'/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
(req, res) => {
res.redirect('/blockly_unix');
}
);
/* To run on local server remove comment
app.listen(3000, 'localhost', () => {
console.log('Server is running on http://localhost:3000');
});
*/
app.listen(8443, () => {
console.log('Server is running on https://ublocks.balab.aueb.gr');
});