Search in sources :

Example 21 with UserCredentials

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

the class UserCredentialsTest method serialize.

@Test
public void serialize() throws IOException, ClassNotFoundException {
    final URI tokenServer = URI.create("https://foo.com/bar");
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null);
    UserCredentials credentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setAccessToken(accessToken).setHttpTransportFactory(transportFactory).setTokenServerUri(tokenServer).build();
    UserCredentials deserializedCredentials = serializeAndDeserialize(credentials);
    assertEquals(credentials, deserializedCredentials);
    assertEquals(credentials.hashCode(), deserializedCredentials.hashCode());
    assertEquals(credentials.toString(), deserializedCredentials.toString());
    assertSame(deserializedCredentials.clock, Clock.SYSTEM);
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) URI(java.net.URI) Test(org.junit.Test)

Example 22 with UserCredentials

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

the class UserCredentialsTest method toString_containsFields.

@Test
public void toString_containsFields() throws IOException {
    AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null);
    final URI tokenServer = URI.create("https://foo.com/bar");
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    UserCredentials credentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setAccessToken(accessToken).setHttpTransportFactory(transportFactory).setTokenServerUri(tokenServer).build();
    String expectedToString = String.format("UserCredentials{requestMetadata=%s, temporaryAccess=%s, clientId=%s, refreshToken=%s, " + "tokenServerUri=%s, transportFactoryClassName=%s}", ImmutableMap.of(AuthHttpConstants.AUTHORIZATION, ImmutableList.of(OAuth2Utils.BEARER_PREFIX + accessToken.getTokenValue())), accessToken.toString(), CLIENT_ID, REFRESH_TOKEN, tokenServer, MockTokenServerTransportFactory.class.getName());
    assertEquals(expectedToString, credentials.toString());
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) URI(java.net.URI) Test(org.junit.Test)

Example 23 with UserCredentials

use of com.google.auth.oauth2.UserCredentials 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 24 with UserCredentials

use of com.google.auth.oauth2.UserCredentials 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

Test (org.junit.Test)21 MockTokenServerTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory)17 URI (java.net.URI)10 List (java.util.List)7 ImmutableList (com.google.common.collect.ImmutableList)6 MockHttpTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory)5 TestClock (com.google.auth.TestClock)3 UserCredentials (com.google.auth.oauth2.UserCredentials)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 JSONObject (org.json.JSONObject)2 SharedPreferences (android.content.SharedPreferences)1 Editor (android.content.SharedPreferences.Editor)1 AudioDeviceInfo (android.media.AudioDeviceInfo)1 Handler (android.os.Handler)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 ListView (android.widget.ListView)1 ConversationCallback (com.example.androidthings.assistant.EmbeddedAssistant.ConversationCallback)1 RequestCallback (com.example.androidthings.assistant.EmbeddedAssistant.RequestCallback)1