import { Injectable } from '@angular/core'; import { ConfigurationService } from './config.service'; import { Authentication } from './configuration'; import { Http, Headers, Response } from '@angular/http'; import { Observable } from 'rxjs/Rx'; import { Output, EventEmitter } from '@angular/core'; import { JwtHelper } from 'angular2-jwt'; const OAUTH_REFRESH_URL = '/oauth/refresh'; const OAUTH_GETTOKEN_URL = '/oauth/get_auth_token'; const PSERVER_AUTHCODE_URL = '/@oauthgetcode'; const ATLASENSE_LOGIN_URL = '@login'; const ATLASENSE_REFRESH_URL = '@refresh'; @Injectable() export class AuthService { timerRefreshToken: any; auth: Authentication; jwtHelper: JwtHelper = new JwtHelper(); @Output() loggedin = new EventEmitter(); constructor( public http: Http, public config: ConfigurationService) { let local_auth = localStorage.getItem('atlasense_auth'); if (local_auth) { this.auth = JSON.parse(local_auth); } else { this.auth = new Authentication(); } this.loggedin.subscribe( data => this.saveUserToken(data.data, data.refresh) ); } save_auth() { localStorage.setItem('atlasense_auth', JSON.stringify(this.auth)); } // Oauth functions for atlasense.oauth login_oauth(user, password) { let endpoint = this.config.getCanonicalURL() + PSERVER_AUTHCODE_URL + '?client_id=' + this.config.config.client_id + '&scope=' + this.config.config.account; let headers = new Headers(); headers.append('Content-Type', 'application/json'); this.http.options( endpoint, {headers: headers} ).subscribe( res => this.call_real_oauth(res, user, password), err => console.log(err) ); } call_real_oauth(response, user, password) { let endpoint = this.config.getAuthURL() + OAUTH_GETTOKEN_URL; let body = JSON.stringify( { "grant_type": 'user', "code": response.json().auth_code, "username":user, "password": password, "scopes": [this.config.config.account], "client_id": this.config.config.client_id } ); let headers = new Headers(); headers.append('Content-Type', 'application/json'); let refresh = this.auth.oauth + OAUTH_REFRESH_URL; this.http.post( endpoint, body, {headers: headers} ).subscribe( res => this.saveUserToken(res, refresh), err => console.log(err) ); } // User token management saveUserToken(res, refresh) { this.auth.jwt = res; let decoded = this.jwtHelper.decodeToken(res._body); this.auth.token = decoded.token; this.auth.username = decoded.login; let expiration = this.jwtHelper.getTokenExpirationDate(res._body).getTime(); let now = new Date().getTime(); let timeout = expiration - now - 3600000; console.log('Refresh again in ' + timeout); this.save_auth(); this.timerRefreshToken = Observable.timer(timeout); this.timerRefreshToken.subscribe( x => this.refreshToken(refresh) ); } refreshToken(endpoint) { let headers = new Headers(); headers.append('Content-Type', 'application/json'); let body = JSON.stringify( { 'token': this.auth.token, 'user': this.auth.username, 'cliend_id': this.config.config.client_id } ); this.http.post( endpoint, body, {headers: headers} ).subscribe( res => this.saveUserToken(res, endpoint), err => console.log(err) ); } }