Skip to content

Commit

Permalink
use different http client for token
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-akolodziejczyk committed Dec 10, 2024
1 parent 82b9edb commit 87e7c05
Showing 1 changed file with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package net.snowflake.client.authentication;

import static net.snowflake.client.authentication.AuthConnectionParameters.getOauthConnectionParameters;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
Expand Down Expand Up @@ -58,7 +60,7 @@ void shouldThrowErrorForMismatchedOauthUsername() throws IOException, Interrupte
"The user you were trying to authenticate as differs from the user tied to the access token.");
}

private String getToken() throws IOException, InterruptedException {
private String getToken() throws IOException {
List<String> data =
Stream.of(
"username=" + System.getenv("SNOWFLAKE_AUTH_TEST_OKTA_USER"),
Expand All @@ -73,23 +75,27 @@ private String getToken() throws IOException, InterruptedException {
+ System.getenv("SNOWFLAKE_AUTH_TEST_OAUTH_CLIENT_SECRET");
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));

HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request =
HttpRequest.newBuilder()
.uri(URI.create(System.getenv("SNOWFLAKE_AUTH_TEST_OAUTH_URL")))
.header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
.header("Authorization", "Basic " + encodedAuth)
.POST(HttpRequest.BodyPublishers.ofString(String.join("&", data)))
.build();
URL url = new URL(System.getenv("SNOWFLAKE_AUTH_TEST_OAUTH_URL"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
connection.setRequestProperty("Authorization", "Basic " + encodedAuth);
connection.setDoOutput(true);

HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

if (response.statusCode() != 200) {
throw new IOException("Failed to get access token, response code: " + response.statusCode());
try (DataOutputStream out = new DataOutputStream(connection.getOutputStream())) {
out.writeBytes(String.join("&", data));
out.flush();
}

int responseCode = connection.getResponseCode();
assertThat("Failed to get access token, response code: " + responseCode, responseCode, is(200));

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(response.body());
JsonNode jsonNode;
try (InputStream inputStream = connection.getInputStream()) {
jsonNode = mapper.readTree(inputStream);
}
return jsonNode.get("access_token").asText();
}
}

0 comments on commit 87e7c05

Please sign in to comment.