blob: d01eec6aa510786ae11fe2ad0477779d97c08dbb [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 * @file Auth service
20 */
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +053021import { isNullOrUndefined } from 'util';
kumaran.m3b4814a2020-05-01 19:48:54 +053022import { HttpHeaders } from '@angular/common/http';
23import { Injectable, Injector } from '@angular/core';
24import { Router } from '@angular/router';
25import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
26import { Idle } from '@ng-idle/core';
27import { APIURLHEADER, ERRORDATA } from 'CommonModel';
28import { environment } from 'environment';
29import { BehaviorSubject, Observable } from 'rxjs';
30import { map } from 'rxjs/operators';
SANDHYA.JS1b17c432023-04-26 17:54:57 +053031import { SharedService } from 'SharedService';
kumaran.m3b4814a2020-05-01 19:48:54 +053032import { ProjectModel } from '../models/VNFDModel';
33import { RestService } from './RestService';
34
35/**
36 * An Injectable is a class adorned with the @Injectable decorator function.
37 * @Injectable takes a metadata object that tells Angular how to compile and run module code
38 */
39@Injectable()
40export class AuthenticationService {
kumaran.m3b4814a2020-05-01 19:48:54 +053041 /** To inject services @public */
42 public injector: Injector;
43
44 /** Instance for modal service @public */
45 public modalService: NgbModal;
46
47 /** Handle 401 response for multiple API calls */
48 public handle401: boolean = true;
49
50 /** contains return URL link @public */
51 public returnUrl: string;
52
53 /** Holds the username in condition of type BehaviorSubject<string> @public */
54 public userName: BehaviorSubject<string> = new BehaviorSubject<string>('');
55
56 /** Holds the projectname in condition of type BehaviorSubject<string> @public */
57 public projectName$: BehaviorSubject<string> = new BehaviorSubject<string>('');
58
59 /** Holds the instance of router class @private */
60 private router: Router;
61
62 /** Holds the logged in condition of type BehaviorSubject<boolean> @private */
63 private loggedIn: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
64
SANDHYA.JSa9816552022-04-12 09:07:08 +053065 /** Holds the change password in condition of type BehaviorSubject<boolean> @private */
66 private changePassword: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
67
kumaran.m3b4814a2020-05-01 19:48:54 +053068 /** Hold Rest Service Objects */
69 private restService: RestService;
70
71 /** Holds auth payloads @private */
72 private payLoad: {};
73
74 /** Holds header options for auth service @private */
75 private httpOptions: HttpHeaders;
76
77 /** handle idle time out service @private */
78 private idle: Idle;
79
SANDHYA.JS1b17c432023-04-26 17:54:57 +053080 /** Contains all methods related to shared @private */
81 private sharedService: SharedService;
82
kumaran.m3b4814a2020-05-01 19:48:54 +053083 /** create the instance of the component */
84 constructor(injector: Injector) {
85 this.injector = injector;
86 this.router = this.injector.get(Router);
87 this.restService = this.injector.get(RestService);
88 this.modalService = this.injector.get(NgbModal);
89 this.idle = this.injector.get(Idle);
SANDHYA.JS1b17c432023-04-26 17:54:57 +053090 this.sharedService = this.injector.get(SharedService);
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +053091 if (sessionStorage.getItem('username') !== null) {
kumaran.m3b4814a2020-05-01 19:48:54 +053092 this.loggedIn.next(true);
SANDHYA.JSa9816552022-04-12 09:07:08 +053093 this.changePassword.next(false);
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +053094 } else if (sessionStorage.getItem('firstLogin') !== null) {
SANDHYA.JSa9816552022-04-12 09:07:08 +053095 this.changePassword.next(true);
96 this.loggedIn.next(false);
kumaran.m3b4814a2020-05-01 19:48:54 +053097 } else {
98 this.loggedIn.next(false);
99 }
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530100 this.userName.next(sessionStorage.getItem('username'));
SANDHYA.JSa9816552022-04-12 09:07:08 +0530101 this.redirectToPage();
102 }
103
104 /**
105 * Get method for Observable loggedIn
106 */
107 get isLoggedIn(): Observable<boolean> {
108 return this.loggedIn.asObservable();
109 }
110
111 /**
112 * Get method for Observable changepassword
113 */
114 get isChangePassword(): Observable<boolean> {
115 return this.changePassword.asObservable();
116 }
117
118 /**
119 * Get method for Observable Username
120 */
121 get username(): Observable<string> {
122 return this.userName.asObservable();
123 }
124
125 /** Get method for project name */
126 get ProjectName(): Observable<string> {
127 return this.projectName$.asObservable();
kumaran.m3b4814a2020-05-01 19:48:54 +0530128 }
129
130 /**
131 * Send request and authenticate the user
132 * @param user of type User
133 */
134 public login(username: string, password: string): Observable<{}> {
135 this.setHeader();
136 this.setPayLoad(username, password);
137 const apiURLHeader: APIURLHEADER = {
138 url: environment.GENERATETOKEN_URL,
139 httpOptions: { headers: this.httpOptions }
140 };
141 return this.restService.postResource(apiURLHeader, this.payLoad)
SANDHYA.JSa9816552022-04-12 09:07:08 +0530142 .pipe(map((data: ProjectModel): BehaviorSubject<boolean> => {
143 if (data.message === 'change_password') {
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530144 sessionStorage.setItem('firstLogin', 'true');
145 sessionStorage.setItem('id_token', data.id);
146 sessionStorage.setItem('user_id', data.user_id);
SANDHYA.JSa9816552022-04-12 09:07:08 +0530147 this.idle.watch(true);
148 this.changePassword.next(true);
149 this.loggedIn.next(false);
150 return this.changePassword;
151 } else {
kumaran.m3b4814a2020-05-01 19:48:54 +0530152 this.setLocalStorage(data);
153 this.idle.watch(true);
154 this.loggedIn.next(true);
155 this.handle401 = true;
156 this.userName.next(data.username);
157 return this.loggedIn;
158 }
SANDHYA.JSa9816552022-04-12 09:07:08 +0530159 }, (error: ERRORDATA): void => { this.restService.handleError(error, 'post'); }
kumaran.m3b4814a2020-05-01 19:48:54 +0530160 ));
161 }
162
163 /** Set headers for auth session @public */
164 public setHeader(): void {
165 this.httpOptions = new HttpHeaders({
166 'Content-Type': 'application/json; charset=UTF-8',
167 Accept: 'application/json',
168 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
169 });
170 }
171
172 /** Set payloads for auth session @public */
173 public setPayLoad(username: string, password: string): void {
174 this.payLoad = JSON.stringify({
175 username,
176 password
177 });
178 }
179
180 /** set local storage on auth process @public */
181 public setLocalStorage(data: ProjectModel): void {
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530182 sessionStorage.setItem('id_token', data.id);
183 sessionStorage.setItem('expires', data.expires.toString());
184 sessionStorage.setItem('username', data.username);
185 sessionStorage.setItem('isAdmin', (data.admin) ? 'true' : 'false');
186 sessionStorage.setItem('project_id', data.project_id);
187 sessionStorage.setItem('project', data.project_name);
188 sessionStorage.setItem('token_state', data.id);
189 sessionStorage.setItem('user_id', data.user_id);
190 sessionStorage.setItem('user_show', String(data.user_show));
191 sessionStorage.setItem('admin_show', String(data.admin_show));
192 sessionStorage.setItem('last_login', this.sharedService.convertEpochTime(!isNullOrUndefined(data.last_login) ? data.last_login : null));
193 sessionStorage.setItem('failed_count', data.login_count);
kumaran.m3b4814a2020-05-01 19:48:54 +0530194 this.projectName$.next(data.project_name);
195 }
196 /** Destory tokens API response handling @public */
197 public logoutResponse(): void {
198 this.loggedIn.next(false);
SANDHYA.JSa9816552022-04-12 09:07:08 +0530199 this.changePassword.next(false);
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530200 const langCode: string = sessionStorage.getItem('languageCode');
201 const redirecturl: string = isNullOrUndefined(sessionStorage.getItem('returnUrl')) ? '/' : sessionStorage.getItem('returnUrl');
SANDHYA.JS995c6722024-03-08 12:14:11 +0530202 const osmVersion: string = isNullOrUndefined(sessionStorage.getItem('version')) ? '' : sessionStorage.getItem('version');
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530203 sessionStorage.clear();
204 sessionStorage.setItem('languageCode', langCode);
205 sessionStorage.setItem('returnUrl', redirecturl);
206 sessionStorage.setItem('token_state', null);
207 sessionStorage.setItem('osmVersion', osmVersion);
kumaran.m3b4814a2020-05-01 19:48:54 +0530208 this.idle.stop();
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530209 this.router.navigate(['login']).catch((): void => {
210 // Catch Navigation Error
211 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530212 }
213 /**
214 * Logout the user & clearing the token.
215 */
216 public logout(): void {
217 this.returnUrl = this.router.url;
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530218 sessionStorage.setItem('returnUrl', this.returnUrl);
kumaran.m3b4814a2020-05-01 19:48:54 +0530219 this.modalService.dismissAll();
220 this.destoryToken();
221 }
SANDHYA.JSa9816552022-04-12 09:07:08 +0530222 /** Destory tokens on logout @public */
223 public destoryToken(): void {
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530224 const tokenID: string = sessionStorage.getItem('id_token');
kumaran.m3b4814a2020-05-01 19:48:54 +0530225 if (tokenID !== null) {
226 const deletingURl: string = environment.GENERATETOKEN_URL + '/' + tokenID;
SANDHYA.JSa9816552022-04-12 09:07:08 +0530227 this.restService.deleteResource(deletingURl).subscribe((res: {}): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530228 this.logoutResponse();
SANDHYA.JSa9816552022-04-12 09:07:08 +0530229 }, (error: ERRORDATA): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530230 this.restService.handleError(error, 'delete');
231 });
232 }
233 }
SANDHYA.JSa9816552022-04-12 09:07:08 +0530234
235 /** Return to previous page deny access to changepassword */
236 public redirectToPage(): void {
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530237 if (window.location.pathname === '/changepassword' && sessionStorage.getItem('username') !== null) {
SANDHYA.JSa9816552022-04-12 09:07:08 +0530238 window.history.back();
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530239 } else if (window.location.pathname === '/' && sessionStorage.getItem('firstLogin') === 'true') {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530240 this.router.navigate(['/login']).catch((): void => {
241 // Catch Navigation Error
242 });
SANDHYA.JSa9816552022-04-12 09:07:08 +0530243 }
244 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530245}