/* Copyright (c) 2014 Pathways Community Network Institute
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package org.openhmis.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.net.ssl.HttpsURLConnection;

import org.openhmis.exception.AuthenticationFailureException;

import com.google.api.client.auth.oauth2.TokenResponseException;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;

public class Authentication {

	private static final HttpTransport TRANSPORT = new NetHttpTransport();
	private static final JacksonFactory JSON_FACTORY = new JacksonFactory();
	private static final ApplicationPropertyUtil applicationPropertyUtil = new ApplicationPropertyUtil();
	private static final String CLIENT_ID = applicationPropertyUtil.getGoogleClientId();
	private static final String CLIENT_SECRET = applicationPropertyUtil.getGoogleSecret();
	
	private Authentication() {}

	public static String getGoogleToken(String code) {
		try {
			// Upgrade the authorization code into an access and refresh token.
			GoogleTokenResponse tokenResponse =
				new GoogleAuthorizationCodeTokenRequest(TRANSPORT, JSON_FACTORY,
					CLIENT_ID, CLIENT_SECRET, code, "postmessage").execute();

			return tokenResponse.toString();

		} catch (TokenResponseException e) {
			throw new AuthenticationFailureException();
			// return "Token Fail" + e.getMessage();
			// Failed to upgrade the authorization code.
		} catch (IOException e) {
			// TODO: this should be a different exception
			throw new AuthenticationFailureException();
			//	return "Read Google Data Fail";
			// Failed to read token data from Google.
		}
	}

	public static Boolean googleAuthenticate(String tokenString) {
		return true;
		 // if(tokenString == null)
		 // 	return false;
		
		 // try {
		 // 	// Verify that the token is a legitimate google token
		 // 	GoogleIdToken token = GoogleIdToken.parse(JSON_FACTORY, tokenString);
		 // 	GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier(TRANSPORT, JSON_FACTORY);
		 // 	verifier.verify(token);
		 // 	return true;
		 // } catch (IOException e) {
		 // 	return false;
		 // } catch (GeneralSecurityException e) {
		 // 	return false;
		 // }
	}

}

@@ -2699,34 +2699,17 @@
 ng) %7B%0A%09%09
-return true;%0A%09%09 //
+ 
  if(toke
@@ -2724,26 +2724,24 @@
 == null)%0A%09%09 
-//
  %09return fal
@@ -2750,18 +2750,16 @@
 ;%0A%09%09%0A%09%09 
-//
  try %7B%0A%09
@@ -2760,18 +2760,16 @@
 ry %7B%0A%09%09 
-//
  %09// Ver
@@ -2815,26 +2815,24 @@
 le token%0A%09%09 
-//
  %09GoogleIdTo
@@ -2894,18 +2894,16 @@
 ng);%0A%09%09 
-//
  %09Google
@@ -2984,18 +2984,16 @@
 RY);%0A%09%09 
-//
  %09verifi
@@ -3009,26 +3009,24 @@
 (token);%0A%09%09 
-//
  %09return tru
@@ -3027,26 +3027,24 @@
 rn true;%0A%09%09 
-//
  %7D catch (IO
@@ -3053,34 +3053,32 @@
 ception e) %7B%0A%09%09 
-//
  %09return false;%0A
@@ -3080,18 +3080,16 @@
 lse;%0A%09%09 
-//
  %7D catch
@@ -3123,18 +3123,16 @@
 e) %7B%0A%09%09 
-//
  %09return
@@ -3142,18 +3142,16 @@
 lse;%0A%09%09 
-//
  %7D%0A%09%7D%0A%0A%7D

Return auth check removed in be5d7f0