Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/main/java/io/cdap/plugin/http/common/http/OAuthUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@
import io.cdap.plugin.http.common.OAuth2GrantType;
import io.cdap.plugin.http.common.pagination.page.JSONUtil;
import io.cdap.plugin.http.source.common.BaseHttpSourceConfig;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
Expand Down Expand Up @@ -83,14 +89,26 @@ public static AccessToken getAccessToken(BaseHttpConfig config) throws IOExcepti
// get accessToken from service account
return OAuthUtil.getAccessTokenByServiceAccount(config);
case OAUTH2:
HttpClientBuilder httpClientBuilder = HttpClients.custom();

if (config instanceof BaseHttpSourceConfig) {
try (CloseableHttpClient client = HttpClients.custom()
.setSSLSocketFactory(new SSLConnectionSocketFactoryCreator((BaseHttpSourceConfig) config).create())
.build()) {
return getAccessToken(client, config);
httpClientBuilder.setSSLSocketFactory(
new SSLConnectionSocketFactoryCreator((BaseHttpSourceConfig) config).create()
);
}
// Apply Proxy settings if they exist
if (!Strings.isNullOrEmpty(config.getProxyUrl())) {
HttpHost proxyHost = HttpHost.create(config.getProxyUrl());

if (!Strings.isNullOrEmpty(config.getProxyUsername()) && !Strings.isNullOrEmpty(config.getProxyPassword())) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the else part throw an error?

Do we have validation in place earlier which checks that these cannot be empty when proxyUrl is specified?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the ProxyURL without Credentials is a valid use case, so adding an else part for the same is not required.
Similar type of validations are present where the proxy configuration is implemented. I have also tested these changes locally to verify the same.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, sounds good.

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost),
new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
}
httpClientBuilder.setProxy(proxyHost);
}
try (CloseableHttpClient client = HttpClients.createDefault()) {
try (CloseableHttpClient client = httpClientBuilder.build()) {
return getAccessToken(client, config);
}
}
Expand Down
Loading