diff --git a/lib/core/src/lib/auth/basic-auth/basic-alfresco-auth.service.ts b/lib/core/src/lib/auth/basic-auth/basic-alfresco-auth.service.ts index bfa6f059a6..8f7f36e41a 100644 --- a/lib/core/src/lib/auth/basic-auth/basic-alfresco-auth.service.ts +++ b/lib/core/src/lib/auth/basic-auth/basic-alfresco-auth.service.ts @@ -54,14 +54,20 @@ export class BasicAlfrescoAuthService extends BaseAuthenticationService { ) { super(appConfig, cookie, logService); + this.appConfig.onLoad.subscribe(() => { + if (this.isLoggedIn()) { + this.onLogin.next('logged-in'); + } + }); + this.contentAuth.onLogout.pipe(map((event) => { this.onLogout.next(event); })); this.contentAuth.onLogin.pipe(map((event) => { - this.onLogout.next(event); + this.onLogin.next(event); })); this.contentAuth.onError.pipe(map((event) => { - this.onLogout.next(event); + this.onError.next(event); })); this.processAuth.onLogout.pipe(map((event) => { this.onLogout.next(event); @@ -185,7 +191,25 @@ export class BasicAlfrescoAuthService extends BaseAuthenticationService { } getToken(): string { - return ''; + if (this.isBPMProvider()) { + return this.processAuth.getToken(); + } else if (this.isECMProvider()) { + return this.contentAuth.getToken(); + } else if (this.isALLProvider()) { + return this.contentAuth.getToken(); + }else{ + return ''; + } + } + + /** @deprecated */ + getTicketEcm(): string{ + return this.contentAuth.getToken(); + } + + /** @deprecated */ + getTicketBpm(): string{ + return this.processAuth.getToken(); } isBpmLoggedIn(): boolean { @@ -213,17 +237,11 @@ export class BasicAlfrescoAuthService extends BaseAuthenticationService { /** * logout Alfresco API * */ - logout(): Promise { + async logout(): Promise { if (this.isBPMProvider()) { return this.processAuth.logout(); } else if (this.isECMProvider()) { - const contentPromise = this.contentAuth.logout(); - contentPromise.then( - () => this.contentAuth.ticket = undefined, - () => { - } - ); - return contentPromise; + return this.contentAuth.logout(); } else if (this.isALLProvider()) { return this.logoutBPMECM(); } @@ -321,9 +339,9 @@ export class BasicAlfrescoAuthService extends BaseAuthenticationService { const contextRoot = this.appConfig.get(AppConfigValues.CONTEXTROOTECM); if (contextRoot && requestUrl.indexOf(contextRoot) !== -1) { - ticket = 'Basic ' + btoa(this.contentAuth.getTicket()); + ticket = 'Basic ' + btoa(this.contentAuth.getToken()); } else if (contextRootBpm && requestUrl.indexOf(contextRootBpm) !== -1) { - ticket = 'Basic ' + this.processAuth.getTicket(); + ticket = 'Basic ' + this.processAuth.getToken(); } return ticket; diff --git a/lib/core/src/lib/auth/basic-auth/content-auth.ts b/lib/core/src/lib/auth/basic-auth/content-auth.ts index 08795e7b81..c8636585f0 100644 --- a/lib/core/src/lib/auth/basic-auth/content-auth.ts +++ b/lib/core/src/lib/auth/basic-auth/content-auth.ts @@ -133,9 +133,9 @@ export class ContentAuth { return new Promise((resolve, reject) => { this.deleteTicket().then( () => { + this.invalidateSession(); this.adfHttpClient.emit('logout'); this.onLogout.next('logout'); - this.invalidateSession(); resolve('logout'); }, (error) => { @@ -164,7 +164,7 @@ export class ContentAuth { /** * Get the current Ticket * */ - getTicket(): string { + getToken(): string { if(!this.ticket){ this.onError.next('error'); } diff --git a/lib/core/src/lib/auth/basic-auth/process-auth.ts b/lib/core/src/lib/auth/basic-auth/process-auth.ts index 6189530525..116902c123 100644 --- a/lib/core/src/lib/auth/basic-auth/process-auth.ts +++ b/lib/core/src/lib/auth/basic-auth/process-auth.ts @@ -91,11 +91,11 @@ export class ProcessAuth { accept: 'application/json' }; - let promise: any = new Promise((resolve, reject) => { + const promise: any = new Promise((resolve, reject) => { this.adfHttpClient.post(this.basePath + '/app/authentication', options).then( () => { this.saveUsername(username); - let ticket = this.basicAuth(this.authentications.basicAuth.username, this.authentications.basicAuth.password); + const ticket = this.basicAuth(this.authentications.basicAuth.username, this.authentications.basicAuth.password); this.setTicket(ticket); this.onLogin.next('success'); this.adfHttpClient.emit('success'); @@ -188,7 +188,7 @@ export class ProcessAuth { /** * Get the current Ticket * */ - getTicket(): string { + getToken(): string { if(!this.ticket){ this.onError.next('error'); return null; diff --git a/lib/core/src/lib/auth/services/authentication.service.spec.ts b/lib/core/src/lib/auth/services/authentication.service.spec.ts index 7c64672539..db9ff38cba 100644 --- a/lib/core/src/lib/auth/services/authentication.service.spec.ts +++ b/lib/core/src/lib/auth/services/authentication.service.spec.ts @@ -94,10 +94,8 @@ describe('AuthenticationService', () => { spyOn(basicAlfrescoAuthService, 'isRememberMeSet').and.returnValue(false); spyOn(authService, 'isECMProvider').and.returnValue(true); spyOn(authService, 'isOauth').and.returnValue(false); - spyOn(apiService, 'getInstance').and.callThrough(); expect(authService.isEcmLoggedIn()).toBeFalsy(); - expect(apiService.getInstance).toHaveBeenCalled(); }); it('should require remember me set for ECM check', () => { @@ -114,7 +112,7 @@ describe('AuthenticationService', () => { it('[ECM] should return an ECM ticket after the login done', (done) => { const disposableLogin = basicAlfrescoAuthService.login('fake-username', 'fake-password').subscribe(() => { expect(authService.isLoggedIn()).toBe(true); - expect(basicAlfrescoAuthService.getToken()).toEqual('fake-post-ticket'); + expect(authService.getToken()).toEqual('fake-post-ticket'); expect(authService.isEcmLoggedIn()).toBe(true); disposableLogin.unsubscribe(); done(); @@ -140,7 +138,7 @@ describe('AuthenticationService', () => { }); })); - it('[ECM] should return a ticket undefined after logout', fakeAsync(() => { + fit('[ECM] should return a ticket undefined after logout', fakeAsync(() => { const disposableLogin = basicAlfrescoAuthService.login('fake-username', 'fake-password').subscribe(() => { const disposableLogout = authService.logout().subscribe(() => { expect(authService.isLoggedIn()).toBe(false); diff --git a/lib/core/src/lib/auth/services/authentication.service.ts b/lib/core/src/lib/auth/services/authentication.service.ts index f5ae1a68a4..ea0f11dd93 100644 --- a/lib/core/src/lib/auth/services/authentication.service.ts +++ b/lib/core/src/lib/auth/services/authentication.service.ts @@ -23,6 +23,7 @@ import { BaseAuthenticationService } from './base-authentication.service'; import { AppConfigService } from '../../app-config'; import { CookieService, LogService } from '../../common'; import { HttpHeaders } from '@angular/common/http'; +import { tap } from 'rxjs/operators'; @Injectable({ providedIn: 'root' @@ -34,6 +35,11 @@ export class AuthenticationService extends BaseAuthenticationService { logService: LogService, private injector: Injector) { super(appConfig, cookie, logService); + + (this.isOauth() ? this.oidcAuthenticationService.onLogin : this.basicAlfrescoAuthService.onLogin) + .pipe( + tap(() => this.onLogin.next()) + ).subscribe(); } private get oidcAuthenticationService(): OidcAuthenticationService {