From c0de0d50872865e9b5067465ec47631b302fe346 Mon Sep 17 00:00:00 2001 From: Cilibiu Bogdan Date: Thu, 14 Jun 2018 12:19:34 +0300 Subject: [PATCH] [ADF-3181] Auth Guard - redirect by string url (#3474) * redirect by string url * auth guard tests fix * revise --- .../login/components/login.component.spec.ts | 6 ++--- lib/core/login/components/login.component.ts | 6 ++--- lib/core/mock/authentication.service.mock.ts | 8 +++---- lib/core/models/redirection.model.ts | 4 ++-- .../services/auth-guard-bpm.service.spec.ts | 16 ++++++------- lib/core/services/auth-guard-bpm.service.ts | 22 ++--------------- .../services/auth-guard-ecm.service.spec.ts | 14 +++++------ lib/core/services/auth-guard-ecm.service.ts | 23 ++---------------- lib/core/services/auth-guard.service.spec.ts | 8 +++---- lib/core/services/auth-guard.service.ts | 23 ++---------------- .../services/authentication.service.spec.ts | 24 +++++++++---------- lib/core/services/authentication.service.ts | 12 +++++----- 12 files changed, 55 insertions(+), 111 deletions(-) diff --git a/lib/core/login/components/login.component.spec.ts b/lib/core/login/components/login.component.spec.ts index e72419a9cb..751cddbd74 100644 --- a/lib/core/login/components/login.component.spec.ts +++ b/lib/core/login/components/login.component.spec.ts @@ -113,12 +113,12 @@ describe('LoginComponent', () => { spyOn(authService, 'login').and.returnValue(Observable.of({ type: 'type', ticket: 'ticket' })); const redirect = '/home'; component.successRoute = redirect; - authService.setRedirect({ provider: 'ECM', navigation: ['some-route'] }); + authService.setRedirect({ provider: 'ECM', url: 'some-route' }); - spyOn(router, 'navigate'); + spyOn(router, 'navigateByUrl'); loginWithCredentials('fake-username', 'fake-password'); - expect(router.navigate).toHaveBeenCalledWith(['some-route']); + expect(router.navigateByUrl).toHaveBeenCalledWith('some-route'); }); it('should update user preferences upon login', async(() => { diff --git a/lib/core/login/components/login.component.ts b/lib/core/login/components/login.component.ts index 57aeea26d9..7fdee5f0d7 100644 --- a/lib/core/login/components/login.component.ts +++ b/lib/core/login/components/login.component.ts @@ -219,16 +219,16 @@ export class LoginComponent implements OnInit { this.authService.login(values.username, values.password, this.rememberMe) .subscribe( (token: any) => { - const redirect = this.authService.getRedirect(this.providers); + const redirectUrl = this.authService.getRedirect(this.providers); this.actualLoginStep = LoginSteps.Welcome; this.userPreferences.setStoragePrefix(values.username); values.password = null; this.success.emit(new LoginSuccessEvent(token, values.username, null)); - if (redirect) { + if (redirectUrl) { this.authService.setRedirect(null); - this.router.navigate(redirect); + this.router.navigateByUrl(redirectUrl); } else if (this.successRoute) { this.router.navigate([this.successRoute]); } diff --git a/lib/core/mock/authentication.service.mock.ts b/lib/core/mock/authentication.service.mock.ts index 5e18834d5f..61d502f2be 100644 --- a/lib/core/mock/authentication.service.mock.ts +++ b/lib/core/mock/authentication.service.mock.ts @@ -21,14 +21,14 @@ import { RedirectionModel } from '../models/redirection.model'; // TODO: should be extending AuthenticationService export class AuthenticationMock /*extends AuthenticationService*/ { - private redirect: RedirectionModel = null; + private redirectUrl: RedirectionModel = null; setRedirectUrl(url: RedirectionModel) { - this.redirect = url; + this.redirectUrl = url; } - getRedirectUrl(): any[] { - return this.redirect ? this.redirect.navigation : null; + getRedirectUrl(): string|null { + return this.redirectUrl ? this.redirectUrl.url : null; } // TODO: real auth service returns Observable diff --git a/lib/core/models/redirection.model.ts b/lib/core/models/redirection.model.ts index 61a24a4f9d..c44359d843 100644 --- a/lib/core/models/redirection.model.ts +++ b/lib/core/models/redirection.model.ts @@ -21,12 +21,12 @@ export class RedirectionModel { provider: string; - navigation?: any[]; + url?: string; constructor(obj?: any) { if (obj) { this.provider = obj.provider; - this.navigation = obj.navigation || null; + this.url = obj.url || null; } } diff --git a/lib/core/services/auth-guard-bpm.service.spec.ts b/lib/core/services/auth-guard-bpm.service.spec.ts index 729ddac683..7b6c134ee3 100644 --- a/lib/core/services/auth-guard-bpm.service.spec.ts +++ b/lib/core/services/auth-guard-bpm.service.spec.ts @@ -68,7 +68,7 @@ describe('AuthGuardService BPM', () => { expect(routerService.navigate).toHaveBeenCalledWith(['/login']); })); - it('should set redirect navigation commands', async(() => { + it('should set redirect url', async(() => { spyOn(authService, 'setRedirect').and.callThrough(); spyOn(routerService, 'navigate').and.stub(); const router: RouterStateSnapshot = { url: 'some-url' }; @@ -76,9 +76,9 @@ describe('AuthGuardService BPM', () => { authGuard.canActivate(null, router); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'BPM', navigation: ['some-url', {}] + provider: 'BPM', url: 'some-url' }); - expect(authService.getRedirect('BPM')).toEqual(['some-url', {}]); + expect(authService.getRedirect('BPM')).toEqual('some-url'); })); it('should set redirect navigation commands with query params', async(() => { @@ -89,9 +89,9 @@ describe('AuthGuardService BPM', () => { authGuard.canActivate(null, router); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'BPM', navigation: ['some-url', {q: '123'}] + provider: 'BPM', url: 'some-url;q=123' }); - expect(authService.getRedirect('BPM')).toEqual(['some-url', { q: '123' }]); + expect(authService.getRedirect('BPM')).toEqual('some-url;q=123'); })); it('should set redirect navigation commands with query params', async(() => { @@ -102,9 +102,9 @@ describe('AuthGuardService BPM', () => { authGuard.canActivate(null, router); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'BPM', navigation: ['/'] + provider: 'BPM', url: '/' }); - expect(authService.getRedirect('BPM')).toEqual(['/']); + expect(authService.getRedirect('BPM')).toEqual('/'); })); it('should get redirect url from config if there is one configured', async(() => { @@ -116,7 +116,7 @@ describe('AuthGuardService BPM', () => { authGuard.canActivate(null, router); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'BPM', navigation: ['some-url', {}] + provider: 'BPM', url: 'some-url' }); expect(routerService.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']); })); diff --git a/lib/core/services/auth-guard-bpm.service.ts b/lib/core/services/auth-guard-bpm.service.ts index e173c18718..b289c215cb 100644 --- a/lib/core/services/auth-guard-bpm.service.ts +++ b/lib/core/services/auth-guard-bpm.service.ts @@ -18,7 +18,7 @@ import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, RouterStateSnapshot, - Router, PRIMARY_OUTLET, UrlTree, UrlSegmentGroup, UrlSegment + Router } from '@angular/router'; import { AppConfigService } from '../app-config/app-config.service'; import { AuthenticationService } from './authentication.service'; @@ -45,9 +45,7 @@ export class AuthGuardBpm implements CanActivate, CanActivateChild { } if (!this.authService.isOauth() || this.isOAuthWithoutSilentLogin() ) { - const navigation = this.getNavigationCommands(redirectUrl); - - this.authService.setRedirect({ provider: 'BPM', navigation }); + this.authService.setRedirect({ provider: 'BPM', url: redirectUrl }); const pathToLogin = this.getRouteDestinationForLogin(); this.router.navigate(['/' + pathToLogin]); } @@ -64,20 +62,4 @@ export class AuthGuardBpm implements CanActivate, CanActivateChild { this.appConfig.get('loginRoute') ? this.appConfig.get('loginRoute') : 'login'; } - - private getNavigationCommands(redirectUrl: string): any[] { - const urlTree: UrlTree = this.router.parseUrl(redirectUrl); - const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; - - if (!urlSegmentGroup) { - return [redirectUrl]; - } - - const urlSegments: UrlSegment[] = urlSegmentGroup.segments; - - return urlSegments.reduce(function(acc, item) { - acc.push(item.path, item.parameters); - return acc; - }, []); - } } diff --git a/lib/core/services/auth-guard-ecm.service.spec.ts b/lib/core/services/auth-guard-ecm.service.spec.ts index 4c6a460187..58d37fa835 100644 --- a/lib/core/services/auth-guard-ecm.service.spec.ts +++ b/lib/core/services/auth-guard-ecm.service.spec.ts @@ -76,9 +76,9 @@ describe('AuthGuardService ECM', () => { authGuard.canActivate(null, router); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'ECM', navigation: ['some-url', {}] + provider: 'ECM', url: 'some-url' }); - expect(authService.getRedirect('ECM')).toEqual(['some-url', {}]); + expect(authService.getRedirect('ECM')).toEqual('some-url'); })); it('should set redirect navigation commands with query params', async(() => { @@ -89,9 +89,9 @@ describe('AuthGuardService ECM', () => { authGuard.canActivate(null, router); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'ECM', navigation: ['some-url', {q: '123'}] + provider: 'ECM', url: 'some-url;q=123' }); - expect(authService.getRedirect('ECM')).toEqual(['some-url', { q: '123' }]); + expect(authService.getRedirect('ECM')).toEqual('some-url;q=123'); })); it('should set redirect navigation commands with query params', async(() => { @@ -102,9 +102,9 @@ describe('AuthGuardService ECM', () => { authGuard.canActivate(null, router); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'ECM', navigation: ['/'] + provider: 'ECM', url: '/' }); - expect(authService.getRedirect('ECM')).toEqual(['/']); + expect(authService.getRedirect('ECM')).toEqual('/'); })); it('should get redirect url from config if there is one configured', async(() => { @@ -116,7 +116,7 @@ describe('AuthGuardService ECM', () => { authGuard.canActivate(null, router); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'ECM', navigation: ['some-url', {}] + provider: 'ECM', url: 'some-url' }); expect(routerService.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']); })); diff --git a/lib/core/services/auth-guard-ecm.service.ts b/lib/core/services/auth-guard-ecm.service.ts index 5d3079f7a7..37918870eb 100644 --- a/lib/core/services/auth-guard-ecm.service.ts +++ b/lib/core/services/auth-guard-ecm.service.ts @@ -17,8 +17,7 @@ import { Injectable } from '@angular/core'; import { - ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, Router, - PRIMARY_OUTLET, UrlTree, UrlSegmentGroup, UrlSegment + ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, Router } from '@angular/router'; import { AuthenticationService } from './authentication.service'; import { AppConfigService } from '../app-config/app-config.service'; @@ -44,9 +43,7 @@ export class AuthGuardEcm implements CanActivate { return true; } - const navigation = this.getNavigationCommands(redirectUrl); - - this.authService.setRedirect({ provider: 'ECM', navigation }); + this.authService.setRedirect({ provider: 'ECM', url: redirectUrl }); const pathToLogin = this.getRouteDestinationForLogin(); this.router.navigate(['/' + pathToLogin]); @@ -58,20 +55,4 @@ export class AuthGuardEcm implements CanActivate { this.appConfig.get('loginRoute') ? this.appConfig.get('loginRoute') : 'login'; } - - private getNavigationCommands(redirectUrl: string): any[] { - const urlTree: UrlTree = this.router.parseUrl(redirectUrl); - const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; - - if (!urlSegmentGroup) { - return [redirectUrl]; - } - - const urlSegments: UrlSegment[] = urlSegmentGroup.segments; - - return urlSegments.reduce(function(acc, item) { - acc.push(item.path, item.parameters); - return acc; - }, []); - } } diff --git a/lib/core/services/auth-guard.service.spec.ts b/lib/core/services/auth-guard.service.spec.ts index 1af5adb916..01315bb02e 100644 --- a/lib/core/services/auth-guard.service.spec.ts +++ b/lib/core/services/auth-guard.service.spec.ts @@ -70,7 +70,7 @@ describe('AuthGuardService', () => { service.canActivate(null, state); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'ALL', navigation: ['some-url', {}] + provider: 'ALL', url: 'some-url' }); expect(router.navigate).toHaveBeenCalledWith(['/login']); })); @@ -85,7 +85,7 @@ describe('AuthGuardService', () => { service.canActivate(null, state); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'ALL', navigation: ['some-url', { q: 'query' } ] + provider: 'ALL', url: 'some-url;q=query' }); expect(router.navigate).toHaveBeenCalledWith(['/login']); })); @@ -100,7 +100,7 @@ describe('AuthGuardService', () => { service.canActivate(null, state); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'ALL', navigation: ['some-url', {}] + provider: 'ALL', url: 'some-url' }); expect(router.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']); })); @@ -114,7 +114,7 @@ describe('AuthGuardService', () => { service.canActivate(null, state); expect(authService.setRedirect).toHaveBeenCalledWith({ - provider: 'ALL', navigation: ['/'] + provider: 'ALL', url: '/' }); })); }); diff --git a/lib/core/services/auth-guard.service.ts b/lib/core/services/auth-guard.service.ts index dbef96def1..a23b2508bc 100644 --- a/lib/core/services/auth-guard.service.ts +++ b/lib/core/services/auth-guard.service.ts @@ -18,8 +18,7 @@ import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, - CanActivateChild, RouterStateSnapshot, Router, - PRIMARY_OUTLET, UrlTree, UrlSegmentGroup, UrlSegment + CanActivateChild, RouterStateSnapshot, Router } from '@angular/router'; import { AuthenticationService } from './authentication.service'; import { Observable } from 'rxjs/Observable'; @@ -47,9 +46,7 @@ export class AuthGuard implements CanActivate, CanActivateChild { return true; } if (!this.authService.isOauth() || this.isOAuthWithoutSilentLogin() ) { - const navigation = this.getNavigationCommands(redirectUrl); - - this.authService.setRedirect({ provider: 'ALL', navigation } ); + this.authService.setRedirect({ provider: 'ALL', url: redirectUrl } ); const pathToLogin = this.getRouteDestinationForLogin(); this.router.navigate(['/' + pathToLogin]); @@ -67,20 +64,4 @@ export class AuthGuard implements CanActivate, CanActivateChild { this.appConfig.get('loginRoute') ? this.appConfig.get('loginRoute') : 'login'; } - - public getNavigationCommands(redirectUrl: string): any[] { - const urlTree: UrlTree = this.router.parseUrl(redirectUrl); - const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; - - if (!urlSegmentGroup) { - return [redirectUrl]; - } - - const urlSegments: UrlSegment[] = urlSegmentGroup.segments; - - return urlSegments.reduce(function(acc, item) { - acc.push(item.path, item.parameters); - return acc; - }, []); - } } diff --git a/lib/core/services/authentication.service.spec.ts b/lib/core/services/authentication.service.spec.ts index 8246464440..727e7e8c33 100644 --- a/lib/core/services/authentication.service.spec.ts +++ b/lib/core/services/authentication.service.spec.ts @@ -274,13 +274,13 @@ describe('AuthenticationService', () => { }); it('[ECM] should set/get redirectUrl when provider is ECM', () => { - authService.setRedirect({ provider: 'ECM', navigation: ['some-url'] }); + authService.setRedirect({ provider: 'ECM', url: 'some-url' }); - expect(authService.getRedirect(preferences.providers)).toEqual(['some-url']); + expect(authService.getRedirect(preferences.providers)).toEqual('some-url'); }); it('[ECM] should set/get redirectUrl when provider is BPM', () => { - authService.setRedirect({ provider: 'BPM', navigation: ['some-url'] }); + authService.setRedirect({ provider: 'BPM', url: 'some-url' }); expect(authService.getRedirect(preferences.providers)).toBeNull(); }); @@ -427,13 +427,13 @@ describe('AuthenticationService', () => { }); it('[BPM] should set/get redirectUrl when provider is BPM', () => { - authService.setRedirect({ provider: 'BPM', navigation: ['some-url'] }); + authService.setRedirect({ provider: 'BPM', url: 'some-url' }); - expect(authService.getRedirect(preferences.providers)).toEqual(['some-url']); + expect(authService.getRedirect(preferences.providers)).toEqual('some-url'); }); it('[BPM] should set/get redirectUrl when provider is ECM', () => { - authService.setRedirect({ provider: 'ECM', navigation: ['some-url'] }); + authService.setRedirect({ provider: 'ECM', url: 'some-url' }); expect(authService.getRedirect(preferences.providers)).toBeNull(); }); @@ -544,21 +544,21 @@ describe('AuthenticationService', () => { }); it('[ALL] should set/get redirectUrl when provider is ALL', () => { - authService.setRedirect({ provider: 'ALL', navigation: ['some-url'] }); + authService.setRedirect({ provider: 'ALL', url: 'some-url' }); - expect(authService.getRedirect(preferences.providers)).toEqual(['some-url']); + expect(authService.getRedirect(preferences.providers)).toEqual('some-url'); }); it('[ALL] should set/get redirectUrl when provider is BPM', () => { - authService.setRedirect({ provider: 'BPM', navigation: ['some-url'] }); + authService.setRedirect({ provider: 'BPM', url: 'some-url' }); - expect(authService.getRedirect(preferences.providers)).toEqual(['some-url']); + expect(authService.getRedirect(preferences.providers)).toEqual('some-url'); }); it('[ALL] should set/get redirectUrl when provider is ECM', () => { - authService.setRedirect({ provider: 'ECM', navigation: ['some-url'] }); + authService.setRedirect({ provider: 'ECM', url: 'some-url' }); - expect(authService.getRedirect(preferences.providers)).toEqual(['some-url']); + expect(authService.getRedirect(preferences.providers)).toEqual('some-url'); }); it('[ALL] should return null as redirectUrl when redirectUrl field is not set', () => { diff --git a/lib/core/services/authentication.service.ts b/lib/core/services/authentication.service.ts index 02898dc752..1a2e7dd2ad 100644 --- a/lib/core/services/authentication.service.ts +++ b/lib/core/services/authentication.service.ts @@ -33,7 +33,7 @@ const REMEMBER_ME_UNTIL = 1000 * 60 * 60 * 24 * 30 ; @Injectable() export class AuthenticationService { - private redirect: RedirectionModel = null; + private redirectUrl: RedirectionModel = null; onLogin: Subject = new Subject(); onLogout: Subject = new Subject(); @@ -249,23 +249,23 @@ export class AuthenticationService { * @param url URL to redirect to */ setRedirect(url: RedirectionModel) { - this.redirect = url; + this.redirectUrl = url; } /** Gets the URL to redirect to after login. * @param provider Service provider. Can be "ECM", "BPM" or "ALL". * @returns The redirect URL */ - getRedirect(provider: string): any[] { - return this.hasValidRedirection(provider) ? this.redirect.navigation : null; + getRedirect(provider: string): string { + return this.hasValidRedirection(provider) ? this.redirectUrl.url : null; } private hasValidRedirection(provider: string): boolean { - return this.redirect && (this.redirect.provider === provider || this.hasSelectedProviderAll(provider)); + return this.redirectUrl && (this.redirectUrl.provider === provider || this.hasSelectedProviderAll(provider)); } private hasSelectedProviderAll(provider: string): boolean { - return this.redirect && (this.redirect.provider === 'ALL' || provider === 'ALL'); + return this.redirectUrl && (this.redirectUrl.provider === 'ALL' || provider === 'ALL'); } /**