Search in sources :

Example 46 with AccessToken

use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project terra-workspace-manager by DataBiosphere.

the class ClientTestUtils method getClientForTestRunnerSA.

/**
 * Build the Workspace Manager Service API client object for the given server specification. It is
 * setting the access token for the Test Runner SA specified in the given server specification.
 *
 * <p>A Test Runner SA is a GCP SA with appropriate permissions / scopes to run all the client
 * tests within this repo. For example, to run resiliency tests against K8S infrastructure, you'll
 * need a SA powerful enough to do things like list, read, and update.
 *
 * @param server the server we are testing against
 * @return the API client object
 */
public static ApiClient getClientForTestRunnerSA(ServerSpecification server) throws IOException {
    if (server.testRunnerServiceAccount == null) {
        throw new IllegalArgumentException("Workspace Manager Service client service account is required");
    }
    // refresh the client service account token
    GoogleCredentials serviceAccountCredential = AuthenticationUtils.getServiceAccountCredential(server.testRunnerServiceAccount, AuthenticationUtils.userLoginScopes);
    AccessToken accessToken = AuthenticationUtils.getAccessToken(serviceAccountCredential);
    logger.debug("Generated access token for workspace manager service client SA: {}", server.testRunnerServiceAccount.name);
    return buildClient(accessToken, server);
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials)

Example 47 with AccessToken

use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project terra-workspace-manager by DataBiosphere.

the class ClientTestUtils method getClientForTestUser.

/**
 * Build the Workspace Manager API client object for the given test user and server
 * specifications. The test user's token is always refreshed
 *
 * @param testUser the test user whose credentials are supplied to the API client object
 * @param server the server we are testing against
 * @return the API client object for this user
 */
public static ApiClient getClientForTestUser(TestUserSpecification testUser, ServerSpecification server) throws IOException {
    AccessToken accessToken = null;
    // this is useful if the caller wants to make ONLY unauthenticated calls
    if (testUser != null) {
        logger.debug("Fetching credentials and building Workspace Manager ApiClient object for test user: {}", testUser.name);
        GoogleCredentials userCredential = AuthenticationUtils.getDelegatedUserCredential(testUser, TEST_USER_SCOPES);
        accessToken = AuthenticationUtils.getAccessToken(userCredential);
    }
    return buildClient(accessToken, server);
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials)

Example 48 with AccessToken

use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project terra-workspace-manager by DataBiosphere.

the class SamClientUtils method getSamApiClient.

private static ApiClient getSamApiClient(TestUserSpecification testUser, ServerSpecification server) throws Exception {
    AccessToken accessToken = null;
    // this is useful if the caller wants to make ONLY unauthenticated calls
    if (testUser != null) {
        logger.debug("Fetching credentials and building Sam ApiClient object for test user: {}", testUser.name);
        GoogleCredentials userCredential = AuthenticationUtils.getDelegatedUserCredential(testUser, ClientTestUtils.TEST_USER_SCOPES);
        accessToken = AuthenticationUtils.getAccessToken(userCredential);
    }
    return buildSamClient(accessToken, server);
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials)

Example 49 with AccessToken

use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project java-bigtable by googleapis.

the class BigtableChannelPrimerTest method testErrorsAreLoggedForBasic.

@Test
public void testErrorsAreLoggedForBasic() {
    BigtableChannelPrimer basicPrimer = BigtableChannelPrimer.create(OAuth2Credentials.create(new AccessToken(TOKEN_VALUE, null)), "fake-project", "fake-instance", "fake-app-profile", ImmutableList.<String>of());
    ManagedChannel channel = Mockito.mock(ManagedChannel.class, new ThrowsException(new UnsupportedOperationException()));
    primer.primeChannel(channel);
    assertThat(logHandler.logs).hasSize(1);
    for (LogRecord log : logHandler.logs) {
        assertThat(log.getMessage()).contains("Unexpected");
    }
}
Also used : ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) LogRecord(java.util.logging.LogRecord) AccessToken(com.google.auth.oauth2.AccessToken) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Example 50 with AccessToken

use of com.microsoft.identity.common.internal.providers.oauth2.AccessToken in project hadoop-connectors by GoogleCloudDataproc.

the class HadoopCredentialsConfigurationTest method userCredentials_credentialFactory_noNewRefreshToken.

@Test
public void userCredentials_credentialFactory_noNewRefreshToken() throws IOException {
    // GIVEN
    String initialRefreshToken = "FAKE_REFRESH_TOKEN";
    String tokenServerUrl = "http://localhost/token";
    configuration.set(getConfigKey(TOKEN_SERVER_URL_SUFFIX), tokenServerUrl);
    configuration.setEnum(getConfigKey(AUTHENTICATION_TYPE_SUFFIX), AuthenticationType.USER_CREDENTIALS);
    configuration.set(getConfigKey(AUTH_REFRESH_TOKEN_SUFFIX), initialRefreshToken);
    configuration.set(getConfigKey(AUTH_CLIENT_ID_SUFFIX), "FAKE_CLIENT_ID");
    configuration.set(getConfigKey(AUTH_CLIENT_SECRET_SUFFIX), "FAKE_CLIENT_SECRET");
    long expireInSec = 300L;
    String accessTokenAsString = "SlAV32hkKG";
    TokenResponse tokenResponse = new TokenResponse().setAccessToken(accessTokenAsString).setExpiresInSeconds(expireInSec);
    MockHttpTransport transport = mockTransport(jsonDataResponse(tokenResponse));
    // WHEN
    GoogleCredentials credentials = getCredentials(transport);
    credentials.refresh();
    // THEN
    assertThat(credentials).isInstanceOf(UserCredentials.class);
    UserCredentials userCredentials = (UserCredentials) credentials;
    assertThat(userCredentials.getClientId()).isEqualTo("FAKE_CLIENT_ID");
    assertThat(userCredentials.getClientSecret()).isEqualTo("FAKE_CLIENT_SECRET");
    AccessToken accessToken = userCredentials.getAccessToken();
    assertThat(accessToken).isNotNull();
    // To avoid any timebase issue, we test a time range instead
    assertThat(accessToken.getExpirationTime()).isGreaterThan(Date.from(Instant.now().plusSeconds(expireInSec - 10)));
    assertThat(accessToken.getExpirationTime()).isLessThan(Date.from(Instant.now().plusSeconds(expireInSec + 10)));
    String refreshToken = userCredentials.getRefreshToken();
    assertThat(refreshToken).isEqualTo(initialRefreshToken);
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) TokenResponse(com.google.api.client.auth.oauth2.TokenResponse) AccessToken(com.google.auth.oauth2.AccessToken) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) UserCredentials(com.google.auth.oauth2.UserCredentials) Test(org.junit.Test)

Aggregations

AccessToken (com.google.auth.oauth2.AccessToken)78 Test (org.junit.Test)44 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)33 Date (java.util.Date)23 IOException (java.io.IOException)20 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)16 Instant (java.time.Instant)10 Client (javax.ws.rs.client.Client)10 AccessToken (org.glassfish.jersey.client.oauth1.AccessToken)10 ConsumerCredentials (org.glassfish.jersey.client.oauth1.ConsumerCredentials)10 JsonObject (io.vertx.core.json.JsonObject)9 URI (java.net.URI)9 Feature (javax.ws.rs.core.Feature)8 JerseyTest (org.glassfish.jersey.test.JerseyTest)8 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)6 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)6 Credential (io.cdap.cdap.proto.security.Credential)6 InputStreamReader (java.io.InputStreamReader)6 Clock (java.time.Clock)6 WebTarget (javax.ws.rs.client.WebTarget)6