feat: rediseño UI completo + infra email + stats
This commit is contained in:
parent
93d75ddafe
commit
24401c0ee5
37 changed files with 2162 additions and 412 deletions
|
|
@ -1,30 +1,49 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* RESETEA.NET — OAuth Gmail
|
||||
*
|
||||
* Flujo:
|
||||
* GET /api/gmail/auth → redirige a Google para autorización
|
||||
* GET /api/gmail/callback → intercambia code por token de un solo uso
|
||||
* POST /api/gmail/send → envía la carta GDPR desde el Gmail del usuario
|
||||
* (el token se usa y se descarta — nunca se persiste)
|
||||
*
|
||||
* PREREQUISITO:
|
||||
* Crea un proyecto en Google Cloud Console:
|
||||
* https://console.cloud.google.com/
|
||||
* → Habilita "Gmail API"
|
||||
* → Crea credenciales OAuth2 (tipo "Aplicación web")
|
||||
* → URI de redirección: https://resetea.net/api/gmail/callback
|
||||
* → Copia GOOGLE_CLIENT_ID y GOOGLE_CLIENT_SECRET en .env
|
||||
*/
|
||||
|
||||
const { google } = require('googleapis');
|
||||
const crypto = require('crypto');
|
||||
const { google } = require('googleapis');
|
||||
const { buildLetterText, PROVIDER_DATA } = require('../services/mailer');
|
||||
|
||||
// ── Estado temporal en memoria (un objeto por token de sesión) ──
|
||||
// NO se persiste en disco. Si el servidor reinicia, se pierden los
|
||||
// tokens pendientes (el usuario debe repetir el flujo OAuth).
|
||||
const pendingSends = new Map(); // sessionId → { token, letterParams }
|
||||
const ALLOWED_PROVIDERS = new Set(Object.keys(PROVIDER_DATA));
|
||||
const STATE_MAX_BYTES = 4096;
|
||||
const SALT = () => process.env.SALT || 'resetea-oauth-default-insecure';
|
||||
|
||||
/* ── Elimina CRLF y tabuladores — previene header injection en RFC 2822 ── */
|
||||
function sanitizeHeader(str, maxLen = 300) {
|
||||
return String(str || '').replace(/[\r\n\t\x00-\x1F\x7F]/g, ' ').trim().slice(0, maxLen);
|
||||
}
|
||||
|
||||
/* ── Firma HMAC-SHA256 del state — previene state forgery en OAuth callback ── */
|
||||
function signState(payload) {
|
||||
const nonce = crypto.randomBytes(16).toString('hex');
|
||||
const data = { ...payload, _nonce: nonce };
|
||||
const sig = crypto.createHmac('sha256', SALT())
|
||||
.update(JSON.stringify(data))
|
||||
.digest('hex');
|
||||
return Buffer.from(JSON.stringify({ ...data, _sig: sig })).toString('base64url');
|
||||
}
|
||||
|
||||
function verifyState(raw) {
|
||||
if (!raw || typeof raw !== 'string') throw new Error('State ausente');
|
||||
if (Buffer.byteLength(raw, 'utf8') > STATE_MAX_BYTES) throw new Error('State demasiado grande');
|
||||
|
||||
let obj;
|
||||
try { obj = JSON.parse(Buffer.from(raw, 'base64url').toString('utf8')); }
|
||||
catch { throw new Error('State malformado'); }
|
||||
|
||||
const { _sig, ...data } = obj;
|
||||
if (typeof _sig !== 'string' || _sig.length !== 64) throw new Error('Firma ausente o malformada');
|
||||
|
||||
const expected = crypto.createHmac('sha256', SALT()).update(JSON.stringify(data)).digest('hex');
|
||||
const sigBuf = Buffer.from(_sig, 'hex');
|
||||
const expectedBuf = Buffer.from(expected, 'hex');
|
||||
|
||||
if (!crypto.timingSafeEqual(sigBuf, expectedBuf)) throw new Error('Firma inválida');
|
||||
|
||||
/* Devuelve payload sin los campos internos */
|
||||
const { _nonce, ...params } = data;
|
||||
return params;
|
||||
}
|
||||
|
||||
function getOAuth2Client() {
|
||||
return new google.auth.OAuth2(
|
||||
|
|
@ -34,25 +53,31 @@ function getOAuth2Client() {
|
|||
);
|
||||
}
|
||||
|
||||
// ── GET /api/gmail/auth ──────────────────────────────────────────
|
||||
// El frontend envía los parámetros de la carta como query params
|
||||
// para que los podamos recuperar en el callback.
|
||||
/* ── GET /api/gmail/auth ─────────────────────────────────────────── */
|
||||
exports.authInit = (req, res) => {
|
||||
const { provider, name, email, nickname, phone, address, extra, requestType } = req.query;
|
||||
|
||||
if (!provider || !email || !name) {
|
||||
if (!provider || !email || !name)
|
||||
return res.status(400).json({ error: 'Faltan parámetros obligatorios (provider, name, email).' });
|
||||
}
|
||||
if (!PROVIDER_DATA[provider]) {
|
||||
return res.status(400).json({ error: 'Proveedor no soportado.' });
|
||||
}
|
||||
|
||||
// Guardamos el state en base64 (no sensible: no contiene credenciales)
|
||||
const state = Buffer.from(JSON.stringify({ provider, name, email, nickname, phone, address, extra, requestType })).toString('base64url');
|
||||
if (!ALLOWED_PROVIDERS.has(provider))
|
||||
return res.status(400).json({ error: 'Proveedor no soportado.' });
|
||||
|
||||
/* Sanitizamos los campos que luego irán a cabeceras de email */
|
||||
const state = signState({
|
||||
provider,
|
||||
name: sanitizeHeader(name, 200),
|
||||
email: sanitizeHeader(email, 200),
|
||||
nickname: sanitizeHeader(nickname, 100),
|
||||
phone: sanitizeHeader(phone, 30),
|
||||
address: sanitizeHeader(address, 300),
|
||||
extra: sanitizeHeader(extra, 500),
|
||||
requestType: sanitizeHeader(requestType, 20),
|
||||
});
|
||||
|
||||
const oauth2Client = getOAuth2Client();
|
||||
const authUrl = oauth2Client.generateAuthUrl({
|
||||
access_type: 'online', // no refresh token — uso puntual
|
||||
access_type: 'online',
|
||||
scope: ['https://www.googleapis.com/auth/gmail.send'],
|
||||
prompt: 'consent',
|
||||
state,
|
||||
|
|
@ -61,48 +86,55 @@ exports.authInit = (req, res) => {
|
|||
res.redirect(authUrl);
|
||||
};
|
||||
|
||||
// ── GET /api/gmail/callback ──────────────────────────────────────
|
||||
/* ── GET /api/gmail/callback ─────────────────────────────────────── */
|
||||
exports.authCallback = async (req, res) => {
|
||||
const { code, state, error } = req.query;
|
||||
|
||||
if (error) {
|
||||
return res.redirect('/plantillas.html?oauth=cancelled');
|
||||
}
|
||||
if (!code || !state) {
|
||||
if (error) return res.redirect('/plantillas.html?oauth=cancelled');
|
||||
|
||||
if (!code || !state)
|
||||
return res.status(400).send('Parámetros OAuth inválidos.');
|
||||
}
|
||||
|
||||
let params;
|
||||
try {
|
||||
params = JSON.parse(Buffer.from(state, 'base64url').toString());
|
||||
} catch {
|
||||
return res.status(400).send('State OAuth inválido.');
|
||||
params = verifyState(state);
|
||||
} catch (e) {
|
||||
console.warn('OAuth state rejection:', e.message);
|
||||
return res.status(400).send('State OAuth inválido o expirado.');
|
||||
}
|
||||
|
||||
/* Validar provider contra whitelist (por si el state fue manipulado antes de que
|
||||
implementáramos la firma, o si la firma falla silenciosamente en el futuro) */
|
||||
if (!ALLOWED_PROVIDERS.has(params.provider)) {
|
||||
return res.status(400).send('Proveedor no soportado.');
|
||||
}
|
||||
|
||||
try {
|
||||
const oauth2Client = getOAuth2Client();
|
||||
const { tokens } = await oauth2Client.getToken(code);
|
||||
|
||||
// Enviamos el email inmediatamente (no almacenamos el token)
|
||||
const { tokens } = await oauth2Client.getToken(code);
|
||||
oauth2Client.setCredentials(tokens);
|
||||
const gmail = google.gmail({ version: 'v1', auth: oauth2Client });
|
||||
|
||||
const gmail = google.gmail({ version: 'v1', auth: oauth2Client });
|
||||
const providerInfo = PROVIDER_DATA[params.provider];
|
||||
|
||||
if (!providerInfo.email) {
|
||||
return res.redirect(`/plantillas.html?oauth=no_email&provider=${params.provider}&formUrl=${encodeURIComponent(providerInfo.formUrl || '')}`);
|
||||
return res.redirect(
|
||||
`/plantillas.html?oauth=no_email&provider=${encodeURIComponent(params.provider)}` +
|
||||
`&formUrl=${encodeURIComponent(providerInfo.formUrl || '')}`
|
||||
);
|
||||
}
|
||||
|
||||
const letterText = buildLetterText({
|
||||
providerInfo,
|
||||
senderName: params.name,
|
||||
senderEmail: params.email,
|
||||
senderNick: params.nickname || '',
|
||||
senderPhone: params.phone || '',
|
||||
senderAddress: params.address || '',
|
||||
extra: params.extra || '',
|
||||
senderNick: params.nickname || '',
|
||||
senderPhone: params.phone || '',
|
||||
senderAddress: params.address || '',
|
||||
extra: params.extra || '',
|
||||
});
|
||||
|
||||
// Construir email RFC 2822 en base64url
|
||||
/* Cabeceras RFC 2822 — name y email ya vienen sanitizados desde authInit */
|
||||
const subject = `Ejercicio derecho de supresión (RGPD Art. 17) — ${providerInfo.name}`;
|
||||
const rawEmail = [
|
||||
`From: ${params.name} <${params.email}>`,
|
||||
|
|
@ -114,14 +146,11 @@ exports.authCallback = async (req, res) => {
|
|||
letterText,
|
||||
].join('\r\n');
|
||||
|
||||
const encoded = Buffer.from(rawEmail).toString('base64url');
|
||||
|
||||
await gmail.users.messages.send({
|
||||
userId: 'me',
|
||||
requestBody: { raw: encoded },
|
||||
requestBody: { raw: Buffer.from(rawEmail).toString('base64url') },
|
||||
});
|
||||
|
||||
// Token descartado aquí (fuera de scope, GC lo recogerá)
|
||||
res.redirect(`/plantillas.html?oauth=ok&provider=${encodeURIComponent(providerInfo.name)}`);
|
||||
|
||||
} catch (err) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue