[ACS-3551] feat: add context to outgoing requests for auth intgerceptors (#7913)

This commit is contained in:
Mikołaj Serwicki
2022-10-24 10:28:06 +02:00
committed by GitHub
parent 2d57ca31ca
commit 18a5197b5a
14 changed files with 246 additions and 23 deletions

View File

@@ -61,10 +61,11 @@ import { SearchTextModule } from './search-text/search-text-input.module';
import { versionCompatibilityFactory } from './services/version-compatibility-factory';
import { VersionCompatibilityService } from './services/version-compatibility.service';
import { AlfrescoJsClientsModule } from '@alfresco/adf-core/api';
import { AuthenticationInterceptor, Authentication } from '@alfresco/adf-core/auth';
import { LegacyApiClientModule } from './api-factories/legacy-api-client.module';
import { RichTextEditorModule } from './rich-text-editor/rich-text-editor.module';
import { HttpClientModule, HttpClientXsrfModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { AuthBearerInterceptor } from './services/auth-bearer.interceptor';
import { AuthenticationService } from './services/authentication.service';
@NgModule({
imports: [
@@ -175,7 +176,8 @@ export class CoreModule {
deps: [VersionCompatibilityService],
multi: true
},
{ provide: HTTP_INTERCEPTORS, useClass: AuthBearerInterceptor, multi: true }
{ provide: HTTP_INTERCEPTORS, useClass: AuthenticationInterceptor, multi: true },
{ provide: Authentication, useClass: AuthenticationService }
]
};
}

View File

@@ -27,13 +27,14 @@ import { catchError, mergeMap } from 'rxjs/operators';
@Injectable()
export class AuthBearerInterceptor implements HttpInterceptor {
private excludedUrlsRegex: RegExp[];
private authService: AuthenticationService;
constructor(private injector: Injector, private authService: AuthenticationService) { }
constructor(private injector: Injector) { }
private loadExcludedUrlsRegex() {
const excludedUrls: string[] = this.authService.getBearerExcludedUrls();
this.excludedUrlsRegex = excludedUrls.map((urlPattern) => new RegExp(urlPattern, 'gi')) || [];
this.excludedUrlsRegex = [...excludedUrls].map((urlPattern) => new RegExp(urlPattern, 'i')) || [];
}
intercept(req: HttpRequest<any>, next: HttpHandler):
@@ -50,7 +51,7 @@ export class AuthBearerInterceptor implements HttpInterceptor {
}
const urlRequest = req.url;
const shallPass: boolean = this.excludedUrlsRegex.some((regex) => regex.test(urlRequest));
const shallPass: boolean = !!this.excludedUrlsRegex.find((regex) => regex.test(urlRequest));
if (shallPass) {
return next.handle(req)
.pipe(
@@ -72,19 +73,7 @@ export class AuthBearerInterceptor implements HttpInterceptor {
}
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;
return headers.set('Content-Type', 'application/json;charset=UTF-8');
}
}

View File

@@ -15,6 +15,7 @@
* limitations under the License.
*/
import { Authentication } from '@alfresco/adf-core/auth';
import { Injectable } from '@angular/core';
import { Observable, from, throwError, Observer, ReplaySubject, forkJoin } from 'rxjs';
import { AlfrescoApiService } from './alfresco-api.service';
@@ -34,7 +35,7 @@ const REMEMBER_ME_UNTIL = 1000 * 60 * 60 * 24 * 30;
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
export class AuthenticationService extends Authentication {
private redirectUrl: RedirectionModel = null;
private bearerExcludedUrls: string[] = ['auth/realms', 'resources/', 'assets/'];
@@ -66,6 +67,7 @@ export class AuthenticationService {
private alfrescoApi: AlfrescoApiService,
private cookie: CookieService,
private logService: LogService) {
super();
this.alfrescoApi.alfrescoApiInitialized.subscribe(() => {
this.alfrescoApi.getInstance().reply('logged-in', () => {
this.onLogin.next();