Files
clients/libs/angular/src/auth/components/user-verification.component.ts

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

86 lines
2.6 KiB
TypeScript
Raw Normal View History

import { Directive, OnInit } from "@angular/core";
import { ControlValueAccessor, FormControl } from "@angular/forms";
import { UserVerificationService } from "@bitwarden/common/abstractions/userVerification/userVerification.service.abstraction";
Auth/ps 2298 reorg auth (#4564) * Move auth service factories to Auth team * Move authentication componenets to Auth team * Move auth guard services to Auth team * Move Duo content script to Auth team * Move auth CLI commands to Auth team * Move Desktop Account components to Auth Team * Move Desktop guards to Auth team * Move two-factor provider images to Auth team * Move web Accounts components to Auth Team * Move web settings components to Auth Team * Move web two factor images to Auth Team * Fix missed import changes for Auth Team * Fix Linting errors * Fix missed CLI imports * Fix missed Desktop imports * Revert images move * Fix missed imports in Web * Move angular lib components to Auth Team * Move angular auth guards to Auth team * Move strategy specs to Auth team * Update .eslintignore for new paths * Move lib common abstractions to Auth team * Move services to Auth team * Move common lib enums to Auth team * Move webauthn iframe to Auth team * Move lib common domain models to Auth team * Move common lib requests to Auth team * Move response models to Auth team * Clean up whitelist * Move bit web components to Auth team * Move SSO and SCIM files to Auth team * Revert move SCIM to Auth team SCIM belongs to Admin Console team * Move captcha to Auth team * Move key connector to Auth team * Move emergency access to auth team * Delete extra file * linter fixes * Move kdf config to auth team * Fix whitelist * Fix duo autoformat * Complete two factor provider request move * Fix whitelist names * Fix login capitalization * Revert hint dependency reordering * Revert hint dependency reordering * Revert hint component This components is being picked up as a move between clients * Move web hint component to Auth team * Move new files to auth team * Fix desktop build * Fix browser build
2023-02-06 16:53:37 -05:00
import { KeyConnectorService } from "@bitwarden/common/auth/abstractions/key-connector.service";
import { VerificationType } from "@bitwarden/common/auth/enums/verification-type";
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.
*/
@Directive({
selector: "app-user-verification",
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,
});
}
}