2 Copyright 2020 TATA ELXSI
4 Licensed under the Apache License, Version 2.0 (the 'License');
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
20 * @file HttpInterceptor file
23 HttpErrorResponse, HttpHandler, HttpHeaderResponse, HttpInterceptor, HttpProgressEvent,
24 HttpRequest, HttpResponse, HttpSentEvent, HttpUserEvent
25 } from '@angular/common/http';
26 import { Injectable, Injector } from '@angular/core';
27 import { TranslateService } from '@ngx-translate/core';
28 import { NotifierService } from 'angular-notifier';
29 import { AuthenticationService } from 'AuthenticationService';
30 import * as HttpStatus from 'http-status-codes';
31 import { Observable, throwError } from 'rxjs';
32 import { catchError, retry } from 'rxjs/operators';
35 * An Injectable is a class adorned with the @Injectable decorator function.
36 * @Injectable takes a metadata object that tells Angular how to compile and run module code
39 export class AuthInterceptorService implements HttpInterceptor {
40 /** Holds header options @private */
41 private clonedReq: HttpRequest<{}>;
43 /** To inject services @private */
44 private injector: Injector;
46 /** Notifier service to popup notification @private */
47 private notifierService: NotifierService;
49 /** Contains tranlsate instance @private */
50 private translateService: TranslateService;
52 /** Utilizes auth service for any auth operations @private */
53 private authService: AuthenticationService;
55 /** create the instance of the component */
56 constructor(injector: Injector) {
57 this.injector = injector;
58 this.notifierService = this.injector.get(NotifierService);
59 this.authService = this.injector.get(AuthenticationService);
60 this.translateService = this.injector.get(TranslateService);
68 public intercept(req: HttpRequest<{}>, next: HttpHandler): Observable<HttpSentEvent |
69 // tslint:disable-next-line:no-any
70 HttpHeaderResponse | HttpProgressEvent | HttpResponse<{}> | HttpUserEvent<any> | any> {
71 const idToken: string = localStorage.getItem('id_token');
72 const excludedUrl: string[] = ['osm/admin/v1/tokens', 'assets/i18n/', 'osm/version'];
73 if (excludedUrl.some((x: string): boolean => { return req.url.includes(x); })) { return next.handle(req); }
74 if (idToken.length > 0) {
75 this.setHeader(req, idToken);
76 return next.handle(this.clonedReq).pipe(
77 catchError((err: HttpErrorResponse) => {
78 this.errorRes(err, req, next);
79 return throwError(err);
83 //TODO: Handle error via notification service
87 /** Set header options @public */
88 // tslint:disable-next-line:no-any
89 public setHeader(req: HttpRequest<any>, idToken: string): void {
90 if (req.body !== null && req.body.byteLength !== null) {
91 this.clonedReq = req.clone({
92 setHeaders: { Authorization: 'Bearer ' + idToken, 'Cache-Control': 'no-cache', Pragma: 'no-cache' }
95 this.clonedReq = req.clone({
96 setHeaders: { Authorization: 'Bearer ' + idToken, 'Content-Type': 'charset=UTF-8',
97 'Cache-Control': 'no-cache', Pragma: 'no-cache' }
102 /** Handles error response @public */
103 public errorRes(err: HttpErrorResponse, req: HttpRequest<{}>, next: HttpHandler): Observable<{}> {
104 if (err instanceof HttpErrorResponse) {
105 switch (err.status) {
106 case HttpStatus.UNAUTHORIZED:
107 case HttpStatus.FORBIDDEN:
108 this.handleError(err);
110 case HttpStatus.GATEWAY_TIMEOUT:
111 case HttpStatus.BAD_GATEWAY:
112 this.notifierService.hideAll();
113 this.authService.logoutResponse();
115 default: return throwError(err);
117 } else { return throwError(err); }
120 /** Method to handle 401, 403 & 502 error */
121 private handleError(err: HttpErrorResponse): void {
122 if (err.error.detail !== 'Access denied: lack of permissions.') {
123 this.notifierService.hideAll();
124 this.authService.logoutResponse();
125 if (this.authService.handle401) {
126 this.notifierService.notify('error', this.translateService.instant('SESSIONEXPIRY'));
127 this.authService.handle401 = false;