mirror of
https://github.com/bitwarden/jslib
synced 2025-12-21 10:43:23 +00:00
[EndUserVaultRefresh] Add base routing guard (#732)
* Add a base class for Angular routing guards * Update Guard naming convention
This commit is contained in:
@@ -6,15 +6,19 @@ import { MessagingService } from "jslib-common/abstractions/messaging.service";
|
||||
import { StateService } from "jslib-common/abstractions/state.service";
|
||||
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
||||
|
||||
import { BaseGuard } from "./base.guard";
|
||||
|
||||
@Injectable()
|
||||
export class AuthGuardService implements CanActivate {
|
||||
export class AuthGuard extends BaseGuard implements CanActivate {
|
||||
constructor(
|
||||
router: Router,
|
||||
private vaultTimeoutService: VaultTimeoutService,
|
||||
private router: Router,
|
||||
private messagingService: MessagingService,
|
||||
private keyConnectorService: KeyConnectorService,
|
||||
private stateService: StateService
|
||||
) {}
|
||||
) {
|
||||
super(router);
|
||||
}
|
||||
|
||||
async canActivate(route: ActivatedRouteSnapshot, routerState: RouterStateSnapshot) {
|
||||
const isAuthed = await this.stateService.getIsAuthenticated();
|
||||
@@ -28,16 +32,14 @@ export class AuthGuardService implements CanActivate {
|
||||
if (routerState != null) {
|
||||
this.messagingService.send("lockedUrl", { url: routerState.url });
|
||||
}
|
||||
this.router.navigate(["lock"], { queryParams: { promptBiometric: true } });
|
||||
return false;
|
||||
return this.redirect("lock", { queryParams: { promptBiometric: true } });
|
||||
}
|
||||
|
||||
if (
|
||||
!routerState.url.includes("remove-password") &&
|
||||
(await this.keyConnectorService.getConvertAccountRequired())
|
||||
) {
|
||||
this.router.navigate(["/remove-password"]);
|
||||
return false;
|
||||
return this.redirect("/remove-password");
|
||||
}
|
||||
|
||||
return true;
|
||||
19
angular/src/guards/base.guard.ts
Normal file
19
angular/src/guards/base.guard.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { NavigationExtras, Router } from "@angular/router";
|
||||
|
||||
const homeRoute = ["/"];
|
||||
|
||||
export class BaseGuard {
|
||||
constructor(protected router: Router) {}
|
||||
|
||||
/**
|
||||
* Cancels the current navigation and redirects the user to the Url specified, or back to a default route if no
|
||||
* route is specified. Arguments are the same as Router.navigate.
|
||||
* You should return the result of this method from a CanActivate guard to cancel the current navigation.
|
||||
*/
|
||||
protected redirect(redirectUrl: string | any[] = homeRoute, extras?: NavigationExtras): false {
|
||||
const command = Array.isArray(redirectUrl) ? redirectUrl : [redirectUrl];
|
||||
|
||||
this.router.navigate(command, extras);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -4,15 +4,19 @@ import { CanActivate, Router } from "@angular/router";
|
||||
import { StateService } from "jslib-common/abstractions/state.service";
|
||||
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
||||
|
||||
import { BaseGuard } from "./base.guard";
|
||||
|
||||
@Injectable()
|
||||
export class LockGuardService implements CanActivate {
|
||||
export class LockGuard extends BaseGuard implements CanActivate {
|
||||
protected homepage = "vault";
|
||||
protected loginpage = "login";
|
||||
constructor(
|
||||
router: Router,
|
||||
private vaultTimeoutService: VaultTimeoutService,
|
||||
private router: Router,
|
||||
private stateService: StateService
|
||||
) {}
|
||||
) {
|
||||
super(router);
|
||||
}
|
||||
|
||||
async canActivate() {
|
||||
if (await this.vaultTimeoutService.isLocked()) {
|
||||
@@ -23,7 +27,6 @@ export class LockGuardService implements CanActivate {
|
||||
? [this.homepage]
|
||||
: [this.loginpage];
|
||||
|
||||
this.router.navigate(redirectUrl);
|
||||
return false;
|
||||
return this.redirect(redirectUrl);
|
||||
}
|
||||
}
|
||||
@@ -4,25 +4,28 @@ import { CanActivate, Router } from "@angular/router";
|
||||
import { StateService } from "jslib-common/abstractions/state.service";
|
||||
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
||||
|
||||
import { BaseGuard as BaseGuard } from "./base.guard";
|
||||
|
||||
@Injectable()
|
||||
export class UnauthGuardService implements CanActivate {
|
||||
export class UnauthGuard extends BaseGuard implements CanActivate {
|
||||
protected homepage = "vault";
|
||||
constructor(
|
||||
router: Router,
|
||||
private vaultTimeoutService: VaultTimeoutService,
|
||||
private router: Router,
|
||||
private stateService: StateService
|
||||
) {}
|
||||
) {
|
||||
super(router);
|
||||
}
|
||||
|
||||
async canActivate() {
|
||||
const isAuthed = await this.stateService.getIsAuthenticated();
|
||||
if (isAuthed) {
|
||||
const locked = await this.vaultTimeoutService.isLocked();
|
||||
if (locked) {
|
||||
this.router.navigate(["lock"]);
|
||||
return this.redirect("lock");
|
||||
} else {
|
||||
this.router.navigate([this.homepage]);
|
||||
return this.redirect(this.homepage);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -72,12 +72,13 @@ import { UserVerificationService } from "jslib-common/services/userVerification.
|
||||
import { VaultTimeoutService } from "jslib-common/services/vaultTimeout.service";
|
||||
import { WebCryptoFunctionService } from "jslib-common/services/webCryptoFunction.service";
|
||||
|
||||
import { AuthGuardService } from "./auth-guard.service";
|
||||
import { AuthGuard } from "../guards/auth.guard";
|
||||
import { LockGuard } from "../guards/lock.guard";
|
||||
import { UnauthGuard } from "../guards/unauth.guard";
|
||||
|
||||
import { BroadcasterService } from "./broadcaster.service";
|
||||
import { LockGuardService } from "./lock-guard.service";
|
||||
import { ModalService } from "./modal.service";
|
||||
import { PasswordRepromptService } from "./passwordReprompt.service";
|
||||
import { UnauthGuardService } from "./unauth-guard.service";
|
||||
import { ValidationService } from "./validation.service";
|
||||
|
||||
@NgModule({
|
||||
@@ -90,9 +91,9 @@ import { ValidationService } from "./validation.service";
|
||||
deps: [I18nServiceAbstraction],
|
||||
},
|
||||
ValidationService,
|
||||
AuthGuardService,
|
||||
UnauthGuardService,
|
||||
LockGuardService,
|
||||
AuthGuard,
|
||||
UnauthGuard,
|
||||
LockGuard,
|
||||
ModalService,
|
||||
{
|
||||
provide: AppIdServiceAbstraction,
|
||||
|
||||
Reference in New Issue
Block a user