-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (119 loc) · 5.01 KB
/
index.js
File metadata and controls
129 lines (119 loc) · 5.01 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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const path = require('path');
const express = require('express');
const cors = require('cors'); // <-- Agregado
const { rotateMasterKey } = require('./src/keyManager');
const { registerDevice, getMasterKeyForDevice } = require('./src/deviceManager');
const { isScheduleEvent, isHttpEvent } = require('./src/helpers');
const logger = require('./src/logger');
// Appwrite cron event
if (isScheduleEvent()) {
logger.info('Schedule event detected — rotating master key');
rotateMasterKey()
.then(() => {
logger.info('Master key rotated');
process.exit(0);
})
.catch(err => {
logger.error('Rotation error:', err);
process.exit(1);
});
} else if (isHttpEvent()) {
// Appwrite HTTP event
module.exports = async (req, res) => {
if (req.method === 'POST' && req.url === '/devices/register') {
let body = '';
req.on('data', chunk => { body += chunk; });
req.on('end', async () => {
const { uuid } = JSON.parse(body);
if (!uuid) return res.writeHead(400).end('Missing uuid');
const signature = await registerDevice(uuid);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ signature }));
});
} else if (req.method === 'POST' && req.url === '/keys/get') {
let body = '';
req.on('data', chunk => { body += chunk; });
req.on('end', async () => {
const { signature } = JSON.parse(body);
if (!signature) return res.writeHead(400).end('Missing signature');
const encrypted = await getMasterKeyForDevice(signature);
if (!encrypted) return res.writeHead(404).end('Device not found');
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ masterKey: encrypted }));
});
} else {
res.writeHead(404).end('Not found');
}
};
} else {
// Local dev server with Express
const app = express();
// ✅ Habilitar CORS para todos los orígenes (para que fetch funcione desde tu HTML)
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.post('/devices/register', async (req, res) => {
const { uuid } = req.body;
if (!uuid) return res.status(400).json({ error: 'Missing uuid' });
logger.info('[registerDevice] Iniciando registro con UUID:', uuid);
try {
const signature = await registerDevice(uuid);
logger.info('[registerDevice] Registro exitoso, signature:', signature);
res.json({ signature });
} catch (err) {
logger.error('[registerDevice] Error:', err);
if (err && err.statusCode === 409) return res.status(409).json({ error: err.message });
res.status(500).json({ error: err.message || 'Internal error' });
}
});
app.post('/keys/get', async (req, res) => {
const { signature } = req.body;
if (!signature) return res.status(400).json({ error: 'Missing signature' });
logger.info('[getMasterKey] Solicitando master key para signature:', signature);
try {
const encrypted = await getMasterKeyForDevice(signature);
if (!encrypted) {
logger.warn('[getMasterKey] Dispositivo no encontrado para signature:', signature);
return res.status(404).json({ error: 'Device not found' });
}
logger.info('[getMasterKey] Master key obtenida');
res.json({ masterKey: encrypted });
} catch (err) {
logger.error('[getMasterKey] Error:', err);
res.status(500).json({ error: err.message });
}
});
app.post('/credentials/get', async (req, res) => {
const { signature } = req.body;
if (!signature) return res.status(400).json({ error: 'Missing signature' });
logger.info('[getCredentials] Solicitando credenciales para signature:', signature);
try {
const { getEncryptedEnvCredentialsForDevice } = require('./src/keyManager');
const payload = await getEncryptedEnvCredentialsForDevice(signature);
res.json(payload);
} catch (err) {
logger.error('[getCredentials] Error:', err);
res.status(500).json({ error: err.message || 'Internal error' });
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
logger.info(`Express server running on http://localhost:${PORT}`);
logger.info('ENV VARS:', {
APPWRITE_PROJECT_ID: process.env.APPWRITE_PROJECT_ID,
APPWRITE_API_KEY: process.env.APPWRITE_API_KEY ? '***' : undefined,
APPWRITE_ENDPOINT: process.env.APPWRITE_ENDPOINT,
APPWRITE_DATABASE_ID: process.env.APPWRITE_DATABASE_ID,
SECURE_KEY_COLLECTION_ID: process.env.SECURE_KEY_COLLECTION_ID,
DEVICE_COLLECTION_ID: process.env.DEVICE_COLLECTION_ID,
BACKEND_SECRET: process.env.BACKEND_SECRET ? '***' : undefined,
NODE_ENV: process.env.NODE_ENV,
});
});
}