2018-07-14 05:15:09 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
OnDestroy,
|
|
|
|
OnInit,
|
|
|
|
} from '@angular/core';
|
2018-06-27 15:20:09 +02:00
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
import { UpdateTwoFactorAuthenticatorRequest } from 'jslib/models/request/updateTwoFactorAuthenticatorRequest';
|
|
|
|
import { TwoFactorAuthenticatorResponse } from 'jslib/models/response/twoFactorAuthenticatorResponse';
|
|
|
|
|
|
|
|
import { TwoFactorProviderType } from 'jslib/enums/twoFactorProviderType';
|
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
import { TwoFactorBaseComponent } from './two-factor-base.component';
|
|
|
|
|
2018-06-27 15:20:09 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-two-factor-authenticator',
|
|
|
|
templateUrl: 'two-factor-authenticator.component.html',
|
|
|
|
})
|
2018-07-14 05:15:09 +02:00
|
|
|
export class TwoFactorAuthenticatorComponent extends TwoFactorBaseComponent implements OnInit, OnDestroy {
|
2018-07-18 23:10:26 +02:00
|
|
|
type = TwoFactorProviderType.Authenticator;
|
2018-06-27 15:20:09 +02:00
|
|
|
key: string;
|
|
|
|
token: string;
|
|
|
|
formPromise: Promise<any>;
|
|
|
|
|
2018-07-14 05:15:09 +02:00
|
|
|
private qrScript: HTMLScriptElement;
|
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
constructor(apiService: ApiService, i18nService: I18nService,
|
2021-04-14 23:43:40 +02:00
|
|
|
toasterService: ToasterService, private userService: UserService,
|
|
|
|
platformUtilsService: PlatformUtilsService) {
|
|
|
|
super(apiService, i18nService, toasterService, platformUtilsService);
|
2018-07-14 05:15:09 +02:00
|
|
|
this.qrScript = window.document.createElement('script');
|
|
|
|
this.qrScript.src = 'scripts/qrious.min.js';
|
|
|
|
this.qrScript.async = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
window.document.body.appendChild(this.qrScript);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
window.document.body.removeChild(this.qrScript);
|
2018-06-28 05:55:50 +02:00
|
|
|
}
|
2018-06-27 15:20:09 +02:00
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
auth(authResponse: any) {
|
|
|
|
super.auth(authResponse);
|
|
|
|
return this.processResponse(authResponse.response);
|
2018-06-27 15:20:09 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
submit() {
|
2018-06-27 15:20:09 +02:00
|
|
|
if (this.enabled) {
|
2018-06-28 05:55:50 +02:00
|
|
|
return super.disable(this.formPromise);
|
2018-06-27 15:20:09 +02:00
|
|
|
} else {
|
2018-06-28 05:55:50 +02:00
|
|
|
return this.enable();
|
2018-06-27 15:20:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 05:55:50 +02:00
|
|
|
protected enable() {
|
2018-06-27 15:20:09 +02:00
|
|
|
const request = new UpdateTwoFactorAuthenticatorRequest();
|
|
|
|
request.masterPasswordHash = this.masterPasswordHash;
|
|
|
|
request.token = this.token;
|
|
|
|
request.key = this.key;
|
2018-06-28 05:55:50 +02:00
|
|
|
|
|
|
|
return super.enable(async () => {
|
2018-06-27 15:20:09 +02:00
|
|
|
this.formPromise = this.apiService.putTwoFactorAuthenticator(request);
|
|
|
|
const response = await this.formPromise;
|
|
|
|
await this.processResponse(response);
|
2018-06-28 05:55:50 +02:00
|
|
|
});
|
2018-06-27 15:20:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async processResponse(response: TwoFactorAuthenticatorResponse) {
|
|
|
|
this.token = null;
|
|
|
|
this.enabled = response.enabled;
|
|
|
|
this.key = response.key;
|
2018-07-14 05:15:09 +02:00
|
|
|
const email = await this.userService.getEmail();
|
|
|
|
window.setTimeout(() => {
|
|
|
|
const qr = new (window as any).QRious({
|
|
|
|
element: document.getElementById('qr'),
|
|
|
|
value: 'otpauth://totp/Bitwarden:' + encodeURIComponent(email) +
|
|
|
|
'?secret=' + encodeURIComponent(this.key) + '&issuer=Bitwarden',
|
|
|
|
size: 160,
|
|
|
|
});
|
|
|
|
}, 100);
|
2018-06-27 15:20:09 +02:00
|
|
|
}
|
|
|
|
}
|