bitwarden-estensione-browser/src/app/settings/two-factor-email.component.ts

81 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Component } from '@angular/core';
2018-06-27 22:56:11 +02:00
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import { ApiService } from 'jslib/abstractions/api.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { UserService } from 'jslib/abstractions/user.service';
2018-06-28 04:09:50 +02:00
import { TwoFactorEmailRequest } from 'jslib/models/request/twoFactorEmailRequest';
2018-06-27 22:56:11 +02:00
import { TwoFactorProviderType } from 'jslib/enums/twoFactorProviderType';
import { UpdateTwoFactorEmailRequest } from 'jslib/models/request/updateTwoFactorEmailRequest';
import { TwoFactorEmailResponse } from 'jslib/models/response/twoFactorEmailResponse';
import { TwoFactorBaseComponent } from './two-factor-base.component';
2018-06-27 22:56:11 +02:00
@Component({
selector: 'app-two-factor-email',
templateUrl: 'two-factor-email.component.html',
})
export class TwoFactorEmailComponent extends TwoFactorBaseComponent {
2018-06-27 22:56:11 +02:00
email: string;
token: string;
sentEmail: string;
formPromise: Promise<any>;
emailPromise: Promise<any>;
constructor(apiService: ApiService, i18nService: I18nService,
analytics: Angulartics2, toasterService: ToasterService,
platformUtilsService: PlatformUtilsService, private userService: UserService) {
super(apiService, i18nService, analytics, toasterService, platformUtilsService,
TwoFactorProviderType.Email);
}
2018-06-27 22:56:11 +02:00
auth(authResponse: any) {
super.auth(authResponse);
return this.processResponse(authResponse.response);
2018-06-27 22:56:11 +02:00
}
submit() {
2018-06-27 22:56:11 +02:00
if (this.enabled) {
return super.disable(this.formPromise);
2018-06-27 22:56:11 +02:00
} else {
return this.enable();
2018-06-27 22:56:11 +02:00
}
}
async sendEmail() {
try {
const request = new TwoFactorEmailRequest(this.email, this.masterPasswordHash);
this.emailPromise = this.apiService.postTwoFactorEmailSetup(request);
await this.emailPromise;
this.sentEmail = this.email;
} catch { }
}
protected enable() {
2018-06-27 22:56:11 +02:00
const request = new UpdateTwoFactorEmailRequest();
request.masterPasswordHash = this.masterPasswordHash;
request.email = this.email;
request.token = this.token;
return super.enable(async () => {
2018-06-27 22:56:11 +02:00
this.formPromise = this.apiService.putTwoFactorEmail(request);
const response = await this.formPromise;
await this.processResponse(response);
});
2018-06-27 22:56:11 +02:00
}
private async processResponse(response: TwoFactorEmailResponse) {
this.token = null;
this.email = response.email;
this.enabled = response.enabled;
if (!this.enabled && (this.email == null || this.email === '')) {
this.email = await this.userService.getEmail();
}
}
}