bitwarden-estensione-browser/src/app/settings/change-kdf.component.ts

71 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-08-28 04:40:03 +02:00
import {
Component,
OnInit,
} from '@angular/core';
import { ToasterService } from 'angular2-toaster';
import { ApiService } from 'jslib-common/abstractions/api.service';
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { LogService } from 'jslib-common/abstractions/log.service';
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
import { UserService } from 'jslib-common/abstractions/user.service';
2018-08-28 04:40:03 +02:00
import { KdfRequest } from 'jslib-common/models/request/kdfRequest';
2018-08-28 04:40:03 +02:00
import { KdfType } from 'jslib-common/enums/kdfType';
2018-08-28 04:40:03 +02:00
@Component({
selector: 'app-change-kdf',
templateUrl: 'change-kdf.component.html',
})
export class ChangeKdfComponent implements OnInit {
masterPassword: string;
kdfIterations: number;
kdf = KdfType.PBKDF2_SHA256;
kdfOptions: any[] = [];
formPromise: Promise<any>;
constructor(private apiService: ApiService, private i18nService: I18nService,
private toasterService: ToasterService, private cryptoService: CryptoService,
private messagingService: MessagingService, private userService: UserService,
private logService: LogService) {
2018-08-28 04:40:03 +02:00
this.kdfOptions = [
{ name: 'PBKDF2 SHA-256', value: KdfType.PBKDF2_SHA256 },
];
}
async ngOnInit() {
this.kdf = await this.userService.getKdf();
this.kdfIterations = await this.userService.getKdfIterations();
}
async submit() {
const hasEncKey = await this.cryptoService.hasEncKey();
if (!hasEncKey) {
this.toasterService.popAsync('error', null, this.i18nService.t('updateKey'));
return;
}
const request = new KdfRequest();
request.kdf = this.kdf;
request.kdfIterations = this.kdfIterations;
request.masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, null);
const email = await this.userService.getEmail();
const newKey = await this.cryptoService.makeKey(this.masterPassword, email, this.kdf, this.kdfIterations);
request.newMasterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, newKey);
const newEncKey = await this.cryptoService.remakeEncKey(newKey);
request.key = newEncKey[1].encryptedString;
try {
this.formPromise = this.apiService.postAccountKdf(request);
await this.formPromise;
this.toasterService.popAsync('success', this.i18nService.t('encKeySettingsChanged'),
this.i18nService.t('logBackIn'));
this.messagingService.send('logout');
} catch (e) {
this.logService.error(e);
}
2018-08-28 04:40:03 +02:00
}
}