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 // eslint-disable-next-line @typescript-eslint/no-explicit-any
70 HttpHeaderResponse | HttpProgressEvent | HttpResponse<{}> | HttpUserEvent<any> | any> {
71 const idToken: string = sessionStorage.getItem('id_token');
72 const excludedUrl: string[] = ['osm/admin/v1/tokens', 'assets/i18n/', 'osm/version'];
73 if (excludedUrl.some((x: string): boolean => 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 // eslint-disable-next-line @typescript-eslint/no-explicit-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({
97 Authorization: 'Bearer ' + idToken,
98 'Content-Type': 'charset=UTF-8',
99 'Cache-Control': 'no-cache',
106 /** Handles error response @public */
107 public errorRes(err: HttpErrorResponse, req: HttpRequest<{}>, next: HttpHandler): Observable<{}> {
108 if (err instanceof HttpErrorResponse) {
109 switch (err.status) {
110 case HttpStatus.UNAUTHORIZED:
111 case HttpStatus.FORBIDDEN:
112 this.handleError(err);
114 case HttpStatus.GATEWAY_TIMEOUT:
115 case HttpStatus.BAD_GATEWAY:
116 this.notifierService.hideAll();
117 this.authService.logoutResponse();
119 default: return throwError(err);
121 } else { return throwError(err); }
124 /** Method to handle 401, 403 & 502 error */
125 private handleError(err: HttpErrorResponse): void {
126 if (err.error.detail !== 'Access denied: lack of permissions.' && err.error.detail !== 'You cannot remove system_admin role from admin user') {
127 this.notifierService.hideAll();
128 this.authService.logoutResponse();
129 if (this.authService.handle401) {
130 this.notifierService.notify('error', this.translateService.instant('SESSIONEXPIRY'));
131 this.authService.handle401 = false;