1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/services/auth.service.ts

292 lines
12 KiB
TypeScript
Raw Normal View History

2018-08-14 21:12:10 +02:00
import { KdfType } from '../enums/kdfType';
import { TwoFactorProviderType } from '../enums/twoFactorProviderType';
2018-02-02 04:55:49 +01:00
import { AuthResult } from '../models/domain/authResult';
import { SymmetricCryptoKey } from '../models/domain/symmetricCryptoKey';
2018-02-02 04:55:49 +01:00
import { DeviceRequest } from '../models/request/deviceRequest';
2018-07-03 18:06:01 +02:00
import { KeysRequest } from '../models/request/keysRequest';
2018-08-14 21:12:10 +02:00
import { PreloginRequest } from '../models/request/preloginRequest';
import { TokenRequest } from '../models/request/tokenRequest';
2018-01-23 21:58:13 +01:00
2018-08-14 21:12:10 +02:00
import { ErrorResponse } from '../models/response/errorResponse';
import { IdentityTokenResponse } from '../models/response/identityTokenResponse';
import { IdentityTwoFactorResponse } from '../models/response/identityTwoFactorResponse';
2018-02-02 04:55:49 +01:00
import { ApiService } from '../abstractions/api.service';
import { AppIdService } from '../abstractions/appId.service';
import { CryptoService } from '../abstractions/crypto.service';
import { I18nService } from '../abstractions/i18n.service';
import { MessagingService } from '../abstractions/messaging.service';
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
import { TokenService } from '../abstractions/token.service';
import { UserService } from '../abstractions/user.service';
2018-01-23 21:58:13 +01:00
2018-02-02 04:55:49 +01:00
export const TwoFactorProviders = {
[TwoFactorProviderType.Authenticator]: {
type: TwoFactorProviderType.Authenticator,
2018-02-02 04:55:49 +01:00
name: null as string,
description: null as string,
priority: 1,
2018-06-27 04:39:39 +02:00
sort: 1,
premium: false,
2018-02-02 04:55:49 +01:00
},
[TwoFactorProviderType.Yubikey]: {
type: TwoFactorProviderType.Yubikey,
2018-02-02 04:55:49 +01:00
name: null as string,
description: null as string,
priority: 3,
2018-06-27 04:39:39 +02:00
sort: 2,
premium: true,
2018-02-02 04:55:49 +01:00
},
[TwoFactorProviderType.Duo]: {
type: TwoFactorProviderType.Duo,
2018-02-02 04:55:49 +01:00
name: 'Duo',
description: null as string,
priority: 2,
2018-06-27 04:39:39 +02:00
sort: 3,
premium: true,
2018-02-02 04:55:49 +01:00
},
2018-05-16 03:11:20 +02:00
[TwoFactorProviderType.OrganizationDuo]: {
type: TwoFactorProviderType.OrganizationDuo,
name: 'Duo (Organization)',
description: null as string,
priority: 10,
2018-06-27 04:39:39 +02:00
sort: 4,
premium: false,
2018-05-16 03:11:20 +02:00
},
2018-02-02 04:55:49 +01:00
[TwoFactorProviderType.U2f]: {
type: TwoFactorProviderType.U2f,
2018-02-02 04:55:49 +01:00
name: null as string,
description: null as string,
priority: 4,
2018-06-27 04:39:39 +02:00
sort: 5,
premium: true,
2018-02-02 04:55:49 +01:00
},
[TwoFactorProviderType.Email]: {
type: TwoFactorProviderType.Email,
2018-02-02 04:55:49 +01:00
name: null as string,
description: null as string,
priority: 0,
2018-06-27 04:39:39 +02:00
sort: 6,
premium: false,
2018-02-02 04:55:49 +01:00
},
};
2018-01-23 21:58:13 +01:00
export class AuthService {
2018-02-02 04:55:49 +01:00
email: string;
masterPasswordHash: string;
twoFactorProviders: Map<TwoFactorProviderType, { [key: string]: string; }>;
selectedTwoFactorProviderType: TwoFactorProviderType = null;
2018-02-02 04:55:49 +01:00
private key: SymmetricCryptoKey;
2018-08-14 21:12:10 +02:00
private kdf: KdfType;
private kdfIterations: number;
2018-02-02 04:55:49 +01:00
2018-04-25 18:42:52 +02:00
constructor(private cryptoService: CryptoService, private apiService: ApiService,
private userService: UserService, private tokenService: TokenService,
private appIdService: AppIdService, private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService, private messagingService: MessagingService,
private setCryptoKeys = true) { }
2018-01-23 21:58:13 +01:00
2018-02-02 04:55:49 +01:00
init() {
TwoFactorProviders[TwoFactorProviderType.Email].name = this.i18nService.t('emailTitle');
TwoFactorProviders[TwoFactorProviderType.Email].description = this.i18nService.t('emailDesc');
TwoFactorProviders[TwoFactorProviderType.Authenticator].name = this.i18nService.t('authenticatorAppTitle');
TwoFactorProviders[TwoFactorProviderType.Authenticator].description =
this.i18nService.t('authenticatorAppDesc');
TwoFactorProviders[TwoFactorProviderType.Duo].description = this.i18nService.t('duoDesc');
2018-01-23 21:58:13 +01:00
2018-04-03 21:11:03 +02:00
TwoFactorProviders[TwoFactorProviderType.OrganizationDuo].name =
'Duo (' + this.i18nService.t('organization') + ')';
TwoFactorProviders[TwoFactorProviderType.OrganizationDuo].description =
this.i18nService.t('duoOrganizationDesc');
2018-02-02 04:55:49 +01:00
TwoFactorProviders[TwoFactorProviderType.U2f].name = this.i18nService.t('u2fTitle');
TwoFactorProviders[TwoFactorProviderType.U2f].description = this.i18nService.t('u2fDesc');
TwoFactorProviders[TwoFactorProviderType.Yubikey].name = this.i18nService.t('yubiKeyTitle');
TwoFactorProviders[TwoFactorProviderType.Yubikey].description = this.i18nService.t('yubiKeyDesc');
}
async logIn(email: string, masterPassword: string): Promise<AuthResult> {
this.selectedTwoFactorProviderType = null;
2018-08-14 21:12:10 +02:00
const key = await this.makePreloginKey(masterPassword, email);
2018-01-23 21:58:13 +01:00
const hashedPassword = await this.cryptoService.hashPassword(masterPassword, key);
2018-02-02 04:55:49 +01:00
return await this.logInHelper(email, hashedPassword, key);
}
async logInTwoFactor(twoFactorProvider: TwoFactorProviderType, twoFactorToken: string,
remember?: boolean): Promise<AuthResult> {
return await this.logInHelper(this.email, this.masterPasswordHash, this.key, twoFactorProvider,
twoFactorToken, remember);
}
2018-01-23 21:58:13 +01:00
2018-05-16 03:11:20 +02:00
async logInComplete(email: string, masterPassword: string, twoFactorProvider: TwoFactorProviderType,
twoFactorToken: string, remember?: boolean): Promise<AuthResult> {
this.selectedTwoFactorProviderType = null;
2018-08-14 21:12:10 +02:00
const key = await this.makePreloginKey(masterPassword, email);
2018-05-16 03:11:20 +02:00
const hashedPassword = await this.cryptoService.hashPassword(masterPassword, key);
return await this.logInHelper(email, hashedPassword, key, twoFactorProvider, twoFactorToken, remember);
}
2018-02-02 04:55:49 +01:00
logOut(callback: Function) {
callback();
2018-02-15 19:39:18 +01:00
this.messagingService.send('loggedOut');
2018-02-02 04:55:49 +01:00
}
2018-05-16 03:11:20 +02:00
getSupportedTwoFactorProviders(win: Window): any[] {
const providers: any[] = [];
if (this.twoFactorProviders == null) {
return providers;
}
2018-05-16 21:30:28 +02:00
if (this.twoFactorProviders.has(TwoFactorProviderType.OrganizationDuo) &&
this.platformUtilsService.supportsDuo()) {
2018-05-16 03:11:20 +02:00
providers.push(TwoFactorProviders[TwoFactorProviderType.OrganizationDuo]);
}
if (this.twoFactorProviders.has(TwoFactorProviderType.Authenticator)) {
providers.push(TwoFactorProviders[TwoFactorProviderType.Authenticator]);
}
if (this.twoFactorProviders.has(TwoFactorProviderType.Yubikey)) {
providers.push(TwoFactorProviders[TwoFactorProviderType.Yubikey]);
}
2018-05-16 21:30:28 +02:00
if (this.twoFactorProviders.has(TwoFactorProviderType.Duo) && this.platformUtilsService.supportsDuo()) {
2018-05-16 03:11:20 +02:00
providers.push(TwoFactorProviders[TwoFactorProviderType.Duo]);
}
if (this.twoFactorProviders.has(TwoFactorProviderType.U2f) && this.platformUtilsService.supportsU2f(win)) {
providers.push(TwoFactorProviders[TwoFactorProviderType.U2f]);
}
if (this.twoFactorProviders.has(TwoFactorProviderType.Email)) {
providers.push(TwoFactorProviders[TwoFactorProviderType.Email]);
}
return providers;
}
2018-02-02 04:55:49 +01:00
getDefaultTwoFactorProvider(u2fSupported: boolean): TwoFactorProviderType {
if (this.twoFactorProviders == null) {
return null;
}
if (this.selectedTwoFactorProviderType != null &&
this.twoFactorProviders.has(this.selectedTwoFactorProviderType)) {
return this.selectedTwoFactorProviderType;
}
2018-02-02 04:55:49 +01:00
let providerType: TwoFactorProviderType = null;
let providerPriority = -1;
this.twoFactorProviders.forEach((value, type) => {
const provider = (TwoFactorProviders as any)[type];
if (provider != null && provider.priority > providerPriority) {
2018-02-02 04:55:49 +01:00
if (type === TwoFactorProviderType.U2f && !u2fSupported) {
return;
}
providerType = type;
providerPriority = provider.priority;
}
});
return providerType;
}
2018-08-14 21:12:10 +02:00
async makePreloginKey(masterPassword: string, email: string): Promise<SymmetricCryptoKey> {
2018-09-08 14:13:33 +02:00
email = email.trim().toLowerCase();
2018-08-14 21:12:10 +02:00
this.kdf = null;
this.kdfIterations = null;
try {
const preloginResponse = await this.apiService.postPrelogin(new PreloginRequest(email));
if (preloginResponse != null) {
this.kdf = preloginResponse.kdf;
this.kdfIterations = preloginResponse.kdfIterations;
}
} catch (e) {
2018-08-15 15:01:00 +02:00
if (e == null || e.statusCode !== 404) {
2018-08-14 21:12:10 +02:00
throw e;
}
}
return this.cryptoService.makeKey(masterPassword, email, this.kdf, this.kdfIterations);
}
2018-02-02 04:55:49 +01:00
private async logInHelper(email: string, hashedPassword: string, key: SymmetricCryptoKey,
twoFactorProvider?: TwoFactorProviderType, twoFactorToken?: string, remember?: boolean): Promise<AuthResult> {
const storedTwoFactorToken = await this.tokenService.getTwoFactorToken(email);
const appId = await this.appIdService.getAppId();
2018-01-23 21:58:13 +01:00
const deviceRequest = new DeviceRequest(appId, this.platformUtilsService);
let request: TokenRequest;
if (twoFactorToken != null && twoFactorProvider != null) {
request = new TokenRequest(email, hashedPassword, twoFactorProvider, twoFactorToken, remember,
deviceRequest);
2018-02-02 04:55:49 +01:00
} else if (storedTwoFactorToken != null) {
request = new TokenRequest(email, hashedPassword, TwoFactorProviderType.Remember,
2018-01-23 21:58:13 +01:00
storedTwoFactorToken, false, deviceRequest);
} else {
request = new TokenRequest(email, hashedPassword, null, null, false, deviceRequest);
}
const response = await this.apiService.postIdentityToken(request);
2018-02-02 04:55:49 +01:00
this.clearState();
const result = new AuthResult();
result.twoFactor = !(response as any).accessToken;
if (result.twoFactor) {
2018-01-23 21:58:13 +01:00
// two factor required
2018-02-02 04:55:49 +01:00
const twoFactorResponse = response as IdentityTwoFactorResponse;
this.email = email;
this.masterPasswordHash = hashedPassword;
2018-04-25 18:42:52 +02:00
this.key = this.setCryptoKeys ? key : null;
2018-02-02 04:55:49 +01:00
this.twoFactorProviders = twoFactorResponse.twoFactorProviders2;
result.twoFactorProviders = twoFactorResponse.twoFactorProviders2;
return result;
2018-01-23 21:58:13 +01:00
}
2018-02-02 04:55:49 +01:00
const tokenResponse = response as IdentityTokenResponse;
if (tokenResponse.twoFactorToken != null) {
this.tokenService.setTwoFactorToken(tokenResponse.twoFactorToken, email);
2018-01-23 21:58:13 +01:00
}
2018-02-02 04:55:49 +01:00
await this.tokenService.setTokens(tokenResponse.accessToken, tokenResponse.refreshToken);
2018-08-14 21:12:10 +02:00
await this.userService.setInformation(this.tokenService.getUserId(), this.tokenService.getEmail(),
this.kdf, this.kdfIterations);
2018-04-25 18:42:52 +02:00
if (this.setCryptoKeys) {
await this.cryptoService.setKey(key);
await this.cryptoService.setKeyHash(hashedPassword);
await this.cryptoService.setEncKey(tokenResponse.key);
2018-07-03 18:06:01 +02:00
// User doesn't have a key pair yet (old account), let's generate one for them
if (tokenResponse.privateKey == null) {
try {
const keyPair = await this.cryptoService.makeKeyPair();
await this.apiService.postAccountKeys(new KeysRequest(keyPair[0], keyPair[1].encryptedString));
tokenResponse.privateKey = keyPair[1].encryptedString;
} catch (e) {
// tslint:disable-next-line
console.error(e);
}
}
2018-04-25 18:42:52 +02:00
await this.cryptoService.setEncPrivateKey(tokenResponse.privateKey);
}
2018-01-23 21:58:13 +01:00
this.messagingService.send('loggedIn');
2018-02-02 04:55:49 +01:00
return result;
2018-01-23 21:58:13 +01:00
}
2018-02-02 04:55:49 +01:00
private clearState(): void {
this.email = null;
this.masterPasswordHash = null;
this.twoFactorProviders = null;
this.selectedTwoFactorProviderType = null;
2018-01-23 21:58:13 +01:00
}
}