use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class ServiceAccountCredentialsTest method toString_containsFields.
@Test
public void toString_containsFields() throws IOException {
final URI tokenServer = URI.create("https://foo.com/bar");
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
OAuth2Credentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, tokenServer, SERVICE_ACCOUNT_USER);
String expectedToString = String.format("ServiceAccountCredentials{clientId=%s, clientEmail=%s, privateKeyId=%s, " + "transportFactoryClassName=%s, tokenServerUri=%s, scopes=%s, serviceAccountUser=%s}", SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_ID, MockTokenServerTransportFactory.class.getName(), tokenServer, SCOPES, SERVICE_ACCOUNT_USER);
assertEquals(expectedToString, credentials.toString());
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class ServiceAccountCredentialsTest method hashCode_equals.
@Test
public void hashCode_equals() throws IOException {
final URI tokenServer = URI.create("https://foo.com/bar");
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
OAuth2Credentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, tokenServer);
OAuth2Credentials otherCredentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, tokenServer);
assertEquals(credentials.hashCode(), otherCredentials.hashCode());
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class ServiceAccountCredentialsTest method equals_false_clientId.
@Test
public void equals_false_clientId() throws IOException {
final URI tokenServer1 = URI.create("https://foo1.com/bar");
MockTokenServerTransportFactory serverTransportFactory = new MockTokenServerTransportFactory();
OAuth2Credentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, serverTransportFactory, tokenServer1);
OAuth2Credentials otherCredentials = ServiceAccountCredentials.fromPkcs8("otherClientId", SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, serverTransportFactory, tokenServer1);
assertFalse(credentials.equals(otherCredentials));
assertFalse(otherCredentials.equals(credentials));
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory 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.GoogleCredentialsTest.MockTokenServerTransportFactory 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