[MNT-21636] Fix redirect URL for viewer (#6564)

* fix redirect URL for viewer

* fix unit
fix login SSO show when user is logged in

* update js-api

* remove protractor change
This commit is contained in:
Eugenio Romano
2021-01-22 17:17:14 +00:00
committed by GitHub
parent b126c14a07
commit 8f0633b133
9 changed files with 99 additions and 65 deletions

View File

@@ -150,20 +150,21 @@ export class LoginComponent implements OnInit, OnDestroy {
this.initFormFieldsDefault(); this.initFormFieldsDefault();
this.initFormFieldsMessages(); this.initFormFieldsMessages();
if (this.authService.isOauth()) {
const oauth: OauthConfigModel = this.appConfig.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null);
if (oauth && oauth.implicitFlow) {
this.implicitFlow = true;
}
if (oauth && oauth.silentLogin && !this.authService.isLoggedIn()) {
this.alfrescoApiService.getInstance().oauth2Auth.implicitLogin();
}
}
if (this.authService.isLoggedIn()) { if (this.authService.isLoggedIn()) {
this.router.navigate([this.successRoute]); this.router.navigate([this.successRoute]);
} else { } else {
if (this.authService.isOauth()) {
const oauth: OauthConfigModel = this.appConfig.get<OauthConfigModel>(AppConfigValues.OAUTHCONFIG, null);
if (oauth && oauth.implicitFlow) {
this.implicitFlow = true;
}
if (oauth && oauth.silentLogin) {
this.alfrescoApiService.getInstance().oauth2Auth.implicitLogin();
}
}
this.route.queryParams.subscribe((params: Params) => { this.route.queryParams.subscribe((params: Params) => {
const url = params['redirectUrl']; const url = params['redirectUrl'];
const provider = this.appConfig.get<string>(AppConfigValues.PROVIDERS); const provider = this.appConfig.get<string>(AppConfigValues.PROVIDERS);

View File

@@ -50,7 +50,6 @@ export abstract class AuthGuardBase implements CanActivate, CanActivateChild {
private storageService: StorageService private storageService: StorageService
) { ) {
} }
ls;
abstract checkLogin( abstract checkLogin(
activeRoute: ActivatedRouteSnapshot, activeRoute: ActivatedRouteSnapshot,
@@ -63,11 +62,14 @@ ls;
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const redirectFragment = this.storageService.getItem('loginFragment'); const redirectFragment = this.storageService.getItem('loginFragment');
if (this.authenticationService.isLoggedIn() || this.withCredentials) { if (this.authenticationService.isLoggedIn() || this.withCredentials) {
if (redirectFragment) {
if (redirectFragment && this.getLoginRoute() !== redirectFragment) {
this.storageService.removeItem('loginFragment'); this.storageService.removeItem('loginFragment');
return this.router.createUrlTree([redirectFragment]); this.redirectToUrl(redirectFragment);
} }
return true; return true;
} }
@@ -86,24 +88,32 @@ ls;
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.canActivate(route, state); return this.canActivate(route, state);
} }
protected redirectToUrl(provider: string, url: string) {
const pathToLogin = `/${this.getLoginRoute()}`; protected redirectToUrl(url: string) {
let urlToRedirect; let urlToRedirect;
this.dialog.closeAll(); this.dialog.closeAll();
if (!this.authenticationService.isOauth()) { if (!this.authenticationService.isLoggedIn()) {
this.authenticationService.setRedirect({ provider, url }); const pathToLogin = `/${this.getLoginRoute()}`;
urlToRedirect = `${pathToLogin}?redirectUrl=${url}`; if (!this.authenticationService.isOauth()) {
this.router.navigateByUrl(urlToRedirect); this.authenticationService.setRedirect({
} else if (this.getOauthConfig().silentLogin && !this.authenticationService.isPublicUrl()) { provider: this.getProvider(),
this.authenticationService.ssoImplicitLogin(); url
});
urlToRedirect = `${pathToLogin}?redirectUrl=${url}`;
this.router.navigateByUrl(urlToRedirect);
} else if (this.getOauthConfig().silentLogin && !this.authenticationService.isPublicUrl()) {
this.authenticationService.ssoImplicitLogin();
} else {
urlToRedirect = pathToLogin;
this.router.navigateByUrl(urlToRedirect);
}
} else { } else {
urlToRedirect = pathToLogin; this.router.navigateByUrl(url);
this.router.navigateByUrl(urlToRedirect);
} }
} }
protected getOauthConfig(): OauthConfigModel { protected getOauthConfig(): OauthConfigModel {
@@ -126,6 +136,16 @@ ls;
); );
} }
protected getProvider(): string {
return (
this.appConfigService &&
this.appConfigService.get<string>(
AppConfigValues.PROVIDERS,
'ALL'
)
);
}
protected isOAuthWithoutSilentLogin(): boolean { protected isOAuthWithoutSilentLogin(): boolean {
const oauth = this.appConfigService.get<OauthConfigModel>( const oauth = this.appConfigService.get<OauthConfigModel>(
AppConfigValues.OAUTHCONFIG, AppConfigValues.OAUTHCONFIG,

View File

@@ -64,7 +64,8 @@ describe('AuthGuardService BPM', () => {
redirectUri: '/', redirectUri: '/',
clientId: 'activiti', clientId: 'activiti',
publicUrl: 'settings', publicUrl: 'settings',
scope: 'openid' scope: 'openid',
provider: 'BPM'
}; };
const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'abc'}; const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'abc'};

View File

@@ -40,7 +40,7 @@ export class AuthGuardBpm extends AuthGuardBase {
if (this.authenticationService.isBpmLoggedIn() || this.withCredentials) { if (this.authenticationService.isBpmLoggedIn() || this.withCredentials) {
return true; return true;
} }
this.redirectToUrl('BPM', redirectUrl); this.redirectToUrl(redirectUrl);
return false; return false;
} }
} }

View File

@@ -43,7 +43,7 @@ export class AuthGuardEcm extends AuthGuardBase {
return true; return true;
} }
this.redirectToUrl('ECM', redirectUrl); this.redirectToUrl(redirectUrl);
return false; return false;
} }

