bitwarden-estensione-browser/libs/angular/src/components/user-verification.component.ts

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

100 lines
3.0 KiB
TypeScript
Raw Normal View History

import { animate, style, transition, trigger } from "@angular/animations";
import { Component, OnInit } from "@angular/core";
import { ControlValueAccessor, NG_VALUE_ACCESSOR, FormControl } from "@angular/forms";
2022-06-14 17:10:53 +02:00
import { KeyConnectorService } from "@bitwarden/common/abstractions/keyConnector.service";
import { UserVerificationService } from "@bitwarden/common/abstractions/userVerification/userVerification.service.abstraction";
2022-06-14 17:10:53 +02:00
import { VerificationType } from "@bitwarden/common/enums/verificationType";
import { Utils } from "@bitwarden/common/misc/utils";
2022-06-14 17:10:53 +02:00
import { Verification } from "@bitwarden/common/types/verification";
/**
* Used for general-purpose user verification throughout the app.
* Collects the user's master password, or if they are using Key Connector, prompts for an OTP via email.
* This is exposed to the parent component via the ControlValueAccessor interface (e.g. bind it to a FormControl).
* Use UserVerificationService to verify the user's input.
*/
@Component({
selector: "app-user-verification",
templateUrl: "user-verification.component.html",
providers: [
{
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: UserVerificationComponent,
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
]),
],
})
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
export class UserVerificationComponent 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
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
this.secret.valueChanges.subscribe((secret: string) => 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: Utils.isNullOrWhitespace(secret) ? null : secret,
});
}
}