Search in sources :

Example 1 with GoogleIdToken

use of com.google.api.client.googleapis.auth.oauth2.GoogleIdToken in project mobile-android by photo.

the class AccountTroveboxApiTest method testSignInViaGoogle.

public void testSignInViaGoogle() throws ClientProtocolException, IllegalStateException, IOException, JSONException, UserRecoverableAuthException, GoogleAuthException, GeneralSecurityException {
    // how to setup environment
    // http://android-developers.blogspot.in/2013/01/verifying-back-end-calls-from-android.html
    String[] names = getAccountNames();
    assertTrue(names != null && names.length > 0);
    String accountName = names[0];
    String audience = CommonUtils.getStringResource(R.string.google_auth_server_client_id);
    String SCOPE = "audience:server:client_id:" + audience;
    String tokenString = GoogleAuthUtil.getToken(getContext(), accountName, SCOPE);
    // token verification part, this should be done on server side
    GoogleIdTokenVerifier mVerifier;
    JsonFactory mJFactory;
    NetHttpTransport transport = new NetHttpTransport();
    mJFactory = new GsonFactory();
    mVerifier = new GoogleIdTokenVerifier(transport, mJFactory);
    GoogleIdToken token = GoogleIdToken.parse(mJFactory, tokenString);
    assertTrue(mVerifier.verify(token));
    GoogleIdToken.Payload tempPayload = token.getPayload();
    assertTrue(tempPayload.getAudience().equals(audience));
    assertNotNull(tempPayload.getEmail());
    // end of token verification part
    AccountTroveboxResponse response = mApi.signInViaGoogle(tokenString);
    assertNotNull(response);
    assertTrue(response.isSuccess());
    Credentials[] credentials = response.getCredentials();
    assertNotNull(credentials);
    assertTrue(credentials.length > 0);
    Credentials c = credentials[0];
    checkoAuthString(c.getoAuthConsumerKey());
    checkoAuthString(c.getoAuthConsumerSecret());
    checkoAuthString(c.getoAuthToken());
    checkoAuthString(c.getoAuthConsumerSecret());
}
Also used : GsonFactory(com.google.api.client.json.gson.GsonFactory) AccountTroveboxResponse(com.trovebox.android.app.net.account.AccountTroveboxResponse) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleIdTokenVerifier(com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier) JsonFactory(com.google.api.client.json.JsonFactory) GoogleIdToken(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken) Credentials(com.trovebox.android.common.model.Credentials)

Example 2 with GoogleIdToken

use of com.google.api.client.googleapis.auth.oauth2.GoogleIdToken in project pratilipi by Pratilipi.

the class GoogleApi method getUserData.

public static UserData getUserData(String googleIdToken) throws InvalidArgumentException, UnexpectedServerException {
    try {
        GoogleIdToken idToken = UxModeFilter.isAndroidApp() ? getAndroidIdTokenVerifier().verify(googleIdToken) : getWebIdTokenVerifier().verify(googleIdToken);
        String authorisedParty = UxModeFilter.isAndroidApp() ? getAppClientId() : getWebClientId();
        if (idToken == null || idToken.getPayload() == null || !idToken.getPayload().getAuthorizedParty().equals(authorisedParty)) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("googleIdToken", "Invalid GoogleIdToken !");
            throw new InvalidArgumentException(jsonObject);
        }
        Payload payload = idToken.getPayload();
        logger.log(Level.INFO, "GoogleApi Payload : " + new Gson().toJson(payload));
        if (payload.get("given_name") == null || ((String) payload.get("given_name")).isEmpty()) {
            logger.log(Level.SEVERE, "Google given_name is missing for GoogleUser: " + payload.getSubject());
            throw new UnexpectedServerException();
        }
        UserData userData = new UserData();
        userData.setGoogleId(payload.getSubject());
        userData.setFirstName((String) payload.get("given_name"));
        userData.setLastName((String) payload.get("family_name"));
        userData.setEmail(payload.getEmail());
        return userData;
    } catch (GeneralSecurityException | IOException e) {
        logger.log(Level.SEVERE, "Google id token verification failed: " + e);
        throw new UnexpectedServerException();
    }
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) UserData(com.pratilipi.data.client.UserData) GeneralSecurityException(java.security.GeneralSecurityException) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Payload(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload) IOException(java.io.IOException) GoogleIdToken(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken)

Aggregations

GoogleIdToken (com.google.api.client.googleapis.auth.oauth2.GoogleIdToken)2 Payload (com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload)1 GoogleIdTokenVerifier (com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1 GsonFactory (com.google.api.client.json.gson.GsonFactory)1 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 UserData (com.pratilipi.data.client.UserData)1 AccountTroveboxResponse (com.trovebox.android.app.net.account.AccountTroveboxResponse)1 Credentials (com.trovebox.android.common.model.Credentials)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1