2018-06-21 17:47:23 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
|
|
|
|
import { ApiService } from 'jslib/abstractions/api.service';
|
2018-06-21 20:28:00 +02:00
|
|
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
2018-06-21 17:47:23 +02:00
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
2018-06-21 20:28:00 +02:00
|
|
|
import { MessagingService } from 'jslib/abstractions/messaging.service';
|
2018-08-14 21:14:04 +02:00
|
|
|
import { UserService } from 'jslib/abstractions/user.service';
|
2018-06-21 20:28:00 +02:00
|
|
|
|
|
|
|
import { EmailRequest } from 'jslib/models/request/emailRequest';
|
|
|
|
import { EmailTokenRequest } from 'jslib/models/request/emailTokenRequest';
|
2018-06-21 17:47:23 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-change-email',
|
|
|
|
templateUrl: 'change-email.component.html',
|
|
|
|
})
|
|
|
|
export class ChangeEmailComponent {
|
2018-06-21 20:28:00 +02:00
|
|
|
masterPassword: string;
|
|
|
|
newEmail: string;
|
|
|
|
token: string;
|
|
|
|
tokenSent = false;
|
|
|
|
|
2018-06-21 17:47:23 +02:00
|
|
|
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 userService: UserService) { }
|
2018-06-21 17:47:23 +02:00
|
|
|
|
|
|
|
async submit() {
|
2018-07-12 23:09:09 +02:00
|
|
|
const hasEncKey = await this.cryptoService.hasEncKey();
|
|
|
|
if (!hasEncKey) {
|
2018-06-21 20:28:00 +02:00
|
|
|
this.toasterService.popAsync('error', null, this.i18nService.t('updateKey'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-08 14:13:47 +02:00
|
|
|
this.newEmail = this.newEmail.trim().toLowerCase();
|
2018-06-21 20:28:00 +02:00
|
|
|
if (!this.tokenSent) {
|
|
|
|
const request = new EmailTokenRequest();
|
|
|
|
request.newEmail = this.newEmail;
|
|
|
|
request.masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, null);
|
|
|
|
try {
|
|
|
|
this.formPromise = this.apiService.postEmailToken(request);
|
|
|
|
await this.formPromise;
|
|
|
|
this.tokenSent = true;
|
|
|
|
} catch { }
|
|
|
|
} else {
|
|
|
|
const request = new EmailRequest();
|
|
|
|
request.token = this.token;
|
|
|
|
request.newEmail = this.newEmail;
|
|
|
|
request.masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, null);
|
2018-08-14 21:14:04 +02:00
|
|
|
const kdf = await this.userService.getKdf();
|
|
|
|
const kdfIterations = await this.userService.getKdfIterations();
|
|
|
|
const newKey = await this.cryptoService.makeKey(this.masterPassword, this.newEmail, kdf, kdfIterations);
|
2018-06-21 20:28:00 +02:00
|
|
|
request.newMasterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, newKey);
|
2018-08-28 01:09:26 +02:00
|
|
|
const newEncKey = await this.cryptoService.remakeEncKey(newKey);
|
|
|
|
request.key = newEncKey[1].encryptedString;
|
2018-06-21 20:28:00 +02:00
|
|
|
try {
|
|
|
|
this.formPromise = this.apiService.postEmail(request);
|
|
|
|
await this.formPromise;
|
|
|
|
this.reset();
|
|
|
|
this.toasterService.popAsync('success', this.i18nService.t('emailChanged'),
|
|
|
|
this.i18nService.t('logBackIn'));
|
|
|
|
this.messagingService.send('logout');
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 17:47:23 +02:00
|
|
|
|
2018-06-21 20:28:00 +02:00
|
|
|
reset() {
|
|
|
|
this.token = this.newEmail = this.masterPassword = null;
|
|
|
|
this.tokenSent = false;
|
2018-06-21 17:47:23 +02:00
|
|
|
}
|
|
|
|
}
|