-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
306 lines (259 loc) · 10.2 KB
/
server.js
File metadata and controls
306 lines (259 loc) · 10.2 KB
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
import 'dotenv/config';
import express from 'express';
import session from 'express-session';
import passport from 'passport';
import { Strategy as GitHubStrategy } from 'passport-github2';
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import { Strategy as LinkedInStrategy } from 'passport-linkedin-oauth2';
import path from 'path';
import { fileURLToPath } from 'url';
import db from './database.js';
import connectSqlite3 from 'connect-sqlite3';
const SQLiteStore = connectSqlite3(session);
const app = express();
const PORT = process.env.PORT || 3000;
// --- Basic Express Setup ---
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.enable("trust proxy");
app.use(express.json({ limit: '1mb' })); // Middleware to parse JSON bodies, with a reasonable limit
app.use(express.static(path.join(__dirname, '/')));
// --- Session Management ---
// Enforce session secret in production, provide a default for development
const sessionSecret = process.env.SESSION_SECRET || 'dev-secret';
if (process.env.NODE_ENV === 'production' && sessionSecret === 'dev-secret') {
throw new Error('FATAL: SESSION_SECRET environment variable must be set in production.');
}
app.use(session({
store: new SQLiteStore({
db: 'session.db',
dir: './',
table: 'sessions'
}),
secret: sessionSecret,
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
}
}));
// --- Passport.js Initialization ---
app.use(passport.initialize());
app.use(passport.session());
// --- User Serialization/Deserialization ---
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
try {
// Fetch the canonical user from the users table
const user = await db.get('SELECT * FROM users WHERE id = ?', id);
done(null, user);
} catch (err) {
done(err, null);
}
});
// --- Generic OAuth Strategy Handler ---
const findOrCreateUser = async (profile, done) => {
try {
const { provider, id: provider_id, displayName, emails, photos } = profile;
const email = emails?.[0]?.value;
const photoUrl = photos?.[0]?.value;
// An email is required to link accounts.
if (!email) {
return done(new Error('Email not provided by OAuth provider. Cannot link account.'), null);
}
// 1. Find an existing identity
const identity = await db.get('SELECT * FROM identities WHERE provider = ? AND provider_id = ?', [provider, provider_id]);
if (identity) {
const user = await db.get('SELECT * FROM users WHERE id = ?', [identity.user_id]);
return done(null, user);
}
// 2. No identity found, find or create user by email
let user = await db.get('SELECT * FROM users WHERE email = ?', [email]);
if (!user) {
// 3. If no user with this email, create one
const result = await db.run(
'INSERT INTO users (email, displayName) VALUES (?, ?)',
[email, displayName]
);
user = await db.get('SELECT * FROM users WHERE id = ?', result.lastID);
}
// 4. Create and link the new identity to the user (either found or newly created)
await db.run(
'INSERT INTO identities (user_id, provider, provider_id, photos) VALUES (?, ?, ?, ?)',
[user.id, provider, provider_id, photoUrl ? JSON.stringify(photos) : null]
);
return done(null, user);
} catch (err) {
return done(err);
}
};
// --- Passport.js Strategies ---
// Defang provides the DEFANG_HOST environment variable with the public hostname of the service.
const callbackBaseUrl = process.env.DEFANG_HOST ? `https://${process.env.DEFANG_HOST}` : '';
// GitHub
if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
passport.use(new GitHubStrategy({
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: `${callbackBaseUrl}/auth/github/callback`
}, (accessToken, refreshToken, profile, done) => findOrCreateUser(profile, done)));
}
// Google
if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: `${callbackBaseUrl}/auth/google/callback`
}, (accessToken, refreshToken, profile, done) => findOrCreateUser(profile, done)));
}
// LinkedIn
if (process.env.LINKEDIN_CLIENT_ID && process.env.LINKEDIN_CLIENT_SECRET) {
passport.use(new LinkedInStrategy({
clientID: process.env.LINKEDIN_CLIENT_ID,
clientSecret: process.env.LINKEDIN_CLIENT_SECRET,
callbackURL: `${callbackBaseUrl}/auth/linkedin/callback`,
scope: ['r_emailaddress', 'r_liteprofile'],
state: true
}, (accessToken, refreshToken, profile, done) => findOrCreateUser(profile, done)));
}
// --- Authentication Routes ---
// GitHub
app.get('/auth/github', passport.authenticate('github', { scope: ['user:email'] }));
app.get('/auth/github/callback',
passport.authenticate('github', { failureRedirect: '/' }),
(req, res) => res.redirect('/')
);
// Google
app.get('/auth/google', passport.authenticate('google', { scope: ['profile', 'email'] }));
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/' }),
(req, res) => res.redirect('/')
);
// LinkedIn
app.get('/auth/linkedin', passport.authenticate('linkedin')); // 'state' is now handled by the strategy
app.get('/auth/linkedin/callback',
passport.authenticate('linkedin', { failureRedirect: '/' }),
(req, res) => res.redirect('/')
);
// --- API Routes ---
// API endpoint to get the list of configured OAuth providers
app.get('/api/auth/providers', (req, res) => {
const providers = [];
if (process.env.GITHUB_CLIENT_ID) providers.push('github');
if (process.env.GOOGLE_CLIENT_ID) providers.push('google');
if (process.env.LINKEDIN_CLIENT_ID) providers.push('linkedin');
res.json(providers);
});
// Middleware to ensure a user is authenticated
const ensureAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}
res.status(401).json({ error: 'User not authenticated' });
};
// Get current user
app.get('/api/user', (req, res) => {
if (req.isAuthenticated()) {
res.json({ user: req.user });
} else {
res.json({ user: null });
}
});
// GET endpoint to retrieve all user data (configs and stats)
app.get('/api/sync', ensureAuthenticated, async (req, res) => {
try {
const data = await db.all('SELECT type, key, value FROM user_data WHERE user_id = ?', [req.user.id]);
const result = {
configs: {},
cardStats: {}
};
data.forEach(row => {
if (row.type === 'configs') {
result.configs[row.key] = JSON.parse(row.value);
} else if (row.type === 'cardStat') {
result.cardStats[row.key] = JSON.parse(row.value);
}
});
res.json(result);
} catch (error) {
console.error('Error fetching user data:', error);
res.status(500).json({ error: 'Failed to retrieve data' });
}
});
// POST endpoint to bulk save user data
app.post('/api/sync', ensureAuthenticated, async (req, res) => {
const { configs, cardStats } = req.body;
if (!configs && !cardStats) {
return res.status(400).json({ error: 'No data provided to sync' });
}
const userId = req.user.id;
try {
await db.run('BEGIN TRANSACTION');
const upsert = async (type, key, value) => {
const valueJson = JSON.stringify(value);
await db.run(`
INSERT INTO user_data (user_id, type, key, value, updated_at)
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(user_id, type, key) DO UPDATE SET
value = excluded.value,
updated_at = CURRENT_TIMESTAMP
`, [userId, type, key, valueJson]);
};
if (configs) {
for (const key in configs) {
await upsert('configs', key, configs[key]);
}
}
if (cardStats) {
for (const key in cardStats) {
await upsert('cardStat', key, cardStats[key]);
}
}
await db.run('COMMIT');
res.status(200).json({ message: 'Data saved successfully' });
} catch (error) {
await db.run('ROLLBACK');
console.error('Error saving user data:', error);
res.status(500).json({ error: 'Failed to save data' });
}
});
// --- Logging ingestion endpoint ---
// Accepts basic JSON logs from the client for debugging/diagnostics.
// Body shape: { category?: string, event?: string, payload?: any }
app.post('/api/logs', (req, res) => {
try {
const { category, event, payload } = req.body || {};
// Basic size/shape checks to avoid huge payloads hitting the console
if (!category && !event && !payload) {
return res.status(400).json({ error: 'Empty log payload' });
}
// Print a concise, structured log to the server console for now.
console.log('[CLIENT-LOG]', category || 'unknown', event || '-', JSON.stringify(payload));
// Respond quickly. In future we can persist or forward to telemetry.
res.status(200).json({ received: true });
} catch (err) {
console.error('Error receiving client log:', err);
res.status(500).json({ error: 'Failed to receive log' });
}
});
// Logout
app.post('/api/logout', (req, res, next) => {
req.logout((err) => {
if (err) { return next(err); }
req.session.destroy(() => {
res.clearCookie('connect.sid');
res.json({ message: 'Logged out successfully' });
});
});
});
// --- Serve Frontend ---
// All other GET requests not handled before will return the app
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '/', 'index.html'));
});
// --- Server Start ---
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});