lundi 27 mars 2017

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 :

http://ift.tt/1B6jRDo

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 ?




Aucun commentaire:

Enregistrer un commentaire