Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / services / authentication.service.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import {Injectable, Inject} from '@angular/core';
22 import {IAppConfigurtaion, ICookie} from "../../models/app-config";
23 import {Cookie2Service} from "./cookie.service";
24 import { Observable } from 'rxjs/Observable';
25 import {SdcConfigToken, ISdcConfig} from "../config/sdc-config.config";
26 import { IUserProperties } from "app/models";
27 import { CacheService } from "app/ng2/services/cache.service";
28 import { HttpClient, HttpHeaders } from '@angular/common/http';
29
30 @Injectable()
31 export class AuthenticationService {
32
33     private _loggedinUser:IUserProperties;
34     
35     constructor(private cookieService:Cookie2Service, private http: HttpClient, @Inject(SdcConfigToken) private sdcConfig:ISdcConfig, private cacheService: CacheService) {
36         this.cookieService = cookieService;
37     }
38
39     private getAuthHeaders():HttpHeaders {
40         let cookie:ICookie = this.sdcConfig.cookie;
41         let authHeaders: HttpHeaders = new HttpHeaders();
42         authHeaders = authHeaders.set(cookie.userFirstName, this.cookieService.getFirstName())
43         .set(cookie.userLastName, this.cookieService.getLastName())
44         .set(cookie.userEmail, this.cookieService.getEmail())
45         .set(cookie.userIdSuffix, this.cookieService.getUserId())
46         return authHeaders;
47     }
48
49     public authenticate(): Observable<IUserProperties> {
50         let authUrl = this.sdcConfig.api.root + this.sdcConfig.api.GET_user_authorize;
51         return this.http.get<IUserProperties>(authUrl, {headers: this.getAuthHeaders()});
52     }
53
54     public getLoggedinUser():IUserProperties {
55         return this._loggedinUser;
56     }
57
58     public setLoggedinUser(loggedinUser:IUserProperties) {
59         this._loggedinUser = loggedinUser;
60         this.cacheService.set('user', loggedinUser);
61     };
62
63 }