Search in sources :

Example 1 with MockTokenServerTransport

use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.

the class DefaultCredentialProviderTest method testDefaultCredentialUser.

private void testDefaultCredentialUser(File userFile, TestDefaultCredentialProvider testProvider) throws IOException {
    final String clientSecret = "jakuaL9YyieakhECKL2SwZcu";
    final String clientId = "ya29.1.AADtN_UtlxH8cruGAxrN2XQnZTVRvDyVWnYq4I6dws";
    final String refreshToken = "1/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
    // Define a transport that can simulate refreshing tokens
    MockTokenServerTransport transport = new MockTokenServerTransport();
    transport.addClient(clientId, clientSecret);
    transport.addRefreshToken(refreshToken, ACCESS_TOKEN);
    String json = GoogleCredentialTest.createUserJson(clientId, clientSecret, refreshToken);
    try {
        // Write out user file
        PrintWriter writer = new PrintWriter(userFile);
        writer.println(json);
        writer.close();
        Credential credential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
        assertNotNull(credential);
        assertEquals(refreshToken, credential.getRefreshToken());
        assertTrue(credential.refreshToken());
        assertEquals(ACCESS_TOKEN, credential.getAccessToken());
    } finally {
        if (userFile.exists()) {
            userFile.delete();
        }
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport) PrintWriter(java.io.PrintWriter)

Example 2 with MockTokenServerTransport

use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.

the class GoogleCredentialTest method testFromStreamUser.

public void testFromStreamUser() throws IOException {
    final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
    final String clientSecret = "jakuaL9YyieakhECKL2SwZcu";
    final String clientId = "ya29.1.AADtN_UtlxN3PSc5yhCqfA9nDFp1dfvH8cruGAxrN2XQnZTVRvDyVWnYq4I6dws";
    final String refreshToken = "1/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
    MockTokenServerTransport transport = new MockTokenServerTransport();
    transport.addClient(clientId, clientSecret);
    transport.addRefreshToken(refreshToken, accessToken);
    // Create user stream.
    String json = createUserJson(clientId, clientSecret, refreshToken);
    InputStream userStream = new ByteArrayInputStream(json.getBytes());
    GoogleCredential defaultCredential = GoogleCredential.fromStream(userStream, transport, JSON_FACTORY);
    assertNotNull(defaultCredential);
    assertEquals(refreshToken, defaultCredential.getRefreshToken());
    assertTrue(defaultCredential.refreshToken());
    assertEquals(accessToken, defaultCredential.getAccessToken());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Example 3 with MockTokenServerTransport

use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.

the class GoogleCredentialTest method testFromStreamUsertMissingClientIdThrows.

public void testFromStreamUsertMissingClientIdThrows() throws IOException {
    final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
    final String clientSecret = "jakuaL9YyieakhECKL2SwZcu";
    final String clientId = "ya29.1.AADtN_UtlxN3PSruGAxrN2XQnZTVRvDyVWnYq4I6dws";
    final String refreshToken = "1/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
    MockTokenServerTransport transport = new MockTokenServerTransport();
    transport.addClient(clientId, clientSecret);
    transport.addRefreshToken(refreshToken, accessToken);
    // Write out user file
    String json = createUserJson(null, clientSecret, refreshToken);
    InputStream userStream = new ByteArrayInputStream(json.getBytes());
    try {
        GoogleCredential.fromStream(userStream, transport, JSON_FACTORY);
        fail();
    } catch (IOException expected) {
        assertTrue(expected.getMessage().contains("client_id"));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Example 4 with MockTokenServerTransport

use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.

the class GoogleCredentialTest method testFromStreamServiceAccountMissingPrivateKeyThrows.

public void testFromStreamServiceAccountMissingPrivateKeyThrows() throws IOException {
    final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com";
    final String serviceAccountEmail = "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com";
    MockHttpTransport transport = new MockTokenServerTransport();
    // Write out user file
    GenericJson serviceAccountContents = new GenericJson();
    serviceAccountContents.setFactory(JSON_FACTORY);
    serviceAccountContents.put("client_id", serviceAccountId);
    serviceAccountContents.put("client_email", serviceAccountEmail);
    serviceAccountContents.put("private_key_id", SA_KEY_ID);
    serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE);
    String json = serviceAccountContents.toPrettyString();
    InputStream serviceAccountStream = new ByteArrayInputStream(json.getBytes());
    try {
        GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY);
        fail();
    } catch (IOException expected) {
        assertTrue(expected.getMessage().contains("private_key"));
    }
}
Also used : GenericJson(com.google.api.client.json.GenericJson) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Example 5 with MockTokenServerTransport

use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.

the class GoogleCredentialTest method testRefreshToken_User.

public void testRefreshToken_User() throws Exception {
    final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
    final String clientSecret = "jakuaL9YyieakhECKL2SwZcu";
    final String clientId = "ya29.1.AADtN_UtlxN3PuGAxrN2XQnZTVRvDyVWnYq4I6dws";
    final String refreshToken = "1/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
    MockTokenServerTransport transport = new MockTokenServerTransport();
    transport.addClient(clientId, clientSecret);
    transport.addRefreshToken(refreshToken, accessToken);
    GoogleCredential credential = new GoogleCredential.Builder().setClientSecrets(clientId, clientSecret).setTransport(transport).setJsonFactory(JSON_FACTORY).build();
    credential.setRefreshToken(refreshToken);
    assertTrue(credential.refreshToken());
    assertEquals(accessToken, credential.getAccessToken());
}
Also used : MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Aggregations

MockTokenServerTransport (com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)17 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)10 IOException (java.io.IOException)8 GenericJson (com.google.api.client.json.GenericJson)7 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 PrintWriter (java.io.PrintWriter)3 Credential (com.google.api.client.auth.oauth2.Credential)2 File (java.io.File)2