Search in sources :

Example 1 with UserCredentials

use of com.google.auth.oauth2.UserCredentials in project google-cloud-intellij by GoogleCloudPlatform.

the class DefaultGoogleApiClientFactory method getCloudStorageApiClient.

@Override
public Storage getCloudStorageApiClient(@NotNull String projectId, @NotNull CredentialedUser credentialedUser) {
    GoogleLoginState loginState = credentialedUser.getGoogleLoginState();
    String clientId = loginState.fetchOAuth2ClientId();
    String clientSecret = loginState.fetchOAuth2ClientSecret();
    String refreshToken = loginState.fetchOAuth2RefreshToken();
    return StorageOptions.newBuilder().setProjectId(projectId).setCredentials(new UserCredentials(clientId, clientSecret, refreshToken)).build().getService();
}
Also used : GoogleLoginState(com.google.gdt.eclipse.login.common.GoogleLoginState) UserCredentials(com.google.auth.oauth2.UserCredentials)

Example 2 with UserCredentials

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

the class UserAuthorizerTest method getCredentialsFromCode_conevertsCodeToTokens.

@Test
public void getCredentialsFromCode_conevertsCodeToTokens() throws IOException {
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET);
    transportFactory.transport.addAuthorizationCode(CODE, REFRESH_TOKEN, ACCESS_TOKEN_VALUE);
    TokenStore tokenStore = new MemoryTokensStorage();
    UserAuthorizer authorizer = UserAuthorizer.newBuilder().setClientId(CLIENT_ID).setScopes(SCOPES).setTokenStore(tokenStore).setHttpTransportFactory(transportFactory).build();
    UserCredentials credentials = authorizer.getCredentialsFromCode(CODE, BASE_URI);
    assertEquals(REFRESH_TOKEN, credentials.getRefreshToken());
    assertEquals(ACCESS_TOKEN_VALUE, credentials.getAccessToken().getTokenValue());
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) Test(org.junit.Test)

Example 3 with UserCredentials

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

the class UserAuthorizerTest method getAndStoreCredentialsFromCode_getAndStoresCredentials.

@Test
public void getAndStoreCredentialsFromCode_getAndStoresCredentials() throws IOException {
    final String accessTokenValue1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
    final String accessTokenValue2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    transportFactory.transport.addClient(CLIENT_ID_VALUE, CLIENT_SECRET);
    transportFactory.transport.addAuthorizationCode(CODE, REFRESH_TOKEN, accessTokenValue1);
    TokenStore tokenStore = new MemoryTokensStorage();
    UserAuthorizer authorizer = UserAuthorizer.newBuilder().setClientId(CLIENT_ID).setScopes(SCOPES).setTokenStore(tokenStore).setHttpTransportFactory(transportFactory).build();
    UserCredentials credentials1 = authorizer.getAndStoreCredentialsFromCode(USER_ID, CODE, BASE_URI);
    assertEquals(REFRESH_TOKEN, credentials1.getRefreshToken());
    assertEquals(accessTokenValue1, credentials1.getAccessToken().getTokenValue());
    // Refresh the token to get update from token server
    transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessTokenValue2);
    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) Test(org.junit.Test)

Example 4 with UserCredentials

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

the class OAuth2CredentialsTest method refresh_refreshesToken.

@Test
public void refresh_refreshesToken() throws IOException {
    final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
    final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
    transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken1);
    OAuth2Credentials userCredentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setHttpTransportFactory(transportFactory).build();
    // Use a fixed clock so tokens don't exire
    userCredentials.clock = new TestClock();
    // Get a first token
    Map<String, List<String>> metadata = userCredentials.getRequestMetadata(CALL_URI);
    TestUtils.assertContainsBearerToken(metadata, accessToken1);
    assertEquals(1, transportFactory.transport.buildRequestCount--);
    // Change server to a different token
    transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken2);
    // Confirm token being cached
    TestUtils.assertContainsBearerToken(metadata, accessToken1);
    assertEquals(0, transportFactory.transport.buildRequestCount);
    // Refresh to force getting next token
    userCredentials.refresh();
    metadata = userCredentials.getRequestMetadata(CALL_URI);
    TestUtils.assertContainsBearerToken(metadata, accessToken2);
    assertEquals(1, transportFactory.transport.buildRequestCount--);
}
Also used : TestClock(com.google.auth.TestClock) MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test)

Example 5 with UserCredentials

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

the class OAuth2CredentialsTest method removeChangeListener_unregisters_observer.

@Test
public void removeChangeListener_unregisters_observer() throws IOException {
    final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
    final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
    transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken1);
    OAuth2Credentials userCredentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setHttpTransportFactory(transportFactory).build();
    // Use a fixed clock so tokens don't expire
    userCredentials.clock = new TestClock();
    TestChangeListener listener = new TestChangeListener();
    userCredentials.addChangeListener(listener);
    assertEquals(0, listener.callCount);
    // Get a first token
    userCredentials.getRequestMetadata(CALL_URI);
    assertEquals(1, listener.callCount);
    // Change server to a different token and refresh
    transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken2);
    // Refresh to force getting next token
    userCredentials.refresh();
    assertEquals(2, listener.callCount);
    // Remove the listener and refresh the credential again
    userCredentials.removeChangeListener(listener);
    transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken2);
    userCredentials.refresh();
    assertEquals(2, listener.callCount);
}
Also used : TestClock(com.google.auth.TestClock) MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) 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