2018-07-14 05:15:09 +02:00
|
|
|
import { Component, OnDestroy, OnInit } from "@angular/core";
|
2018-06-27 15:20:09 +02:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
|
|
|
import { UserVerificationService } from "@bitwarden/common/abstractions/userVerification.service";
|
|
|
|
import { TwoFactorProviderType } from "@bitwarden/common/enums/twoFactorProviderType";
|
|
|
|
import { UpdateTwoFactorAuthenticatorRequest } from "@bitwarden/common/models/request/updateTwoFactorAuthenticatorRequest";
|
|
|
|
import { TwoFactorAuthenticatorResponse } from "@bitwarden/common/models/response/twoFactorAuthenticatorResponse";
|
2018-06-27 15:20:09 +02:00
|
|
|
|
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;
|
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
constructor(
|
|
|
|
apiService: ApiService,
|
|
|
|
i18nService: I18nService,
|
2021-12-07 20:41:45 +01:00
|
|
|
userVerificationService: UserVerificationService,
|
2021-12-14 17:10:26 +01:00
|
|
|
platformUtilsService: PlatformUtilsService,
|
|
|
|
logService: LogService,
|
|
|
|
private stateService: StateService
|
|
|
|
) {
|
|
|
|
super(apiService, i18nService, platformUtilsService, logService, userVerificationService);
|
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
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-27 15:20:09 +02:00
|
|
|
|
2021-11-09 19:24:26 +01:00
|
|
|
protected async enable() {
|
|
|
|
const request = await this.buildRequestModel(UpdateTwoFactorAuthenticatorRequest);
|
2018-06-27 15:20:09 +02:00
|
|
|
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;
|
2021-12-14 17:10:26 +01:00
|
|
|
const email = await this.stateService.getEmail();
|
2018-07-14 05:15:09 +02:00
|
|
|
window.setTimeout(() => {
|
2022-02-24 12:10:07 +01:00
|
|
|
new (window as any).QRious({
|
2018-07-14 05:15:09 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|