import { AppStorageService } from './../common/app-storage.service'; import { Injectable } from '@angular/core'; import { Http, Response, Headers, RequestOptions } from '@angular/http'; import { ActivatedRoute, Params } from '@angular/router'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; import { UserService } from './user.service'; import { SessionStorageService } from './../common/session-storage.service'; import { Authentication } from './../../models/Authentication'; import { environment } from './../../environments/environment'; import { AppConstants } from './../../common/AppConstants'; @Injectable() export class AuthService { private oAuthCallbackUrl: string; private tokenUrl: string; private authenticated = false; private token: string; private expires: any = 0; private clientId: string; private user: UserService; private state: string; private authorizeUrl: string; private logoutUrl: string; constructor(private http: Http, user: UserService, private sessionStorageService: SessionStorageService, private activatedRoute: ActivatedRoute, private appStorageService: AppStorageService) { this.user = user; this.sessionStorageService = sessionStorageService; this.activatedRoute = activatedRoute; this.activatedRoute.queryParams.subscribe((params: Params) => { this.state = params['state']; }); const config = environment.authConfig; this.oAuthCallbackUrl = config.callbackUrl; this.clientId = config.clientId; this.tokenUrl = config.tokenUrl; this.authorizeUrl = config.authorizeUrl; this.logoutUrl = config.logoutUrl; } /** * Login to application **/ public login(authenticationData: any) { const auth = { accessToken: authenticationData.access_token, subscriptionInfo: authenticationData.subscription_info, userId: authenticationData.subscription_info.UID, userName: authenticationData.subscription_info.LogonID, license: authenticationData.subscription_info.AuthorizationEntryList[0].Licenses, company: authenticationData.subscription_info.CustomerID, eulaAcceptance: this.appStorageService.get(AppConstants.ISB_EULA) }; console.log('Session Storage ', this.sessionStorageService); // if (auth.accessToken && (!storagekey || (storagekey && storagekey === this.state))) { if (auth.accessToken) { this.user.setAuthenticated({ accessToken: auth.accessToken, license: auth.license, userId: auth.userId, userName: auth.userName, company: auth.company, eulaAcceptance: auth.eulaAcceptance }); console.log('Auth Login: ', auth.eulaAcceptance); return true; } return false; } public logout() { var accessToken = ''; if (this.user && this.user.authentication) { accessToken = this.user.authentication.accessToken; } const lang = this.appStorageService.get(AppConstants.LOCALE); const url = this.logoutUrl.replace('__lang__', lang).replace('__clientId__', this.clientId).replace('__accessToken__', accessToken); // invalidate authentication on client this.user.invalidateAuthentication(); this.user.removeOptions(); this.appStorageService.remove(AppConstants.ReturnUrl); this.appStorageService.remove(AppConstants.LOCALE); if (environment.envName !== 'test') { location.assign(url); } } public getAuthorizeUrl(lang: string, state: string): string { const url = this.authorizeUrl.replace('__lang__', lang).replace('__clientId__', this.clientId) .replace('__callbackUrl__', this.oAuthCallbackUrl).replace('__state__', state); return url; } public getToken(code: string, state: string): Observable { if (environment.envName !== 'test') { const headers = new Headers(); headers.append('Content-Type', 'application/x-www-form-urlencoded'); const options = new RequestOptions({ headers: headers }); const data = 'redirect_uri=' + this.oAuthCallbackUrl + '&grant_type=authorization_code&client_id=' + this.clientId + '&code=' + code; return this.http.post(this.tokenUrl, data, options) .map((res: Response) => res.json()) // ...and calling .json() on the response to return data .catch((error: any) => Observable.throw(error ? error || 'unable-to-get-security-token' : 'unable-to-get-security-token')); // ...errors if any } else { return this.http.get('assets/auth-response.json') .map((res: Response) => res.json()) // ...and calling .json() on the response to return data .catch((error: any) => Observable.throw(error ? error || 'unable-to-get-security-token' : 'unable-to-get-security-token')); // ...errors if any } } }