move auth interceptor to the core lib [ci:force]

rebasing develop
This commit is contained in:
Denys Vuika
2025-07-21 13:36:12 +02:00
committed by Anton Ramanovich
parent 880c670593
commit 57854b41cd
13 changed files with 147 additions and 40 deletions
@@ -0,0 +1,69 @@
/*!
* @license
* Copyright © 2005-2024 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 { HttpHandler, HttpHeaders, HttpRequest } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { Observable, of } from 'rxjs';
import { Authentication } from '../authentication';
import { AuthenticationInterceptor } from './authentication.interceptor';
import { SHOULD_ADD_AUTH_TOKEN } from '@alfresco/adf-core/api';
class MockAuthentication extends Authentication {
addTokenToHeader(_: string, httpHeaders: HttpHeaders): Observable<HttpHeaders> {
return of(httpHeaders);
}
}
const mockNext: HttpHandler = {
handle: () =>
new Observable((subscriber) => {
subscriber.complete();
})
};
const request = new HttpRequest('GET', 'http://localhost:4200');
describe('AuthenticationInterceptor', () => {
let interceptor: AuthenticationInterceptor;
let addTokenToHeaderSpy: jasmine.Spy<any>;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [AuthenticationInterceptor, { provide: Authentication, useClass: MockAuthentication }]
});
interceptor = TestBed.inject(AuthenticationInterceptor);
addTokenToHeaderSpy = spyOn(interceptor['authService'], 'addTokenToHeader');
});
it('should call add auth token method when SHOULD_ADD_AUTH_TOKEN context is set to true', () => {
addTokenToHeaderSpy.and.callThrough();
request.context.set(SHOULD_ADD_AUTH_TOKEN, true);
interceptor.intercept(request, mockNext);
expect(addTokenToHeaderSpy).toHaveBeenCalled();
});
it('should not call add auth token method when SHOULD_ADD_AUTH_TOKEN context is set to false', () => {
request.context.set(SHOULD_ADD_AUTH_TOKEN, false);
interceptor.intercept(request, mockNext);
expect(addTokenToHeaderSpy).not.toHaveBeenCalled();
});
it('should not call add auth token method when SHOULD_ADD_AUTH_TOKEN context is not provided', () => {
interceptor.intercept(request, mockNext);
expect(addTokenToHeaderSpy).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,70 @@
/*!
* @license
* Copyright © 2005-2024 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 {
HttpHandler,
HttpHeaderResponse,
HttpHeaders,
HttpInterceptor,
HttpProgressEvent,
HttpRequest,
HttpResponse,
HttpSentEvent,
HttpUserEvent
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError as observableThrowError } from 'rxjs';
import { catchError, mergeMap } from 'rxjs/operators';
import { Authentication } from '../authentication';
import { SHOULD_ADD_AUTH_TOKEN } from '@alfresco/adf-core/api';
@Injectable()
export class AuthenticationInterceptor implements HttpInterceptor {
constructor(private authService: Authentication) {}
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
if (req.context.get(SHOULD_ADD_AUTH_TOKEN)) {
return this.authService.addTokenToHeader(req.url, req.headers).pipe(
mergeMap((headersWithBearer) => {
const headerWithContentType = this.appendJsonContentType(headersWithBearer);
const kcReq = req.clone({ headers: headerWithContentType });
return next.handle(kcReq).pipe(catchError((error) => observableThrowError(error)));
})
);
}
return next.handle(req).pipe(catchError((error) => observableThrowError(error)));
}
private appendJsonContentType(headers: HttpHeaders): HttpHeaders {
// prevent adding any content type, to properly handle formData with boundary browser generated value,
// as adding any Content-Type its going to break the upload functionality
if (headers.get('Content-Type') === 'multipart/form-data') {
return headers.delete('Content-Type');
}
if (!headers.get('Content-Type')) {
return headers.set('Content-Type', 'application/json;charset=UTF-8');
}
return headers;
}
}
+23
View File
@@ -0,0 +1,23 @@
/*!
* @license
* Copyright © 2005-2025 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 { HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
export abstract class Authentication {
public abstract addTokenToHeader(requestUrl: string, headers: HttpHeaders): Observable<HttpHeaders>;
}
+2 -1
View File
@@ -15,8 +15,9 @@
* limitations under the License.
*/
export * from './authentication';
export * from './authentication-interceptor/auth-bearer.interceptor';
export * from './authentication-interceptor/authentication.interceptor';
export * from './guard/auth-guard.service';
export * from './guard/auth-guard';
export * from './guard/auth-guard-ecm.service';
+1 -1
View File
@@ -39,8 +39,8 @@ import { CORE_DIRECTIVES } from './directives/directive.module';
import { CORE_PIPES } from './pipes/pipe.module';
import { TranslateLoaderService } from './translation/translate-loader.service';
import { SEARCH_TEXT_INPUT_DIRECTIVES } from './search-text/search-text-input.module';
import { AuthenticationInterceptor, Authentication } from '@alfresco/adf-core/auth';
import { HTTP_INTERCEPTORS, HttpClient, provideHttpClient, withXsrfConfiguration, withInterceptorsFromDi } from '@angular/common/http';
import { AuthenticationInterceptor, Authentication } from './auth';
import { AuthenticationService } from './auth/services/authentication.service';
import { MAT_SNACK_BAR_DEFAULT_OPTIONS } from '@angular/material/snack-bar';
import { AppConfigPipe } from './app-config';