Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change how AuthGuardService works #589

Merged
merged 1 commit into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions static/skywire-manager-src/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import { AllLabelsComponent } from './components/pages/settings/all-labels/all-l
const routes: Routes = [
{
path: 'login',
component: LoginComponent,
canActivate: [AuthGuardService]
component: LoginComponent
},
{
path: 'nodes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MatDialog } from '@angular/material/dialog';
import { Subscription } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';

import { AuthService } from '../../../services/auth.service';
import { AuthService, AuthStates } from '../../../services/auth.service';
import { SnackbarService } from '../../../services/snackbar.service';
import { InitialSetupComponent } from './initial-setup/initial-setup.component';
import { OperationError } from '../../../utils/operation-error';
Expand All @@ -23,7 +23,8 @@ export class LoginComponent implements OnInit, OnDestroy {
form: FormGroup;
loading = false;

private subscription: Subscription;
private verificationSubscription: Subscription;
private loginSubscription: Subscription;

constructor(
private authService: AuthService,
Expand All @@ -33,15 +34,24 @@ export class LoginComponent implements OnInit, OnDestroy {
) { }

ngOnInit() {
// Check if the user is already logged.
this.verificationSubscription = this.authService.checkLogin().subscribe(response => {
if (response !== AuthStates.NotLogged) {
this.router.navigate(['nodes'], { replaceUrl: true });
}
});

this.form = new FormGroup({
'password': new FormControl('', Validators.required),
});
}

ngOnDestroy() {
if (this.subscription) {
this.subscription.unsubscribe();
if (this.loginSubscription) {
this.loginSubscription.unsubscribe();
}

this.verificationSubscription.unsubscribe();
}

login() {
Expand All @@ -50,7 +60,7 @@ export class LoginComponent implements OnInit, OnDestroy {
}

this.loading = true;
this.subscription = this.authService.login(this.form.get('password').value).subscribe(
this.loginSubscription = this.authService.login(this.form.get('password').value).subscribe(
() => this.onLoginSuccess(),
err => this.onLoginError(err)
);
Expand Down
60 changes: 20 additions & 40 deletions static/skywire-manager-src/src/app/services/auth-guard.service.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,44 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, CanActivateChild } from '@angular/router';
import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { MatDialog } from '@angular/material/dialog';

import { AuthService, AuthStates } from './auth.service';

/**
* Redirects unauthorized users to the login page during the first load and always redirects
* authorized users from the login page to the node list. The api service is in chage of
* redirecting the unauthorized users to the login page in other cases.
/**
* Redirects the user to the login page if the forceFail property is set to true. The api
* service is in chage of redirecting the unauthorized users to the login page in other cases.
* It must be used in the canActivate and canActivateChild properties of the routing module.
*/
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate, CanActivateChild {
private authChecked = false;
private forceFailInternal = false;
/**
* If true, the user will be redirected to the login page.
*/
set forceFail(val: boolean) {
this.forceFailInternal = val;
}

constructor(
private authService: AuthService,
private router: Router,
private matDialog: MatDialog,
) { }

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.checkIfCanActivate(route);
return this.checkIfCanActivate();
}

canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.checkIfCanActivate(childRoute);
return this.checkIfCanActivate();
}

private checkIfCanActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
if (this.authChecked && route.routeConfig.path !== 'login') {
return of(true);
}

return this.authService.checkLogin().pipe(catchError(e => {
return of(AuthStates.AuthDisabled);
}), map((authState: AuthStates) => {
this.authChecked = true;
private checkIfCanActivate(): Observable<boolean> {
if (this.forceFailInternal) {
// Redirect the user.
this.router.navigate(['login'], { replaceUrl: true });

// If the user is trying to access "Login" page while he is already logged in or the
// auth is disabled, redirect him to "Nodes" page
if (route.routeConfig.path === 'login' && (authState === AuthStates.Logged || authState === AuthStates.AuthDisabled)) {
this.router.navigate(['nodes'], { replaceUrl: true });

return false;
}

// If the user is trying to access a protected part of the application while not logged in,
// redirect him to "Login" page
if (route.routeConfig.path !== 'login' && (authState !== AuthStates.Logged && authState !== AuthStates.AuthDisabled)) {
this.router.navigate(['login'], { replaceUrl: true });
this.matDialog.closeAll();

return false;
}
return of(false);
}

return true;
}));
return of(true);
}
}
8 changes: 8 additions & 0 deletions static/skywire-manager-src/src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HttpErrorResponse } from '@angular/common/http';
import { ApiService, ResponseTypes, RequestOptions } from './api.service';
import { OperationError } from '../utils/operation-error';
import { processServiceError } from '../utils/errors';
import { AuthGuardService } from './auth-guard.service';

export enum AuthStates {
AuthDisabled, Logged, NotLogged
Expand All @@ -22,6 +23,7 @@ export class AuthService {
constructor(
private apiService: ApiService,
private translateService: TranslateService,
private authGuardService: AuthGuardService,
) { }

/**
Expand All @@ -34,6 +36,8 @@ export class AuthService {
if (status !== true) {
throw new Error();
}

this.authGuardService.forceFail = false;
}),
);
}
Expand All @@ -56,6 +60,8 @@ export class AuthService {

// The user is not logged.
if (err.originalError && (err.originalError as HttpErrorResponse).status === 401) {
this.authGuardService.forceFail = true;

return of(AuthStates.NotLogged);
}

Expand All @@ -74,6 +80,8 @@ export class AuthService {
if (status !== true) {
throw new Error();
}

this.authGuardService.forceFail = true;
}),
);
}
Expand Down