Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[osm/NG-UI.git] / src / services / AuthInterceptorService.ts
1 /*
2  Copyright 2020 TATA ELXSI
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15
16  Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17  */
18
19 /**
20  * @file HttpInterceptor file
21  */
22 import {
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';
33
34 /**
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
37  */
38 @Injectable()
39 export class AuthInterceptorService implements HttpInterceptor {
40     /** Holds header options @private */
41     private clonedReq: HttpRequest<{}>;
42
43     /** To inject services @private */
44     private injector: Injector;
45
46     /** Notifier service to popup notification @private */
47     private notifierService: NotifierService;
48
49     /** Contains tranlsate instance @private */
50     private translateService: TranslateService;
51
52     /** Utilizes auth service for any auth operations @private */
53     private authService: AuthenticationService;
54
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);
61     }
62
63     /**
64      * intercept logic
65      * @param req
66      * @param next
67      */
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 = localStorage.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);
80                 })
81             );
82         } else {
83             //TODO: Handle error via notification service
84         }
85     }
86
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' }
93             });
94         } else {
95             this.clonedReq = req.clone({
96                 setHeaders: {
97                     Authorization: 'Bearer ' + idToken,
98                     'Content-Type': 'charset=UTF-8',
99                     'Cache-Control': 'no-cache',
100                     Pragma: 'no-cache'
101                 }
102             });
103         }
104     }
105
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);
113                     break;
114                 case HttpStatus.GATEWAY_TIMEOUT:
115                 case HttpStatus.BAD_GATEWAY:
116                     this.notifierService.hideAll();
117                     this.authService.logoutResponse();
118                     break;
119                 default: return throwError(err);
120             }
121         } else { return throwError(err); }
122     }
123
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;
132             }
133         }
134     }
135 }