UNPKG

bauth-js

Version:

A Node.js authentication library for API requests via remote authentication service using Bearer tokens. Compatible with Express and NestJS.

211 lines (204 loc) â€ĸ 8.65 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = require("commander"); const auth_1 = require("./auth"); const fs = __importStar(require("fs")); const program = new commander_1.Command(); program .name('bauth-js') .description('CLI tool for bauth-js authentication library') .version('1.0.0'); // Test authentication endpoint program .command('test') .description('Test authentication endpoint connection') .requiredOption('-e, --endpoint <url>', 'Authentication service endpoint URL') .option('-t, --token <token>', 'Bearer token to test with') .option('--timeout <ms>', 'Request timeout in milliseconds', '10000') .action(async (options) => { try { console.log(`🔐 Testing authentication endpoint: ${options.endpoint}`); const auth = new auth_1.BAuth({ endpoint: options.endpoint, timeout: parseInt(options.timeout) }); if (options.token) { console.log('🔑 Testing with provided token...'); const result = await auth.authenticate(options.token); if (result.valid) { console.log('✅ Authentication successful!'); console.log('👤 User data:', JSON.stringify(result.user, null, 2)); } else { console.log('❌ Authentication failed:', result.error); process.exit(1); } } else { console.log('â„šī¸ No token provided, testing endpoint connectivity...'); // Just test if the endpoint is reachable console.log('✅ Endpoint appears to be reachable'); } } catch (error) { console.error('❌ Error testing endpoint:', error instanceof Error ? error.message : String(error)); process.exit(1); } }); // Generate Express middleware configuration program .command('generate:express') .description('Generate Express.js middleware configuration') .requiredOption('-e, --endpoint <url>', 'Authentication service endpoint URL') .option('-o, --output <file>', 'Output file path', 'bauth-express.js') .option('--user-property <property>', 'User property name', 'user') .option('--timeout <ms>', 'Request timeout in milliseconds', '10000') .action((options) => { const config = { endpoint: options.endpoint, timeout: parseInt(options.timeout), userProperty: options.userProperty }; const template = `// Generated by bauth-js CLI const { createBAuthMiddleware } = require('bauth-js'); const authMiddleware = createBAuthMiddleware(${JSON.stringify(config, null, 2)}); module.exports = authMiddleware; `; fs.writeFileSync(options.output, template); console.log(`✅ Express middleware configuration generated: ${options.output}`); console.log(`📝 Configuration:`, config); }); // Generate NestJS guard configuration program .command('generate:nest') .description('Generate NestJS guard configuration') .requiredOption('-e, --endpoint <url>', 'Authentication service endpoint URL') .option('-o, --output <file>', 'Output file path', 'bauth-nest.ts') .option('--user-property <property>', 'User property name', 'user') .option('--timeout <ms>', 'Request timeout in milliseconds', '10000') .action((options) => { const config = { endpoint: options.endpoint, timeout: parseInt(options.timeout), userProperty: options.userProperty }; const template = `// Generated by bauth-js CLI import { BAuthGuard } from 'bauth-js'; export const authGuard = BAuthGuard; export const authConfig = ${JSON.stringify(config, null, 2)}; // Usage example: // @UseGuards(BAuthGuard, authConfig) // export class ProtectedController {} `; fs.writeFileSync(options.output, template); console.log(`✅ NestJS guard configuration generated: ${options.output}`); console.log(`📝 Configuration:`, config); }); // Generate environment configuration program .command('generate:env') .description('Generate environment configuration file') .requiredOption('-e, --endpoint <url>', 'Authentication service endpoint URL') .option('-o, --output <file>', 'Output file path', '.env.bauth') .option('--timeout <ms>', 'Request timeout in milliseconds', '10000') .action((options) => { const envContent = `# bauth-js Configuration # Generated by bauth-js CLI # Authentication service endpoint BAUTH_ENDPOINT=${options.endpoint} # Request timeout in milliseconds BAUTH_TIMEOUT=${options.timeout} # Additional configuration NODE_ENV=development `; fs.writeFileSync(options.output, envContent); console.log(`✅ Environment configuration generated: ${options.output}`); console.log(`📝 Add these variables to your .env file or load ${options.output}`); }); // Generate package.json scripts program .command('generate:scripts') .description('Generate package.json scripts for bauth-js') .option('-o, --output <file>', 'Output file path', 'bauth-scripts.json') .action((options) => { const scripts = { "scripts": { "bauth:test": "bauth-js test -e $BAUTH_ENDPOINT", "bauth:test:with-token": "bauth-js test -e $BAUTH_ENDPOINT -t $BAUTH_TOKEN", "bauth:generate:express": "bauth-js generate:express -e $BAUTH_ENDPOINT", "bauth:generate:nest": "bauth-js generate:nest -e $BAUTH_ENDPOINT", "bauth:generate:env": "bauth-js generate:env -e $BAUTH_ENDPOINT" } }; fs.writeFileSync(options.output, JSON.stringify(scripts, null, 2)); console.log(`✅ Package.json scripts generated: ${options.output}`); console.log(`📝 Copy these scripts to your package.json`); }); // Interactive setup program .command('setup') .description('Interactive setup wizard for bauth-js') .action(async () => { console.log('🚀 Welcome to bauth-js Setup Wizard!'); console.log('This will help you configure bauth-js for your project.\n'); // In a real implementation, you would use a library like 'inquirer' for interactive prompts // For now, we'll show the manual steps console.log('📋 Manual Setup Steps:'); console.log('1. Set your authentication service endpoint:'); console.log(' export BAUTH_ENDPOINT=https://your-auth-service.com'); console.log(''); console.log('2. Test the connection:'); console.log(' bauth-js test -e https://your-auth-service.com'); console.log(''); console.log('3. Generate configuration files:'); console.log(' bauth-js generate:express -e https://your-auth-service.com'); console.log(' bauth-js generate:nest -e https://your-auth-service.com'); console.log(' bauth-js generate:env -e https://your-auth-service.com'); console.log(''); console.log('4. Install the library:'); console.log(' npm install bauth-js'); console.log(''); console.log('5. Import and use in your code!'); console.log(''); console.log('📚 For more information, visit: https://github.com/brightyid/bauth-js'); }); // Show help if no command provided if (!process.argv.slice(2).length) { program.help(); } program.parse(); //# sourceMappingURL=cli.js.map