Search in sources :

Example 46 with MockTokenServerTransportFactory

use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.

the class ServiceAccountCredentialsTest method toString_containsFields.

@Test
public void toString_containsFields() throws IOException {
    final URI tokenServer = URI.create("https://foo.com/bar");
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    OAuth2Credentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, tokenServer, SERVICE_ACCOUNT_USER);
    String expectedToString = String.format("ServiceAccountCredentials{clientId=%s, clientEmail=%s, privateKeyId=%s, " + "transportFactoryClassName=%s, tokenServerUri=%s, scopes=%s, serviceAccountUser=%s}", SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_ID, MockTokenServerTransportFactory.class.getName(), tokenServer, SCOPES, SERVICE_ACCOUNT_USER);
    assertEquals(expectedToString, credentials.toString());
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) URI(java.net.URI) Test(org.junit.Test)

Example 47 with MockTokenServerTransportFactory

use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.

the class ServiceAccountCredentialsTest method hashCode_equals.

@Test
public void hashCode_equals() throws IOException {
    final URI tokenServer = URI.create("https://foo.com/bar");
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    OAuth2Credentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, tokenServer);
    OAuth2Credentials otherCredentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, tokenServer);
    assertEquals(credentials.hashCode(), otherCredentials.hashCode());
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) URI(java.net.URI) Test(org.junit.Test)

Example 48 with MockTokenServerTransportFactory

use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.

the class ServiceAccountCredentialsTest method equals_false_clientId.

@Test
public void equals_false_clientId() throws IOException {
    final URI tokenServer1 = URI.create("https://foo1.com/bar");
    MockTokenServerTransportFactory serverTransportFactory = new MockTokenServerTransportFactory();
    OAuth2Credentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, serverTransportFactory, tokenServer1);
    OAuth2Credentials otherCredentials = ServiceAccountCredentials.fromPkcs8("otherClientId", SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, serverTransportFactory, tokenServer1);
    assertFalse(credentials.equals(otherCredentials));
    assertFalse(otherCredentials.equals(credentials));
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) URI(java.net.URI) Test(org.junit.Test)

Example 49 with MockTokenServerTransportFactory

use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.

the class UserAuthorizerTest method revokeAuthorization_revokesAndClears.

@Test
public void revokeAuthorization_revokesAndClears() throws IOException {
    TokenStore tokenStore = new MemoryTokensStorage();
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET);
    transportFactory.transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN_VALUE);
    UserCredentials initialCredentials = UserCredentials.newBuilder().setClientId(CLIENT_ID_VALUE).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setAccessToken(ACCESS_TOKEN).build();
    UserAuthorizer authorizer = UserAuthorizer.newBuilder().setClientId(CLIENT_ID).setScopes(SCOPES).setTokenStore(tokenStore).setHttpTransportFactory(transportFactory).build();
    authorizer.storeCredentials(USER_ID, initialCredentials);
    UserCredentials credentials1 = authorizer.getCredentials(USER_ID);
    assertEquals(REFRESH_TOKEN, credentials1.getRefreshToken());
    credentials1.refresh();
    assertEquals(ACCESS_TOKEN_VALUE, credentials1.getAccessToken().getTokenValue());
    authorizer.revokeAuthorization(USER_ID);
    try {
        credentials1.refresh();
        fail("Credentials should not refresh after revoke.");
    } catch (IOException expected) {
    // Expected
    }
    UserCredentials credentials2 = authorizer.getCredentials(USER_ID);
    assertNull(credentials2);
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) IOException(java.io.IOException) Test(org.junit.Test)

Example 50 with MockTokenServerTransportFactory

use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.

the class UserAuthorizerTest method getCredentials_refreshedToken_stored.

@Test
public void getCredentials_refreshedToken_stored() throws IOException {
    final String accessTokenValue1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
    final String accessTokenValue2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
    AccessToken accessToken1 = new AccessToken(accessTokenValue1, new Date(EXPIRATION_TIME));
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET);
    transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessTokenValue2);
    TokenStore tokenStore = new MemoryTokensStorage();
    UserAuthorizer authorizer = UserAuthorizer.newBuilder().setClientId(CLIENT_ID).setScopes(SCOPES).setTokenStore(tokenStore).setHttpTransportFactory(transportFactory).build();
    UserCredentials originalCredentials = UserCredentials.newBuilder().setClientId(CLIENT_ID_VALUE).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setAccessToken(accessToken1).setHttpTransportFactory(transportFactory).build();
    authorizer.storeCredentials(USER_ID, originalCredentials);
    UserCredentials credentials1 = authorizer.getCredentials(USER_ID);
    assertEquals(REFRESH_TOKEN, credentials1.getRefreshToken());
    assertEquals(accessTokenValue1, credentials1.getAccessToken().getTokenValue());
    // Refresh the token to get update from token server
    credentials1.refresh();
    assertEquals(REFRESH_TOKEN, credentials1.getRefreshToken());
    assertEquals(accessTokenValue2, credentials1.getAccessToken().getTokenValue());
    // Load a second credentials instance
    UserCredentials credentials2 = authorizer.getCredentials(USER_ID);
    // Verify that token refresh stored the updated tokens
    assertEquals(REFRESH_TOKEN, credentials2.getRefreshToken());
    assertEquals(accessTokenValue2, credentials2.getAccessToken().getTokenValue());
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) Date(java.util.Date) Test(org.junit.Test)

Aggregations

MockTokenServerTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory)50 Test (org.junit.Test)49 URI (java.net.URI)17 List (java.util.List)15 ImmutableList (com.google.common.collect.ImmutableList)9 TestClock (com.google.auth.TestClock)6 IOException (java.io.IOException)6 GenericJson (com.google.api.client.json.GenericJson)4 InputStream (java.io.InputStream)4 HttpRequest (com.google.api.client.http.HttpRequest)3 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)3 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)3 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)3 GenericUrl (com.google.api.client.http.GenericUrl)2 HttpHeaders (com.google.api.client.http.HttpHeaders)2 MockHttpTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HttpResponse (com.google.api.client.http.HttpResponse)1 HttpTransport (com.google.api.client.http.HttpTransport)1 FixedClock (com.google.api.client.testing.http.FixedClock)1