[ADF-3938] withCredentials TRUE the authguard should be skipped (#4259)

* should skip authguard if withCredentials is true

* missing comma

* common auth guard fix
This commit is contained in:
Eugenio Romano
2019-02-05 00:31:32 +00:00
committed by GitHub
parent bf718d905f
commit da379eefd7
7 changed files with 80 additions and 40 deletions

View File

@@ -195,7 +195,7 @@ export class ContentNodeSelectorPanelComponent implements OnInit {
private isExcludedSiteContent(row: ShareDataRow): boolean { private isExcludedSiteContent(row: ShareDataRow): boolean {
const entry = row.node.entry; const entry = row.node.entry;
if (this._excludeSiteContent.length && if (this._excludeSiteContent && this._excludeSiteContent.length &&
entry && entry &&
entry.properties && entry.properties &&
entry.properties['st:componentId']) { entry.properties['st:componentId']) {
@@ -368,7 +368,7 @@ export class ContentNodeSelectorPanelComponent implements OnInit {
* @param entry * @param entry
*/ */
private attemptNodeSelection(entry: Node): void { private attemptNodeSelection(entry: Node): void {
if (this.isSelectionValid(entry)) { if (entry && this.isSelectionValid(entry)) {
this.chosenNode = entry; this.chosenNode = entry;
} else { } else {
this.resetChosenNode(); this.resetChosenNode();

View File

@@ -42,6 +42,7 @@ describe('AuthGuardService BPM', () => {
appConfigService = TestBed.get(AppConfigService); appConfigService = TestBed.get(AppConfigService);
appConfigService.config.providers = 'BPM'; appConfigService.config.providers = 'BPM';
appConfigService.config.auth = {};
}); });
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(() => {
@@ -51,6 +52,15 @@ describe('AuthGuardService BPM', () => {
expect(authGuard.canActivate(null, router)).toBeTruthy(); expect(authGuard.canActivate(null, router)).toBeTruthy();
})); }));
it('if the alfresco js api is configured with withCredentials true should canActivate be true', async(() => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(true);
appConfigService.config.auth.withCredentials = true;
const router: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};
expect(authGuard.canActivate(null, router)).toBeTruthy();
}));
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(() => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(false); spyOn(authService, 'isBpmLoggedIn').and.returnValue(false);
spyOn(routerService, 'navigate').and.stub(); spyOn(routerService, 'navigate').and.stub();

View File

@@ -25,7 +25,9 @@ import { OauthConfigModel } from '../models/oauth-config.model';
providedIn: 'root' providedIn: 'root'
}) })
export class AuthGuardBpm implements CanActivate, CanActivateChild { export class AuthGuardBpm implements CanActivate, CanActivateChild {
constructor(private authService: AuthenticationService, private router: Router, private appConfig: AppConfigService) {}
constructor(private authService: AuthenticationService, private router: Router, private appConfigService: AppConfigService) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.checkLogin(state.url); return this.checkLogin(state.url);
@@ -36,7 +38,9 @@ export class AuthGuardBpm implements CanActivate, CanActivateChild {
} }
checkLogin(redirectUrl: string): boolean { checkLogin(redirectUrl: string): boolean {
if (this.authService.isBpmLoggedIn()) { let withCredentialsMode = this.appConfigService.get<boolean>('auth.withCredentials', false);
if (this.authService.isBpmLoggedIn() || withCredentialsMode) {
return true; return true;
} }
@@ -50,11 +54,11 @@ export class AuthGuardBpm implements CanActivate, CanActivateChild {
} }
isOAuthWithoutSilentLogin() { isOAuthWithoutSilentLogin() {
let oauth: OauthConfigModel = this.appConfig.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null); let oauth: OauthConfigModel = this.appConfigService.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null);
return this.authService.isOauth() && oauth.silentLogin === false; return this.authService.isOauth() && oauth.silentLogin === false;
} }
private getRouteDestinationForLogin(): string { private getRouteDestinationForLogin(): string {
return this.appConfig && this.appConfig.get<string>(AppConfigValues.LOGIN_ROUTE) ? this.appConfig.get<string>(AppConfigValues.LOGIN_ROUTE) : 'login'; return this.appConfigService && this.appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE) ? this.appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE) : 'login';
} }
} }

View File

