Search in sources :

Example 6 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 testCreateScopesNotSet.

public void testCreateScopesNotSet() throws Exception {
    final String serviceAccountEmail = "36680232662-vrd7ji19q3ne0ah2csanun6bnr@developer.gserviceaccount.com";
    final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
    MockTokenServerTransport transport = new MockTokenServerTransport();
    transport.addServiceAccount(serviceAccountEmail, accessToken);
    GoogleCredential credential = new GoogleCredential.Builder().setServiceAccountId(serviceAccountEmail).setServiceAccountPrivateKey(SecurityTestUtils.newRsaPrivateKey()).setTransport(transport).setJsonFactory(JSON_FACTORY).build();
    // Note that setServiceAccountScopes() is not called, so it is uninitialized (i.e. null) on the
    // builder.
    assertTrue(credential.getServiceAccountScopes().isEmpty());
}
Also used : MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Example 7 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 testCreateScoped.

public void testCreateScoped() throws Exception {
    final String serviceAccountEmail = "36680232662-vrd7ji19q3ne0ah2csanun6bnr@developer.gserviceaccount.com";
    final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
    MockTokenServerTransport transport = new MockTokenServerTransport();
    transport.addServiceAccount(serviceAccountEmail, accessToken);
    GoogleCredential credential = new GoogleCredential.Builder().setServiceAccountId(serviceAccountEmail).setServiceAccountScopes(EMPTY_SCOPES).setServiceAccountPrivateKey(SecurityTestUtils.newRsaPrivateKey()).setTransport(transport).setJsonFactory(JSON_FACTORY).build();
    assertTrue(credential.createScopedRequired());
    try {
        credential.refreshToken();
        fail("Should not be able to refresh token without scopes.");
    } catch (Exception expected) {
    }
    GoogleCredential scopedCredential = credential.createScoped(SCOPES);
    assertFalse(scopedCredential.createScopedRequired());
    assertNotSame(credential, scopedCredential);
    assertTrue(scopedCredential.refreshToken());
    assertEquals(accessToken, scopedCredential.getAccessToken());
    assertSame(credential.getTransport(), scopedCredential.getTransport());
    assertSame(credential.getJsonFactory(), scopedCredential.getJsonFactory());
    assertSame(credential.getServiceAccountId(), scopedCredential.getServiceAccountId());
    assertSame(credential.getServiceAccountUser(), scopedCredential.getServiceAccountUser());
    assertSame(credential.getServiceAccountPrivateKey(), scopedCredential.getServiceAccountPrivateKey());
}
Also used : IOException(java.io.IOException) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Example 8 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 testFromStreamServiceAccount.

public void testFromStreamServiceAccount() throws IOException {
    final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
    final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com";
    final String serviceAccountEmail = "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com";
    MockTokenServerTransport transport = new MockTokenServerTransport();
    transport.addServiceAccount(serviceAccountEmail, accessToken);
    // 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", SA_KEY_TEXT);
    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());
    GoogleCredential defaultCredential = GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY);
    assertNotNull(defaultCredential);
    defaultCredential = defaultCredential.createScoped(SCOPES);
    assertTrue(defaultCredential.refreshToken());
    assertEquals(accessToken, defaultCredential.getAccessToken());
}
Also used : GenericJson(com.google.api.client.json.GenericJson) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Example 9 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 testDefaultCredentialServiceAccount.

