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 { StateService } from "jslib-common/abstractions/state.service";
|
||||||
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
||||||
|
|
||||||
|
import { BaseGuard } from "./base.guard";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthGuardService implements CanActivate {
|
export class AuthGuard extends BaseGuard implements CanActivate {
|
||||||
constructor(
|
constructor(
|
||||||
|
router: Router,
|
||||||
private vaultTimeoutService: VaultTimeoutService,
|
private vaultTimeoutService: VaultTimeoutService,
|
||||||
private router: Router,
|
|
||||||
private messagingService: MessagingService,
|
private messagingService: MessagingService,
|
||||||
private keyConnectorService: KeyConnectorService,
|
private keyConnectorService: KeyConnectorService,
|
||||||
private stateService: StateService
|
private stateService: StateService
|
||||||
) {}
|
) {
|
||||||
|
super(router);
|
||||||
|
}
|
||||||
|
|
||||||
async canActivate(route: ActivatedRouteSnapshot, routerState: RouterStateSnapshot) {
|
async canActivate(route: ActivatedRouteSnapshot, routerState: RouterStateSnapshot) {
|
||||||
const isAuthed = await this.stateService.getIsAuthenticated();
|
const isAuthed = await this.stateService.getIsAuthenticated();
|
||||||
@@ -28,16 +32,14 @@ export class AuthGuardService implements CanActivate {
|
|||||||
if (routerState != null) {
|
if (routerState != null) {
|
||||||
this.messagingService.send("lockedUrl", { url: routerState.url });
|
this.messagingService.send("lockedUrl", { url: routerState.url });
|
||||||
}
|
}
|
||||||
this.router.navigate(["lock"], { queryParams: { promptBiometric: true } });
|
return this.redirect("lock", { queryParams: { promptBiometric: true } });
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!routerState.url.includes("remove-password") &&
|
!routerState.url.includes("remove-password") &&
|
||||||
(await this.keyConnectorService.getConvertAccountRequired())
|
(await this.keyConnectorService.getConvertAccountRequired())
|
||||||
) {
|
) {
|
||||||
this.router.navigate(["/remove-password"]);
|
return this.redirect("/remove-password");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
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 { StateService } from "jslib-common/abstractions/state.service";
|
||||||
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
||||||
|
|
||||||
|
import { BaseGuard } from "./base.guard";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LockGuardService implements CanActivate {
|
export class LockGuard extends BaseGuard implements CanActivate {
|
||||||
protected homepage = "vault";
|
protected homepage = "vault";
|
||||||
protected loginpage = "login";
|
protected loginpage = "login";
|
||||||
constructor(
|
constructor(
|
||||||
|
router: Router,
|
||||||
private vaultTimeoutService: VaultTimeoutService,
|
private vaultTimeoutService: VaultTimeoutService,
|
||||||
private router: Router,
|
|
||||||
private stateService: StateService
|
private stateService: StateService
|
||||||
) {}
|
) {
|
||||||
|
super(router);
|
||||||
|
}
|
||||||
|
|
||||||
async canActivate() {
|
async canActivate() {
|
||||||
if (await this.vaultTimeoutService.isLocked()) {
|
if (await this.vaultTimeoutService.isLocked()) {
|
||||||
@@ -23,7 +27,6 @@ export class LockGuardService implements CanActivate {
|
|||||||
? [this.homepage]
|
? [this.homepage]
|
||||||
: [this.loginpage];
|
: [this.loginpage];
|
||||||
|
|
||||||
this.router.navigate(redirectUrl);
|
return this.redirect(redirectUrl);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,25 +4,28 @@ import { CanActivate, Router } from "@angular/router";
|
|||||||
import { StateService } from "jslib-common/abstractions/state.service";
|
import { StateService } from "jslib-common/abstractions/state.service";
|
||||||
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
|
||||||
|
|
||||||
|
import { BaseGuard as BaseGuard } from "./base.guard";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UnauthGuardService implements CanActivate {
|
export class UnauthGuard extends BaseGuard implements CanActivate {
|
||||||
protected homepage = "vault";
|
protected homepage = "vault";
|
||||||
constructor(
|
constructor(
|
||||||
|
router: Router,
|
||||||
private vaultTimeoutService: VaultTimeoutService,
|
private vaultTimeoutService: VaultTimeoutService,
|
||||||
private router: Router,
|
|
||||||
private stateService: StateService
|
private stateService: StateService
|
||||||
) {}
|
) {
|
||||||
|
super(router);
|
||||||
|
}
|
||||||
|
|
||||||
async canActivate() {
|
async canActivate() {
|
||||||
const isAuthed = await this.stateService.getIsAuthenticated();
|
const isAuthed = await this.stateService.getIsAuthenticated();
|
||||||
if (isAuthed) {
|
if (isAuthed) {
|
||||||
const locked = await this.vaultTimeoutService.isLocked();
|
const locked = await this.vaultTimeoutService.isLocked();
|
||||||
if (locked) {
|
if (locked) {
|
||||||
this.router.navigate(["lock"]);
|
return this.redirect("lock");
|
||||||
} else {
|
} else {
|
||||||
this.router.navigate([this.homepage]);
|
return this.redirect(this.homepage);
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -72,12 +72,13 @@ import { UserVerificationService } from "jslib-common/services/userVerification.
|
|||||||
import { VaultTimeoutService } from "jslib-common/services/vaultTimeout.service";
|
import { VaultTimeoutService } from "jslib-common/services/vaultTimeout.service";
|
||||||
import { WebCryptoFunctionService } from "jslib-common/services/webCryptoFunction.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 { BroadcasterService } from "./broadcaster.service";
|
||||||
import { LockGuardService } from "./lock-guard.service";
|
|
||||||
import { ModalService } from "./modal.service";
|
import { ModalService } from "./modal.service";
|
||||||
import { PasswordRepromptService } from "./passwordReprompt.service";
|
import { PasswordRepromptService } from "./passwordReprompt.service";
|
||||||
import { UnauthGuardService } from "./unauth-guard.service";
|
|
||||||
import { ValidationService } from "./validation.service";
|
import { ValidationService } from "./validation.service";
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
@@ -90,9 +91,9 @@ import { ValidationService } from "./validation.service";
|
|||||||
deps: [I18nServiceAbstraction],
|
deps: [I18nServiceAbstraction],
|
||||||
},
|
},
|
||||||
ValidationService,
|
ValidationService,
|
||||||
AuthGuardService,
|
AuthGuard,
|
||||||
UnauthGuardService,
|
UnauthGuard,
|
||||||
LockGuardService,
|
LockGuard,
|
||||||
ModalService,
|
ModalService,
|
||||||
{
|
{
|
||||||
provide: AppIdServiceAbstraction,
|
provide: AppIdServiceAbstraction,
|
||||||
|
|||||||
Reference in New Issue
Block a user