[ACS-6748] BasicAlfrescoAuthService.requireAlfTicket should not be in adf/core (#9393)

This commit is contained in:
dominikiwanekhyland
2024-03-04 10:02:03 +01:00
committed by GitHub
parent 255f472b39
commit 325eef1c32
5 changed files with 158 additions and 10 deletions

View File

@@ -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();
}

View File

@@ -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<void>;
beforeEach(() => {
onLoginSubject = new Subject<void>();
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();
}));
});

View File

@@ -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();
}
}
});
}
}

View File

@@ -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
}
]
};

View File

@@ -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<any> {
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();
}