@@ -42,6 +42,7 @@ describe('AuthGuardService ECM', () => {
appConfigService = TestBed.get(AppConfigService); appConfigService = TestBed.get(AppConfigService);
appConfigService.config.providers = 'ECM'; appConfigService.config.providers = 'ECM';
appConfigService.config.auth = {};
}); });
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(() => {
@@ -51,6 +52,15 @@ describe('AuthGuardService ECM', () => {
expect(authGuard.canActivate(null, router)).toBeTruthy(); expect(authGuard.canActivate(null, router)).toBeTruthy();
})); }));
it('if the alfresco js api is configured with withCredentials true should canActivate be true', async(() => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(true);
appConfigService.config.auth.withCredentials = true;
const router: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};
expect(authGuard.canActivate(null, router)).toBeTruthy();
}));
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(() => {
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false); spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
spyOn(routerService, 'navigate').and.stub(); spyOn(routerService, 'navigate').and.stub();

View File

@@ -27,9 +27,10 @@ import { OauthConfigModel } from '../models/oauth-config.model';
providedIn: 'root' providedIn: 'root'
}) })
export class AuthGuardEcm implements CanActivate { export class AuthGuardEcm implements CanActivate {
constructor(private authService: AuthenticationService, constructor(private authService: AuthenticationService,
private router: Router, private router: Router,
private appConfig: AppConfigService) { private appConfigService: AppConfigService) {
} }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
@@ -41,7 +42,9 @@ export class AuthGuardEcm implements CanActivate {
} }
checkLogin(redirectUrl: string): boolean { checkLogin(redirectUrl: string): boolean {
if (this.authService.isEcmLoggedIn()) { let withCredentialsMode = this.appConfigService.get<boolean>('auth.withCredentials', false);
if (this.authService.isEcmLoggedIn() || withCredentialsMode) {
return true; return true;
} }
@@ -55,13 +58,13 @@ export class AuthGuardEcm implements CanActivate {
} }
isOAuthWithoutSilentLogin() { isOAuthWithoutSilentLogin() {
let oauth: OauthConfigModel = this.appConfig.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null); let oauth: OauthConfigModel = this.appConfigService.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null);
return this.authService.isOauth() && oauth.silentLogin === false; return this.authService.isOauth() && oauth.silentLogin === false;
} }
private getRouteDestinationForLogin(): string { private getRouteDestinationForLogin(): string {
return this.appConfig && return this.appConfigService &&
this.appConfig.get<string>(AppConfigValues.LOGIN_ROUTE) ? this.appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE) ?
this.appConfig.get<string>(AppConfigValues.LOGIN_ROUTE) : 'login'; this.appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE) : 'login';
} }
} }

View File

