2020-08-03 18:30:32 +02:00
|
|
|
import * as program from 'commander';
|
2021-08-10 19:35:41 +02:00
|
|
|
import * as inquirer from 'inquirer';
|
2020-08-03 18:30:32 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { ApiService } from 'jslib-common/abstractions/api.service';
|
|
|
|
import { AuthService } from 'jslib-common/abstractions/auth.service';
|
2021-08-10 19:35:41 +02:00
|
|
|
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
|
2021-09-15 15:57:43 +02:00
|
|
|
import { CryptoFunctionService } from 'jslib-common/abstractions/cryptoFunction.service';
|
2021-06-07 19:25:55 +02:00
|
|
|
import { EnvironmentService } from 'jslib-common/abstractions/environment.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-11-09 19:00:16 +01:00
|
|
|
import { KeyConnectorService } from 'jslib-common/abstractions/keyConnector.service';
|
2021-06-07 19:25:55 +02:00
|
|
|
import { PasswordGenerationService } from 'jslib-common/abstractions/passwordGeneration.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
2021-08-10 19:35:41 +02:00
|
|
|
import { PolicyService } from 'jslib-common/abstractions/policy.service';
|
2021-06-07 19:25:55 +02:00
|
|
|
import { SyncService } from 'jslib-common/abstractions/sync.service';
|
2021-08-10 19:35:41 +02:00
|
|
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { MessageResponse } from 'jslib-node/cli/models/response/messageResponse';
|
2018-05-13 03:24:28 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { Utils } from 'jslib-common/misc/utils';
|
2018-05-16 03:11:58 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { LoginCommand as BaseLoginCommand } from 'jslib-node/cli/commands/login.command';
|
2019-03-18 15:33:43 +01:00
|
|
|
|
|
|
|
export class LoginCommand extends BaseLoginCommand {
|
2021-02-03 18:44:33 +01:00
|
|
|
private options: program.OptionValues;
|
2020-08-03 18:30:32 +02:00
|
|
|
|
2019-03-18 15:33:43 +01:00
|
|
|
constructor(authService: AuthService, apiService: ApiService,
|
|
|
|
cryptoFunctionService: CryptoFunctionService, syncService: SyncService,
|
2020-08-03 18:30:32 +02:00
|
|
|
i18nService: I18nService, environmentService: EnvironmentService,
|
2021-08-10 19:35:41 +02:00
|
|
|
passwordGenerationService: PasswordGenerationService, platformUtilsService: PlatformUtilsService,
|
2021-10-12 23:51:14 +02:00
|
|
|
userService: UserService, cryptoService: CryptoService, policyService: PolicyService,
|
2021-11-09 19:00:16 +01:00
|
|
|
keyConnectorService: KeyConnectorService, private logoutCallback: () => Promise<void>) {
|
2020-08-03 18:30:32 +02:00
|
|
|
super(authService, apiService, i18nService, environmentService, passwordGenerationService,
|
2021-10-12 23:51:14 +02:00
|
|
|
cryptoFunctionService, platformUtilsService, userService, cryptoService, policyService,
|
2021-11-09 19:00:16 +01:00
|
|
|
'cli', syncService, keyConnectorService);
|
2021-10-12 23:51:14 +02:00
|
|
|
this.logout = this.logoutCallback;
|
2019-03-18 15:33:43 +01:00
|
|
|
this.validatedParams = async () => {
|
|
|
|
const key = await cryptoFunctionService.randomBytes(64);
|
|
|
|
process.env.BW_SESSION = Utils.fromBufferToB64(key);
|
|
|
|
};
|
|
|
|
this.success = async () => {
|
2021-11-09 19:00:16 +01:00
|
|
|
const usesKeyConnector = await this.keyConnectorService.getUsesKeyConnector();
|
2021-08-10 19:35:41 +02:00
|
|
|
|
2021-11-09 19:00:16 +01:00
|
|
|
if ((this.options.sso != null || this.options.apikey != null) && this.canInteract && !usesKeyConnector) {
|
2021-10-12 23:51:14 +02:00
|
|
|
const res = new MessageResponse('You are logged in!', '\n' +
|
|
|
|
'To unlock your vault, use the `unlock` command. ex:\n' +
|
|
|
|
'$ bw unlock');
|
|
|
|
return res;
|
|
|
|
} else {
|
|
|
|
const res = new MessageResponse('You are logged in!', '\n' +
|
|
|
|
'To unlock your vault, set your session key to the `BW_SESSION` environment variable. ex:\n' +
|
|
|
|
'$ export BW_SESSION="' + process.env.BW_SESSION + '"\n' +
|
|
|
|
'> $env:BW_SESSION="' + process.env.BW_SESSION + '"\n\n' +
|
|
|
|
'You can also pass the session key to any command with the `--session` option. ex:\n' +
|
|
|
|
'$ bw list items --session ' + process.env.BW_SESSION);
|
|
|
|
res.raw = process.env.BW_SESSION;
|
|
|
|
return res;
|
2020-08-03 18:30:32 +02:00
|
|
|
}
|
2019-03-18 15:33:43 +01:00
|
|
|
};
|
2018-05-16 03:11:58 +02:00
|
|
|
}
|
2020-08-03 18:30:32 +02:00
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
run(email: string, password: string, options: program.OptionValues) {
|
|
|
|
this.options = options;
|
2021-08-10 19:35:41 +02:00
|
|
|
this.email = email;
|
2021-02-03 18:44:33 +01:00
|
|
|
return super.run(email, password, options);
|
2020-08-03 18:30:32 +02:00
|
|
|
}
|
2018-05-12 21:12:28 +02:00
|
|
|
}
|