2018-05-16 16:25:25 +02:00
|
|
|
import * as program from 'commander';
|
2018-05-23 17:16:23 +02:00
|
|
|
import * as inquirer from 'inquirer';
|
2018-05-16 16:25:25 +02:00
|
|
|
|
|
|
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
|
|
|
import { CryptoFunctionService } from 'jslib/abstractions/cryptoFunction.service';
|
|
|
|
import { UserService } from 'jslib/abstractions/user.service';
|
|
|
|
|
2019-03-16 03:34:59 +01:00
|
|
|
import { Response } from 'jslib/cli/models/response';
|
|
|
|
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
|
2018-05-16 16:25:25 +02:00
|
|
|
|
|
|
|
import { Utils } from 'jslib/misc/utils';
|
|
|
|
|
|
|
|
export class UnlockCommand {
|
|
|
|
constructor(private cryptoService: CryptoService, private userService: UserService,
|
|
|
|
private cryptoFunctionService: CryptoFunctionService) { }
|
|
|
|
|
|
|
|
async run(password: string, cmd: program.Command) {
|
2019-10-21 22:04:51 +02:00
|
|
|
const canInteract = process.stdout.isTTY && process.env.BW_NOINTERACTION !== 'true';
|
|
|
|
if ((password == null || password === '') && canInteract) {
|
2018-06-19 04:07:45 +02:00
|
|
|
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({
|
2018-05-23 17:16:23 +02:00
|
|
|
type: 'password',
|
|
|
|
name: 'password',
|
|
|
|
message: 'Master password:',
|
2018-05-16 16:25:25 +02:00
|
|
|
});
|
2018-05-23 17:16:23 +02:00
|
|
|
password = answer.password;
|
2018-05-16 16:25:25 +02:00
|
|
|
}
|
|
|
|
if (password == null || password === '') {
|
|
|
|
return Response.badRequest('Master password is required.');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setNewSessionKey();
|
|
|
|
const email = await this.userService.getEmail();
|
2018-08-14 21:13:40 +02:00
|
|
|
const kdf = await this.userService.getKdf();
|
|
|
|
const kdfIterations = await this.userService.getKdfIterations();
|
|
|
|
const key = await this.cryptoService.makeKey(password, email, kdf, kdfIterations);
|
2018-05-16 16:25:25 +02:00
|
|
|
const keyHash = await this.cryptoService.hashPassword(password, key);
|
|
|
|
const storedKeyHash = await this.cryptoService.getKeyHash();
|
|
|
|
if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) {
|
|
|
|
await this.cryptoService.setKey(key);
|
|
|
|
const res = new MessageResponse('Your vault is now unlocked!', '\n' +
|
|
|
|
'To unlock your vault, set your session key to the `BW_SESSION` environment variable. ex:\n' +
|
2018-05-17 05:27:47 +02:00
|
|
|
'$ export BW_SESSION="' + process.env.BW_SESSION + '"\n' +
|
|
|
|
'> $env:BW_SESSION="' + process.env.BW_SESSION + '"\n\n' +
|
2018-05-16 16:25:25 +02:00
|
|
|
'You can also pass the session key to any command with the `--session` option. ex:\n' +
|
2018-05-18 15:16:34 +02:00
|
|
|
'$ bw list items --session ' + process.env.BW_SESSION);
|
2018-05-16 16:25:25 +02:00
|
|
|
res.raw = process.env.BW_SESSION;
|
|
|
|
return Response.success(res);
|
|
|
|
} else {
|
|
|
|
return Response.error('Invalid master password.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async setNewSessionKey() {
|
|
|
|
const key = await this.cryptoFunctionService.randomBytes(64);
|
|
|
|
process.env.BW_SESSION = Utils.fromBufferToB64(key);
|
|
|
|
}
|
|
|
|
}
|