Skip to content

Commit d459b44

Browse files
committed
Merge pull request cloudant#107 from cloudant/51118-proxy-auth
51118 proxy auth
2 parents 9553675 + bc69f24 commit d459b44

File tree

6 files changed

+62
-3
lines changed

6 files changed

+62
-3
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased
2+
- [IMPROVED] Added Basic Auth for HTTP proxies. Configure via `ConnectOption#setProxyUser`
3+
and `ConnectOptions#setProxyPassword`.
4+
15
# 1.2.2 (2015-10-01)
26
- [CHANGED] Added default of 5 minutes for both connection and socket timeouts instead of waiting forever.
37
- [IMPROVED] Upgraded Apache HttpClient from 4.3.3 to 4.3.6.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,8 +960,10 @@ ConnectOptions connectOptions = new ConnectOptions()
960960
.setSocketTimeout(50)
961961
.setConnectionTimeout(50)
962962
.setMaxConnections(100)
963-
.setProxyHost("http://localhost")
963+
.setProxyHost("localhost")
964964
.setProxyPort(8080)
965+
.setProxyUser("exampleProxyUser")
966+
.setProxyPassword("exampleProxyPassword")
965967
.setSSLAuthenticationDisabled(true);
966968
CloudantClient client = new CloudantClient("cloudant.com","test","password",
967969
connectOptions );

src/main/java/com/cloudant/client/api/CloudantClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ private void doInit(String scheme, String hostname, int port,
535535

536536
props.setProxyHost(connectOptions.getProxyHost());
537537
props.setProxyPort(connectOptions.getProxyPort());
538+
props.setProxyUser(connectOptions.getProxyUser());
539+
props.setProxyPassword(connectOptions.getProxyPassword());
538540
props.disableSSLAuthentication(connectOptions.isSSLAuthenticationDisabled());
539541
props.setAuthenticatedModeSSLSocketFactory(connectOptions
540542
.getAuthenticatedModeSSLSocketFactory());

src/main/java/com/cloudant/client/api/model/ConnectOptions.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class ConnectOptions {
1515

1616
private String proxyHost;
1717
private int proxyPort;
18+
private String proxyUser;
19+
private String proxyPassword;
1820
private boolean isSSLAuthenticationDisabled;
1921
private SSLSocketFactory authenticatedModeSSLSocketFactory;
2022

@@ -47,6 +49,16 @@ public ConnectOptions setProxyPort(int proxyPort) {
4749
return this;
4850
}
4951

52+
public ConnectOptions setProxyUser(String proxyUser) {
53+
this.proxyUser = proxyUser;
54+
return this;
55+
}
56+
57+
public ConnectOptions setProxyPassword(String proxyPassword) {
58+
this.proxyPassword = proxyPassword;
59+
return this;
60+
}
61+
5062
/**
5163
* Sets whether hostname verification, certificate chain validation,
5264
* and the use of the optional
@@ -94,6 +106,14 @@ public int getProxyPort() {
94106
return proxyPort;
95107
}
96108

109+
public String getProxyUser() {
110+
return proxyUser;
111+
}
112+
113+
public String getProxyPassword() {
114+
return proxyPassword;
115+
}
116+
97117
/**
98118
* @return true if hostname verification, certificate chain validation,
99119
* and the use of the optional

src/main/java/org/lightcouch/CouchDbClient.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,30 @@ HttpClient createHttpClient(CouchDbProperties props) {
205205
.setDefaultRequestConfig(RequestConfig.custom()
206206
.setSocketTimeout(props.getSocketTimeout())
207207
.setConnectTimeout(props.getConnectionTimeout()).build());
208+
209+
//set up a credsProvider, but it will only be used if creds have been supplied
210+
CredentialsProvider credsProvider = new BasicCredentialsProvider();
211+
boolean credsProviderUsed = false;
208212
if (props.getProxyHost() != null) {
209213
clientBuilder.setProxy(new HttpHost(props.getProxyHost(), props.getProxyPort()));
214+
if (props.getProxyUser() != null) {
215+
credsProviderUsed = true;
216+
credsProvider.setCredentials(new AuthScope(props.getProxyHost(), props
217+
.getProxyPort()), new UsernamePasswordCredentials(props.getProxyUser
218+
(), props.getProxyPassword()));
219+
}
210220
}
211221
clientBuilder.setDefaultCookieStore(cookies); // use AUTH cookies
212222
if (props.getUsername() != null) {
223+
credsProviderUsed = true;
213224
// this one is for non account endpoints.
214-
CredentialsProvider credsProvider = new BasicCredentialsProvider();
215225
credsProvider.setCredentials(new AuthScope(props.getHost(),
216226
props.getPort()),
217227
new UsernamePasswordCredentials(props.getUsername(),
218228
props.getPassword()));
229+
}
230+
if (credsProviderUsed) {
219231
clientBuilder.setDefaultCredentialsProvider(credsProvider);
220-
//props.clearPassword();
221232
}
222233
registerInterceptors(clientBuilder);
223234
return clientBuilder.build();

src/main/java/org/lightcouch/CouchDbProperties.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class CouchDbProperties {
4343
private int maxConnections = 6;
4444
private String proxyHost;
4545
private int proxyPort;
46+
private String proxyUser;
47+
private String proxyPassword;
4648
private boolean disableSSLAuthentication;
4749
private SSLSocketFactory authenticatedModeSSLSocketFactory;
4850

@@ -112,6 +114,14 @@ public int getProxyPort() {
112114
return proxyPort;
113115
}
114116

117+
public String getProxyUser() {
118+
return proxyUser;
119+
}
120+
121+
public String getProxyPassword() {
122+
return proxyPassword;
123+
}
124+
115125
public CouchDbProperties setProtocol(String protocol) {
116126
this.protocol = protocol;
117127
return this;
@@ -167,6 +177,16 @@ public CouchDbProperties setProxyPort(int proxyPort) {
167177
return this;
168178
}
169179

180+
public CouchDbProperties setProxyUser(String proxyUser) {
181+
this.proxyUser = proxyUser;
182+
return this;
183+
}
184+
185+
public CouchDbProperties setProxyPassword(String proxyPassword) {
186+
this.proxyPassword = proxyPassword;
187+
return this;
188+
}
189+
170190
public String getAuthCookie() {
171191
return authCookie;
172192
}

0 commit comments

Comments
 (0)