Spaces:
Running
Running
Update app.js
Browse files
app.js
CHANGED
|
@@ -43,7 +43,7 @@ const verifyFirebaseUser = async (req, res, next) => {
|
|
| 43 |
const debugMode = process.env.DEBUG_NO_AUTH === 'true';
|
| 44 |
|
| 45 |
if (debugMode) {
|
| 46 |
-
req.user = { uid: "
|
| 47 |
return next();
|
| 48 |
}
|
| 49 |
|
|
@@ -121,7 +121,6 @@ app.post('/redeem', async (req, res) => {
|
|
| 121 |
const data = tempKeys.get(key);
|
| 122 |
const sessionSecret = uuidv4();
|
| 123 |
|
| 124 |
-
// Set expiresIn to '3d' (3 days)
|
| 125 |
const token = jwt.sign(
|
| 126 |
{ uid: data.uid, projectId: data.projectId },
|
| 127 |
sessionSecret,
|
|
@@ -141,7 +140,6 @@ app.post('/redeem', async (req, res) => {
|
|
| 141 |
res.json({ token });
|
| 142 |
});
|
| 143 |
|
| 144 |
-
// --- NEW ENDPOINT: Simple Verification Check ---
|
| 145 |
app.post('/verify', async (req, res) => {
|
| 146 |
const { token } = req.body;
|
| 147 |
if (!token) return res.status(400).json({ valid: false, error: 'Token required' });
|
|
@@ -159,7 +157,6 @@ app.post('/verify', async (req, res) => {
|
|
| 159 |
|
| 160 |
try {
|
| 161 |
jwt.verify(token, secret);
|
| 162 |
-
// Explicitly check 3 day max age
|
| 163 |
const threeDaysInSeconds = 3 * 24 * 60 * 60;
|
| 164 |
const nowInSeconds = Math.floor(Date.now() / 1000);
|
| 165 |
if (decoded.iat && (nowInSeconds - decoded.iat > threeDaysInSeconds)) {
|
|
@@ -172,8 +169,49 @@ app.post('/verify', async (req, res) => {
|
|
| 172 |
}
|
| 173 |
});
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
app.post('/poll', async (req, res) => {
|
| 176 |
-
const { token
|
| 177 |
|
| 178 |
if (!token) return res.status(400).json({ error: 'Token required' });
|
| 179 |
|
|
@@ -195,7 +233,6 @@ app.post('/poll', async (req, res) => {
|
|
| 195 |
const nowInSeconds = Math.floor(Date.now() / 1000);
|
| 196 |
|
| 197 |
if (verifiedData.iat && (nowInSeconds - verifiedData.iat > threeDaysInSeconds)) {
|
| 198 |
-
console.log("⚠️ Token is valid signature, but too old.");
|
| 199 |
return res.status(403).json({ error: 'Token expired (older than 3 days)' });
|
| 200 |
}
|
| 201 |
|
|
@@ -203,8 +240,8 @@ app.post('/poll', async (req, res) => {
|
|
| 203 |
|
| 204 |
try {
|
| 205 |
const response = await axios.post(externalUrl, {
|
| 206 |
-
projectId: verifiedData.projectId
|
| 207 |
-
//
|
| 208 |
});
|
| 209 |
|
| 210 |
return res.json({ status: 'success', externalResponse: response.data });
|
|
@@ -221,7 +258,7 @@ app.post('/poll', async (req, res) => {
|
|
| 221 |
});
|
| 222 |
|
| 223 |
app.get('/cleanup', (req, res) => {
|
| 224 |
-
const THRESHOLD = 1000 * 60 * 60;
|
| 225 |
const now = Date.now();
|
| 226 |
let cleanedCount = 0;
|
| 227 |
|
|
@@ -231,7 +268,6 @@ app.get('/cleanup', (req, res) => {
|
|
| 231 |
cleanedCount++;
|
| 232 |
}
|
| 233 |
}
|
| 234 |
-
|
| 235 |
for (const [key, value] of tempKeys.entries()) {
|
| 236 |
if (now - value.createdAt > (1000 * 60 * 4)) {
|
| 237 |
tempKeys.delete(key);
|
|
@@ -245,7 +281,6 @@ app.post('/nullify', verifyFirebaseUser, async (req, res) => {
|
|
| 245 |
if (!projectId) return res.status(400).json({ error: 'projectId required' });
|
| 246 |
|
| 247 |
const cacheKey = `${req.user.uid}:${projectId}`;
|
| 248 |
-
|
| 249 |
const existedInMemory = activeSessions.delete(cacheKey);
|
| 250 |
|
| 251 |
let deletedTempKeys = 0;
|
|
@@ -264,10 +299,10 @@ app.post('/nullify', verifyFirebaseUser, async (req, res) => {
|
|
| 264 |
}
|
| 265 |
}
|
| 266 |
|
| 267 |
-
console.log(`☢️ NULLIFIED session for ${cacheKey}
|
| 268 |
res.json({
|
| 269 |
success: true,
|
| 270 |
-
message: 'Session
|
| 271 |
wasCached: existedInMemory,
|
| 272 |
tempKeysRemoved: deletedTempKeys
|
| 273 |
});
|
|
|
|
| 43 |
const debugMode = process.env.DEBUG_NO_AUTH === 'true';
|
| 44 |
|
| 45 |
if (debugMode) {
|
| 46 |
+
req.user = { uid: "debug_user_001" };
|
| 47 |
return next();
|
| 48 |
}
|
| 49 |
|
|
|
|
| 121 |
const data = tempKeys.get(key);
|
| 122 |
const sessionSecret = uuidv4();
|
| 123 |
|
|
|
|
| 124 |
const token = jwt.sign(
|
| 125 |
{ uid: data.uid, projectId: data.projectId },
|
| 126 |
sessionSecret,
|
|
|
|
| 140 |
res.json({ token });
|
| 141 |
});
|
| 142 |
|
|
|
|
| 143 |
app.post('/verify', async (req, res) => {
|
| 144 |
const { token } = req.body;
|
| 145 |
if (!token) return res.status(400).json({ valid: false, error: 'Token required' });
|
|
|
|
| 157 |
|
| 158 |
try {
|
| 159 |
jwt.verify(token, secret);
|
|
|
|
| 160 |
const threeDaysInSeconds = 3 * 24 * 60 * 60;
|
| 161 |
const nowInSeconds = Math.floor(Date.now() / 1000);
|
| 162 |
if (decoded.iat && (nowInSeconds - decoded.iat > threeDaysInSeconds)) {
|
|
|
|
| 169 |
}
|
| 170 |
});
|
| 171 |
|
| 172 |
+
// --- NEW ENDPOINT: Feedback Forwarder ---
|
| 173 |
+
app.post('/feedback', async (req, res) => {
|
| 174 |
+
const { token, prompt, logs } = req.body;
|
| 175 |
+
|
| 176 |
+
if (!token || !prompt) return res.status(400).json({ error: 'Token and prompt required' });
|
| 177 |
+
|
| 178 |
+
// 1. Verify User Session
|
| 179 |
+
const decoded = jwt.decode(token);
|
| 180 |
+
if (!decoded || !decoded.uid || !decoded.projectId) {
|
| 181 |
+
return res.status(401).json({ error: 'Malformed token' });
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
const secret = await getSessionSecret(decoded.uid, decoded.projectId);
|
| 185 |
+
if (!secret) return res.status(404).json({ error: 'Session revoked' });
|
| 186 |
+
|
| 187 |
+
try {
|
| 188 |
+
jwt.verify(token, secret); // Validate signature
|
| 189 |
+
|
| 190 |
+
const externalBase = process.env.EXTERNAL_SERVER_URL || 'https://httpbin.org/post';
|
| 191 |
+
// Strip the query parameters or path from base if necessary, or just append
|
| 192 |
+
// Assuming EXTERNAL_SERVER_URL is the root (e.g. https://my-ai-api.com)
|
| 193 |
+
// We append /project/feedback
|
| 194 |
+
const targetUrl = externalBase.replace(/\/$/, '') + '/project/feedback';
|
| 195 |
+
|
| 196 |
+
console.log(`📨 Forwarding feedback for ${decoded.projectId} to ${targetUrl}`);
|
| 197 |
+
|
| 198 |
+
const response = await axios.post(targetUrl, {
|
| 199 |
+
user: decoded.uid,
|
| 200 |
+
projectId: decoded.projectId,
|
| 201 |
+
prompt: prompt,
|
| 202 |
+
logs: logs || ""
|
| 203 |
+
});
|
| 204 |
+
|
| 205 |
+
return res.json({ success: true, externalResponse: response.data });
|
| 206 |
+
|
| 207 |
+
} catch (err) {
|
| 208 |
+
console.error("Feedback Error:", err.message);
|
| 209 |
+
return res.status(502).json({ error: 'Failed to forward feedback to AI server' });
|
| 210 |
+
}
|
| 211 |
+
});
|
| 212 |
+
|
| 213 |
app.post('/poll', async (req, res) => {
|
| 214 |
+
const { token } = req.body;
|
| 215 |
|
| 216 |
if (!token) return res.status(400).json({ error: 'Token required' });
|
| 217 |
|
|
|
|
| 233 |
const nowInSeconds = Math.floor(Date.now() / 1000);
|
| 234 |
|
| 235 |
if (verifiedData.iat && (nowInSeconds - verifiedData.iat > threeDaysInSeconds)) {
|
|
|
|
| 236 |
return res.status(403).json({ error: 'Token expired (older than 3 days)' });
|
| 237 |
}
|
| 238 |
|
|
|
|
| 240 |
|
| 241 |
try {
|
| 242 |
const response = await axios.post(externalUrl, {
|
| 243 |
+
projectId: verifiedData.projectId
|
| 244 |
+
// Just polling for tasks, no user prompt here anymore
|
| 245 |
});
|
| 246 |
|
| 247 |
return res.json({ status: 'success', externalResponse: response.data });
|
|
|
|
| 258 |
});
|
| 259 |
|
| 260 |
app.get('/cleanup', (req, res) => {
|
| 261 |
+
const THRESHOLD = 1000 * 60 * 60;
|
| 262 |
const now = Date.now();
|
| 263 |
let cleanedCount = 0;
|
| 264 |
|
|
|
|
| 268 |
cleanedCount++;
|
| 269 |
}
|
| 270 |
}
|
|
|
|
| 271 |
for (const [key, value] of tempKeys.entries()) {
|
| 272 |
if (now - value.createdAt > (1000 * 60 * 4)) {
|
| 273 |
tempKeys.delete(key);
|
|
|
|
| 281 |
if (!projectId) return res.status(400).json({ error: 'projectId required' });
|
| 282 |
|
| 283 |
const cacheKey = `${req.user.uid}:${projectId}`;
|
|
|
|
| 284 |
const existedInMemory = activeSessions.delete(cacheKey);
|
| 285 |
|
| 286 |
let deletedTempKeys = 0;
|
|
|
|
| 299 |
}
|
| 300 |
}
|
| 301 |
|
| 302 |
+
console.log(`☢️ NULLIFIED session for ${cacheKey}.`);
|
| 303 |
res.json({
|
| 304 |
success: true,
|
| 305 |
+
message: 'Session purged.',
|
| 306 |
wasCached: existedInMemory,
|
| 307 |
tempKeysRemoved: deletedTempKeys
|
| 308 |
});
|