[ADF-3570] ADF Migration - Add the interceptor layer (#3813)

* Enable impliciFlow and add new interceptor

* Fix errors

* add excluded path rules

* Add the needed methods to the AuthenticationService core

* Remove unused library and service
This commit is contained in:
Maurizio Vitale
2018-09-20 22:17:05 +01:00
committed by Eugenio Romano
parent 244d1aae85
commit cfbe3cfd86
5 changed files with 131 additions and 3 deletions

View File

@@ -20,7 +20,7 @@ import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { FlexLayoutModule } from '@angular/flex-layout';
import { ChartsModule } from 'ng2-charts';
import { HttpClientModule } from '@angular/common/http';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppConfigService, TRANSLATION_PROVIDER, DebugAppConfigService, CoreModule } from '@alfresco/adf-core';
@@ -79,6 +79,7 @@ import { MonacoEditorModule } from 'ngx-monaco-editor';
import { ContentModule } from '@alfresco/adf-content-services';
import { InsightsModule } from '@alfresco/adf-insights';
import { ProcessModule } from '@alfresco/adf-process-services';
import { AuthBearerInterceptor } from './services';
@NgModule({
imports: [
@@ -144,6 +145,10 @@ import { ProcessModule } from '@alfresco/adf-process-services';
ConfigEditorComponent
],
providers: [
{
provide: HTTP_INTERCEPTORS, useClass:
AuthBearerInterceptor, multi: true
},
{ provide: AppConfigService, useClass: DebugAppConfigService }, // not use this service in production
{
provide: TRANSLATION_PROVIDER,

View File

@@ -0,0 +1,77 @@
/*!
* @license
* Copyright 2018 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
} from '@angular/common/http';
import { AuthenticationService } from '@alfresco/adf-core';
import { catchError, mergeMap } from 'rxjs/operators';
@Injectable()
export class AuthBearerInterceptor implements HttpInterceptor {
private excludedUrlsRegex: RegExp[];
private authService: AuthenticationService;
constructor(private injector: Injector) { }
private loadExcludedUrlsRegex(): void {
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 => {
return observableThrowError(error);
})
);
}
return this.authService.addTokenToHeader(req.headers)
.pipe(
mergeMap(headersWithBearer => {
const kcReq = req.clone({ headers: headersWithBearer });
return next.handle(kcReq)
.pipe(
catchError(error => {
return observableThrowError(error);
})
);
})
);
}
}

View File

@@ -0,0 +1,18 @@
/*!
* @license
* Copyright 2018 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.
*/
export * from './auth-bearer.interceptor';