Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Web GoogleSignin - GoogleIdTokenVerifier verify token string returns null

I am adding google register/signin to my web app and i have encountered a problem.

This is my code :

private static final HttpTransport transport = new NetHttpTransport();
private static final JsonFactory jsonFactory = new JacksonFactory();
private static final String MY_APP_GOOGLE_CLIENT_ID = "wouldntyouliketoknow";

public UsernamePasswordAuthenticationToken verify(final String idTokenString){

    GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
                                            .setAudience(Collections.singletonList(MY_APP_GOOGLE_CLIENT_ID))
                                            .build();

    try {
        GoogleIdToken idToken = verifier.verify(idTokenString);// <-- verifier.verify returns null !!!
        if (idToken != null) {
            Payload payload = idToken.getPayload();
            String email = payload.getEmail();
            if(Boolean.valueOf(payload.getEmailVerified())){
                UserJPA jpa = userRepository.findByEmail(email);
                if(jpa==null){
                    throw new UsernameNotFoundException("Cannot find user with email = "+email);
                }
                if(!jpa.isRegisterredWithGoogle()){
                    throw new UsernameNotFoundException("This user did not use the 'Register with google' option.");
                }
                bokiAuthenticationProvider.checkUserActiveAndUnlocked(jpa);

                return new UsernamePasswordAuthenticationToken(jpa.getUsername(), jpa.getPasswordHesh(), 
                        bokiAuthenticationProvider.getAuthorities(jpa.getUserHasRoleSecurityList()));
            }
        }else{
            System.out.println("The *idToken* object is null !!!");
        }
    } catch (GeneralSecurityException | IOException e) {
        e.printStackTrace();
    }

    throw new MyCustomException("Google token is invalid or has expired");
}

To create my CLIENT_ID I followed instructions here :

https://developers.google.com/identity/sign-in/web/devconsole-project

The problem is that verifier.verify keeps returning null.

I have checked :

  • my user did register with google and the database fields are properly filled

  • i am getting different string tokens from google each time i try google_sign_in

  • my CLIENT_ID is valid and active in the google console.

To add to the confusion, this whole thing worked fine just a month ago. I went away on sick leave and when i came back, my boss welcomed me with this issue.

Anyone have any idea what might have happened ?

like image 560
Lazaruss Avatar asked Mar 27 '17 10:03

Lazaruss


2 Answers

I finally figured it out.

Since no one knew how to help me, i dropped the designated google libraries and went and made my own token verification from scratch.

I went and used the google-token-verifier-url-tool here :

https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123

At the bottom of the page here

https://developers.google.com/identity/protocols/OpenIDConnect

, i found how to decipher the json.

What i do is, I contact their online tool in code, get the json response and verify it manually. This is my code :

    private Map<String,String> getMapFromGoogleTokenString(final String idTokenString){
        BufferedReader in = null;
        try {
            // get information from token by contacting the google_token_verify_tool url :
            in = new BufferedReader(new InputStreamReader(
                                        ((HttpURLConnection) (new URL("https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=" + idTokenString.trim()))
                                        .openConnection()).getInputStream(), Charset.forName("UTF-8")));

            // read information into a string buffer :
            StringBuffer b = new StringBuffer();
            String inputLine;
            while ((inputLine = in.readLine()) != null){
                b.append(inputLine + "\n");
            }

            // transforming json string into Map<String,String> :
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(b.toString(), objectMapper.getTypeFactory().constructMapType(Map.class, String.class, String.class));

        // exception handling :
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch(Exception e){
            System.out.println("\n\n\tFailed to transform json to string\n");
            e.printStackTrace();
        } finally{
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    // chack the "email_verified" and "email" values in token payload 
    private boolean verifyEmail(final Map<String,String> tokenPayload){
        if(tokenPayload.get("email_verified")!=null && tokenPayload.get("email")!=null){
            try{
                return Boolean.valueOf(tokenPayload.get("email_verified")) && tokenPayload.get("email").contains("@gmail.");
            }catch(Exception e){
                System.out.println("\n\n\tCheck emailVerified failed - cannot parse "+tokenPayload.get("email_verified")+" to boolean\n");
            }
        }else{
            System.out.println("\n\n\tCheck emailVerified failed - required information missing in the token");
        }
        return false;
    }

    // check token expiration is after now :
    private boolean checkExpirationTime(final Map<String,String> tokenPayload){
        try{
            if(tokenPayload.get("exp")!=null){
                // the "exp" value is in seconds and Date().getTime is in mili seconds
                return Long.parseLong(tokenPayload.get("exp")+"000") > new java.util.Date().getTime();
            }else{
                System.out.println("\n\n\tCheck expiration failed - required information missing in the token\n");
            }
        }catch(Exception e){
            System.out.println("\n\n\tCheck expiration failed - cannot parse "+tokenPayload.get("exp")+" into long\n");
        }
        return false;
    }

    // check that at least one CLIENT_ID matches with token values
    private boolean checkAudience(final Map<String,String> tokenPayload){
        if(tokenPayload.get("aud")!=null && tokenPayload.get("azp")!=null){
            List<String> pom = Arrays.asList("MY_CLIENT_ID_1",
                                             "MY_CLIENT_ID_2",
                                             "MY_CLIENT_ID_3");

            if(pom.contains(tokenPayload.get("aud")) || pom.contains(tokenPayload.get("azp"))){
                return true;
            }else{
                System.out.println("\n\n\tCheck audience failed - audiences differ\n");
                return false;
            }
        }
        System.out.println("\n\n\tCheck audience failed - required information missing in the token\n");
        return false;
    }

    // verify google token payload :
    private boolean doTokenVerification(final Map<String,String> tokenPayload){
        if(tokenPayload!=null){
            return verifyEmail(tokenPayload) // check that email address is verifies 
                && checkExpirationTime(tokenPayload) // check that token is not expired
                && checkAudience(tokenPayload) // check audience
                ; 
        }
        return false;
    }

Once i have had this detailed verification, i was able to see exactly where the mistake was ; The front end was sending me invalid CLIENT_ID value. [grumble, grumble] I have asked them about it about a hundred times and they told me that those values match. BAH !

So the mistake was not somewhere in my original code for token verification, it was an error in communication in my office.

I will revert back to the original code, with the proper CLIENT_ID this time for the sake of security. Still, i must make one complaint about the google library - it never told me why token verification was failing. It took me DAYS of pain staking effort to finally figure it out. I know they did it out of security, but still, the lack of support is galling.

like image 83
Lazaruss Avatar answered Oct 09 '22 01:10

Lazaruss


Had similar issue. It was so stupid, but I just have wrong time on my server. I think verification always fail because expiration time of token is end in my server time universe.

like image 21
Zeon Avatar answered Oct 09 '22 01:10

Zeon