Update app.js
Browse files
app.js
CHANGED
|
@@ -35,6 +35,28 @@ app.post('/internal/notify', (req, res) => {
|
|
| 35 |
res.json({ success: false });
|
| 36 |
});
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
// WS Upgrade
|
| 39 |
server.on('upgrade', async (request, socket, head) => {
|
| 40 |
const url = new URL(request.url, `http://${request.headers.host}`);
|
|
@@ -42,9 +64,11 @@ server.on('upgrade', async (request, socket, head) => {
|
|
| 42 |
|
| 43 |
if (!token) { socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); socket.destroy(); return; }
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
wss.handleUpgrade(request, socket, head, (ws) => {
|
| 49 |
wss.emit('connection', ws, request, user);
|
| 50 |
});
|
|
|
|
| 35 |
res.json({ success: false });
|
| 36 |
});
|
| 37 |
|
| 38 |
+
// Inside Gateway WebSocket upgrade or API middleware
|
| 39 |
+
async function verifyThrustToken(token) {
|
| 40 |
+
const decoded = jwt.decode(token);
|
| 41 |
+
if (!decoded || !decoded.sid) return null;
|
| 42 |
+
|
| 43 |
+
// 1. Fetch the secret and verify existence in one go
|
| 44 |
+
const { data: session } = await supabase
|
| 45 |
+
.from('user_sessions')
|
| 46 |
+
.select('session_secret')
|
| 47 |
+
.eq('id', decoded.sid)
|
| 48 |
+
.single();
|
| 49 |
+
|
| 50 |
+
if (!session) return null; // Session was deleted/revoked
|
| 51 |
+
|
| 52 |
+
try {
|
| 53 |
+
// 2. Verify signature against the stored secret
|
| 54 |
+
return jwt.verify(token, session.session_secret);
|
| 55 |
+
} catch (e) {
|
| 56 |
+
return null;
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
// WS Upgrade
|
| 61 |
server.on('upgrade', async (request, socket, head) => {
|
| 62 |
const url = new URL(request.url, `http://${request.headers.host}`);
|
|
|
|
| 64 |
|
| 65 |
if (!token) { socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); socket.destroy(); return; }
|
| 66 |
|
| 67 |
+
// const { data: { user }, error } = await supabase.auth.getUser(token);
|
| 68 |
+
const isTrue= verifyThrustToken(token);
|
| 69 |
+
// if (error || !user) { socket.write('HTTP/1.1 403 Forbidden\r\n\r\n'); socket.destroy(); return; }
|
| 70 |
+
if (isTrue || isTrue != null) { socket.write('HTTP/1.1 403 Forbidden\r\n\r\n'); socket.destroy(); return; }
|
| 71 |
+
|
| 72 |
wss.handleUpgrade(request, socket, head, (ws) => {
|
| 73 |
wss.emit('connection', ws, request, user);
|
| 74 |
});
|