Search in sources :

Example 1 with Payload

use of com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload in project data-transfer-project by google.

the class GoogleAuthDataGenerator method generateAuthData.

@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, String id, AuthData initialAuthData, String extra) {
    Preconditions.checkState(initialAuthData == null, "Earlier auth data not expected for Google flow");
    AuthorizationCodeFlow flow;
    TokenResponse response;
    try {
        flow = createFlow();
        response = flow.newTokenRequest(authCode).setRedirectUri(// TODO(chuy): Parameterize
        callbackBaseUrl + redirectPath).execute();
    } catch (IOException e) {
        throw new RuntimeException("Error calling AuthorizationCodeFlow.execute ", e);
    }
    // Figure out storage
    Credential credential = null;
    try {
        credential = flow.createAndStoreCredential(response, id);
    } catch (IOException e) {
        throw new RuntimeException("Error calling AuthorizationCodeFlow.createAndStoreCredential ", e);
    }
    // GoogleIdToken.Payload payload = ((GoogleTokenResponse) response).parseIdToken().getPayload();
    return new TokensAndUrlAuthData(credential.getAccessToken(), credential.getRefreshToken(), credential.getTokenServerEncodedUrl());
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) TokensAndUrlAuthData(org.dataportabilityproject.types.transfer.auth.TokensAndUrlAuthData) IOException(java.io.IOException) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)

Example 2 with Payload

use of com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload in project data-transfer-project by google.

the class GoogleAuth method generateAuthData.

@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, UUID jobId, @Nullable AuthData initialAuthData, @Nullable String extra) throws IOException {
    Preconditions.checkState(initialAuthData == null, "Earlier auth data not expected for Google flow");
    AuthorizationCodeFlow flow = createFlow();
    TokenResponse response = flow.newTokenRequest(authCode).setRedirectUri(// TODO(chuy): Parameterize
    callbackBaseUrl + CALLBACK_PATH).execute();
    // Figure out storage
    Credential credential = flow.createAndStoreCredential(response, jobId.toString());
    // GoogleIdToken.Payload payload = ((GoogleTokenResponse) response).parseIdToken().getPayload();
    return toAuthData(credential);
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)

Example 3 with Payload

use of com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload in project google-api-java-client by google.

the class GoogleIdTokenVerifierTest method newPayload.

private static Payload newPayload(String clientId) {
    Payload payload = new Payload();
    payload.setIssuer("accounts.google.com");
    payload.setAudience(clientId);
    payload.setAuthorizedParty(clientId);
    payload.setExpirationTimeSeconds(100L);
    payload.setIssuedAtTimeSeconds(0L);
    return payload;
}
Also used : Payload(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload)

Example 4 with Payload

use of com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload in project google-api-java-client by google.

the class GoogleIdTokenVerifierTest method testVerify.

public void testVerify() throws Exception {
    GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new GooglePublicKeysManagerTest.PublicCertsMockHttpTransport(), new JacksonFactory()).build();
    Header header = new Header();
    header.setAlgorithm("RS25");
    Payload payload = newPayload(CLIENT_ID);
    Payload payload2 = newPayload(CLIENT_ID + "2");
    GoogleIdToken idToken = new GoogleIdToken(header, payload, new byte[0], new byte[0]);
    GoogleIdToken idToken2 = new GoogleIdToken(header, payload2, new byte[0], new byte[0]);
    assertFalse(verifier.verify(idToken));
    assertFalse(verifier.verify(idToken2));
    verifier = new GoogleIdTokenVerifier(new GooglePublicKeysManagerTest.PublicCertsMockHttpTransport(), new JacksonFactory());
    assertFalse(verifier.verify(idToken));
    assertFalse(verifier.verify(idToken2));
// TODO(yanivi): add a unit test that returns true
}
Also used : Header(com.google.api.client.json.webtoken.JsonWebSignature.Header) Payload(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Example 5 with Payload

use of com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload in project google-api-java-client by google.

the class GoogleIdTokenTest method testEmailVerified.

public void testEmailVerified() {
    Payload payload = newPayload(USER_ID, CLIENT_ID);
    assertNull(payload.getEmailVerified());
    payload.setEmailVerified(true);
    assertTrue(payload.getEmailVerified());
    payload.setEmailVerified(false);
    assertFalse(payload.getEmailVerified());
    payload.setEmailVerified(null);
    assertNull(payload.getEmailVerified());
    payload.set(EMAIL_VERIFIED_KEY, "true");
    assertTrue(payload.getEmailVerified());
    payload.set(EMAIL_VERIFIED_KEY, true);
    assertTrue(payload.getEmailVerified());
    payload.set(EMAIL_VERIFIED_KEY, "false");
    assertFalse(payload.getEmailVerified());
    payload.set(EMAIL_VERIFIED_KEY, false);
    assertFalse(payload.getEmailVerified());
    payload.set(EMAIL_VERIFIED_KEY, "RandomString");
    assertFalse(payload.getEmailVerified());
    payload.set(EMAIL_VERIFIED_KEY, "");
    assertFalse(payload.getEmailVerified());
    payload.set(EMAIL_VERIFIED_KEY, null);
    assertNull(payload.getEmailVerified());
    // Wrong type.
    payload.set(EMAIL_VERIFIED_KEY, new Integer(5));
    try {
        payload.getEmailVerified();
        fail();
    } catch (ClassCastException e) {
    // Expected.
    }
}
Also used : Payload(com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload)

Aggregations

Payload (com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload)7 AuthorizationCodeFlow (com.google.api.client.auth.oauth2.AuthorizationCodeFlow)2 Credential (com.google.api.client.auth.oauth2.Credential)2 TokenResponse (com.google.api.client.auth.oauth2.TokenResponse)2 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)2 IOException (java.io.IOException)2 GoogleIdToken (com.google.api.client.googleapis.auth.oauth2.GoogleIdToken)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 Header (com.google.api.client.json.webtoken.JsonWebSignature.Header)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 GeneralSecurityException (java.security.GeneralSecurityException)1 TokensAndUrlAuthData (org.dataportabilityproject.types.transfer.auth.TokensAndUrlAuthData)1 Before (org.junit.Before)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1