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