diff --git a/lib/content-services/src/lib/auth-loader/content-auth-loader-factory.ts b/lib/content-services/src/lib/auth-loader/content-auth-loader-factory.ts new file mode 100644 index 0000000000..8028268263 --- /dev/null +++ b/lib/content-services/src/lib/auth-loader/content-auth-loader-factory.ts @@ -0,0 +1,29 @@ +/*! + * @license + * Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ContentAuthLoaderService } from './content-auth-loader.service'; + +// eslint-disable-next-line prefer-arrow/prefer-arrow-functions +/** + * Create a content auth factory + * + * @param authLoaderService service dependency + * @returns factory function + */ +export function contentAuthLoaderFactory(authLoaderService: ContentAuthLoaderService): () => void { + return () => authLoaderService.init(); +} diff --git a/lib/content-services/src/lib/auth-loader/content-auth-loader.service.spec.ts b/lib/content-services/src/lib/auth-loader/content-auth-loader.service.spec.ts new file mode 100644 index 0000000000..0b874ecede --- /dev/null +++ b/lib/content-services/src/lib/auth-loader/content-auth-loader.service.spec.ts @@ -0,0 +1,78 @@ +/*! + * @license + * Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { fakeAsync, TestBed, tick } from '@angular/core/testing'; +import { AuthenticationService, BasicAlfrescoAuthService } from '@alfresco/adf-core'; +import { ContentAuthLoaderService } from './content-auth-loader.service'; +import { Subject } from 'rxjs'; + +describe('ContentAuthLoaderService', () => { + let service: ContentAuthLoaderService; + let authService: AuthenticationService; + let basicAlfrescoAuthService: BasicAlfrescoAuthService; + let onLoginSubject: Subject; + + beforeEach(() => { + onLoginSubject = new Subject(); + TestBed.configureTestingModule({ + providers: [ + ContentAuthLoaderService, + { + provide: AuthenticationService, + useValue: { + onLogin: onLoginSubject.asObservable(), + isOauth: () => false, + isALLProvider: () => false, + isECMProvider: () => false + } + }, + { + provide: BasicAlfrescoAuthService, + useValue: { + requireAlfTicket: jasmine.createSpy() + } + } + ] + }); + + service = TestBed.inject(ContentAuthLoaderService); + authService = TestBed.inject(AuthenticationService); + basicAlfrescoAuthService = TestBed.inject(BasicAlfrescoAuthService); + }); + + + it('should require Alf ticket on login if OAuth and provider is ALL or ECM', fakeAsync(() => { + spyOn(authService, 'isOauth').and.returnValue(true); + spyOn(authService, 'isALLProvider').and.returnValue(true); + + service.init(); + onLoginSubject.next(); + tick(); + + expect(basicAlfrescoAuthService.requireAlfTicket).toHaveBeenCalled(); + })); + + it('should not require Alf ticket on login if not OAuth', fakeAsync(() => { + spyOn(authService, 'isOauth').and.returnValue(false); + + service.init(); + onLoginSubject.next(); + tick(); + + expect(basicAlfrescoAuthService.requireAlfTicket).not.toHaveBeenCalled(); + })); +}); diff --git a/lib/content-services/src/lib/auth-loader/content-auth-loader.service.ts b/lib/content-services/src/lib/auth-loader/content-auth-loader.service.ts new file mode 100644 index 0000000000..f92e6ec3e1 --- /dev/null +++ b/lib/content-services/src/lib/auth-loader/content-auth-loader.service.ts @@ -0,0 +1,42 @@ +/*! + * @license + * Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Injectable } from '@angular/core'; +import { AuthenticationService, BasicAlfrescoAuthService } from '@alfresco/adf-core'; +import { take } from 'rxjs/operators'; + +@Injectable() +export class ContentAuthLoaderService { + + constructor( + private readonly basicAlfrescoAuthService: BasicAlfrescoAuthService, + private readonly authService: AuthenticationService + ) { + } + + init(): void { + this.authService.onLogin + .pipe(take(1)) + .subscribe({ + next: async () => { + if (this.authService.isOauth() && (this.authService.isALLProvider() || this.authService.isECMProvider())) { + await this.basicAlfrescoAuthService.requireAlfTicket(); + } + } + }); + } +} diff --git a/lib/content-services/src/lib/content.module.ts b/lib/content-services/src/lib/content.module.ts index c82047ef47..d5b9e3db24 100644 --- a/lib/content-services/src/lib/content.module.ts +++ b/lib/content-services/src/lib/content.module.ts @@ -51,6 +51,8 @@ import { AlfrescoViewerModule } from './viewer/alfresco-viewer.module'; import { ContentUserInfoModule } from './content-user-info/content-user-info.module'; import { SecurityControlsServiceModule } from './security/services/security-controls-service.module'; import { CategoriesModule } from './category/category.module'; +import { contentAuthLoaderFactory } from './auth-loader/content-auth-loader-factory'; +import { ContentAuthLoaderService } from './auth-loader/content-auth-loader.service'; @NgModule({ imports: [ @@ -128,11 +130,18 @@ export class ContentModule { ngModule: ContentModule, providers: [ provideTranslations('adf-content-services', 'assets/adf-content-services'), + ContentAuthLoaderService, { provide: APP_INITIALIZER, useFactory: versionCompatibilityFactory, deps: [VersionCompatibilityService], multi: true + }, + { + provide: APP_INITIALIZER, + useFactory: contentAuthLoaderFactory, + deps: [ContentAuthLoaderService], + multi: true } ] }; diff --git a/lib/core/src/lib/api-factories/alfresco-api-v2-loader.service.ts b/lib/core/src/lib/api-factories/alfresco-api-v2-loader.service.ts index 7c8b653102..fe78b599d4 100644 --- a/lib/core/src/lib/api-factories/alfresco-api-v2-loader.service.ts +++ b/lib/core/src/lib/api-factories/alfresco-api-v2-loader.service.ts @@ -20,7 +20,6 @@ import { Injectable } from '@angular/core'; import { AppConfigService, AppConfigValues } from '../app-config/app-config.service'; import { AlfrescoApiService } from '../services/alfresco-api.service'; import { StorageService } from '../common/services/storage.service'; -import { AuthenticationService, BasicAlfrescoAuthService } from '../auth'; /** * Create a factory to resolve an api service instance @@ -38,20 +37,11 @@ export function createAlfrescoApiInstance(angularAlfrescoApiService: AlfrescoApi export class AlfrescoApiLoaderService { constructor(private readonly appConfig: AppConfigService, private readonly apiService: AlfrescoApiService, - private readonly basicAlfrescoAuthService: BasicAlfrescoAuthService, - private readonly authService: AuthenticationService, private storageService: StorageService) { } async init(): Promise { await this.appConfig.load(); - - this.authService.onLogin.subscribe(async () => { - if (this.authService.isOauth() && (this.authService.isALLProvider() || this.authService.isECMProvider())) { - await this.basicAlfrescoAuthService.requireAlfTicket(); - } - }); - return this.initAngularAlfrescoApi(); }