1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-09 05:57:40 +02:00
bitwarden-browser/src/commands/login.command.ts

110 lines
4.6 KiB
TypeScript
Raw Normal View History

2018-05-12 21:12:28 +02:00
import * as program from 'commander';
2018-05-15 20:21:42 +02:00
import * as readline from 'readline-sync';
2018-05-12 21:12:28 +02:00
2018-05-15 23:17:47 +02:00
import { TwoFactorProviderType } from 'jslib/enums/twoFactorProviderType';
2018-05-12 21:12:28 +02:00
import { AuthResult } from 'jslib/models/domain/authResult';
2018-05-15 23:17:47 +02:00
import { TwoFactorEmailRequest } from 'jslib/models/request/twoFactorEmailRequest';
2018-05-12 21:12:28 +02:00
2018-05-15 23:17:47 +02:00
import { ApiService } from 'jslib/abstractions/api.service';
2018-05-12 21:12:28 +02:00
import { AuthService } from 'jslib/abstractions/auth.service';
2018-05-14 20:54:19 +02:00
import { Response } from '../models/response';
2018-05-13 03:24:28 +02:00
2018-05-14 20:54:19 +02:00
export class LoginCommand {
2018-05-15 23:17:47 +02:00
constructor(private authService: AuthService, private apiService: ApiService) { }
2018-05-12 21:12:28 +02:00
2018-05-13 06:19:14 +02:00
async run(email: string, password: string, cmd: program.Command) {
2018-05-15 20:21:42 +02:00
if (email == null || email === '') {
2018-05-15 23:17:47 +02:00
email = readline.question('Email address: ');
2018-05-15 20:21:42 +02:00
}
if (email == null || email.trim() === '') {
return Response.badRequest('Email address is required.');
}
if (email.indexOf('@') === -1) {
return Response.badRequest('Email address is invalid.');
}
if (password == null || password === '') {
2018-05-15 23:17:47 +02:00
password = readline.question('Master password: ', {
2018-05-15 20:21:42 +02:00
hideEchoBack: true,
mask: '*',
});
}
if (password == null || password === '') {
return Response.badRequest('Master password is required.');
}
2018-05-15 23:17:47 +02:00
let twoFactorToken: string = cmd.code;
let twoFactorMethod: TwoFactorProviderType = null;
2018-05-13 06:19:14 +02:00
try {
2018-05-15 23:17:47 +02:00
if (cmd.method != null) {
twoFactorMethod = parseInt(cmd.method, null);
}
} catch (e) {
return Response.error('Invalid two-step login method.');
}
2018-05-15 20:21:42 +02:00
2018-05-15 23:17:47 +02:00
try {
let response: AuthResult = null;
if (twoFactorToken != null && twoFactorMethod != null) {
response = await this.authService.logInComplete(email, password, twoFactorMethod,
twoFactorToken, false);
} else {
response = await this.authService.logIn(email, password);
if (response.twoFactor) {
let selectedProvider: any = null;
const twoFactorProviders = this.authService.getSupportedTwoFactorProviders(null);
if (twoFactorProviders.length === 0) {
return Response.badRequest('No providers available for this client.');
2018-05-15 20:21:42 +02:00
}
2018-05-15 23:17:47 +02:00
if (twoFactorMethod != null) {
try {
selectedProvider = twoFactorProviders.filter((p) => p.type === twoFactorMethod)[0];
} catch (e) {
return Response.error('Invalid two-step login method.');
}
}
2018-05-15 20:21:42 +02:00
2018-05-15 23:17:47 +02:00
if (selectedProvider == null) {
if (twoFactorProviders.length === 1) {
selectedProvider = twoFactorProviders[0];
} else {
const options = twoFactorProviders.map((p) => p.name);
const i = readline.keyInSelect(options, 'Two-step login method: ', { cancel: 'Cancel' });
if (i < 0) {
return Response.error('Login failed.');
}
selectedProvider = twoFactorProviders[i];
}
}
if (twoFactorToken == null && response.twoFactorProviders.size > 1 &&
selectedProvider.type === TwoFactorProviderType.Email) {
const emailReq = new TwoFactorEmailRequest(this.authService.email,
this.authService.masterPasswordHash);
await this.apiService.postTwoFactorEmail(emailReq);
}
if (twoFactorToken == null) {
twoFactorToken = readline.question('Two-step login code for ' + selectedProvider.name + ': ');
if (twoFactorToken == null || twoFactorToken === '') {
return Response.badRequest('Code is required.');
}
}
const twoFactorResponse = await this.authService.logInTwoFactor(selectedProvider.type,
twoFactorToken, false);
if (twoFactorResponse.twoFactor) {
return Response.error('Login failed.');
}
2018-05-15 20:21:42 +02:00
}
}
2018-05-14 20:54:19 +02:00
return Response.success();
2018-05-13 06:19:14 +02:00
} catch (e) {
2018-05-15 17:08:55 +02:00
return Response.error(e);
2018-05-13 06:19:14 +02:00
}
2018-05-12 21:12:28 +02:00
}
}