Server-Side Authentication Security
This document outlines the enhanced server-side authentication security features implemented in the application.Overview
The server-side authentication system provides:- Strict token validation - Enhanced cookie-based token validation
- Rate limiting - Protection against brute force attacks
- IP verification - Detects suspicious login locations
- User agent monitoring - Alerts on unusual device changes
- Role-based access control - Fine-grained permission enforcement
- Comprehensive audit logging - Detailed security event tracking
Implementation
The core implementation is in thesrc/lib/auth/serverAuth.ts file, which provides:
verifyServerAuth()- Core authentication function with multiple security checksprotectRoute()- Higher-order function that wraps Astro API routestrackSuspiciousActivity()- Utility for logging suspicious behaviors
Usage
Protecting API Routes
Configuration Options
TheprotectRoute() function accepts the following options:
| Option | Type | Default | Description |
|---|---|---|---|
requiredRole | AuthRole | undefined | Required role for accessing the route |
validateIPMatch | boolean | true | Check if IP matches previous requests |
validateUserAgent | boolean | true | Check if user agent matches previous requests |
Manual Authentication
For custom authentication flows, use theverifyServerAuth() function directly:
Security Features
Rate Limiting
The system implements IP-based rate limiting to prevent brute force attacks:- Maximum 5 failed auth attempts in 15 minutes
- IP is blocked for 1 hour after exceeding attempts
- Failed attempts are logged for security auditing
IP Verification
The system tracks IP addresses and detects when a user logs in from a new location:- Previous IP addresses are stored in Redis
- Changes trigger security alerts and audit logs
- Admins can review suspicious location changes
User Agent Monitoring
Changes in device fingerprints are tracked:- Browser and device information is stored
- Significant changes trigger security alerts
- Helps detect potential account takeovers
Audit Logging
Comprehensive security events are logged:- All authentication attempts (success/failure)
- Suspicious activity with detailed metadata
- IP and user agent changes
- Rate limit triggers and blocks
Integration with Existing Auth
The server-side auth system builds on the existing authentication framework:- Uses the same token validation mechanisms
- Extends with additional security features
- Fully compatible with existing auth middleware
Best Practices
When implementing authentication:- Always use
protectRoute()for API endpoints that require authentication - Set appropriate roles for admin-only functionality
- Check locals.user instead of making redundant auth calls
- Handle auth errors with appropriate status codes and messages
- Log security events for auditing and monitoring
Security Considerations
- Redis is used for rate limiting and session tracking
- IP and user agent data is stored for 7 days
- Security alerts are logged but don’t block legitimate access
- Consider adding email notifications for suspicious events
Testing
Unit tests are available in thesrc/lib/auth/__tests__/serverAuth.test.ts file, which tests:
- Authentication validation logic
- Role-based access control
- IP and user agent verification
- Rate limiting functionality
Performance Impact
The enhanced security checks add minimal overhead:- Redis operations are optimized for low latency
- Authentication results are not cached to ensure security
- Failed attempts have rate limiting applied
- Successful auth has quick pathway with minimal checks