BUG 1221:Reset of NG-UI service leaves unusable UI
[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         // 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);
80                 })
81             );
82         } else {
83             //TODO: Handle error via notification service
84         }
85     }
86
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' }
93             });
94         } else {
95             this.clonedReq = req.clone({
96                 setHeaders: { Authorization: 'Bearer ' + idToken, 'Content-Type': 'charset=UTF-8',
97                 'Cache-Control': 'no-cache', Pragma: 'no-cache' }
98             });
99         }
100     }
101
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);
109                     break;
110                 case HttpStatus.GATEWAY_TIMEOUT:
111                 case HttpStatus.BAD_GATEWAY:
112                     this.notifierService.hideAll();
113                     this.authService.logoutResponse();
114                     break;
115                 default: return throwError(err);
116             }
117         } else { return throwError(err); }
118     }
119
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;
128             }
129         }
130     }
131 }