Search in sources :

Example 36 with AccessToken

use of org.glassfish.jersey.client.oauth1.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 37 with AccessToken

use of org.glassfish.jersey.client.oauth1.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 38 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project airy by airyhq.

the class Api method sendMessage.

public void sendMessage(final String conversationId, JsonNode sendMessagePayload) throws Exception {
    String reqUrl = String.format(requestTemplate, conversationId);
    final byte[] serializedServiceAccount = objectMapper.writeValueAsString(serviceAccount).getBytes();
    GoogleCredentials credentials = GoogleCredentials.fromStream(new ByteArrayInputStream(serializedServiceAccount)).createScoped(List.of("https://www.googleapis.com/auth/businessmessages"));
    credentials.refreshIfExpired();
    AccessToken token = credentials.getAccessToken();
    restTemplate.postForEntity(reqUrl, new HttpEntity<>(sendMessagePayload, getHeaders(token.getTokenValue())), String.class);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AccessToken(com.google.auth.oauth2.AccessToken) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials)

Example 39 with AccessToken

use of org.glassfish.jersey.client.oauth1.AccessToken in project gcs-uploader by GoogleCloudPlatform.

the class AuthConfig method updateToken.

public void updateToken(final AuthConfigListener listener) {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.add("x-content-refresh-token", authInfo.getRefreshToken());
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setContentLength(0L);
    try {
        RequestEntity requestEntity = new RequestEntity(headers, HttpMethod.POST, new URI(envConfig.getAuthEndpoint() + "/auth/renew_access_token"));
        ResponseEntity<AuthInfo> authInfoResponse = restTemplate.exchange(requestEntity, AuthInfo.class);
        AuthInfo newAuthInfo = authInfoResponse.getBody();
        newAuthInfo.setRefreshToken(authInfo.getRefreshToken());
        authInfo = newAuthInfo;
        credentials = GoogleCredentials.create(new AccessToken(authInfo.getAccessToken(), new Date(authInfo.getExpirationSeconds() * 1000)));
        buildStorage();
        validateAuthorization();
        if (listener != null) {
            listener.authInfoUpdated();
        }
    } catch (Exception e) {
        e.printStackTrace();
        if (listener != null) {
            listener.authInfoError(e);
        }
    }
}
Also used : AuthInfo(com.google.ce.media.contentuploader.message.AuthInfo) AccessToken(com.google.auth.oauth2.AccessToken) RestTemplate(org.springframework.web.client.RestTemplate) URI(java.net.URI) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) StorageException(com.google.cloud.storage.StorageException)

Example 40 with AccessToken

use of org.glassfish.jersey.client.oauth1.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)

Aggregations

AccessToken (com.google.auth.oauth2.AccessToken)71 Test (org.junit.Test)41 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)29 Date (java.util.Date)22 IOException (java.io.IOException)19 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)16 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 InputStreamReader (java.io.InputStreamReader)6 Instant (java.time.Instant)6 WebTarget (javax.ws.rs.client.WebTarget)6 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)6 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)5 OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)5