PoC: Make @alfresco/js-api to be compatible with angular's http client

This commit is contained in:
Andras Popovics
2022-02-25 15:33:31 +01:00
parent a0c7631abb
commit e432ba6d37
14 changed files with 574 additions and 12 deletions

View File

@@ -1,79 +0,0 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* 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 { throwError as observableThrowError, Observable } from 'rxjs';
import { Injectable, Injector } from '@angular/core';
import {
HttpHandler, HttpInterceptor, HttpRequest,
HttpSentEvent, HttpHeaderResponse, HttpProgressEvent, HttpResponse, HttpUserEvent, HttpHeaders
} from '@angular/common/http';
import { AuthenticationService } from './authentication.service';
import { catchError, mergeMap } from 'rxjs/operators';
@Injectable()
export class AuthBearerInterceptor implements HttpInterceptor {
private excludedUrlsRegex: RegExp[];
private authService: AuthenticationService;
constructor(private injector: Injector) { }
private loadExcludedUrlsRegex() {
const excludedUrls: string[] = this.authService.getBearerExcludedUrls();
this.excludedUrlsRegex = excludedUrls.map((urlPattern) => new RegExp(urlPattern, 'gi')) || [];
}
intercept(req: HttpRequest<any>, next: HttpHandler):
Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
this.authService = this.injector.get(AuthenticationService);
if (!this.authService || !this.authService.getBearerExcludedUrls()) {
return next.handle(req);
}
if (!this.excludedUrlsRegex) {
this.loadExcludedUrlsRegex();
}
const urlRequest = req.url;
const shallPass: boolean = !!this.excludedUrlsRegex.find((regex) => regex.test(urlRequest));
if (shallPass) {
return next.handle(req)
.pipe(
catchError((error) => observableThrowError(error))
);
}
return this.authService.addTokenToHeader(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))
);
})
);
}
private appendJsonContentType(headers: HttpHeaders): HttpHeaders {
return headers.set('Content-Type', 'application/json;charset=UTF-8');
}
}

View File

@@ -18,11 +18,12 @@
import { Injectable } from '@angular/core';
import { from, Observable, throwError, Subject } from 'rxjs';
import { catchError, map, switchMap, filter, take } from 'rxjs/operators';
import { AboutApi, DiscoveryApi, RepositoryInfo, SystemPropertiesApi, SystemPropertiesRepresentation } from '@alfresco/js-api';
import { AboutApi, RepositoryInfo, SystemPropertiesApi, SystemPropertiesRepresentation } from '@alfresco/js-api';
import { BpmProductVersionModel } from '../models/product-version.model';
import { AlfrescoApiService } from './alfresco-api.service';
import { AuthenticationService } from './authentication.service';
import { AlfrescoApiClientFactory } from '../alfresco-api';
@Injectable({
providedIn: 'root'
@@ -36,7 +37,8 @@ export class DiscoveryApiService {
constructor(
private apiService: AlfrescoApiService,
private authenticationService: AuthenticationService) {
private authenticationService: AuthenticationService,
private alfrescoApiClientFactory: AlfrescoApiClientFactory) {
this.authenticationService.onLogin
.pipe(
@@ -53,7 +55,8 @@ export class DiscoveryApiService {
* @returns ProductVersionModel containing product details
*/
getEcmProductInfo(): Observable<RepositoryInfo> {
const discoveryApi = new DiscoveryApi(this.apiService.getInstance());
// const discoveryApi = new DiscoveryApi(this.apiService.getInstance());
const discoveryApi = this.alfrescoApiClientFactory.getDiscoveryApi();
return from(discoveryApi.getRepositoryInformation())
.pipe(

View File

@@ -62,7 +62,7 @@ export * from './identity-user.service';
export * from './identity-group.service';
export * from './identity-role.service';
export * from './version-compatibility.service';
export * from './auth-bearer.interceptor';
export * from '../alfresco-api/services/auth-bearer.interceptor';
export * from './oauth2.service';
export * from './language.service';
export * from './identity-user.service.interface';