1773ed5f95
Mirror to GitHub / mirror (push) Successful in 26s
Test and Lint / backend-test (push) Successful in 1m11s
continuous-integration/drone/push Build is passing
Test and Lint / frontend-test (push) Successful in 2m28s
Version and Release / version-bump (push) Successful in 32s
Version and Release / trigger-drone (push) Has been skipped
Original: feat: enhance security logging and ensure rate limit blocks are properly tracked - Add comprehensive logging for rate limit blocks with full request details - IP address (with proper proxy detection), user agent, headers, timestamps - Rate limit info (current count, limit, remaining, reset time) - Separate tracking for auth vs general endpoints - Enhance authentication failure logging - JWT validation failures with detailed error info - Admin auth attempts without token - Failed token validation with user context - All events include IP, path, method, user agent - Improve Winston logger configuration for production - Add automatic log rotation (10MB errors, 50MB combined) - Create separate security.log for auth/rate limit events - Ensure logs directory exists automatically - Add structured JSON format for log aggregation - Support container logging with LOG_TO_CONSOLE env var - Create comprehensive documentation - Security logging guide with examples - Monitoring recommendations - Configuration reference - Add test script to verify logging functionality All rate limit settings remain configurable via admin panel: - Window duration, max requests, auth limits - Skip authenticated requests option - Public endpoints only option 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
167 lines
4.8 KiB
JavaScript
167 lines
4.8 KiB
JavaScript
const jwt = require('jsonwebtoken');
|
|
const { db } = require('../database/db');
|
|
const { formatBoolean } = require('../utils/dbCompat');
|
|
const { isTokenRevoked } = require('../utils/tokenRevocation');
|
|
const logger = require('../utils/logger');
|
|
|
|
/**
|
|
* Enhanced admin authentication middleware with revocation checking
|
|
*/
|
|
async function adminAuth(req, res, next) {
|
|
try {
|
|
const token = req.headers.authorization?.split(' ')[1];
|
|
if (!token) {
|
|
return res.status(401).json({ error: 'No token provided' });
|
|
}
|
|
|
|
let decoded;
|
|
try {
|
|
decoded = jwt.verify(token, process.env.JWT_SECRET, {
|
|
issuer: 'picpeak-auth',
|
|
complete: true
|
|
});
|
|
decoded = decoded.payload;
|
|
} catch (err) {
|
|
if (err.name === 'TokenExpiredError') {
|
|
return res.status(401).json({ error: 'Token expired', code: 'TOKEN_EXPIRED' });
|
|
}
|
|
return res.status(401).json({ error: 'Invalid token' });
|
|
}
|
|
|
|
// Check if token is revoked
|
|
if (await isTokenRevoked(decoded)) {
|
|
logger.warn('Revoked token used', {
|
|
userId: decoded.id,
|
|
tokenType: decoded.type
|
|
});
|
|
return res.status(401).json({ error: 'Token has been revoked', code: 'TOKEN_REVOKED' });
|
|
}
|
|
|
|
// Verify token type
|
|
if (decoded.type !== 'admin') {
|
|
logger.warn('Non-admin token used for admin endpoint', {
|
|
userId: decoded.id,
|
|
tokenType: decoded.type
|
|
});
|
|
return res.status(403).json({ error: 'Insufficient permissions' });
|
|
}
|
|
|
|
// IP validation (optional - can be strict or just log)
|
|
const currentIp = req.ip || req.connection.remoteAddress;
|
|
if (decoded.ip && decoded.ip !== currentIp) {
|
|
logger.warn('Token used from different IP', {
|
|
userId: decoded.id,
|
|
tokenIp: decoded.ip,
|
|
currentIp: currentIp
|
|
});
|
|
}
|
|
|
|
// Check if admin still exists and is active
|
|
const admin = await db('admin_users')
|
|
.where({ id: decoded.id, is_active: formatBoolean(true) })
|
|
.first();
|
|
|
|
if (!admin) {
|
|
return res.status(401).json({ error: 'Invalid token' });
|
|
}
|
|
|
|
// Check if password was changed after token was issued
|
|
if (admin.password_changed_at) {
|
|
const passwordChangedTime = new Date(admin.password_changed_at).getTime() / 1000;
|
|
if (decoded.iat < passwordChangedTime) {
|
|
logger.warn('Token used after password change', { userId: decoded.id });
|
|
return res.status(401).json({
|
|
error: 'Token invalid due to password change',
|
|
code: 'PASSWORD_CHANGED'
|
|
});
|
|
}
|
|
}
|
|
|
|
// Add user info to request
|
|
req.admin = {
|
|
id: admin.id,
|
|
username: admin.username,
|
|
email: admin.email
|
|
};
|
|
req.token = token; // Store token for potential revocation
|
|
|
|
next();
|
|
} catch (error) {
|
|
logger.error('Auth middleware error:', error);
|
|
res.status(401).json({ error: 'Authentication failed' });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enhanced gallery authentication middleware with revocation checking
|
|
*/
|
|
async function galleryAuth(req, res, next) {
|
|
try {
|
|
const token = req.headers.authorization?.split(' ')[1];
|
|
if (!token) {
|
|
return res.status(401).json({ error: 'No token provided' });
|
|
}
|
|
|
|
let decoded;
|
|
try {
|
|
decoded = jwt.verify(token, process.env.JWT_SECRET, {
|
|
issuer: 'picpeak-auth',
|
|
complete: true
|
|
});
|
|
decoded = decoded.payload;
|
|
} catch (err) {
|
|
if (err.name === 'TokenExpiredError') {
|
|
return res.status(401).json({ error: 'Session expired', code: 'TOKEN_EXPIRED' });
|
|
}
|
|
return res.status(401).json({ error: 'Invalid session' });
|
|
}
|
|
|
|
// Check if token is revoked
|
|
if (await isTokenRevoked(decoded)) {
|
|
return res.status(401).json({ error: 'Session has been invalidated', code: 'TOKEN_REVOKED' });
|
|
}
|
|
|
|
// Verify token type
|
|
if (decoded.type !== 'gallery') {
|
|
return res.status(403).json({ error: 'Invalid access token' });
|
|
}
|
|
|
|
// Check if event still exists and is active
|
|
const event = await db('events')
|
|
.where({
|
|
id: decoded.eventId,
|
|
is_active: true,
|
|
is_archived: false
|
|
})
|
|
.first();
|
|
|
|
if (!event) {
|
|
return res.status(404).json({ error: 'Gallery not found or expired' });
|
|
}
|
|
|
|
// Check if gallery has expired
|
|
if (new Date(event.expires_at) < new Date()) {
|
|
return res.status(410).json({
|
|
error: 'Gallery has expired',
|
|
code: 'GALLERY_EXPIRED'
|
|
});
|
|
}
|
|
|
|
// Add event info to request
|
|
req.event = event;
|
|
req.galleryToken = decoded;
|
|
req.token = token;
|
|
|
|
next();
|
|
} catch (error) {
|
|
logger.error('Gallery auth middleware error:', error);
|
|
res.status(401).json({ error: 'Authentication failed' });
|
|
}
|
|
}
|
|
|
|
// Export other middleware functions from original file...
|
|
module.exports = {
|
|
adminAuth,
|
|
galleryAuth,
|
|
// ... other exports
|
|
}; |