diff --git a/e2e/protractor.conf.js b/e2e/protractor.conf.js index 9d5eb157f1..065313508c 100644 --- a/e2e/protractor.conf.js +++ b/e2e/protractor.conf.js @@ -131,7 +131,7 @@ exports.config = { } } }, - args: ['--incognito', + args: [ `--window-size=${width},${height}`, '--disable-gpu', '--no-sandbox', diff --git a/lib/core/services/auth-guard-base.ts b/lib/core/services/auth-guard-base.ts index 27fa1d42ce..d82a19d9da 100644 --- a/lib/core/services/auth-guard-base.ts +++ b/lib/core/services/auth-guard-base.ts @@ -20,9 +20,9 @@ import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, - CanActivateChild + CanActivateChild, + UrlTree } from '@angular/router'; -import { Observable } from 'rxjs'; import { AuthenticationService } from './authentication.service'; import { AppConfigService, @@ -30,12 +30,15 @@ import { } from '../app-config/app-config.service'; import { OauthConfigModel } from '../models/oauth-config.model'; import { MatDialog } from '@angular/material/dialog'; +import { StorageService } from './storage.service'; +import { Observable } from 'rxjs'; export abstract class AuthGuardBase implements CanActivate, CanActivateChild { + abstract checkLogin( activeRoute: ActivatedRouteSnapshot, redirectUrl: string - ): Observable | Promise | boolean; + ): Observable | Promise | boolean | UrlTree; protected get withCredentials(): boolean { return this.appConfigService.get( @@ -48,13 +51,25 @@ export abstract class AuthGuardBase implements CanActivate, CanActivateChild { protected authenticationService: AuthenticationService, protected router: Router, protected appConfigService: AppConfigService, - protected dialog: MatDialog - ) {} + protected dialog: MatDialog, + private storageService: StorageService + ) { + } canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot - ): Observable | Promise | boolean { + ): Observable | Promise | boolean | UrlTree { + + const redirectFragment = this.storageService.getItem('loginFragment'); + if (this.authenticationService.isEcmLoggedIn() || this.withCredentials) { + if (redirectFragment) { + this.storageService.removeItem('loginFragment'); + return this.router.createUrlTree([redirectFragment]); + } + return true; + } + const checkLogin = this.checkLogin(route, state.url); if (!checkLogin) { @@ -67,7 +82,7 @@ export abstract class AuthGuardBase implements CanActivate, CanActivateChild { canActivateChild( route: ActivatedRouteSnapshot, state: RouterStateSnapshot - ): Observable | Promise | boolean { + ): Observable | Promise | boolean | UrlTree { return this.canActivate(route, state); } diff --git a/lib/core/services/auth-guard-bpm.service.ts b/lib/core/services/auth-guard-bpm.service.ts index 21032848c8..e0627541d1 100644 --- a/lib/core/services/auth-guard-bpm.service.ts +++ b/lib/core/services/auth-guard-bpm.service.ts @@ -20,8 +20,8 @@ import { ActivatedRouteSnapshot, Router } from '@angular/router'; import { AppConfigService } from '../app-config/app-config.service'; import { AuthenticationService } from './authentication.service'; import { AuthGuardBase } from './auth-guard-base'; -import { Observable } from 'rxjs'; import { MatDialog } from '@angular/material/dialog'; +import { StorageService } from './storage.service'; @Injectable({ providedIn: 'root' @@ -31,12 +31,12 @@ export class AuthGuardBpm extends AuthGuardBase { constructor(authenticationService: AuthenticationService, router: Router, appConfigService: AppConfigService, - dialog: MatDialog - ) { - super(authenticationService, router, appConfigService, dialog); + dialog: MatDialog, + storageService: StorageService) { + super(authenticationService, router, appConfigService, dialog, storageService); } - checkLogin(_: ActivatedRouteSnapshot, redirectUrl: string): Observable | Promise | boolean { + checkLogin(_: ActivatedRouteSnapshot, redirectUrl: string): boolean { if (this.authenticationService.isBpmLoggedIn() || this.withCredentials) { return true; } diff --git a/lib/core/services/auth-guard-ecm.service.ts b/lib/core/services/auth-guard-ecm.service.ts index fcc74d6546..0f134f9d8b 100644 --- a/lib/core/services/auth-guard-ecm.service.ts +++ b/lib/core/services/auth-guard-ecm.service.ts @@ -22,7 +22,6 @@ import { import { AuthenticationService } from './authentication.service'; import { AppConfigService } from '../app-config/app-config.service'; import { AuthGuardBase } from './auth-guard-base'; -import { Observable } from 'rxjs'; import { MatDialog } from '@angular/material/dialog'; import { StorageService } from './storage.service'; @@ -35,19 +34,11 @@ export class AuthGuardEcm extends AuthGuardBase { router: Router, appConfigService: AppConfigService, dialog: MatDialog, - private storageService: StorageService) { - super(authenticationService, router, appConfigService, dialog); + storageService: StorageService) { + super(authenticationService, router, appConfigService, dialog, storageService); } - checkLogin(_: ActivatedRouteSnapshot, redirectUrl: string): Observable | Promise | boolean { - const redirectFragment = this.storageService.getItem('loginFragment'); - if (this.authenticationService.isEcmLoggedIn() || this.withCredentials) { - if (redirectFragment) { - this.router.navigateByUrl(redirectFragment); - this.storageService.removeItem('loginFragment'); - } - return true; - } + checkLogin(_: ActivatedRouteSnapshot, redirectUrl: string): boolean { this.redirectToUrl('ECM', redirectUrl); if (!this.authenticationService.isEcmLoggedIn() && this.isSilentLogin() && !this.authenticationService.isPublicUrl()) { this.authenticationService.ssoImplicitLogin(); diff --git a/lib/core/services/auth-guard.service.spec.ts b/lib/core/services/auth-guard.service.spec.ts index b7d56ca75e..d95d9d09ad 100644 --- a/lib/core/services/auth-guard.service.spec.ts +++ b/lib/core/services/auth-guard.service.spec.ts @@ -50,70 +50,70 @@ describe('AuthGuardService', () => { appConfigService.config.oauth2 = {}; }); - it('if the alfresco js api is logged in should canActivate be true', async(() => { + it('if the alfresco js api is logged in should canActivate be true', async(async () => { spyOn(router, 'navigateByUrl'); spyOn(authService, 'isLoggedIn').and.returnValue(true); - expect(authGuard.canActivate(null, state)).toBeTruthy(); + expect(await authGuard.canActivate(null, state)).toBeTruthy(); expect(router.navigateByUrl).not.toHaveBeenCalled(); })); - it('if the alfresco js api is NOT logged in should canActivate be false', async(() => { + it('if the alfresco js api is NOT logged in should canActivate be false', async(async () => { state.url = 'some-url'; spyOn(router, 'navigateByUrl'); spyOn(authService, 'isLoggedIn').and.returnValue(false); - expect(authGuard.canActivate(null, state)).toBeFalsy(); + expect(await authGuard.canActivate(null, state)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalled(); })); - it('if the alfresco js api is configured with withCredentials true should canActivate be true', async(() => { + it('if the alfresco js api is configured with withCredentials true should canActivate be true', async(async () => { spyOn(authService, 'isBpmLoggedIn').and.returnValue(true); appConfigService.config.auth.withCredentials = true; - const route: RouterStateSnapshot = { url: 'some-url' }; + const route: RouterStateSnapshot = { url: 'some-url' }; - expect(authGuard.canActivate(null, route)).toBeTruthy(); + expect(await authGuard.canActivate(null, route)).toBeTruthy(); })); - it('should redirect url if the User is NOT logged in and isOAuthWithoutSilentLogin', async(() => { + it('should redirect url if the User is NOT logged in and isOAuthWithoutSilentLogin', async(async () => { spyOn(router, 'navigateByUrl').and.stub(); spyOn(authService, 'isLoggedIn').and.returnValue(false); spyOn(authService, 'isOauth').and.returnValue(true); appConfigService.config.oauth2.silentLogin = false; - expect(authGuard.canActivate(null, state)).toBeFalsy(); + expect(await authGuard.canActivate(null, state)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalled(); })); - it('should redirect url if the User is NOT logged in and isOAuth but no silentLogin configured', async(() => { + it('should redirect url if the User is NOT logged in and isOAuth but no silentLogin configured', async(async () => { spyOn(router, 'navigateByUrl').and.stub(); spyOn(authService, 'isLoggedIn').and.returnValue(false); spyOn(authService, 'isOauth').and.returnValue(true); appConfigService.config.oauth2.silentLogin = undefined; - expect(authGuard.canActivate(null, state)).toBeFalsy(); + expect(await authGuard.canActivate(null, state)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalled(); })); - it('should NOT redirect url if the User is NOT logged in and isOAuth but with silentLogin configured', async(() => { + it('should NOT redirect url if the User is NOT logged in and isOAuth but with silentLogin configured', async(async () => { spyOn(router, 'navigateByUrl').and.stub(); spyOn(authService, 'isLoggedIn').and.returnValue(false); spyOn(authService, 'isOauth').and.returnValue(true); appConfigService.config.oauth2.silentLogin = true; - expect(authGuard.canActivate(null, state)).toBeFalsy(); + expect(await authGuard.canActivate(null, state)).toBeFalsy(); expect(router.navigateByUrl).not.toHaveBeenCalled(); })); - it('should set redirect url', async(() => { + it('should set redirect url', async(async () => { state.url = 'some-url'; appConfigService.config.loginRoute = 'login'; spyOn(router, 'navigateByUrl'); spyOn(authService, 'setRedirect'); - authGuard.canActivate(null, state); + await authGuard.canActivate(null, state); expect(authService.setRedirect).toHaveBeenCalledWith({ provider: 'ALL', url: 'some-url' @@ -121,14 +121,14 @@ describe('AuthGuardService', () => { expect(router.navigateByUrl).toHaveBeenCalledWith('/login?redirectUrl=some-url'); })); - it('should set redirect url with query params', async(() => { + it('should set redirect url with query params', async(async () => { state.url = 'some-url;q=query'; appConfigService.config.loginRoute = 'login'; spyOn(router, 'navigateByUrl'); spyOn(authService, 'setRedirect'); - authGuard.canActivate(null, state); + await authGuard.canActivate(null, state); expect(authService.setRedirect).toHaveBeenCalledWith({ provider: 'ALL', url: 'some-url;q=query' @@ -136,14 +136,14 @@ describe('AuthGuardService', () => { expect(router.navigateByUrl).toHaveBeenCalledWith('/login?redirectUrl=some-url;q=query'); })); - it('should get redirect url from config if there is one configured', async(() => { + it('should get redirect url from config if there is one configured', async(async () => { state.url = 'some-url'; appConfigService.config.loginRoute = 'fakeLoginRoute'; spyOn(router, 'navigateByUrl'); spyOn(authService, 'setRedirect'); - authGuard.canActivate(null, state); + await authGuard.canActivate(null, state); expect(authService.setRedirect).toHaveBeenCalledWith({ provider: 'ALL', url: 'some-url' @@ -151,13 +151,13 @@ describe('AuthGuardService', () => { expect(router.navigateByUrl).toHaveBeenCalledWith('/fakeLoginRoute?redirectUrl=some-url'); })); - it('should pass actual redirect when no state segments exists', async(() => { + it('should pass actual redirect when no state segments exists', async(async () => { state.url = '/'; spyOn(router, 'navigateByUrl'); spyOn(authService, 'setRedirect'); - authGuard.canActivate(null, state); + await authGuard.canActivate(null, state); expect(authService.setRedirect).toHaveBeenCalledWith({ provider: 'ALL', url: '/' diff --git a/lib/core/services/auth-guard.service.ts b/lib/core/services/auth-guard.service.ts index a509171ebc..0eb8e0e78a 100644 --- a/lib/core/services/auth-guard.service.ts +++ b/lib/core/services/auth-guard.service.ts @@ -18,11 +18,11 @@ import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, Router } from '@angular/router'; import { AuthenticationService } from './authentication.service'; -import { Observable } from 'rxjs'; import { AppConfigService } from '../app-config/app-config.service'; import { AuthGuardBase } from './auth-guard-base'; import { JwtHelperService } from './jwt-helper.service'; import { MatDialog } from '@angular/material/dialog'; +import { StorageService } from './storage.service'; @Injectable({ providedIn: 'root' @@ -35,8 +35,9 @@ export class AuthGuard extends AuthGuardBase { authenticationService: AuthenticationService, router: Router, appConfigService: AppConfigService, - dialog: MatDialog) { - super(authenticationService, router, appConfigService, dialog); + dialog: MatDialog, + storageService: StorageService) { + super(authenticationService, router, appConfigService, dialog, storageService); this.ticketChangeBind = this.ticketChange.bind(this); window.addEventListener('storage', this.ticketChangeBind); @@ -68,7 +69,7 @@ export class AuthGuard extends AuthGuardBase { window.removeEventListener('storage', this.ticketChangeBind); } - checkLogin(_: ActivatedRouteSnapshot, redirectUrl: string): Observable | Promise | boolean { + async checkLogin(_: ActivatedRouteSnapshot, redirectUrl: string): Promise { if (this.authenticationService.isLoggedIn() || this.withCredentials) { return true; }