2021-12-20 18:04:00 +01:00
|
|
|
import * as program from "commander";
|
2020-08-03 18:30:32 +02:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { AuthService } from "@bitwarden/common/abstractions/auth.service";
|
|
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
|
|
|
|
import { CryptoFunctionService } from "@bitwarden/common/abstractions/cryptoFunction.service";
|
|
|
|
import { EnvironmentService } from "@bitwarden/common/abstractions/environment.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { KeyConnectorService } from "@bitwarden/common/abstractions/keyConnector.service";
|
|
|
|
import { PasswordGenerationService } from "@bitwarden/common/abstractions/passwordGeneration.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
2022-08-08 11:04:36 +02:00
|
|
|
import { PolicyService } from "@bitwarden/common/abstractions/policy/policy.service.abstraction";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
2022-08-29 20:14:59 +02:00
|
|
|
import { SyncService } from "@bitwarden/common/abstractions/sync/sync.service.abstraction";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { TwoFactorService } from "@bitwarden/common/abstractions/twoFactor.service";
|
|
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
|
|
|
import { LoginCommand as BaseLoginCommand } from "@bitwarden/node/cli/commands/login.command";
|
|
|
|
import { MessageResponse } from "@bitwarden/node/cli/models/response/messageResponse";
|
2019-03-18 15:33:43 +01:00
|
|
|
|
|
|
|
export class LoginCommand extends BaseLoginCommand {
|
2021-12-20 18:04:00 +01:00
|
|
|
private options: program.OptionValues;
|
2020-08-03 18:30:32 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
constructor(
|
|
|
|
authService: AuthService,
|
|
|
|
apiService: ApiService,
|
|
|
|
cryptoFunctionService: CryptoFunctionService,
|
|
|
|
i18nService: I18nService,
|
|
|
|
environmentService: EnvironmentService,
|
|
|
|
passwordGenerationService: PasswordGenerationService,
|
|
|
|
platformUtilsService: PlatformUtilsService,
|
2021-12-28 21:38:51 +01:00
|
|
|
stateService: StateService,
|
2021-12-20 18:04:00 +01:00
|
|
|
cryptoService: CryptoService,
|
|
|
|
policyService: PolicyService,
|
2022-02-08 00:31:36 +01:00
|
|
|
twoFactorService: TwoFactorService,
|
2022-01-20 21:03:37 +01:00
|
|
|
private syncService: SyncService,
|
|
|
|
private keyConnectorService: KeyConnectorService,
|
2021-12-20 18:04:00 +01:00
|
|
|
private logoutCallback: () => Promise<void>
|
|
|
|
) {
|
|
|
|
super(
|
|
|
|
authService,
|
|
|
|
apiService,
|
|
|
|
i18nService,
|
|
|
|
environmentService,
|
|
|
|
passwordGenerationService,
|
|
|
|
cryptoFunctionService,
|
|
|
|
platformUtilsService,
|
2021-12-28 21:38:51 +01:00
|
|
|
stateService,
|
2021-12-20 18:04:00 +01:00
|
|
|
cryptoService,
|
|
|
|
policyService,
|
2022-02-08 00:31:36 +01:00
|
|
|
twoFactorService,
|
2022-01-20 21:03:37 +01:00
|
|
|
"cli"
|
2021-12-20 18:04:00 +01:00
|
|
|
);
|
|
|
|
this.logout = this.logoutCallback;
|
|
|
|
this.validatedParams = async () => {
|
|
|
|
const key = await cryptoFunctionService.randomBytes(64);
|
|
|
|
process.env.BW_SESSION = Utils.fromBufferToB64(key);
|
|
|
|
};
|
|
|
|
this.success = async () => {
|
2022-01-20 21:03:37 +01:00
|
|
|
await this.syncService.fullSync(true);
|
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
const usesKeyConnector = await this.keyConnectorService.getUsesKeyConnector();
|
2021-08-10 19:35:41 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
if (
|
|
|
|
(this.options.sso != null || this.options.apikey != null) &&
|
|
|
|
this.canInteract &&
|
|
|
|
!usesKeyConnector
|
|
|
|
) {
|
|
|
|
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
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
run(email: string, password: string, options: program.OptionValues) {
|
|
|
|
this.options = options;
|
|
|
|
this.email = email;
|
|
|
|
return super.run(email, password, options);
|
|
|
|
}
|
2018-05-12 21:12:28 +02:00
|
|
|
}
|