UNPKG

realtimecursor

Version:

Real-time collaboration system with cursor tracking and approval workflow

85 lines (69 loc) 1.63 kB
/** * API Key Service for RealtimeCursor * Manages API key generation, validation, and tracking */ const crypto = require('crypto'); class ApiKeyService { static apiKeys = new Map(); /** * Generate a new API key for a user */ static generateApiKey(userId, plan = 'free') { // Generate a random API key const apiKey = crypto.randomBytes(16).toString('hex'); // Store the API key with user info this.apiKeys.set(apiKey, { userId, plan, createdAt: Date.now(), active: true }); return apiKey; } /** * Validate an API key */ static validateApiKey(apiKey) { const keyData = this.apiKeys.get(apiKey); if (!keyData) { return { valid: false, message: 'Invalid API key' }; } if (!keyData.active) { return { valid: false, message: 'API key is inactive' }; } return { valid: true, userId: keyData.userId, plan: keyData.plan }; } /** * Get all API keys for a user */ static getUserApiKeys(userId) { const userKeys = []; for (const [key, data] of this.apiKeys.entries()) { if (data.userId === userId) { userKeys.push({ key, plan: data.plan, createdAt: data.createdAt, active: data.active }); } } return userKeys; } /** * Deactivate an API key */ static deactivateApiKey(apiKey) { const keyData = this.apiKeys.get(apiKey); if (!keyData) { return false; } keyData.active = false; return true; } } module.exports = ApiKeyService;