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();
}
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());
}
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());
}
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--);
}
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);
}
Aggregations