Implement complete JWT-based authentication system with comprehensive security features:
Database:
- Migration 005: Add 4 new tables (refresh_tokens, password_reset_tokens, audit_log, entity_permissions)
- Enhanced users table with email verification, account status, lockout protection
Services:
- auth.service.js: Full authentication lifecycle (register, login, refresh, logout, password reset, email verification)
- audit.service.js: Comprehensive security event logging and tracking
Routes:
- auth.routes.js: 9 authentication endpoints (register, login, refresh, logout, profile, password operations, email verification)
Middleware:
- auth.middleware.js: Token authentication, email verification, account status checks
Security Features:
- bcrypt password hashing (cost 12)
- JWT access tokens (15-minute expiry)
- Refresh tokens (7-day expiry, SHA256 hashed, revocable)
- Account lockout (5 failed attempts = 15 minutes)
- Token rotation on password reset
- Email verification workflow
- Comprehensive audit logging
Scripts:
- run-migration.js: Automated database migration runner
- test-auth.js: Comprehensive test suite (10 tests)
- check-audit-log.js: Audit log verification tool
All tests passing. Production-ready implementation.
🤖 Generated with Claude Code
38 lines
1 KiB
JavaScript
38 lines
1 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Check Audit Log Script
|
|
* Verifies audit logging is working correctly
|
|
*/
|
|
|
|
import { getDb } from '../config/db.js';
|
|
import { getEventStats, getRecentAuditLogs } from '../services/audit.service.js';
|
|
|
|
const db = getDb();
|
|
|
|
console.log('\n=== Audit Log Statistics ===\n');
|
|
|
|
// Total events
|
|
const total = db.prepare('SELECT COUNT(*) as count FROM audit_log').get();
|
|
console.log(`Total audit events: ${total.count}`);
|
|
|
|
// Event breakdown
|
|
console.log('\nEvent breakdown:');
|
|
const stats = getEventStats({
|
|
startDate: 0,
|
|
endDate: Math.floor(Date.now() / 1000)
|
|
});
|
|
|
|
stats.forEach(stat => {
|
|
console.log(` ${stat.event_type} (${stat.status}): ${stat.count}`);
|
|
});
|
|
|
|
// Recent events
|
|
console.log('\nRecent audit events (last 10):');
|
|
const recent = getRecentAuditLogs({ limit: 10 });
|
|
|
|
recent.forEach((event, i) => {
|
|
const date = new Date(event.created_at * 1000).toISOString();
|
|
console.log(` ${i + 1}. ${event.event_type} - ${event.status} - ${date}`);
|
|
});
|
|
|
|
console.log('\n=== Audit Log Check Complete ===\n');
|