2018-07-17 23:22:51 +02:00
|
|
|
import { Component } from '@angular/core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Toast,
|
|
|
|
ToasterService,
|
|
|
|
} from 'angular2-toaster';
|
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { ApiService } from 'jslib-common/abstractions/api.service';
|
|
|
|
import { CipherService } from 'jslib-common/abstractions/cipher.service';
|
|
|
|
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
|
|
|
|
import { FolderService } from 'jslib-common/abstractions/folder.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-10-20 18:30:04 +02:00
|
|
|
import { LogService } from 'jslib-common/abstractions/log.service';
|
2021-06-07 20:13:58 +02:00
|
|
|
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
|
|
|
|
import { SyncService } from 'jslib-common/abstractions/sync.service';
|
|
|
|
|
|
|
|
import { EncString } from 'jslib-common/models/domain/encString';
|
|
|
|
|
|
|
|
import { CipherWithIdRequest } from 'jslib-common/models/request/cipherWithIdRequest';
|
|
|
|
import { FolderWithIdRequest } from 'jslib-common/models/request/folderWithIdRequest';
|
|
|
|
import { UpdateKeyRequest } from 'jslib-common/models/request/updateKeyRequest';
|
2018-07-17 23:22:51 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-update-key',
|
|
|
|
templateUrl: 'update-key.component.html',
|
|
|
|
})
|
|
|
|
export class UpdateKeyComponent {
|
|
|
|
masterPassword: string;
|
|
|
|
formPromise: Promise<any>;
|
|
|
|
|
|
|
|
constructor(private apiService: ApiService, private i18nService: I18nService,
|
2021-04-14 23:43:40 +02:00
|
|
|
private toasterService: ToasterService, private cryptoService: CryptoService,
|
|
|
|
private messagingService: MessagingService, private syncService: SyncService,
|
2021-10-20 18:30:04 +02:00
|
|
|
private folderService: FolderService, private cipherService: CipherService,
|
|
|
|
private logService: LogService) { }
|
2018-07-17 23:22:51 +02:00
|
|
|
|
|
|
|
async submit() {
|
|
|
|
const hasEncKey = await this.cryptoService.hasEncKey();
|
|
|
|
if (hasEncKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.masterPassword == null || this.masterPassword === '') {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('masterPassRequired'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-02-03 18:41:33 +01:00
|
|
|
this.formPromise = this.makeRequest().then(request => {
|
2018-07-17 23:22:51 +02:00
|
|
|
return this.apiService.postAccountKey(request);
|
|
|
|
});
|
|
|
|
await this.formPromise;
|
|
|
|
const toast: Toast = {
|
|
|
|
type: 'success',
|
|
|
|
title: this.i18nService.t('keyUpdated'),
|
|
|
|
body: this.i18nService.t('logBackInOthersToo'),
|
|
|
|
timeout: 15000,
|
|
|
|
};
|
|
|
|
this.toasterService.popAsync(toast);
|
|
|
|
this.messagingService.send('logout');
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2018-07-17 23:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async makeRequest(): Promise<UpdateKeyRequest> {
|
|
|
|
const key = await this.cryptoService.getKey();
|
|
|
|
const encKey = await this.cryptoService.makeEncKey(key);
|
|
|
|
const privateKey = await this.cryptoService.getPrivateKey();
|
2021-04-21 21:20:20 +02:00
|
|
|
let encPrivateKey: EncString = null;
|
2018-07-17 23:22:51 +02:00
|
|
|
if (privateKey != null) {
|
|
|
|
encPrivateKey = await this.cryptoService.encrypt(privateKey, encKey[0]);
|
|
|
|
}
|
|
|
|
const request = new UpdateKeyRequest();
|
|
|
|
request.privateKey = encPrivateKey != null ? encPrivateKey.encryptedString : null;
|
|
|
|
request.key = encKey[1].encryptedString;
|
|
|
|
request.masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, null);
|
|
|
|
|
|
|
|
await this.syncService.fullSync(true);
|
|
|
|
|
|
|
|
const folders = await this.folderService.getAllDecrypted();
|
|
|
|
for (let i = 0; i < folders.length; i++) {
|
2018-07-19 19:31:14 +02:00
|
|
|
if (folders[i].id == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-17 23:22:51 +02:00
|
|
|
const folder = await this.folderService.encrypt(folders[i], encKey[0]);
|
|
|
|
request.folders.push(new FolderWithIdRequest(folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
const ciphers = await this.cipherService.getAllDecrypted();
|
|
|
|
for (let i = 0; i < ciphers.length; i++) {
|
|
|
|
if (ciphers[i].organizationId != null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const cipher = await this.cipherService.encrypt(ciphers[i], encKey[0]);
|
|
|
|
request.ciphers.push(new CipherWithIdRequest(cipher));
|
|
|
|
}
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
}
|