View File

@@ -23,12 +23,14 @@ import { AuthenticationService } from './authentication.service';
import { setupTestBed } from '../testing/setup-test-bed'; import { setupTestBed } from '../testing/setup-test-bed';
import { CoreTestingModule } from '../testing/core.testing.module'; import { CoreTestingModule } from '../testing/core.testing.module';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { StorageService } from './storage.service';
describe('AuthGuardService', () => { describe('AuthGuardService', () => {
let state; let state;
let authService: AuthenticationService; let authService: AuthenticationService;
let router: Router; let router: Router;
let authGuard: AuthGuard; let authGuard: AuthGuard;
let storageService: StorageService;
let appConfigService: AppConfigService; let appConfigService: AppConfigService;
setupTestBed({ setupTestBed({
@@ -48,6 +50,7 @@ describe('AuthGuardService', () => {
appConfigService.config.auth = {}; appConfigService.config.auth = {};
appConfigService.config.oauth2 = {}; appConfigService.config.oauth2 = {};
storageService = TestBed.inject(StorageService);
}); });
it('if the alfresco js api is logged in should canActivate be true', async(async () => { it('if the alfresco js api is logged in should canActivate be true', async(async () => {
@@ -76,6 +79,18 @@ describe('AuthGuardService', () => {
expect(await authGuard.canActivate(null, route)).toBeTruthy(); expect(await authGuard.canActivate(null, route)).toBeTruthy();
})); }));
it('should not redirect to login', async(async () => {
storageService.setItem('loginFragment', 'login');
spyOn(router, 'navigateByUrl').and.stub();
spyOn(authService, 'isLoggedIn').and.returnValue(true);
spyOn(authService, 'isOauth').and.returnValue(true);
appConfigService.config.oauth2.silentLogin = false;
expect(await authGuard.canActivate(null, state)).toBeTruthy();
expect(router.navigateByUrl).not.toHaveBeenCalled();
}));
it('should redirect url if the User is NOT logged in and isOAuthWithoutSilentLogin', async(async () => { it('should redirect url if the User is NOT logged in and isOAuthWithoutSilentLogin', async(async () => {
spyOn(router, 'navigateByUrl').and.stub(); spyOn(router, 'navigateByUrl').and.stub();
spyOn(authService, 'isLoggedIn').and.returnValue(false); spyOn(authService, 'isLoggedIn').and.returnValue(false);
@@ -124,6 +139,7 @@ describe('AuthGuardService', () => {
it('should set redirect url with query params', async(async () => { it('should set redirect url with query params', async(async () => {
state.url = 'some-url;q=query'; state.url = 'some-url;q=query';
appConfigService.config.loginRoute = 'login'; appConfigService.config.loginRoute = 'login';
appConfigService.config.provider = 'ALL';
spyOn(router, 'navigateByUrl'); spyOn(router, 'navigateByUrl');
spyOn(authService, 'setRedirect'); spyOn(authService, 'setRedirect');

View File

@@ -45,23 +45,23 @@ export class AuthGuard extends AuthGuardBase {
ticketChange(event: StorageEvent) { ticketChange(event: StorageEvent) {
if (event.key.includes('ticket-ECM') && event.newValue !== event.oldValue) { if (event.key.includes('ticket-ECM') && event.newValue !== event.oldValue) {
this.ticketChangeRedirect(event, 'ECM'); this.ticketChangeRedirect(event);
} }
if (event.key.includes('ticket-BPM') && event.newValue !== event.oldValue) { if (event.key.includes('ticket-BPM') && event.newValue !== event.oldValue) {
this.ticketChangeRedirect(event, 'BPM'); this.ticketChangeRedirect(event);
} }
if (event.key.endsWith(JwtHelperService.USER_ACCESS_TOKEN) && if (event.key.endsWith(JwtHelperService.USER_ACCESS_TOKEN) &&
this.jwtHelperService.getValueFromToken(event.newValue, JwtHelperService.USER_PREFERRED_USERNAME) !== this.jwtHelperService.getValueFromToken(event.newValue, JwtHelperService.USER_PREFERRED_USERNAME) !==
this.jwtHelperService.getValueFromToken(event.oldValue, JwtHelperService.USER_PREFERRED_USERNAME)) { this.jwtHelperService.getValueFromToken(event.oldValue, JwtHelperService.USER_PREFERRED_USERNAME)) {
this.ticketChangeRedirect(event, 'ALL'); this.ticketChangeRedirect(event);
} }
} }
private ticketChangeRedirect(event: StorageEvent, provider: string) { private ticketChangeRedirect(event: StorageEvent) {
if (!event.newValue) { if (!event.newValue) {
this.redirectToUrl(provider, this.router.url); this.redirectToUrl(this.router.url);
} else { } else {
window.location.reload(); window.location.reload();
} }
@@ -71,7 +71,7 @@ export class AuthGuard extends AuthGuardBase {
if (this.authenticationService.isLoggedIn() || this.withCredentials) { if (this.authenticationService.isLoggedIn() || this.withCredentials) {
return true; return true;
} }
this.redirectToUrl('ALL', redirectUrl); this.redirectToUrl( redirectUrl);
return false; return false;
} }
} }

54
package-lock.json generated
View File

@@ -5,9 +5,9 @@
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@alfresco/js-api": { "@alfresco/js-api": {
"version": "4.3.0-61350fee919fbeb2b6c949621161b6b076d44133", "version": "4.3.0-f117dba26d7c31cb56a6cf40eb84dff89a174971",
"resolved": "https://registry.npmjs.org/@alfresco/js-api/-/js-api-4.3.0-61350fee919fbeb2b6c949621161b6b076d44133.tgz", "resolved": "https://registry.npmjs.org/@alfresco/js-api/-/js-api-4.3.0-f117dba26d7c31cb56a6cf40eb84dff89a174971.tgz",
"integrity": "sha512-Q6oStMDxWU5psD4UnRBB1ri8Md5A+SgI8fNx3l7RkcEvSJvU3p1cfJe/2BfM4wJK1AU6NHFUdWAH6z2EAA8K/A==", "integrity": "sha512-aNDqBYpfRVem1UivNWW0VAO+e8JZIo7ATTsf+MrIWWkM+2JiKKc+ByG8uj5HjGt0KekwlZh/tXbAwbNHvpo+PA==",
"requires": { "requires": {
"event-emitter": "^0.3.5", "event-emitter": "^0.3.5",
"minimatch": "3.0.4", "minimatch": "3.0.4",
@@ -9308,12 +9308,6 @@
"color-name": "~1.1.4" "color-name": "~1.1.4"
} }
}, },
"commander": {
"version": "6.2.1",
"resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz",
"integrity": "sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA==",
"dev": true
},
"fs-extra": { "fs-extra": {
"version": "9.0.1", "version": "9.0.1",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.1.tgz", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.1.tgz",
@@ -17134,12 +17128,6 @@
"integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==", "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==",
"dev": true "dev": true
}, },
"commander": {
"version": "6.2.1",
"resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz",
"integrity": "sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA==",
"dev": true
},
"fs-extra": { "fs-extra": {
"version": "9.0.1", "version": "9.0.1",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.1.tgz", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.1.tgz",
@@ -26097,9 +26085,9 @@
} }
}, },
"typedoc": { "typedoc": {
"version": "0.20.16", "version": "0.20.14",
"resolved": "https://registry.npmjs.org/typedoc/-/typedoc-0.20.16.tgz", "resolved": "https://registry.npmjs.org/typedoc/-/typedoc-0.20.14.tgz",
"integrity": "sha512-xqIL8lT6ZE3QpP0GN30ckeTR05NSEkrP2pXQlNhC0OFkbvnjqJtDUcWSmCO15BuYyu4qsEbZT+tKYFEAt9Jxew==", "integrity": "sha512-9bsZp5/qkl+gDSv9DRvHbfbY8Sr0tD8fKx7hNIvcluxeAFzBCEo9o0qDCdLUZw+/axbfd9TaqHvSuCVRu+YH6Q==",
"dev": true, "dev": true,
"requires": { "requires": {
"colors": "^1.4.0", "colors": "^1.4.0",
@@ -26112,19 +26100,19 @@
"progress": "^2.0.3", "progress": "^2.0.3",
"shelljs": "^0.8.4", "shelljs": "^0.8.4",
"shiki": "^0.2.7", "shiki": "^0.2.7",
"typedoc-default-themes": "^0.12.4" "typedoc-default-themes": "0.12.1"
}, },
"dependencies": { "dependencies": {
"fs-extra": { "fs-extra": {
"version": "9.1.0", "version": "9.0.1",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.1.tgz",
"integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", "integrity": "sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"at-least-node": "^1.0.0", "at-least-node": "^1.0.0",
"graceful-fs": "^4.2.0", "graceful-fs": "^4.2.0",
"jsonfile": "^6.0.1", "jsonfile": "^6.0.1",
"universalify": "^2.0.0" "universalify": "^1.0.0"
} }
}, },
"jsonfile": { "jsonfile": {
@@ -26135,20 +26123,28 @@
"requires": { "requires": {
"graceful-fs": "^4.1.6", "graceful-fs": "^4.1.6",
"universalify": "^2.0.0" "universalify": "^2.0.0"
},
"dependencies": {
"universalify": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz",
"integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==",
"dev": true
}
} }
}, },
"universalify": { "universalify": {
"version": "2.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", "resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz",
"integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==",
"dev": true "dev": true
} }
} }
}, },
"typedoc-default-themes": { "typedoc-default-themes": {
"version": "0.12.4", "version": "0.12.1",
"resolved": "https://registry.npmjs.org/typedoc-default-themes/-/typedoc-default-themes-0.12.4.tgz", "resolved": "https://registry.npmjs.org/typedoc-default-themes/-/typedoc-default-themes-0.12.1.tgz",
"integrity": "sha512-EZiXBUpogsYWe0dLgy47J8yRZCd+HAn9woGzO28XJxxSCSwZRYGKeQiw1KjyIcm3cBtLWUXiPD5+Bgx24GgZjg==", "integrity": "sha512-6PEvV+/kWAJeUwEtrKgIsZQSbybW5DGCr6s2mMjHsDplpgN8iBHI52UbA+2C+c2TMCxBNMK9TMS6pdeIdwsLSw==",
"dev": true "dev": true
}, },
"typescript": { "typescript": {

View File

@@ -71,7 +71,7 @@
"process services-cloud" "process services-cloud"
], ],
"dependencies": { "dependencies": {
"@alfresco/js-api": "4.3.0-61350fee919fbeb2b6c949621161b6b076d44133", "@alfresco/js-api": "^4.3.0-f117dba26d7c31cb56a6cf40eb84dff89a174971",
"@angular/animations": "^10.0.4", "@angular/animations": "^10.0.4",
"@angular/cdk": "10.1.3", "@angular/cdk": "10.1.3",
"@angular/common": "^10.0.4", "@angular/common": "^10.0.4",
@@ -170,7 +170,7 @@
"ts-node": "^9.0.0", "ts-node": "^9.0.0",
"tsconfig-paths": "^3.9.0", "tsconfig-paths": "^3.9.0",
"tslint": "6.1.3", "tslint": "6.1.3",
"typedoc": "^0.20.16", "typedoc": "^0.20.14",
"typescript": "3.9.7", "typescript": "3.9.7",
"unist-util-select": "^3.0.2", "unist-util-select": "^3.0.2",
"webpack-cli": "^3.3.12" "webpack-cli": "^3.3.12"