|
|
|
|
@ -1,21 +1,23 @@
@@ -1,21 +1,23 @@
|
|
|
|
|
import * as program from 'commander'; |
|
|
|
|
import * as inquirer from 'inquirer'; |
|
|
|
|
|
|
|
|
|
import { CryptoService } from 'jslib-common/abstractions/crypto.service'; |
|
|
|
|
import { ExportService } from 'jslib-common/abstractions/export.service'; |
|
|
|
|
import { KeyConnectorService } from 'jslib-common/abstractions/keyConnector.service'; |
|
|
|
|
import { PolicyService } from 'jslib-common/abstractions/policy.service'; |
|
|
|
|
import { UserVerificationService } from 'jslib-common/abstractions/userVerification.service'; |
|
|
|
|
|
|
|
|
|
import { Response } from 'jslib-node/cli/models/response'; |
|
|
|
|
|
|
|
|
|
import { PolicyType } from 'jslib-common/enums/policyType'; |
|
|
|
|
import { VerificationType } from 'jslib-common/enums/verificationType'; |
|
|
|
|
|
|
|
|
|
import { Utils } from 'jslib-common/misc/utils'; |
|
|
|
|
|
|
|
|
|
import { CliUtils } from '../utils'; |
|
|
|
|
|
|
|
|
|
export class ExportCommand { |
|
|
|
|
constructor(private cryptoService: CryptoService, private exportService: ExportService, |
|
|
|
|
private policyService: PolicyService) { } |
|
|
|
|
constructor(private exportService: ExportService, private policyService: PolicyService, |
|
|
|
|
private keyConnectorService: KeyConnectorService, private userVerificationService: UserVerificationService) { } |
|
|
|
|
|
|
|
|
|
async run(password: string, options: program.OptionValues): Promise<Response> { |
|
|
|
|
if (options.organizationid == null && |
|
|
|
|
@ -24,39 +26,36 @@ export class ExportCommand {
@@ -24,39 +26,36 @@ export class ExportCommand {
|
|
|
|
|
'One or more organization policies prevents you from exporting your personal vault.' |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const canInteract = process.env.BW_NOINTERACTION !== 'true'; |
|
|
|
|
if ((password == null || password === '') && canInteract) { |
|
|
|
|
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({ |
|
|
|
|
type: 'password', |
|
|
|
|
name: 'password', |
|
|
|
|
message: 'Master password:', |
|
|
|
|
}); |
|
|
|
|
password = answer.password; |
|
|
|
|
if (!canInteract) { |
|
|
|
|
return Response.badRequest('User verification is required. Try running this command again in interactive mode.'); |
|
|
|
|
} |
|
|
|
|
if (password == null || password === '') { |
|
|
|
|
return Response.badRequest('Master password is required.'); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
await this.keyConnectorService.getUsesKeyConnector() |
|
|
|
|
? await this.verifyOTP() |
|
|
|
|
: await this.verifyMasterPassword(password); |
|
|
|
|
} catch (e) { |
|
|
|
|
return Response.badRequest(e.message); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (await this.cryptoService.compareAndUpdateKeyHash(password, null)) { |
|
|
|
|
let format = options.format; |
|
|
|
|
if (format !== 'encrypted_json' && format !== 'json') { |
|
|
|
|
format = 'csv'; |
|
|
|
|
} |
|
|
|
|
if (options.organizationid != null && !Utils.isGuid(options.organizationid)) { |
|
|
|
|
return Response.error('`' + options.organizationid + '` is not a GUID.'); |
|
|
|
|
} |
|
|
|
|
let exportContent: string = null; |
|
|
|
|
try { |
|
|
|
|
exportContent = options.organizationid != null ? |
|
|
|
|
await this.exportService.getOrganizationExport(options.organizationid, format) : |
|
|
|
|
await this.exportService.getExport(format); |
|
|
|
|
} catch (e) { |
|
|
|
|
return Response.error(e); |
|
|
|
|
} |
|
|
|
|
return await this.saveFile(exportContent, options, format); |
|
|
|
|
} else { |
|
|
|
|
return Response.error('Invalid master password.'); |
|
|
|
|
let format = options.format; |
|
|
|
|
if (format !== 'encrypted_json' && format !== 'json') { |
|
|
|
|
format = 'csv'; |
|
|
|
|
} |
|
|
|
|
if (options.organizationid != null && !Utils.isGuid(options.organizationid)) { |
|
|
|
|
return Response.error('`' + options.organizationid + '` is not a GUID.'); |
|
|
|
|
} |
|
|
|
|
let exportContent: string = null; |
|
|
|
|
try { |
|
|
|
|
exportContent = options.organizationid != null ? |
|
|
|
|
await this.exportService.getOrganizationExport(options.organizationid, format) : |
|
|
|
|
await this.exportService.getExport(format); |
|
|
|
|
} catch (e) { |
|
|
|
|
return Response.error(e); |
|
|
|
|
} |
|
|
|
|
return await this.saveFile(exportContent, options, format); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async saveFile(exportContent: string, options: program.OptionValues, format: string): Promise<Response> { |
|
|
|
|
@ -79,4 +78,34 @@ export class ExportCommand {
@@ -79,4 +78,34 @@ export class ExportCommand {
|
|
|
|
|
} |
|
|
|
|
return this.exportService.getFileName(prefix, format); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async verifyMasterPassword(password: string) { |
|
|
|
|
if (password == null || password === '') { |
|
|
|
|
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({ |
|
|
|
|
type: 'password', |
|
|
|
|
name: 'password', |
|
|
|
|
message: 'Master password:', |
|
|
|
|
}); |
|
|
|
|
password = answer.password; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
await this.userVerificationService.verifyUser({ |
|
|
|
|
type: VerificationType.MasterPassword, |
|
|
|
|
secret: password, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async verifyOTP() { |
|
|
|
|
await this.userVerificationService.requestOTP(); |
|
|
|
|
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({ |
|
|
|
|
type: 'password', |
|
|
|
|
name: 'otp', |
|
|
|
|
message: 'A verification code has been emailed to you.\n Verification code:', |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
await this.userVerificationService.verifyUser({ |
|
|
|
|
type: VerificationType.OTP, |
|
|
|
|
secret: answer.otp, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|