use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class UserCredentialsTest method getRequestMetadata_initialToken_hasAccessToken.
@Test
public void getRequestMetadata_initialToken_hasAccessToken() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null);
UserCredentials userCredentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setAccessToken(accessToken).setHttpTransportFactory(transportFactory).build();
Map<String, List<String>> metadata = userCredentials.getRequestMetadata(CALL_URI);
TestUtils.assertContainsBearerToken(metadata, ACCESS_TOKEN);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class HttpCredentialsAdapterTest method initialize_noURI.
@Test
public void initialize_noURI() throws IOException {
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String expectedAuthorization = InternalAuthHttpConstants.BEARER_PREFIX + accessToken;
MockTokenServerTransportFactory tokenServerTransportFactory = new MockTokenServerTransportFactory();
tokenServerTransportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
tokenServerTransportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken);
OAuth2Credentials credentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setHttpTransportFactory(tokenServerTransportFactory).build();
HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(credentials);
HttpRequestFactory requestFactory = tokenServerTransportFactory.transport.createRequestFactory();
HttpRequest request = requestFactory.buildGetRequest(null);
adapter.initialize(request);
HttpHeaders requestHeaders = request.getHeaders();
String authorizationHeader = requestHeaders.getAuthorization();
assertEquals(authorizationHeader, expectedAuthorization);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class DefaultCredentialsProviderTest method getDefaultCredentials_envServiceAccount_providesToken.
@Test
public void getDefaultCredentials_envServiceAccount_providesToken() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addServiceAccount(SA_CLIENT_EMAIL, ACCESS_TOKEN);
InputStream serviceAccountStream = ServiceAccountCredentialsTest.writeServiceAccountStream(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID);
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
String serviceAccountPath = "/service_account.json";
testProvider.addFile(serviceAccountPath, serviceAccountStream);
testProvider.setEnv(DefaultCredentialsProvider.CREDENTIAL_ENV_VAR, serviceAccountPath);
GoogleCredentials defaultCredentials = testProvider.getDefaultCredentials(transportFactory);
assertNotNull(defaultCredentials);
defaultCredentials = defaultCredentials.createScoped(SCOPES);
Map<String, List<String>> metadata = defaultCredentials.getRequestMetadata(CALL_URI);
TestUtils.assertContainsBearerToken(metadata, ACCESS_TOKEN);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class OAuth2CredentialsTest method addChangeListener_notifiesOnRefresh.
@Test
public void addChangeListener_notifiesOnRefresh() 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);
Map<String, List<String>> metadata;
assertEquals(0, listener.callCount);
// Get a first token
metadata = userCredentials.getRequestMetadata(CALL_URI);
TestUtils.assertContainsBearerToken(metadata, accessToken1);
assertEquals(accessToken1, listener.accessToken.getTokenValue());
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();
metadata = userCredentials.getRequestMetadata(CALL_URI);
TestUtils.assertContainsBearerToken(metadata, accessToken2);
assertEquals(accessToken2, listener.accessToken.getTokenValue());
assertEquals(2, listener.callCount);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class OAuth2CredentialsTest method getRequestMetadata_async_refreshRace.
@Test
public void getRequestMetadata_async_refreshRace() throws IOException {
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken1);
TestClock clock = new TestClock();
OAuth2Credentials credentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setHttpTransportFactory(transportFactory).build();
credentials.clock = clock;
MockExecutor executor = new MockExecutor();
MockRequestMetadataCallback callback = new MockRequestMetadataCallback();
// Getting the first token, which uses the transport and calls the callback in the executor.
credentials.getRequestMetadata(CALL_URI, executor, callback);
assertEquals(0, transportFactory.transport.buildRequestCount);
assertNull(callback.metadata);
// Asynchronous task is scheduled, but beaten by another blocking get call.
assertEquals(1, executor.numTasks());
Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI);
assertEquals(1, transportFactory.transport.buildRequestCount--);
TestUtils.assertContainsBearerToken(metadata, accessToken1);
// When the task is run, the cached data is used.
assertEquals(1, executor.runTasksExhaustively());
assertEquals(0, transportFactory.transport.buildRequestCount);
assertSame(metadata, callback.metadata);
}
Aggregations