public void testDefaultCredentialServiceAccount() throws IOException {
    File serviceAccountFile = new java.io.File(getTempDirectory(), "DefaultCredentialServiceAccount.json");
    if (serviceAccountFile.exists()) {
        serviceAccountFile.delete();
    }
    final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com";
    final String serviceAccountEmail = "36680232662-vrd7ji19qe3nelgchdcsanun6bnr@developer.gserviceaccount.com";
    MockTokenServerTransport transport = new MockTokenServerTransport();
    transport.addServiceAccount(serviceAccountEmail, ACCESS_TOKEN);
    TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();
    try {
        // Write out service account file
        GenericJson serviceAccountContents = new GenericJson();
        serviceAccountContents.setFactory(JSON_FACTORY);
        serviceAccountContents.put("client_id", serviceAccountId);
        serviceAccountContents.put("client_email", serviceAccountEmail);
        serviceAccountContents.put("private_key", SA_KEY_TEXT);
        serviceAccountContents.put("private_key_id", SA_KEY_ID);
        serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE);
        PrintWriter writer = new PrintWriter(serviceAccountFile);
        String json = serviceAccountContents.toPrettyString();
        writer.println(json);
        writer.close();
        // Point the default credential to the file
        testProvider.setEnv(DefaultCredentialProvider.CREDENTIAL_ENV_VAR, serviceAccountFile.getAbsolutePath());
        GoogleCredential credential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
        assertNotNull(credential);
        credential = credential.createScoped(SCOPES);
        assertTrue(credential.refreshToken());
        assertEquals(ACCESS_TOKEN, credential.getAccessToken());
    } finally {
        if (serviceAccountFile.exists()) {
            serviceAccountFile.delete();
        }
    }
}
Also used : GenericJson(com.google.api.client.json.GenericJson) File(java.io.File) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport) PrintWriter(java.io.PrintWriter)

Example 10 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 testDefaultCredentialEnvironmentVariableWinsOverWellKnownFile.

public void testDefaultCredentialEnvironmentVariableWinsOverWellKnownFile() throws IOException {
    final String clientSecret = "jakuaL9YyieakhECKL2SwZcu";
    final String clientId = "ya29.1.AADtN_UtlxH8cruGAxrN2XQnZTVRvDyVWnYq4I6dws";
    final String refreshTokenEnv = "2/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
    final String accessTokenEnv = "2/MkSJoj1xsli0AccessToken_NKPY2";
    final String refreshTokenWkf = "3/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
    final String accessTokenWkf = "3/MkSJoj1xsli0AccessToken_NKPY2";
    TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();
    // Set up an environment variable file
    File environmentVariableFile = new java.io.File(getTempDirectory(), "EnvVarUser.json");
    if (environmentVariableFile.exists()) {
        environmentVariableFile.delete();
    }
    testProvider.setEnv(DefaultCredentialProvider.CREDENTIAL_ENV_VAR, environmentVariableFile.getAbsolutePath());
    // Also set up a well-known-location file
    File homeDir = getTempDirectory();
    File configDir = new File(homeDir, ".config");
    if (!configDir.exists()) {
        configDir.mkdir();
    }
    File cloudConfigDir = new File(configDir, DefaultCredentialProvider.CLOUDSDK_CONFIG_DIRECTORY);
    if (!cloudConfigDir.exists()) {
        cloudConfigDir.mkdir();
    }
    File wellKnownFile = new File(cloudConfigDir, DefaultCredentialProvider.WELL_KNOWN_CREDENTIALS_FILE);
    if (wellKnownFile.exists()) {
        wellKnownFile.delete();
    }
    testProvider.addFile(wellKnownFile.getAbsolutePath());
    testProvider.setProperty("os.name", "linux");
    testProvider.setProperty("user.home", homeDir.getAbsolutePath());
    // Define a transport that can simulate refreshing tokens
    MockTokenServerTransport transport = new MockTokenServerTransport();
    transport.addClient(clientId, clientSecret);
    transport.addRefreshToken(refreshTokenEnv, accessTokenEnv);
    transport.addRefreshToken(refreshTokenWkf, accessTokenWkf);
    String jsonEnv = GoogleCredentialTest.createUserJson(clientId, clientSecret, refreshTokenEnv);
    String jsonWkf = GoogleCredentialTest.createUserJson(clientId, clientSecret, refreshTokenWkf);
    try {
        // Write out user files
        PrintWriter writer = new PrintWriter(environmentVariableFile);
        writer.println(jsonEnv);
        writer.close();
        writer = new PrintWriter(wellKnownFile);
        writer.println(jsonWkf);
        writer.close();
        Credential credential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
        assertNotNull(credential);
        assertEquals(refreshTokenEnv, credential.getRefreshToken());
        assertTrue(credential.refreshToken());
        assertEquals(accessTokenEnv, credential.getAccessToken());
    } finally {
        if (wellKnownFile.exists()) {
            wellKnownFile.delete();
        }
        if (environmentVariableFile.exists()) {
            environmentVariableFile.delete();
        }
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) File(java.io.File) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport) PrintWriter(java.io.PrintWriter)

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