[ADF-4183] improved auth redirection (#4399)

* base auth guard implementation

* remove code duplication

* unit test fixes
This commit is contained in:
Denys Vuika
2019-03-06 13:14:23 +00:00
committed by Eugenio Romano
parent bf331f1145
commit f6341e31a0
9 changed files with 216 additions and 173 deletions

View File

@@ -16,57 +16,30 @@
*/
import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot, CanActivate,
CanActivateChild, RouterStateSnapshot, Router
} from '@angular/router';
import { ActivatedRouteSnapshot, Router } from '@angular/router';
import { AuthenticationService } from './authentication.service';
import { Observable } from 'rxjs';
import { AppConfigService, AppConfigValues } from '../app-config/app-config.service';
import { OauthConfigModel } from '../models/oauth-config.model';
import { AppConfigService } from '../app-config/app-config.service';
import { AuthGuardBase } from './auth-guard-base';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(private authService: AuthenticationService,
private router: Router,
private appConfigService: AppConfigService) {
export class AuthGuard extends AuthGuardBase {
constructor(authenticationService: AuthenticationService,
router: Router,
appConfigService: AppConfigService) {
super(authenticationService, router, appConfigService);
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
const redirectUrl = state.url;
return this.checkLogin(redirectUrl);
}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.canActivate(route, state);
}
checkLogin(redirectUrl: string): boolean {
let withCredentialsMode = this.appConfigService.get<boolean>('auth.withCredentials', false);
if (this.authService.isLoggedIn() || withCredentialsMode) {
checkLogin(activeRoute: ActivatedRouteSnapshot, redirectUrl: string): Observable<boolean> | Promise<boolean> | boolean {
if (this.authenticationService.isLoggedIn() || this.withCredentials) {
return true;
}
if (!this.authService.isOauth() || this.isOAuthWithoutSilentLogin()) {
this.authService.setRedirect({ provider: 'ALL', url: redirectUrl });
const pathToLogin = this.getRouteDestinationForLogin();
this.router.navigate(['/' + pathToLogin]);
if (!this.authenticationService.isOauth() || this.isOAuthWithoutSilentLogin()) {
this.redirectToUrl('ALL', redirectUrl);
}
return false;
}
isOAuthWithoutSilentLogin() {
let oauth: OauthConfigModel = this.appConfigService.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null);
return this.authService.isOauth() && oauth.silentLogin === false;
}
public getRouteDestinationForLogin(): string {
return this.appConfigService &&
this.appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE) ?
this.appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE) : 'login';
}
}