@@ -16,7 +16,7 @@
*/ */
import { async, TestBed } from '@angular/core/testing'; import { async, TestBed } from '@angular/core/testing';
import { Router } from '@angular/router'; import { Router, RouterStateSnapshot } from '@angular/router';
import { AppConfigService } from '../app-config/app-config.service'; import { AppConfigService } from '../app-config/app-config.service';
import { AuthGuard } from './auth-guard.service'; import { AuthGuard } from './auth-guard.service';
import { AuthenticationService } from './authentication.service'; import { AuthenticationService } from './authentication.service';
@@ -26,8 +26,8 @@ import { CoreTestingModule } from '../testing/core.testing.module';
describe('AuthGuardService', () => { describe('AuthGuardService', () => {
let state; let state;
let authService: AuthenticationService; let authService: AuthenticationService;
let router: Router; let routerService: Router;
let service: AuthGuard; let authGuard: AuthGuard;
let appConfigService: AppConfigService; let appConfigService: AppConfigService;
setupTestBed({ setupTestBed({
@@ -38,80 +38,91 @@ describe('AuthGuardService', () => {
localStorage.clear(); localStorage.clear();
state = { url: '' }; state = { url: '' };
authService = TestBed.get(AuthenticationService); authService = TestBed.get(AuthenticationService);
router = TestBed.get(Router); routerService = TestBed.get(Router);
service = TestBed.get(AuthGuard); authGuard = TestBed.get(AuthGuard);
appConfigService = TestBed.get(AppConfigService); appConfigService = TestBed.get(AppConfigService);
appConfigService.config.auth = {};
}); });
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(() => {
spyOn(router, 'navigate'); spyOn(routerService, 'navigate');
spyOn(authService, 'isLoggedIn').and.returnValue(true); spyOn(authService, 'isLoggedIn').and.returnValue(true);
expect(service.canActivate(null, state)).toBeTruthy(); expect(authGuard.canActivate(null, state)).toBeTruthy();
expect(router.navigate).not.toHaveBeenCalled(); expect(routerService.navigate).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(() => {
state.url = 'some-url'; state.url = 'some-url';
spyOn(router, 'navigate'); spyOn(routerService, 'navigate');
spyOn(authService, 'isLoggedIn').and.returnValue(false); spyOn(authService, 'isLoggedIn').and.returnValue(false);
expect(service.canActivate(null, state)).toBeFalsy(); expect(authGuard.canActivate(null, state)).toBeFalsy();
expect(router.navigate).toHaveBeenCalled(); expect(routerService.navigate).toHaveBeenCalled();
}));
it('if the alfresco js api is configured with withCredentials true should canActivate be true', async(() => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(true);
appConfigService.config.auth.withCredentials = true;
const router: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};
expect(authGuard.canActivate(null, router)).toBeTruthy();
})); }));
it('should set redirect url', async(() => { it('should set redirect url', async(() => {
state.url = 'some-url'; state.url = 'some-url';
appConfigService.config.loginRoute = 'login'; appConfigService.config.loginRoute = 'login';
spyOn(router, 'navigate'); spyOn(routerService, 'navigate');
spyOn(authService, 'setRedirect'); spyOn(authService, 'setRedirect');
service.canActivate(null, state); authGuard.canActivate(null, state);
expect(authService.setRedirect).toHaveBeenCalledWith({ expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ALL', url: 'some-url' provider: 'ALL', url: 'some-url'
}); });
expect(router.navigate).toHaveBeenCalledWith(['/login']); expect(routerService.navigate).toHaveBeenCalledWith(['/login']);
})); }));
it('should set redirect url with query params', async(() => { it('should set redirect url with query params', async(() => {
state.url = 'some-url;q=query'; state.url = 'some-url;q=query';
appConfigService.config.loginRoute = 'login'; appConfigService.config.loginRoute = 'login';
spyOn(router, 'navigate'); spyOn(routerService, 'navigate');
spyOn(authService, 'setRedirect'); spyOn(authService, 'setRedirect');
service.canActivate(null, state); authGuard.canActivate(null, state);
expect(authService.setRedirect).toHaveBeenCalledWith({ expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ALL', url: 'some-url;q=query' provider: 'ALL', url: 'some-url;q=query'
}); });
expect(router.navigate).toHaveBeenCalledWith(['/login']); expect(routerService.navigate).toHaveBeenCalledWith(['/login']);
})); }));
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(() => {
state.url = 'some-url'; state.url = 'some-url';
appConfigService.config.loginRoute = 'fakeLoginRoute'; appConfigService.config.loginRoute = 'fakeLoginRoute';
spyOn(router, 'navigate'); spyOn(routerService, 'navigate');
spyOn(authService, 'setRedirect'); spyOn(authService, 'setRedirect');
service.canActivate(null, state); authGuard.canActivate(null, state);
expect(authService.setRedirect).toHaveBeenCalledWith({ expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ALL', url: 'some-url' provider: 'ALL', url: 'some-url'
}); });
expect(router.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']); expect(routerService.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']);
})); }));
it('should pass actual redirect when no state segments exists', async(() => { it('should pass actual redirect when no state segments exists', async(() => {
state.url = '/'; state.url = '/';
spyOn(router, 'navigate'); spyOn(routerService, 'navigate');
spyOn(authService, 'setRedirect'); spyOn(authService, 'setRedirect');
service.canActivate(null, state); authGuard.canActivate(null, state);
expect(authService.setRedirect).toHaveBeenCalledWith({ expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ALL', url: '/' provider: 'ALL', url: '/'

View File

@@ -31,7 +31,7 @@ import { OauthConfigModel } from '../models/oauth-config.model';
export class AuthGuard implements CanActivate, CanActivateChild { export class AuthGuard implements CanActivate, CanActivateChild {
constructor(private authService: AuthenticationService, constructor(private authService: AuthenticationService,
private router: Router, private router: Router,
private appConfig: AppConfigService) { private appConfigService: AppConfigService) {
} }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
@@ -44,7 +44,9 @@ export class AuthGuard implements CanActivate, CanActivateChild {
} }
checkLogin(redirectUrl: string): boolean { checkLogin(redirectUrl: string): boolean {
if (this.authService.isLoggedIn()) { let withCredentialsMode = this.appConfigService.get<boolean>('auth.withCredentials', false);
if (this.authService.isLoggedIn() || withCredentialsMode) {
return true; return true;
} }
if (!this.authService.isOauth() || this.isOAuthWithoutSilentLogin()) { if (!this.authService.isOauth() || this.isOAuthWithoutSilentLogin()) {
@@ -58,13 +60,13 @@ export class AuthGuard implements CanActivate, CanActivateChild {
} }
isOAuthWithoutSilentLogin() { isOAuthWithoutSilentLogin() {
let oauth: OauthConfigModel = this.appConfig.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null); let oauth: OauthConfigModel = this.appConfigService.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null);
return this.authService.isOauth() && oauth.silentLogin === false; return this.authService.isOauth() && oauth.silentLogin === false;
} }
public getRouteDestinationForLogin(): string { public getRouteDestinationForLogin(): string {
return this.appConfig && return this.appConfigService &&
this.appConfig.get<string>(AppConfigValues.LOGIN_ROUTE) ? this.appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE) ?
this.appConfig.get<string>(AppConfigValues.LOGIN_ROUTE) : 'login'; this.appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE) : 'login';
} }
} }