|
| 1 | +# TourGuideAI Server |
| 2 | + |
| 3 | +Backend server for the TourGuideAI application, handling secure token management, proxy services, and authentication. |
| 4 | + |
| 5 | +## Structure |
| 6 | + |
| 7 | +- **routes**: API endpoint route handlers |
| 8 | +- **middleware**: Express middleware functions |
| 9 | +- **utils**: Utility functions and helpers |
| 10 | +- **logs**: Server log files |
| 11 | +- **config**: Configuration files for different environments |
| 12 | +- **vault**: Secure token storage directory (created at runtime) |
| 13 | +- **scripts**: Administrative and utility scripts |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- **Secure Token Management**: Centralized vault for all API keys, tokens, and secrets |
| 18 | +- **Token Rotation**: Automatic tracking of token age and rotation requirements |
| 19 | +- **Multiple Backend Support**: Local, AWS Secrets Manager, or HashiCorp Vault support |
| 20 | +- **Proxy Services**: Route client requests to external APIs (OpenAI, Google Maps) |
| 21 | +- **Rate Limiting**: Prevent API abuse and manage quotas |
| 22 | +- **Caching**: Reduce duplicate API calls and improve performance |
| 23 | +- **Authentication**: User authorization and access control |
| 24 | +- **Logging**: Comprehensive logging for debugging and monitoring |
| 25 | + |
| 26 | +## Getting Started |
| 27 | + |
| 28 | +1. Install dependencies: |
| 29 | + |
| 30 | +```bash |
| 31 | +npm install |
| 32 | +``` |
| 33 | + |
| 34 | +2. Set up environment variables: |
| 35 | + |
| 36 | +```bash |
| 37 | +cp .env.example .env |
| 38 | +``` |
| 39 | + |
| 40 | +Edit the `.env` file with your vault configuration and API keys. |
| 41 | + |
| 42 | +3. Start the development server: |
| 43 | + |
| 44 | +```bash |
| 45 | +npm run dev |
| 46 | +``` |
| 47 | + |
| 48 | +## Token Management and Rotation |
| 49 | + |
| 50 | +TourGuideAI includes a comprehensive token management system to securely handle API keys, secrets, and credentials: |
| 51 | + |
| 52 | +### Automatic Rotation Detection |
| 53 | + |
| 54 | +The system automatically detects tokens that need rotation based on their age: |
| 55 | + |
| 56 | +| Token Type | Default Rotation Period | |
| 57 | +|------------|-------------------------| |
| 58 | +| API Keys | 90 days | |
| 59 | +| JWT Secrets| 180 days | |
| 60 | +| Encryption Keys | 365 days | |
| 61 | +| OAuth Tokens | 30 days | |
| 62 | + |
| 63 | +### Token Rotation Tool |
| 64 | + |
| 65 | +A dedicated command-line tool is included for securely rotating tokens: |
| 66 | + |
| 67 | +```bash |
| 68 | +npm run rotate-tokens |
| 69 | +``` |
| 70 | + |
| 71 | +This interactive tool provides options to: |
| 72 | +- List tokens that need rotation |
| 73 | +- Rotate tokens securely |
| 74 | +- Add new tokens to the vault |
| 75 | +- List all tokens in the vault |
| 76 | + |
| 77 | +### Security Features |
| 78 | + |
| 79 | +- AES-256-GCM encryption for all stored tokens |
| 80 | +- Secure key derivation with salting |
| 81 | +- Token caching with short TTL to minimize vault access |
| 82 | +- Automatic clearing of tokens from memory |
| 83 | +- Support for hardware security modules (via AWS KMS) |
| 84 | + |
| 85 | +### Production Recommendations |
| 86 | + |
| 87 | +For production environments: |
| 88 | +1. Use a remote vault backend (AWS Secrets Manager or HashiCorp Vault) |
| 89 | +2. Set VAULT_BACKEND to 'aws' or 'hashicorp' in your environment |
| 90 | +3. Configure a secure rotation schedule with alerting |
| 91 | +4. Use the token rotation script in a secure environment only |
| 92 | +5. Consider using an HSM or KMS for the vault encryption key |
| 93 | + |
| 94 | +## Token Vault System |
| 95 | + |
| 96 | +TourGuideAI uses a secure token vault system to protect sensitive credentials: |
| 97 | + |
| 98 | +- **Centralized Management**: All tokens, API keys, and secrets are managed in one place |
| 99 | +- **Encryption**: AES-256-GCM encryption for all stored credentials |
| 100 | +- **Rotation Policy**: Configurable rotation periods based on token type |
| 101 | +- **Multiple Backends**: Support for local file-based vault, AWS Secrets Manager, or HashiCorp Vault |
| 102 | +- **Seamless Integration**: Legacy environment variables still work during transition |
| 103 | + |
| 104 | +### Vault Configuration |
| 105 | + |
| 106 | +Configure the vault in your `.env` file: |
| 107 | + |
| 108 | +``` |
| 109 | +# Token Vault Configuration |
| 110 | +VAULT_BACKEND=local # Options: local, aws, hashicorp, in-memory |
| 111 | +VAULT_ENCRYPTION_KEY=your_vault_encryption_key_here |
| 112 | +VAULT_SALT=your_vault_salt_here |
| 113 | +VAULT_PATH=./vault/vault.enc # For local backend |
| 114 | +IMPORT_ENV_SECRETS=true # Import from environment variables on startup |
| 115 | +``` |
| 116 | + |
| 117 | +### Token Types and Rotation Periods |
| 118 | + |
| 119 | +The vault supports different token types with appropriate rotation periods: |
| 120 | + |
| 121 | +| Token Type | Usage | Default Rotation Period | |
| 122 | +|------------|-------|-------------------------| |
| 123 | +| API Key | External service API keys | 90 days | |
| 124 | +| JWT Secret | Authentication tokens | 180 days | |
| 125 | +| Encryption Key | Data encryption | 365 days | |
| 126 | +| OAuth Token | OAuth authentication | 30 days | |
| 127 | +| Database | Database credentials | 180 days | |
| 128 | + |
| 129 | +## API Endpoints |
| 130 | + |
| 131 | +| Endpoint | Method | Description | |
| 132 | +|----------|--------|-------------| |
| 133 | +| `/api/openai/chat` | POST | Proxy to OpenAI Chat API | |
| 134 | +| `/api/maps/geocode` | GET | Proxy to Google Maps Geocoding API | |
| 135 | +| `/api/maps/directions` | GET | Proxy to Google Maps Directions API | |
| 136 | +| `/api/maps/places` | GET | Proxy to Google Maps Places API | |
| 137 | +| `/api/auth/login` | POST | User authentication | |
| 138 | +| `/api/auth/logout` | POST | User logout | |
| 139 | +| `/api/health` | GET | Server health check | |
| 140 | +| `/api/admin/tokens/rotation` | GET | List tokens needing rotation (admin only) | |
| 141 | + |
| 142 | +## Environment Configuration |
| 143 | + |
| 144 | +The server uses the following environment variables: |
| 145 | + |
| 146 | +- `NODE_ENV`: Application environment (development, production) |
| 147 | +- `PORT`: Server port (default: 3001) |
| 148 | +- `VAULT_BACKEND`: Token vault backend type (local, aws, hashicorp, in-memory) |
| 149 | +- `VAULT_ENCRYPTION_KEY`: Encryption key for the vault |
| 150 | +- `VAULT_SALT`: Salt for key derivation |
| 151 | +- `JWT_EXPIRY`: JWT token expiry time |
| 152 | +- `RATE_LIMIT_WINDOW_MS`: Rate limiting window in milliseconds |
| 153 | +- `RATE_LIMIT_MAX_REQUESTS`: Maximum requests per window |
| 154 | + |
| 155 | +For backward compatibility, the following legacy variables are still supported: |
| 156 | + |
| 157 | +- `OPENAI_API_KEY`: OpenAI API key |
| 158 | +- `GOOGLE_MAPS_API_KEY`: Google Maps API key |
| 159 | +- `JWT_SECRET`: Secret for signing JWT tokens |
| 160 | +- `ENCRYPTION_KEY`: Key for data encryption |
| 161 | +- `SENDGRID_API_KEY`: SendGrid API key |
| 162 | + |
| 163 | +## Folder Structure |
| 164 | + |
| 165 | +``` |
| 166 | +server/ |
| 167 | +├── logs/ # Log files (created at runtime) |
| 168 | +├── vault/ # Secure token storage (created at runtime) |
| 169 | +├── middleware/ # Express middleware |
| 170 | +│ ├── apiKeyValidation.js |
| 171 | +│ ├── authMiddleware.js |
| 172 | +│ ├── caching.js |
| 173 | +│ └── rateLimit.js |
| 174 | +├── routes/ # API route handlers |
| 175 | +│ ├── googlemaps.js |
| 176 | +│ ├── openai.js |
| 177 | +│ └── auth.js |
| 178 | +├── scripts/ # Utility and admin scripts |
| 179 | +│ └── rotateToken.js # Token rotation tool |
| 180 | +├── utils/ # Utility functions |
| 181 | +│ ├── vaultService.js |
| 182 | +│ ├── tokenProvider.js |
| 183 | +│ ├── jwtAuth.js |
| 184 | +│ └── logger.js |
| 185 | +├── .env # Environment variables (create from .env.example) |
| 186 | +├── .env.example # Example environment file |
| 187 | +├── package.json # Project dependencies |
| 188 | +├── server.js # Main server file |
| 189 | +└── test-server.js # Test script |
| 190 | +``` |
| 191 | + |
| 192 | +## Production Deployment |
| 193 | + |
| 194 | +When deploying to production, ensure: |
| 195 | + |
| 196 | +1. Set `NODE_ENV=production` in your environment |
| 197 | +2. Use a secure backend for the token vault (aws or hashicorp recommended) |
| 198 | +3. Configure proper `ALLOWED_ORIGIN` for CORS |
| 199 | +4. Set up a process manager like PM2 or run in Docker |
| 200 | +5. Use HTTPS with valid SSL certificates |
| 201 | +6. Set up proper monitoring and logging |
| 202 | + |
| 203 | +## Security Considerations |
| 204 | + |
| 205 | +- The vault encryption key and salt should be stored securely outside version control |
| 206 | +- In production, consider using a KMS (Key Management Service) for the vault encryption key |
| 207 | +- For highly secure environments, use a dedicated secrets manager like HashiCorp Vault |
| 208 | +- Enable automatic token rotation monitoring and notifications |
| 209 | +- Use environment-specific secrets with different rotation schedules |
| 210 | +- Implement the principle of least privilege for all service accounts |
| 211 | +- Never expose token rotation APIs to public networks |
| 212 | + |
| 213 | +## License |
| 214 | + |
| 215 | +MIT |
0 commit comments