2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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';
31 export class AuthenticationService {
33 private _loggedinUser:IUserProperties;
35 constructor(private cookieService:Cookie2Service, private http: HttpClient, @Inject(SdcConfigToken) private sdcConfig:ISdcConfig, private cacheService: CacheService) {
36 this.cookieService = cookieService;
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())
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()});
54 public getLoggedinUser():IUserProperties {
55 return this._loggedinUser;
58 public setLoggedinUser(loggedinUser:IUserProperties) {
59 this._loggedinUser = loggedinUser;
60 this.cacheService.set('user', loggedinUser);