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 | /** * Authentication Command * Login, logout, and authentication status management */ const { Command } = require('commander') const chalk = require('chalk') const inquirer = require('inquirer') const ora = require('ora') const APIClient = require('../services/api-client') const ConfigManager = require('../utils/config-manager') const authCommand = new Command('auth') .description('Vaultace authentication management') // Login subcommand authCommand .command('login') .description('Login to Vaultace') .option('-e, --email <email>', 'email address') .option('-p, --password <password>', 'password') .option('-r, --remember', 'remember login for longer') .action(async (options) => { const spinner = ora('Authenticating with Vaultace...').start() try { let { email, password } = options // Prompt for credentials if not provided if (!email || !password) { spinner.stop() const answers = await inquirer.prompt([ { type: 'input', name: 'email', message: 'Email:', when: !email, validate: (input) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ return emailRegex.test(input) || 'Please enter a valid email address' } }, { type: 'password', name: 'password', message: 'Password:', when: !password, mask: '*', validate: (input) => input.length >= 8 || 'Password must be at least 8 characters' } ]) email = email || answers.email password = password || answers.password spinner.start('Authenticating with Vaultace...') } // Authenticate const apiClient = new APIClient() await apiClient.login(email, password, options.remember) // Get user info const userInfo = await apiClient.getCurrentUser() spinner.succeed('Successfully logged in to Vaultace!') console.log(chalk.green('\n✅ Authentication successful')) console.log(`${chalk.bold('User:')} ${userInfo.email}`) console.log(`${chalk.bold('Organization:')} ${userInfo.organization_id}`) console.log(`${chalk.bold('Role:')} ${userInfo.role}`) if (!userInfo.is_verified) { console.log(chalk.yellow('\n⚠️ Email verification pending')) console.log('Please check your email and verify your account') } } catch (error) { spinner.fail(`Login failed: ${error.message}`) if (error.message.includes('Invalid credentials')) { console.log(chalk.gray('\nTips:')) console.log(chalk.gray('• Check your email and password')) console.log(chalk.gray('• Use vaultace auth register to create an account')) } process.exit(1) } }) // Register subcommand authCommand .command('register') .description('Create new Vaultace account') .action(async () => { console.log(chalk.bold.cyan('🛡️ Create Your Vaultace Account\n')) try { const answers = await inquirer.prompt([ { type: 'input', name: 'organizationName', message: 'Organization name:', validate: (input) => input.trim().length >= 2 || 'Organization name must be at least 2 characters' }, { type: 'input', name: 'organizationSlug', message: 'Organization slug (lowercase, alphanumeric with dashes):', validate: (input) => { const slugRegex = /^[a-z0-9][a-z0-9-]*[a-z0-9]$/ return slugRegex.test(input) || 'Invalid slug format (use lowercase, numbers, and dashes only)' }, transformer: (input) => input.toLowerCase().replace(/[^a-z0-9-]/g, '-') }, { type: 'input', name: 'email', message: 'Email address:', validate: (input) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ return emailRegex.test(input) || 'Please enter a valid email address' } }, { type: 'password', name: 'password', message: 'Password (min 8 characters):', mask: '*', validate: (input) => { if (input.length < 8) return 'Password must be at least 8 characters' if (!/(?=.*[a-z])/.test(input)) return 'Password must contain lowercase letters' if (!/(?=.*[A-Z])/.test(input)) return 'Password must contain uppercase letters' if (!/(?=.*\d)/.test(input)) return 'Password must contain numbers' return true } }, { type: 'input', name: 'firstName', message: 'First name:', validate: (input) => input.trim().length > 0 || 'First name is required' }, { type: 'input', name: 'lastName', message: 'Last name:', validate: (input) => input.trim().length > 0 || 'Last name is required' } ]) const spinner = ora('Creating your Vaultace account...').start() const apiClient = new APIClient() const userInfo = await apiClient.register( answers.organizationName, answers.organizationSlug, answers.email, answers.password, answers.firstName, answers.lastName ) spinner.succeed('Account created successfully!') console.log(chalk.green('\n🎉 Welcome to Vaultace!')) console.log(`${chalk.bold('User:')} ${userInfo.email}`) console.log(`${chalk.bold('Organization:')} ${answers.organizationName}`) console.log(`${chalk.bold('Slug:')} ${answers.organizationSlug}`) console.log(chalk.yellow('\n📧 Next steps:')) console.log('1. Check your email for verification link') console.log('2. Login with: vaultace auth login') console.log('3. Start scanning: vaultace scan') } catch (error) { console.error(chalk.red(`Registration failed: ${error.message}`)) if (error.message.includes('already exists')) { console.log(chalk.gray('\nTry using vaultace auth login instead')) } process.exit(1) } }) // Logout subcommand authCommand .command('logout') .description('Logout from Vaultace') .action(async () => { const config = ConfigManager.getConfig() if (!config.auth?.accessToken) { console.log(chalk.gray('Not currently logged in')) return } const spinner = ora('Logging out...').start() try { const apiClient = new APIClient(config) await apiClient.logout() spinner.succeed('Successfully logged out') console.log(chalk.gray('Use vaultace auth login to sign in again')) } catch (error) { spinner.warn(`Logout completed with warnings: ${error.message}`) } }) // Status subcommand authCommand .command('status') .description('Show authentication status') .action(async () => { const config = ConfigManager.getConfig() if (!config.auth?.accessToken) { console.log(chalk.gray('❌ Not logged in')) console.log(chalk.gray('Run vaultace auth login to authenticate')) return } const spinner = ora('Checking authentication status...').start() try { const apiClient = new APIClient(config) const userInfo = await apiClient.getCurrentUser() spinner.stop() console.log(chalk.green('✅ Authenticated')) console.log(`${chalk.bold('User:')} ${userInfo.email}`) console.log(`${chalk.bold('Role:')} ${userInfo.role}`) console.log(`${chalk.bold('Organization:')} ${userInfo.organization_id}`) console.log(`${chalk.bold('Verified:')} ${userInfo.is_verified ? '✅' : '❌'}`) console.log(`${chalk.bold('MFA:')} ${userInfo.mfa_enabled ? '✅' : '❌'}`) // Check token expiration if (config.auth.expiresAt) { const expiresIn = Math.max(0, config.auth.expiresAt - Date.now()) const hours = Math.floor(expiresIn / (1000 * 60 * 60)) const minutes = Math.floor((expiresIn % (1000 * 60 * 60)) / (1000 * 60)) if (expiresIn > 0) { console.log(`${chalk.bold('Token expires:')} ${hours}h ${minutes}m`) } else { console.log(chalk.yellow('⚠️ Token expired - automatic refresh will occur')) } } } catch (error) { spinner.fail(`Authentication check failed: ${error.message}`) if (error.message.includes('Authentication required')) { console.log(chalk.gray('Run vaultace auth login to authenticate')) } process.exit(1) } }) module.exports = authCommand |