-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
322 lines (279 loc) · 13.1 KB
/
server.ts
File metadata and controls
322 lines (279 loc) · 13.1 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import express from 'express';
import { createServer as createViteServer } from 'vite';
import cookieParser from 'cookie-parser';
import { google } from 'googleapis';
import fetch from 'node-fetch';
import crypto from 'crypto';
import path from 'path';
const app = express();
const PORT = 3000;
app.use(express.json());
app.use(cookieParser());
// In-memory or fallback secret for encryption/decryption
const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY || crypto.randomBytes(32).toString('hex');
const IV_LENGTH = 16;
function encrypt(text: string) {
if (!text) return text;
let iv = crypto.randomBytes(IV_LENGTH);
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY, 'hex').slice(0, 32), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
function decrypt(text: string) {
if (!text) return text;
let textParts = text.split(':');
let iv = Buffer.from(textParts.shift()!, 'hex');
let encryptedText = Buffer.from(textParts.join(':'), 'hex');
let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(ENCRYPTION_KEY, 'hex').slice(0, 32), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
// -------------------------------------------------------------
// Google Calendar OAuth
// -------------------------------------------------------------
function getGoogleAuth(redirectUri: string) {
return new google.auth.OAuth2(
process.env.GOOGLE_CLIENT_ID || 'mock-google-client-id',
process.env.GOOGLE_CLIENT_SECRET || 'mock-google-client-secret',
redirectUri
);
}
app.get('/api/calendar/auth/google/url', (req, res) => {
// Need exact redirectUri configured in Google Cloud
const redirectUri = `${req.headers['x-forwarded-proto'] || req.protocol}://${req.headers.host}/api/calendar/auth/google/callback`;
const oauth2Client = getGoogleAuth(redirectUri);
const url = oauth2Client.generateAuthUrl({
access_type: 'offline',
prompt: 'consent', // Force to get refresh token
scope: ['https://www.googleapis.com/auth/calendar.events']
});
res.json({ url });
});
app.get('/api/calendar/auth/google/callback', async (req, res) => {
const { code } = req.query;
if (!code) return res.send('No code returned');
try {
const redirectUri = `${req.headers['x-forwarded-proto'] || req.protocol}://${req.headers.host}/api/calendar/auth/google/callback`;
const oauth2Client = getGoogleAuth(redirectUri);
const { tokens } = await oauth2Client.getToken(code as string);
const payload = JSON.stringify({
provider: 'google',
accessToken: encrypt(tokens.access_token || ''),
refreshToken: encrypt(tokens.refresh_token || ''),
expiryDate: tokens.expiry_date
});
res.send(`
<html><body><script>
if (window.opener) {
window.opener.postMessage({ type: 'CALENDAR_AUTH_SUCCESS', payload: ${payload} }, '*');
window.close();
} else {
window.location.href = '/settings';
}
</script></body></html>
`);
} catch (err) {
console.error('Google Auth Error:', err);
res.send('Authentication failed');
}
});
// -------------------------------------------------------------
// Outlook Calendar OAuth (MS Graph)
// -------------------------------------------------------------
app.get('/api/calendar/auth/outlook/url', (req, res) => {
const redirectUri = `${req.headers['x-forwarded-proto'] || req.protocol}://${req.headers.host}/api/calendar/auth/outlook/callback`;
const clientId = process.env.OUTLOOK_CLIENT_ID || 'mock-outlook-client-id';
const params = new URLSearchParams({
client_id: clientId,
response_type: 'code',
redirect_uri: redirectUri,
response_mode: 'query',
scope: 'offline_access Calendars.ReadWrite',
});
res.json({ url: `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?${params}` });
});
app.get('/api/calendar/auth/outlook/callback', async (req, res) => {
const { code } = req.query;
if (!code) return res.send('No code returned');
try {
const redirectUri = `${req.headers['x-forwarded-proto'] || req.protocol}://${req.headers.host}/api/calendar/auth/outlook/callback`;
const clientId = process.env.OUTLOOK_CLIENT_ID || 'mock-outlook-client-id';
const clientSecret = process.env.OUTLOOK_CLIENT_SECRET || 'mock-outlook-client-secret';
const tokenRes = await fetch('https://login.microsoftonline.com/common/oauth2/v2.0/token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
client_id: clientId,
scope: 'offline_access Calendars.ReadWrite',
code: code as string,
redirect_uri: redirectUri,
grant_type: 'authorization_code',
client_secret: clientSecret
})
});
const tokens: any = await tokenRes.json();
if (!tokens.access_token) throw new Error('No access token');
const payload = JSON.stringify({
provider: 'outlook',
accessToken: encrypt(tokens.access_token),
refreshToken: encrypt(tokens.refresh_token),
expiryDate: Date.now() + (tokens.expires_in * 1000)
});
res.send(`
<html><body><script>
if (window.opener) {
window.opener.postMessage({ type: 'CALENDAR_AUTH_SUCCESS', payload: ${payload} }, '*');
window.close();
} else {
window.location.href = '/settings';
}
</script></body></html>
`);
} catch (err) {
console.error('Outlook Auth Error:', err);
res.send('Authentication failed');
}
});
// -------------------------------------------------------------
// Calendar Sync Event Handler
// -------------------------------------------------------------
app.post('/api/calendar/sync-event', async (req, res) => {
try {
const { provider, accessToken, refreshToken, eventData } = req.body;
// eventData: { id, title, start, end, description, action: 'create' | 'update' | 'delete', externalEventId? }
const plainAccess = decrypt(accessToken);
const plainRefresh = decrypt(refreshToken);
let externalId = eventData.externalEventId;
if (provider === 'google') {
const oauth2Client = getGoogleAuth('');
oauth2Client.setCredentials({ access_token: plainAccess, refresh_token: plainRefresh });
const calendar = google.calendar({ version: 'v3', auth: oauth2Client });
if (eventData.action === 'delete' && externalId) {
await calendar.events.delete({ calendarId: 'primary', eventId: externalId });
} else {
const gEvent = {
summary: eventData.title,
description: eventData.description,
start: { dateTime: new Date(eventData.start).toISOString() },
end: { dateTime: new Date(eventData.end).toISOString() }
};
if (eventData.action === 'update' && externalId) {
await calendar.events.update({ calendarId: 'primary', eventId: externalId, requestBody: gEvent });
} else {
const created = await calendar.events.insert({ calendarId: 'primary', requestBody: gEvent });
externalId = created.data.id;
}
}
} else if (provider === 'outlook') {
// Very simplified Outlook sync payload
const headers = { 'Authorization': `Bearer ${plainAccess}`, 'Content-Type': 'application/json' };
if (eventData.action === 'delete' && externalId) {
await fetch(`https://graph.microsoft.com/v1.0/me/events/${externalId}`, { method: 'DELETE', headers });
} else {
const mEvent = {
subject: eventData.title,
body: { contentType: 'Text', content: eventData.description },
start: { dateTime: new Date(eventData.start).toISOString(), timeZone: 'UTC' },
end: { dateTime: new Date(eventData.end).toISOString(), timeZone: 'UTC' }
};
if (eventData.action === 'update' && externalId) {
await fetch(`https://graph.microsoft.com/v1.0/me/events/${externalId}`, {
method: 'PATCH', headers, body: JSON.stringify(mEvent)
});
} else {
const outRes = await fetch('https://graph.microsoft.com/v1.0/me/events', {
method: 'POST', headers, body: JSON.stringify(mEvent)
});
const created: any = await outRes.json();
externalId = created.id;
}
}
}
res.json({ success: true, externalEventId: externalId });
} catch (err: any) {
console.error('Sync Error:', err);
res.status(500).json({ success: false, error: err.message || 'Unknown error during sync' });
}
});
app.post('/api/calendar/free-busy', async (req, res) => {
try {
const { provider, accessToken, refreshToken, timeMin, timeMax } = req.body;
const plainAccess = decrypt(accessToken);
const plainRefresh = decrypt(refreshToken);
const busyBlocks: { start: string, end: string }[] = [];
if (provider === 'google') {
const oauth2Client = getGoogleAuth('');
oauth2Client.setCredentials({ access_token: plainAccess, refresh_token: plainRefresh });
const calendar = google.calendar({ version: 'v3', auth: oauth2Client });
const fbRes = await calendar.freebusy.query({
requestBody: {
timeMin,
timeMax,
items: [{ id: 'primary' }]
}
});
const busy = fbRes.data.calendars?.['primary']?.busy || [];
busy.forEach(b => {
if (b.start && b.end) busyBlocks.push({ start: b.start, end: b.end });
});
} else if (provider === 'outlook') {
const headers = {
'Authorization': `Bearer ${plainAccess}`,
'Content-Type': 'application/json',
'Prefer': 'outlook.timezone="UTC"'
};
// Get user's email first for the free/busy query
const profileRes = await fetch('https://graph.microsoft.com/v1.0/me', { headers });
const profileData: any = await profileRes.json();
const scheduleRes = await fetch('https://graph.microsoft.com/v1.0/me/calendar/getSchedule', {
method: 'POST',
headers,
body: JSON.stringify({
schedules: [profileData.userPrincipalName],
startTime: { dateTime: timeMin, timeZone: 'UTC' },
endTime: { dateTime: timeMax, timeZone: 'UTC' },
availabilityViewInterval: 60
})
});
const scheduleData: any = await scheduleRes.json();
if (scheduleData.value && scheduleData.value.length > 0) {
const items = scheduleData.value[0].scheduleItems || [];
items.forEach((item: any) => {
if (item.status === 'busy' || item.status === 'oof' || item.status === 'tentative') {
busyBlocks.push({
start: item.start.dateTime + 'Z',
end: item.end.dateTime + 'Z'
});
}
});
}
}
res.json({ success: true, busyBlocks });
} catch (err: any) {
console.error('Free/Busy Error:', err);
// Explicitly format errors like token expired
res.status(500).json({ success: false, error: err.message || 'Unknown error getting free/busy' });
}
});
async function startServer() {
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), 'dist');
app.use(express.static(distPath));
app.get('*all', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();