package cz.esc.iot.cloudservice.oauth2;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;

import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;

public class Checker {

    private final List mClientIDs;
    private final String mAudience;
    private final GoogleIdTokenVerifier mVerifier;
    private final JsonFactory mJFactory;
    private String mProblem = "Verification failed. (Time-out?)";

    public Checker(String[] clientIDs, String audience) {
        mClientIDs = Arrays.asList(clientIDs);
        mAudience = audience;
        NetHttpTransport transport = new NetHttpTransport();
        mJFactory = new GsonFactory();
        mVerifier = new GoogleIdTokenVerifier(transport, mJFactory);
    }

    public GoogleIdToken.Payload check(String tokenString) {
        GoogleIdToken.Payload payload = null;
        System.out.println("a");
        try {
            GoogleIdToken token = GoogleIdToken.parse(mJFactory, tokenString);
            System.out.println("b: " + token);
            if (mVerifier.verify(token)) {
                GoogleIdToken.Payload tempPayload = token.getPayload();
                System.out.println("c: " + tempPayload);
                if (!tempPayload.getAudience().equals(mAudience)) {
                    mProblem = "Audience mismatch";
                	System.out.println("d");
                //else if (!mClientIDs.contains(tempPayload.getAuthorizedParty()))
                //    mProblem = "Client ID mismatch";
                } else
                    payload = tempPayload;
            }
        } catch (GeneralSecurityException e) {
            mProblem = "Security issue: " + e.getLocalizedMessage();
        } catch (IOException e) {
            mProblem = "Network problem: " + e.getLocalizedMessage();
        }
        return payload;
    }

    public String problem() {
        return mProblem;
    }
}
@@ -300,16 +300,93 @@
 rifier;%0A
+import com.google.api.client.googleapis.auth.oauth2.GooglePublicKeysManager;%0A
 import c
@@ -724,16 +724,72 @@
 actory;%0A
+    //private final GooglePublicKeysManager keyManager;%0A
     priv
@@ -1159,80 +1159,276 @@
     
-%7D%0A%0A    public GoogleIdToken.Payload check(String tokenString) %7B%0A     
+    //keyManager = new GooglePublicKeysManager(transport, mJFactory);%0A    %7D%0A%0A    public GoogleIdToken.Payload check(String tokenString) %7B%0A    %09/*try %7B%0A%09%09%09keyManager.refresh();%0A%09%09%7D catch (GeneralSecurityException %7C IOException e1) %7B%0A%09%09%09e1.printStackTrace();%0A%09%09%7D*/%0A
    
+ %09
 Goog
@@ -1587,16 +1587,140 @@
 tring);%0A
+            mVerifier.getPublicKeysManager().refresh();%0A            System.out.println(mVerifier.verify(token.toString()));%0A
         
@@ -2108,18 +2108,18 @@
         
-//
+%7D 
 else if 
@@ -2174,16 +2174,18 @@
 arty()))
+ %7B
 %0A       
@@ -2193,18 +2193,16 @@
         
-//
     mPro

refresh public keys manager