blob: ede10a8b2ab4d010d87b8f2a2d4bdb90391261ab [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
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 */
22import {
23 HttpErrorResponse, HttpHandler, HttpHeaderResponse, HttpInterceptor, HttpProgressEvent,
24 HttpRequest, HttpResponse, HttpSentEvent, HttpUserEvent
25} from '@angular/common/http';
26import { Injectable, Injector } from '@angular/core';
27import { TranslateService } from '@ngx-translate/core';
28import { NotifierService } from 'angular-notifier';
29import { AuthenticationService } from 'AuthenticationService';
30import * as HttpStatus from 'http-status-codes';
31import { Observable, throwError } from 'rxjs';
32import { 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()
39export 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 || HttpStatus.FORBIDDEN:
107 this.handleError(err);
108 break;
109 default: return throwError(err);
110 }
111 } else { return throwError(err); }
112 }
113
114 /** Method to handle 401 & 403 error */
115 private handleError(err: HttpErrorResponse): void {
116 if (err.error.detail === 'Expired Token or Authorization HTTP header' ||
117 err.error.detail === 'Invalid Token or Authorization HTTP header') {
118 this.notifierService.hideAll();
119 this.authService.logoutResponse();
120 if (this.authService.handle401) {
121 this.notifierService.notify('error', this.translateService.instant('SESSIONEXPIRY'));
122 this.authService.handle401 = false;
123 }
124 }
125 }
126}