File size: 4,528 Bytes
f4656a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';

const crypto = require('crypto');
const jwt = require('jsonwebtoken');
const logger = require('../utils/logger');

const JWT_SECRET = process.env.JWT_SECRET || 'dev-secret-change-in-production';

/**
 * Require valid JWT — checks Authorization header OR smart_token cookie.
 * Returns 401 JSON if missing or invalid (for API routes).
 */
function requireAuth(req, res, next) {
  const header = req.headers['authorization'];
  const cookieToken = req.cookies && req.cookies.smart_token;
  const token = (header && header.startsWith('Bearer ') ? header.slice(7) : null) || cookieToken;

  if (!token) {
    return res.status(401).json({ error: 'Authentication required' });
  }
  try {
    req.user = jwt.verify(token, JWT_SECRET);
    next();
  } catch (err) {
    return res.status(401).json({ error: 'Invalid or expired token' });
  }
}

/**
 * Auth guard for HTML pages — redirects to /login.html if no valid token.
 * Checks Authorization header OR smart_token cookie.
 */
function requireAuthPage(req, res, next) {
  const cookieToken = req.cookies && req.cookies.smart_token;
  const header = req.headers['authorization'];
  const token = (header && header.startsWith('Bearer ') ? header.slice(7) : null) || cookieToken;

  if (!token) {
    return res.redirect('/login.html');
  }
  try {
    req.user = jwt.verify(token, JWT_SECRET);
    next();
  } catch (err) {
    res.clearCookie('smart_token');
    return res.redirect('/login.html');
  }
}

/**
 * Optional JWT — attaches req.user if token present; never blocks.
 */
function optionalAuth(req, res, next) {
  const header = req.headers['authorization'];
  if (header && header.startsWith('Bearer ')) {
    try {
      req.user = jwt.verify(header.slice(7), JWT_SECRET);
    } catch {
      // ignore invalid token in optional context
    }
  }
  next();
}

/**
 * API key authentication middleware for internal endpoints.
 */
function apiKeyAuth(req, res, next) {
  const apiKey = req.headers['x-api-key'] || req.query.api_key;

  if (!apiKey || apiKey !== process.env.API_KEY) {
    logger.warn(`Unauthorized request from ${req.ip} to ${req.path}`);
    return res.status(401).json({ error: 'Unauthorized', message: 'Valid API key required' });
  }

  next();
}

/**
 * Twilio webhook signature verification.
 * Validates that incoming requests are genuinely from Twilio.
 */
function verifyTwilioSignature(req, res, next) {
  // Skip in test/development if no auth token is set
  if (!process.env.TWILIO_AUTH_TOKEN || process.env.NODE_ENV === 'test') {
    return next();
  }

  const twilioSignature = req.headers['x-twilio-signature'];
  if (!twilioSignature) {
    return res.status(403).json({ error: 'Missing Twilio signature' });
  }

  try {
    const { validateRequest } = require('twilio');
    const url = `${process.env.APP_URL}${req.originalUrl}`;
    const isValid = validateRequest(process.env.TWILIO_AUTH_TOKEN, twilioSignature, url, req.body || {});

    if (!isValid) {
      logger.warn(`Invalid Twilio signature from ${req.ip}`);
      return res.status(403).json({ error: 'Invalid Twilio signature' });
    }

    next();
  } catch (err) {
    logger.error('Twilio signature verification error:', err);
    return res.status(403).json({ error: 'Signature verification failed' });
  }
}

/**
 * Meta/Instagram webhook signature verification.
 * Uses HMAC-SHA256 with APP_SECRET to validate X-Hub-Signature-256 header.
 */
function verifyMetaSignature(req, res, next) {
  if (!process.env.META_APP_SECRET || process.env.NODE_ENV === 'test') {
    return next();
  }

  const signature = req.headers['x-hub-signature-256'];
  if (!signature) {
    return res.status(403).json({ error: 'Missing Meta signature' });
  }

  const rawBody = req.rawBody || JSON.stringify(req.body);
  const expectedSig = 'sha256=' + crypto
    .createHmac('sha256', process.env.META_APP_SECRET)
    .update(rawBody)
    .digest('hex');

  if (!crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expectedSig))) {
    logger.warn(`Invalid Meta signature from ${req.ip}`);
    return res.status(403).json({ error: 'Invalid Meta signature' });
  }

  next();
}

/**
 * Capture raw body for signature verification before JSON parsing.
 */
function captureRawBody(req, res, next) {
  let data = '';
  req.on('data', (chunk) => { data += chunk; });
  req.on('end', () => { req.rawBody = data; next(); });
}

module.exports = { apiKeyAuth, verifyTwilioSignature, verifyMetaSignature, captureRawBody, requireAuth, optionalAuth, requireAuthPage };