151 lines
4.2 KiB
JavaScript
151 lines
4.2 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
require('dotenv').config();
|
|
const mysql = require('mysql2/promise');
|
|
const ApiKeyRepository = require('../src/infrastructure/repositories/ApiKeyRepository');
|
|
const logger = require('../src/shared/utils/logger');
|
|
|
|
/**
|
|
* CLI tool for managing API keys
|
|
*/
|
|
async function main() {
|
|
const command = process.argv[2];
|
|
const args = process.argv.slice(3);
|
|
|
|
// Create database connection pool
|
|
const pool = mysql.createPool({
|
|
host: process.env.DB_HOST || 'localhost',
|
|
port: process.env.DB_PORT || 3306,
|
|
user: process.env.DB_USER || 'root',
|
|
password: process.env.DB_PASSWORD || '',
|
|
database: process.env.DB_NAME || 'property_tagging',
|
|
waitForConnections: true,
|
|
connectionLimit: 5
|
|
});
|
|
|
|
const apiKeyRepository = new ApiKeyRepository(pool, logger);
|
|
|
|
try {
|
|
switch (command) {
|
|
case 'create':
|
|
await handleCreate(apiKeyRepository, args);
|
|
break;
|
|
case 'list':
|
|
await handleList(apiKeyRepository);
|
|
break;
|
|
case 'revoke':
|
|
await handleRevoke(apiKeyRepository, args);
|
|
break;
|
|
case 'activate':
|
|
await handleActivate(apiKeyRepository, args);
|
|
break;
|
|
default:
|
|
console.log(`
|
|
Usage: node scripts/manage-api-keys.js <command> [options]
|
|
|
|
Commands:
|
|
create <name> [environment] [description] Generate new API key
|
|
list List all API keys
|
|
revoke <key-id> [reason] Revoke an API key
|
|
activate <key-id> Activate a revoked API key
|
|
|
|
Examples:
|
|
node scripts/manage-api-keys.js create "My App" production "Production API key"
|
|
node scripts/manage-api-keys.js list
|
|
node scripts/manage-api-keys.js revoke abc-123 "Security breach"
|
|
`);
|
|
process.exit(1);
|
|
}
|
|
} catch (error) {
|
|
console.error('Error:', error.message);
|
|
process.exit(1);
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
async function handleCreate(repository, args) {
|
|
if (args.length < 1) {
|
|
throw new Error('Name is required: create <name> [environment] [description]');
|
|
}
|
|
|
|
const name = args[0];
|
|
const environment = args[1] || 'development';
|
|
const description = args[2] || null;
|
|
|
|
if (!['development', 'staging', 'production'].includes(environment)) {
|
|
throw new Error('Environment must be: development, staging, or production');
|
|
}
|
|
|
|
const result = await repository.createKey({ name, environment, description });
|
|
|
|
console.log('\n✅ API Key created successfully!\n');
|
|
console.log('⚠️ IMPORTANT: Save this key now. It will NOT be shown again!\n');
|
|
console.log(`Key ID: ${result.id}`);
|
|
console.log(`Name: ${result.name}`);
|
|
console.log(`Environment: ${result.environment}`);
|
|
console.log(`API Key: ${result.key}\n`);
|
|
console.log('---');
|
|
}
|
|
|
|
async function handleList(repository) {
|
|
const keys = await repository.getAllKeys();
|
|
|
|
if (keys.length === 0) {
|
|
console.log('No API keys found.');
|
|
return;
|
|
}
|
|
|
|
console.log('\nAPI Keys:\n');
|
|
keys.forEach((key, index) => {
|
|
console.log(`${index + 1}. ${key.name}`);
|
|
console.log(` ID: ${key.id}`);
|
|
console.log(` Prefix: ${key.prefix}`);
|
|
console.log(` Environment: ${key.environment}`);
|
|
console.log(` Active: ${key.isActive ? '✅' : '❌'}`);
|
|
console.log(` Created: ${key.createdAt}`);
|
|
if (key.revokedAt) {
|
|
console.log(` Revoked: ${key.revokedAt}${key.revokedReason ? ` (${key.revokedReason})` : ''}`);
|
|
}
|
|
if (key.expiresAt) {
|
|
console.log(` Expires: ${key.expiresAt}`);
|
|
}
|
|
console.log('');
|
|
});
|
|
}
|
|
|
|
async function handleRevoke(repository, args) {
|
|
if (args.length < 1) {
|
|
throw new Error('Key ID is required: revoke <key-id> [reason]');
|
|
}
|
|
|
|
const keyId = args[0];
|
|
const reason = args[1] || null;
|
|
|
|
await repository.revokeKey(keyId, reason);
|
|
console.log(`✅ API key ${keyId} has been revoked.`);
|
|
}
|
|
|
|
async function handleActivate(repository, args) {
|
|
if (args.length < 1) {
|
|
throw new Error('Key ID is required: activate <key-id>');
|
|
}
|
|
|
|
const keyId = args[0];
|
|
await repository.activateKey(keyId);
|
|
console.log(`✅ API key ${keyId} has been activated.`);
|
|
}
|
|
|
|
// Run if called directly
|
|
if (require.main === module) {
|
|
main().catch(error => {
|
|
console.error('Fatal error:', error);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
module.exports = { main };
|
|
|
|
|
|
|