PM-4945 Update Two Factor verify dialog (#8580)

* PM-4945 Update Two Factor verify dialog

* PM-4945 Addressed review comments

* PM-4945 Removed legacy User verification component and used new one
This commit is contained in:
KiruthigaManivannan 2024-04-26 18:24:48 +05:30 committed by GitHub
parent 11ba8d188d
commit 2fa4c6e4f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 142 additions and 87 deletions

View File

@ -10,6 +10,7 @@ import { PolicyService } from "@bitwarden/common/admin-console/abstractions/poli
import { TwoFactorProviderType } from "@bitwarden/common/auth/enums/two-factor-provider-type"; import { TwoFactorProviderType } from "@bitwarden/common/auth/enums/two-factor-provider-type";
import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { DialogService } from "@bitwarden/components";
import { TwoFactorDuoComponent } from "../../../auth/settings/two-factor-duo.component"; import { TwoFactorDuoComponent } from "../../../auth/settings/two-factor-duo.component";
import { TwoFactorSetupComponent as BaseTwoFactorSetupComponent } from "../../../auth/settings/two-factor-setup.component"; import { TwoFactorSetupComponent as BaseTwoFactorSetupComponent } from "../../../auth/settings/two-factor-setup.component";
@ -22,6 +23,7 @@ import { TwoFactorSetupComponent as BaseTwoFactorSetupComponent } from "../../..
export class TwoFactorSetupComponent extends BaseTwoFactorSetupComponent { export class TwoFactorSetupComponent extends BaseTwoFactorSetupComponent {
tabbedHeader = false; tabbedHeader = false;
constructor( constructor(
dialogService: DialogService,
apiService: ApiService, apiService: ApiService,
modalService: ModalService, modalService: ModalService,
messagingService: MessagingService, messagingService: MessagingService,
@ -31,6 +33,7 @@ export class TwoFactorSetupComponent extends BaseTwoFactorSetupComponent {
billingAccountProfileStateService: BillingAccountProfileStateService, billingAccountProfileStateService: BillingAccountProfileStateService,
) { ) {
super( super(
dialogService,
apiService, apiService,
modalService, modalService,
messagingService, messagingService,

View File

@ -15,13 +15,6 @@
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
<app-two-factor-verify
[organizationId]="organizationId"
[type]="type"
(onAuthed)="auth($any($event))"
*ngIf="!authed"
>
</app-two-factor-verify>
<form <form
#form #form
(ngSubmit)="submit()" (ngSubmit)="submit()"

View File

@ -15,13 +15,6 @@
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
<app-two-factor-verify
[organizationId]="organizationId"
[type]="type"
(onAuthed)="auth($any($event))"
*ngIf="!authed"
>
</app-two-factor-verify>
<form <form
#form #form
(ngSubmit)="submit()" (ngSubmit)="submit()"

View File

@ -15,13 +15,6 @@
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
<app-two-factor-verify
[organizationId]="organizationId"
[type]="type"
(onAuthed)="auth($any($event))"
*ngIf="!authed"
>
</app-two-factor-verify>
<form <form
#form #form
(ngSubmit)="submit()" (ngSubmit)="submit()"

View File

@ -15,8 +15,6 @@
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
<app-two-factor-verify [type]="type" (onAuthed)="auth($event)" *ngIf="!authed">
</app-two-factor-verify>
<ng-container *ngIf="authed"> <ng-container *ngIf="authed">
<div class="modal-body text-center"> <div class="modal-body text-center">
<ng-container *ngIf="code"> <ng-container *ngIf="code">

View File

@ -1,5 +1,5 @@
import { Component, OnDestroy, OnInit, Type, ViewChild, ViewContainerRef } from "@angular/core"; import { Component, OnDestroy, OnInit, Type, ViewChild, ViewContainerRef } from "@angular/core";
import { firstValueFrom, Observable, Subject, takeUntil } from "rxjs"; import { firstValueFrom, lastValueFrom, Observable, Subject, takeUntil } from "rxjs";
import { ModalRef } from "@bitwarden/angular/components/modal/modal.ref"; import { ModalRef } from "@bitwarden/angular/components/modal/modal.ref";
import { ModalService } from "@bitwarden/angular/services/modal.service"; import { ModalService } from "@bitwarden/angular/services/modal.service";
@ -8,15 +8,23 @@ import { PolicyService } from "@bitwarden/common/admin-console/abstractions/poli
import { PolicyType } from "@bitwarden/common/admin-console/enums"; import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { TwoFactorProviderType } from "@bitwarden/common/auth/enums/two-factor-provider-type"; import { TwoFactorProviderType } from "@bitwarden/common/auth/enums/two-factor-provider-type";
import { TwoFactorAuthenticatorResponse } from "@bitwarden/common/auth/models/response/two-factor-authenticator.response";
import { TwoFactorDuoResponse } from "@bitwarden/common/auth/models/response/two-factor-duo.response";
import { TwoFactorEmailResponse } from "@bitwarden/common/auth/models/response/two-factor-email.response";
import { TwoFactorWebAuthnResponse } from "@bitwarden/common/auth/models/response/two-factor-web-authn.response";
import { TwoFactorYubiKeyResponse } from "@bitwarden/common/auth/models/response/two-factor-yubi-key.response";
import { TwoFactorProviders } from "@bitwarden/common/auth/services/two-factor.service"; import { TwoFactorProviders } from "@bitwarden/common/auth/services/two-factor.service";
import { AuthResponse } from "@bitwarden/common/auth/types/auth-response";
import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service";
import { ProductType } from "@bitwarden/common/enums"; import { ProductType } from "@bitwarden/common/enums";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { DialogService } from "@bitwarden/components";
import { TwoFactorAuthenticatorComponent } from "./two-factor-authenticator.component"; import { TwoFactorAuthenticatorComponent } from "./two-factor-authenticator.component";
import { TwoFactorDuoComponent } from "./two-factor-duo.component"; import { TwoFactorDuoComponent } from "./two-factor-duo.component";
import { TwoFactorEmailComponent } from "./two-factor-email.component"; import { TwoFactorEmailComponent } from "./two-factor-email.component";
import { TwoFactorRecoveryComponent } from "./two-factor-recovery.component"; import { TwoFactorRecoveryComponent } from "./two-factor-recovery.component";
import { TwoFactorVerifyComponent } from "./two-factor-verify.component";
import { TwoFactorWebAuthnComponent } from "./two-factor-webauthn.component"; import { TwoFactorWebAuthnComponent } from "./two-factor-webauthn.component";
import { TwoFactorYubiKeyComponent } from "./two-factor-yubikey.component"; import { TwoFactorYubiKeyComponent } from "./two-factor-yubikey.component";
@ -52,6 +60,7 @@ export class TwoFactorSetupComponent implements OnInit, OnDestroy {
private twoFactorAuthPolicyAppliesToActiveUser: boolean; private twoFactorAuthPolicyAppliesToActiveUser: boolean;
constructor( constructor(
protected dialogService: DialogService,
protected apiService: ApiService, protected apiService: ApiService,
protected modalService: ModalService, protected modalService: ModalService,
protected messagingService: MessagingService, protected messagingService: MessagingService,
@ -114,50 +123,82 @@ export class TwoFactorSetupComponent implements OnInit, OnDestroy {
this.loading = false; this.loading = false;
} }
async callTwoFactorVerifyDialog(type?: TwoFactorProviderType) {
const twoFactorVerifyDialogRef = TwoFactorVerifyComponent.open(this.dialogService, {
data: { type: type, organizationId: this.organizationId },
});
return await lastValueFrom(twoFactorVerifyDialogRef.closed);
}
async manage(type: TwoFactorProviderType) { async manage(type: TwoFactorProviderType) {
switch (type) { switch (type) {
case TwoFactorProviderType.Authenticator: { case TwoFactorProviderType.Authenticator: {
const result: AuthResponse<TwoFactorAuthenticatorResponse> =
await this.callTwoFactorVerifyDialog(type);
if (!result) {
return;
}
const authComp = await this.openModal( const authComp = await this.openModal(
this.authenticatorModalRef, this.authenticatorModalRef,
TwoFactorAuthenticatorComponent, TwoFactorAuthenticatorComponent,
); );
// eslint-disable-next-line rxjs-angular/prefer-takeuntil await authComp.auth(result);
authComp.onUpdated.subscribe((enabled: boolean) => { authComp.onUpdated.pipe(takeUntil(this.destroy$)).subscribe((enabled: boolean) => {
this.updateStatus(enabled, TwoFactorProviderType.Authenticator); this.updateStatus(enabled, TwoFactorProviderType.Authenticator);
}); });
break; break;
} }
case TwoFactorProviderType.Yubikey: { case TwoFactorProviderType.Yubikey: {
const result: AuthResponse<TwoFactorYubiKeyResponse> =
await this.callTwoFactorVerifyDialog(type);
if (!result) {
return;
}
const yubiComp = await this.openModal(this.yubikeyModalRef, TwoFactorYubiKeyComponent); const yubiComp = await this.openModal(this.yubikeyModalRef, TwoFactorYubiKeyComponent);
// eslint-disable-next-line rxjs-angular/prefer-takeuntil yubiComp.auth(result);
yubiComp.onUpdated.subscribe((enabled: boolean) => { yubiComp.onUpdated.pipe(takeUntil(this.destroy$)).subscribe((enabled: boolean) => {
this.updateStatus(enabled, TwoFactorProviderType.Yubikey); this.updateStatus(enabled, TwoFactorProviderType.Yubikey);
}); });
break; break;
} }
case TwoFactorProviderType.Duo: { case TwoFactorProviderType.Duo: {
const result: AuthResponse<TwoFactorDuoResponse> =
await this.callTwoFactorVerifyDialog(type);
if (!result) {
return;
}
const duoComp = await this.openModal(this.duoModalRef, TwoFactorDuoComponent); const duoComp = await this.openModal(this.duoModalRef, TwoFactorDuoComponent);
// eslint-disable-next-line rxjs-angular/prefer-takeuntil duoComp.auth(result);
duoComp.onUpdated.subscribe((enabled: boolean) => { duoComp.onUpdated.pipe(takeUntil(this.destroy$)).subscribe((enabled: boolean) => {
this.updateStatus(enabled, TwoFactorProviderType.Duo); this.updateStatus(enabled, TwoFactorProviderType.Duo);
}); });
break; break;
} }
case TwoFactorProviderType.Email: { case TwoFactorProviderType.Email: {
const result: AuthResponse<TwoFactorEmailResponse> =
await this.callTwoFactorVerifyDialog(type);
if (!result) {
return;
}
const emailComp = await this.openModal(this.emailModalRef, TwoFactorEmailComponent); const emailComp = await this.openModal(this.emailModalRef, TwoFactorEmailComponent);
// eslint-disable-next-line rxjs-angular/prefer-takeuntil await emailComp.auth(result);
emailComp.onUpdated.subscribe((enabled: boolean) => { emailComp.onUpdated.pipe(takeUntil(this.destroy$)).subscribe((enabled: boolean) => {
this.updateStatus(enabled, TwoFactorProviderType.Email); this.updateStatus(enabled, TwoFactorProviderType.Email);
}); });
break; break;
} }
case TwoFactorProviderType.WebAuthn: { case TwoFactorProviderType.WebAuthn: {
const result: AuthResponse<TwoFactorWebAuthnResponse> =
await this.callTwoFactorVerifyDialog(type);
if (!result) {
return;
}
const webAuthnComp = await this.openModal( const webAuthnComp = await this.openModal(
this.webAuthnModalRef, this.webAuthnModalRef,
TwoFactorWebAuthnComponent, TwoFactorWebAuthnComponent,
); );
// eslint-disable-next-line rxjs-angular/prefer-takeuntil webAuthnComp.auth(result);
webAuthnComp.onUpdated.subscribe((enabled: boolean) => { webAuthnComp.onUpdated.pipe(takeUntil(this.destroy$)).subscribe((enabled: boolean) => {
this.updateStatus(enabled, TwoFactorProviderType.WebAuthn); this.updateStatus(enabled, TwoFactorProviderType.WebAuthn);
}); });
break; break;
@ -167,10 +208,12 @@ export class TwoFactorSetupComponent implements OnInit, OnDestroy {
} }
} }
recoveryCode() { async recoveryCode() {
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling. const result = await this.callTwoFactorVerifyDialog(-1 as TwoFactorProviderType);
// eslint-disable-next-line @typescript-eslint/no-floating-promises if (result) {
this.openModal(this.recoveryModalRef, TwoFactorRecoveryComponent); const recoverComp = await this.openModal(this.recoveryModalRef, TwoFactorRecoveryComponent);
recoverComp.auth(result);
}
} }
async premiumRequired() { async premiumRequired() {

View File

@ -1,15 +1,23 @@
<form #form (ngSubmit)="submit()" [appApiAction]="formPromise" ngNativeValidate> <form [formGroup]="formGroup" [bitSubmit]="submit">
<div class="modal-body"> <bit-dialog dialogSize="default">
<app-user-verification [(ngModel)]="secret" ngDefaultControl name="secret"> <span bitDialogTitle>
</app-user-verification> {{ "twoStepLogin" | i18n }}
</div> <small class="tw-text-muted">{{ dialogTitle }}</small>
<div class="modal-footer"> </span>
<button type="submit" class="btn btn-primary btn-submit" [disabled]="form.loading"> <ng-container bitDialogContent>
<i class="bwi bwi-spinner bwi-spin" title="{{ 'loading' | i18n }}" aria-hidden="true"></i> <app-user-verification-form-input
<span>{{ "continue" | i18n }}</span> formControlName="secret"
</button> ngDefaultControl
<button type="button" class="btn btn-outline-secondary" data-dismiss="modal"> name="secret"
{{ "close" | i18n }} ></app-user-verification-form-input>
</button> </ng-container>
</div> <ng-container bitDialogFooter>
<button bitButton bitFormButton type="submit" buttonType="primary">
{{ "continue" | i18n }}
</button>
<button bitButton type="button" buttonType="secondary" bitDialogClose>
{{ "close" | i18n }}
</button>
</ng-container>
</bit-dialog>
</form> </form>

View File

@ -1,4 +1,6 @@
import { Component, EventEmitter, Input, Output } from "@angular/core"; import { DIALOG_DATA, DialogConfig, DialogRef } from "@angular/cdk/dialog";
import { Component, EventEmitter, Inject, Output } from "@angular/core";
import { FormControl, FormGroup } from "@angular/forms";
import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { UserVerificationService } from "@bitwarden/common/auth/abstractions/user-verification/user-verification.service.abstraction"; import { UserVerificationService } from "@bitwarden/common/auth/abstractions/user-verification/user-verification.service.abstraction";
@ -8,46 +10,74 @@ import { SecretVerificationRequest } from "@bitwarden/common/auth/models/request
import { AuthResponse } from "@bitwarden/common/auth/types/auth-response"; import { AuthResponse } from "@bitwarden/common/auth/types/auth-response";
import { TwoFactorResponse } from "@bitwarden/common/auth/types/two-factor-response"; import { TwoFactorResponse } from "@bitwarden/common/auth/types/two-factor-response";
import { Verification } from "@bitwarden/common/auth/types/verification"; import { Verification } from "@bitwarden/common/auth/types/verification";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { DialogService } from "@bitwarden/components";
type TwoFactorVerifyDialogData = {
type: TwoFactorProviderType;
organizationId: string;
};
@Component({ @Component({
selector: "app-two-factor-verify", selector: "app-two-factor-verify",
templateUrl: "two-factor-verify.component.html", templateUrl: "two-factor-verify.component.html",
}) })
export class TwoFactorVerifyComponent { export class TwoFactorVerifyComponent {
@Input() type: TwoFactorProviderType; type: TwoFactorProviderType;
@Input() organizationId: string; organizationId: string;
@Output() onAuthed = new EventEmitter<AuthResponse<TwoFactorResponse>>(); @Output() onAuthed = new EventEmitter<AuthResponse<TwoFactorResponse>>();
secret: Verification;
formPromise: Promise<TwoFactorResponse>; formPromise: Promise<TwoFactorResponse>;
protected formGroup = new FormGroup({
secret: new FormControl<Verification | null>(null),
});
constructor( constructor(
@Inject(DIALOG_DATA) protected data: TwoFactorVerifyDialogData,
private dialogRef: DialogRef,
private apiService: ApiService, private apiService: ApiService,
private logService: LogService, private i18nService: I18nService,
private userVerificationService: UserVerificationService, private userVerificationService: UserVerificationService,
) {} ) {
this.type = data.type;
this.organizationId = data.organizationId;
}
async submit() { submit = async () => {
let hashedSecret: string; let hashedSecret: string;
this.formPromise = this.userVerificationService
try { .buildRequest(this.formGroup.value.secret)
this.formPromise = this.userVerificationService.buildRequest(this.secret).then((request) => { .then((request) => {
hashedSecret = hashedSecret =
this.secret.type === VerificationType.MasterPassword this.formGroup.value.secret.type === VerificationType.MasterPassword
? request.masterPasswordHash ? request.masterPasswordHash
: request.otp; : request.otp;
return this.apiCall(request); return this.apiCall(request);
}); });
const response = await this.formPromise; const response = await this.formPromise;
this.onAuthed.emit({ this.dialogRef.close({
response: response, response: response,
secret: hashedSecret, secret: hashedSecret,
verificationType: this.secret.type, verificationType: this.formGroup.value.secret.type,
}); });
} catch (e) { };
this.logService.error(e);
get dialogTitle(): string {
switch (this.type) {
case -1 as TwoFactorProviderType:
return this.i18nService.t("recoveryCodeTitle");
case TwoFactorProviderType.Duo:
return "Duo";
case TwoFactorProviderType.Email:
return this.i18nService.t("emailTitle");
case TwoFactorProviderType.WebAuthn:
return this.i18nService.t("webAuthnTitle");
case TwoFactorProviderType.Authenticator:
return this.i18nService.t("authenticatorAppTitle");
case TwoFactorProviderType.Yubikey:
return "Yubikey";
} }
} }
@ -72,4 +102,8 @@ export class TwoFactorVerifyComponent {
return this.apiService.getTwoFactorYubiKey(request); return this.apiService.getTwoFactorYubiKey(request);
} }
} }
static open(dialogService: DialogService, config: DialogConfig<TwoFactorVerifyDialogData>) {
return dialogService.open<AuthResponse<any>>(TwoFactorVerifyComponent, config);
}
} }

View File

@ -15,13 +15,6 @@
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
<app-two-factor-verify
[organizationId]="organizationId"
[type]="type"
(onAuthed)="auth($any($event))"
*ngIf="!authed"
>
</app-two-factor-verify>
<form <form
#form #form
(ngSubmit)="submit()" (ngSubmit)="submit()"

View File

@ -15,13 +15,6 @@
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
<app-two-factor-verify
[organizationId]="organizationId"
[type]="type"
(onAuthed)="auth($any($event))"
*ngIf="!authed"
>
</app-two-factor-verify>
<form <form
#form #form
(ngSubmit)="submit()" (ngSubmit)="submit()"

View File

@ -1,6 +1,9 @@
import { NgModule } from "@angular/core"; import { NgModule } from "@angular/core";
import { PasswordCalloutComponent } from "@bitwarden/auth/angular"; import {
PasswordCalloutComponent,
UserVerificationFormInputComponent,
} from "@bitwarden/auth/angular";
import { LayoutComponent, NavigationModule } from "@bitwarden/components"; import { LayoutComponent, NavigationModule } from "@bitwarden/components";
import { OrganizationLayoutComponent } from "../admin-console/organizations/layouts/organization-layout.component"; import { OrganizationLayoutComponent } from "../admin-console/organizations/layouts/organization-layout.component";
@ -106,6 +109,7 @@ import { SharedModule } from "./shared.module";
OrganizationBadgeModule, OrganizationBadgeModule,
PipesModule, PipesModule,
PasswordCalloutComponent, PasswordCalloutComponent,
UserVerificationFormInputComponent,
DangerZoneComponent, DangerZoneComponent,
LayoutComponent, LayoutComponent,
NavigationModule, NavigationModule,