ca.bitcoco.jsk.google.GoogleClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsk-starter Show documentation
Show all versions of jsk-starter Show documentation
Common service for bitcoco usage
package ca.bitcoco.jsk.google;
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.jackson2.JacksonFactory;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class GoogleClient {
// Set up the HTTP transport and JSON factory
private String GOOGLE_CLIENT_ID;
public GoogleClient(String GOOGLE_CLIENT_ID) {
this.GOOGLE_CLIENT_ID = GOOGLE_CLIENT_ID;
}
private static final JacksonFactory jacksonFactory = new JacksonFactory();
public Map authToken(String idTokenString) throws Exception {
try {
Map response = new HashMap();
GoogleIdTokenVerifier GoogleIdTokenVerifier = new GoogleIdTokenVerifier.Builder(new NetHttpTransport(), jacksonFactory)
.setAudience(Collections.singletonList(GOOGLE_CLIENT_ID))
// .setIssuer("https://accounts.google.com")
.build();
GoogleIdToken idToken = GoogleIdTokenVerifier.verify(idTokenString);
if (idToken != null) {
GoogleIdToken.Payload payload = idToken.getPayload();
// Print user identifier
String authId = payload.getSubject();
// Get profile information from payload
boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
String email = payload.getEmail();
String name = (String) payload.get("name");
String pictureUrl = (String) payload.get("picture");
String locale = (String) payload.get("locale");
String familyName = (String) payload.get("family_name");
String givenName = (String) payload.get("given_name");
response.put("openId", authId);
response.put("email", email);
response.put("name", name);
response.put("pictureUrl", pictureUrl);
response.put("locale", locale);
response.put("lastName", familyName);
response.put("firstName", givenName);
return response;
} else {
return null;
}
} catch (Exception e) {
throw new Exception("Google auth failed, " + e.getLocalizedMessage());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy