use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory 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.GoogleCredentialsTest.MockTokenServerTransportFactory 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.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class DefaultCredentialsProviderTest method getDefaultCredentials_envAndWellKnownFile_envPrecedence.
@Test
public void getDefaultCredentials_envAndWellKnownFile_envPrecedence() throws IOException {
final String refreshTokenEnv = "2/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
final String accessTokenEnv = "2/MkSJoj1xsli0AccessToken_NKPY2";
final String refreshTokenWkf = "3/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
final String accessTokenWkf = "3/MkSJoj1xsli0AccessToken_NKPY2";
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
InputStream envStream = UserCredentialsTest.writeUserStream(USER_CLIENT_ID, USER_CLIENT_SECRET, refreshTokenEnv);
String envPath = "/env.json";
testProvider.setEnv(DefaultCredentialsProvider.CREDENTIAL_ENV_VAR, envPath);
testProvider.addFile(envPath, envStream);
File homeDir = getTempDirectory();
File configDir = new File(homeDir, ".config");
File cloudConfigDir = new File(configDir, DefaultCredentialsProvider.CLOUDSDK_CONFIG_DIRECTORY);
InputStream wkfStream = UserCredentialsTest.writeUserStream(USER_CLIENT_ID, USER_CLIENT_SECRET, refreshTokenWkf);
File wellKnownFile = new File(cloudConfigDir, DefaultCredentialsProvider.WELL_KNOWN_CREDENTIALS_FILE);
testProvider.setProperty("os.name", "linux");
testProvider.setProperty("user.home", homeDir.getAbsolutePath());
testProvider.addFile(wellKnownFile.getAbsolutePath(), wkfStream);
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(USER_CLIENT_ID, USER_CLIENT_SECRET);
transportFactory.transport.addRefreshToken(refreshTokenWkf, accessTokenWkf);
transportFactory.transport.addRefreshToken(refreshTokenEnv, accessTokenEnv);
testUserProvidesToken(testProvider, transportFactory, accessTokenEnv);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class DefaultCredentialsProviderTest method testUserProvidesToken.
private void testUserProvidesToken(TestDefaultCredentialsProvider testProvider, String clientId, String clientSecret, String refreshToken) throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(clientId, clientSecret);
transportFactory.transport.addRefreshToken(refreshToken, ACCESS_TOKEN);
testUserProvidesToken(testProvider, transportFactory, ACCESS_TOKEN);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory 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--);
}
Aggregations