mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 16:53:34 +00:00
Refactored fido2 popup to use auth guard when routing to component, added BrowserRouterService to track previous page and route using that
This commit is contained in:
35
apps/browser/src/auth/guards/fido2-auth.guard.ts
Normal file
35
apps/browser/src/auth/guards/fido2-auth.guard.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { inject } from "@angular/core";
|
||||
import {
|
||||
ActivatedRouteSnapshot,
|
||||
CanActivateFn,
|
||||
Router,
|
||||
RouterStateSnapshot,
|
||||
} from "@angular/router";
|
||||
|
||||
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
|
||||
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
|
||||
|
||||
import { BrowserRouterService } from "../../platform/popup/services/browser-router.service";
|
||||
|
||||
export const fido2AuthGuard: CanActivateFn = async (
|
||||
route: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot
|
||||
) => {
|
||||
const routerService = inject(BrowserRouterService);
|
||||
const authService = inject(AuthService);
|
||||
const router = inject(Router);
|
||||
|
||||
const authStatus = await authService.getAuthStatus();
|
||||
|
||||
if (authStatus === AuthenticationStatus.LoggedOut) {
|
||||
await routerService.setPreviousUrl(state.url);
|
||||
return router.createUrlTree(["/home"], { queryParams: route.queryParams });
|
||||
}
|
||||
|
||||
if (authStatus === AuthenticationStatus.Locked) {
|
||||
await routerService.setPreviousUrl(state.url);
|
||||
return router.createUrlTree(["/lock"], { queryParams: route.queryParams });
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
@@ -0,0 +1,50 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { ActivatedRoute, ActivatedRouteSnapshot, NavigationEnd, Router } from "@angular/router";
|
||||
import { filter } from "rxjs";
|
||||
|
||||
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
||||
|
||||
@Injectable({
|
||||
providedIn: "root",
|
||||
})
|
||||
export class BrowserRouterService {
|
||||
constructor(router: Router, private stateService: StateService) {
|
||||
router.events
|
||||
.pipe(filter((e) => e instanceof NavigationEnd))
|
||||
.subscribe((event: NavigationEnd) => {
|
||||
const state: ActivatedRouteSnapshot = router.routerState.snapshot.root;
|
||||
|
||||
let child = state.firstChild;
|
||||
while (child.firstChild) {
|
||||
child = child.firstChild;
|
||||
}
|
||||
|
||||
const updateUrl = !child?.data?.doNotSaveUrl ?? true;
|
||||
|
||||
if (updateUrl) {
|
||||
this.setPreviousUrl(event.url);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async getPreviousUrl() {
|
||||
return this.stateService.getPreviousUrl();
|
||||
}
|
||||
|
||||
// Check validity of previous url
|
||||
async hasPreviousUrl() {
|
||||
return (await this.getPreviousUrl()) != "/";
|
||||
}
|
||||
|
||||
async setPreviousUrl(url: string) {
|
||||
await this.stateService.setPreviousUrl(url);
|
||||
}
|
||||
|
||||
private getDeepestChild(activatedRoute: ActivatedRoute): ActivatedRoute {
|
||||
let child = activatedRoute;
|
||||
while (child.firstChild) {
|
||||
child = child.firstChild;
|
||||
}
|
||||
return child;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
import { canAccessFeature } from "@bitwarden/angular/guard/feature-flag.guard";
|
||||
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
|
||||
|
||||
import { fido2AuthGuard } from "../auth/guards/fido2-auth.guard";
|
||||
import { EnvironmentComponent } from "../auth/popup/environment.component";
|
||||
import { HintComponent } from "../auth/popup/hint.component";
|
||||
import { HomeComponent } from "../auth/popup/home.component";
|
||||
@@ -72,17 +73,19 @@ const routes: Routes = [
|
||||
path: "home",
|
||||
component: HomeComponent,
|
||||
canActivate: [UnauthGuard],
|
||||
data: { state: "home" },
|
||||
data: { state: "home", doNotSaveUrl: true },
|
||||
},
|
||||
{
|
||||
path: "fido2",
|
||||
component: Fido2Component,
|
||||
canActivate: [fido2AuthGuard],
|
||||
data: { state: "fido2" },
|
||||
},
|
||||
{
|
||||
path: "login",
|
||||
component: LoginComponent,
|
||||
canActivate: [UnauthGuard],
|
||||
data: { state: "login" },
|
||||
data: { state: "login", doNotSaveUrl: true },
|
||||
},
|
||||
{
|
||||
path: "login-with-device",
|
||||
@@ -100,13 +103,13 @@ const routes: Routes = [
|
||||
path: "lock",
|
||||
component: LockComponent,
|
||||
canActivate: [lockGuard()],
|
||||
data: { state: "lock" },
|
||||
data: { state: "lock", doNotSaveUrl: true },
|
||||
},
|
||||
{
|
||||
path: "2fa",
|
||||
component: TwoFactorComponent,
|
||||
canActivate: [UnauthGuard],
|
||||
data: { state: "2fa" },
|
||||
data: { state: "2fa", doNotSaveUrl: true },
|
||||
},
|
||||
{
|
||||
path: "2fa-options",
|
||||
|
||||
Reference in New Issue
Block a user