bitwarden-estensione-browser/angular/src/components/verify-master-password.comp...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

91 lines
2.4 KiB
TypeScript
Raw Normal View History

import { animate, style, transition, trigger } from "@angular/animations";
import { Component, OnInit } from "@angular/core";
import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR } from "@angular/forms";
import { KeyConnectorService } from "jslib-common/abstractions/keyConnector.service";
import { UserVerificationService } from "jslib-common/abstractions/userVerification.service";
import { VerificationType } from "jslib-common/enums/verificationType";
import { Verification } from "jslib-common/types/verification";
@Component({
selector: "app-verify-master-password",
templateUrl: "verify-master-password.component.html",
providers: [
{
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: VerifyMasterPasswordComponent,
2021-12-16 13:36:21 +01:00
},
],
animations: [
trigger("sent", [
transition(":enter", [style({ opacity: 0 }), animate("100ms", style({ opacity: 1 }))]),
2021-12-16 13:36:21 +01:00
]),
],
})
export class VerifyMasterPasswordComponent implements ControlValueAccessor, OnInit {
2022-02-22 15:39:11 +01:00
usesKeyConnector = false;
disableRequestOTP = false;
sentCode = false;
2021-12-16 13:36:21 +01:00
secret = new FormControl("");
2021-12-16 13:36:21 +01:00
private onChange: (value: Verification) => void;
2021-12-16 13:36:21 +01:00
constructor(
private keyConnectorService: KeyConnectorService,
private userVerificationService: UserVerificationService
2021-12-16 13:36:21 +01:00
) {}
async ngOnInit() {
this.usesKeyConnector = await this.keyConnectorService.getUsesKeyConnector();
this.processChanges(this.secret.value);
2021-12-16 13:36:21 +01:00
this.secret.valueChanges.subscribe((secret) => this.processChanges(secret));
}
2021-12-16 13:36:21 +01:00
async requestOTP() {
if (this.usesKeyConnector) {
this.disableRequestOTP = true;
2021-12-16 13:36:21 +01:00
try {
await this.userVerificationService.requestOTP();
this.sentCode = true;
} finally {
this.disableRequestOTP = false;
2021-12-16 13:36:21 +01:00
}
}
2021-12-16 13:36:21 +01:00
}
writeValue(obj: any): void {
this.secret.setValue(obj);
2021-12-16 13:36:21 +01:00
}
registerOnChange(fn: any): void {
this.onChange = fn;
2021-12-16 13:36:21 +01:00
}
registerOnTouched(fn: any): void {
// Not implemented
2021-12-16 13:36:21 +01:00
}
setDisabledState?(isDisabled: boolean): void {
this.disableRequestOTP = isDisabled;
if (isDisabled) {
this.secret.disable();
2021-12-16 13:36:21 +01:00
} else {
this.secret.enable();
}
2021-12-16 13:36:21 +01:00
}
private processChanges(secret: string) {
if (this.onChange == null) {
return;
}
this.onChange({
type: this.usesKeyConnector ? VerificationType.OTP : VerificationType.MasterPassword,
secret: secret,
});
}
}