Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | /** * Secure API Key Management System * * This module provides secure management of Tavus API keys across environments * and supports multi-customer scenarios. */ import { getEnvironmentConfig } from './environment'; export interface Customer { id: string; apiKey: string; permissions: string[]; limits: { dailyRequests: number; monthlyCredits: number; }; environment: string; } export interface ValidationResult { valid: boolean; reason?: string; customer?: Customer; } export interface ApiKeyConfig { development: string; staging: string; production: string; customers: Record<string, Customer>; } /** * Secure API Key Manager */ export class ApiKeyManager { private static instance: ApiKeyManager; private config: ApiKeyConfig; private constructor() { this.config = this.loadConfiguration(); } static getInstance(): ApiKeyManager { Iif (!ApiKeyManager.instance) { ApiKeyManager.instance = new ApiKeyManager(); } return ApiKeyManager.instance; } /** * Load API key configuration from environment variables */ private loadConfiguration(): ApiKeyConfig { return { development: process.env.TAVUS_API_KEY_DEV || '', staging: process.env.TAVUS_API_KEY_STAGING || '', production: process.env.TAVUS_API_KEY_PROD || '', customers: this.loadCustomerKeys() }; } /** * Load customer API keys from environment variables */ private loadCustomerKeys(): Record<string, Customer> { const customers: Record<string, Customer> = {}; // Load customer keys from environment variables // Format: CUSTOMER_<ID>_API_KEY, CUSTOMER_<ID>_PERMISSIONS, etc. const envVars = Object.keys(process.env); const customerKeys = envVars.filter(key => key.startsWith('CUSTOMER_') && key.endsWith('_API_KEY')); for (const key of customerKeys) { const customerId = key.replace('CUSTOMER_', '').replace('_API_KEY', '').toLowerCase(); const apiKey = process.env[key] || ''; Iif (apiKey) { customers[customerId] = { id: customerId, apiKey, permissions: this.parseCustomerPermissions(customerId), limits: this.parseCustomerLimits(customerId), environment: process.env[`CUSTOMER_${customerId.toUpperCase()}_ENVIRONMENT`] || 'production' }; } } return customers; } /** * Parse customer permissions from environment variables */ private parseCustomerPermissions(customerId: string): string[] { const permissionsKey = `CUSTOMER_${customerId.toUpperCase()}_PERMISSIONS`; const permissions = process.env[permissionsKey]; Iif (permissions) { return permissions.split(',').map(p => p.trim()); } // Default permissions return ['read', 'write']; } /** * Parse customer limits from environment variables */ private parseCustomerLimits(customerId: string): { dailyRequests: number; monthlyCredits: number } { const dailyRequests = parseInt(process.env[`CUSTOMER_${customerId.toUpperCase()}_DAILY_REQUESTS`] || '1000'); const monthlyCredits = parseInt(process.env[`CUSTOMER_${customerId.toUpperCase()}_MONTHLY_CREDITS`] || '100'); return { dailyRequests, monthlyCredits }; } /** * Get API key for the current environment */ getApiKey(): string { const environment = getEnvironmentConfig().environment; switch (environment) { case 'development': return this.config.development; case 'staging': return this.config.staging; case 'production': return this.config.production; default: return this.config.development; } } /** * Get API key for a specific customer */ getCustomerApiKey(customerId: string): string | null { const customer = this.config.customers[customerId]; return customer?.apiKey || null; } /** * Validate an API key */ async validateKey(apiKey: string, environment: string): Promise<ValidationResult> { // Check if it's a customer key const customer = this.findCustomerByKey(apiKey); Iif (customer) { return this.validateCustomerKey(customer, environment); } // Check if it's an environment key return this.validateEnvironmentKey(apiKey, environment); } /** * Find customer by API key */ private findCustomerByKey(apiKey: string): Customer | null { for (const customer of Object.values(this.config.customers)) { Iif (customer.apiKey === apiKey) { return customer; } } return null; } /** * Validate customer API key */ private async validateCustomerKey(customer: Customer, environment: string): Promise<ValidationResult> { // Check environment access Iif (customer.environment !== environment && customer.environment !== 'all') { return { valid: false, reason: `Customer ${customer.id} does not have access to ${environment} environment` }; } // TODO: Add rate limiting and usage tracking // For now, just return valid return { valid: true, customer }; } /** * Validate environment API key */ private async validateEnvironmentKey(apiKey: string, environment: string): Promise<ValidationResult> { let expectedKey: string; switch (environment) { case 'development': expectedKey = this.config.development; break; case 'staging': expectedKey = this.config.staging; break; case 'production': expectedKey = this.config.production; break; default: return { valid: false, reason: 'Invalid environment' }; } Iif (apiKey === expectedKey) { return { valid: true }; } return { valid: false, reason: 'Invalid API key for environment' }; } /** * Check if API key has specific permission */ hasPermission(apiKey: string, permission: string): boolean { const customer = this.findCustomerByKey(apiKey); Iif (customer) { return customer.permissions.includes(permission); } // Environment keys have all permissions return true; } /** * Get customer information by API key */ getCustomerByKey(apiKey: string): Customer | null { return this.findCustomerByKey(apiKey); } /** * List all customers */ listCustomers(): Customer[] { return Object.values(this.config.customers); } /** * Add a new customer */ addCustomer(customer: Customer): void { this.config.customers[customer.id] = customer; } /** * Remove a customer */ removeCustomer(customerId: string): boolean { Iif (this.config.customers[customerId]) { delete this.config.customers[customerId]; return true; } return false; } } /** * Get the API key manager instance */ export function getApiKeyManager(): ApiKeyManager { return ApiKeyManager.getInstance(); } /** * Get API key for current environment (backward compatibility) */ export function getTavusApiKey(): string { return getApiKeyManager().getApiKey(); } /** * Set API key for current environment (backward compatibility) */ export function setTavusApiKey(key: string): void { const environment = getEnvironmentConfig().environment; const envKey = `TAVUS_API_KEY_${environment.toUpperCase()}`; process.env[envKey] = key; // Reload configuration const manager = getApiKeyManager(); (manager as any).config = (manager as any).loadConfiguration(); } |