Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 6a353d1

Browse files
authored
Merge pull request #531 from cloudant/528-connect-thru-proxy
Enable proxy support for cookie session and IAM authentication.
2 parents 0df0645 + 290d1d8 commit 6a353d1

5 files changed

Lines changed: 50 additions & 11 deletions

File tree

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# UNRELEASED
2+
- [FIXED] Enable proxy support for session cookie and IAM authentication.
3+
14
# 2.20.0 (2021-08-26)
25
- [FIXED] Type of `sinceSeq` can be also a `String` besides an `Integer`.
36
- [DEPRECATED] This library is now deprecated and will be EOL on Dec 31 2021.

cloudant-client/src/main/java/com/cloudant/client/api/ClientBuilder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2015, 2020 IBM Corp. All rights reserved.
2+
* Copyright © 2015, 2021 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -257,7 +257,7 @@ public CloudantClient build() {
257257

258258
// Create IAM cookie interceptor and set in HttpConnection interceptors
259259
IamCookieInterceptor cookieInterceptor = new IamCookieInterceptor(this.iamApiKey,
260-
this.url.toString());
260+
this.url.toString(), this.proxyURL);
261261

262262
props.addRequestInterceptors(cookieInterceptor);
263263
props.addResponseInterceptors(cookieInterceptor);
@@ -274,7 +274,9 @@ else if (this.username != null && this.password != null) {
274274
//make interceptor if both username and password are not null
275275

276276
//Create cookie interceptor and set in HttpConnection interceptors
277-
CookieInterceptor cookieInterceptor = new CookieInterceptor(username, password, this.url.toString());
277+
CookieInterceptor cookieInterceptor = new CookieInterceptor(username, password,
278+
this.url.toString(), this.proxyURL);
279+
278280

279281
props.addRequestInterceptors(cookieInterceptor);
280282
props.addResponseInterceptors(cookieInterceptor);

cloudant-http/src/main/java/com/cloudant/http/internal/interceptors/CookieInterceptor.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2015, 2019 IBM Corp. All rights reserved.
2+
* Copyright © 2015, 2021 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.io.UnsupportedEncodingException;
2323
import java.net.HttpURLConnection;
24+
import java.net.URL;
2425
import java.net.URLEncoder;
2526
import java.util.Locale;
2627
import java.util.logging.Level;
@@ -41,13 +42,26 @@ public class CookieInterceptor extends CookieInterceptorBase {
4142
* @param baseURL The base URL to use when constructing an `_session` request.
4243
*/
4344
public CookieInterceptor(String username, String password, String baseURL) {
45+
this(username, password, baseURL, null);
46+
}
47+
48+
/**
49+
* Constructs a cookie interceptor with proxy url.
50+
* Credentials should be supplied not URL encoded, this class
51+
* will perform the necessary URL encoding.
52+
*
53+
* @param username The username to use when getting the cookie (not URL encoded)
54+
* @param password The password to use when getting the cookie (not URL encoded)
55+
* @param baseURL The base URL to use when constructing an `_session` request.
56+
* @param proxyURL The URL of the proxy server
57+
*/
58+
public CookieInterceptor(String username, String password, String baseURL, URL proxyURL) {
4459
// Use form encoding for the user/pass submission
45-
super(baseURL, "/_session", "application/x-www-form-urlencoded");
60+
super(baseURL, "/_session", "application/x-www-form-urlencoded", proxyURL);
4661
try {
4762
this.auth = String.format("name=%s&password=%s", URLEncoder.encode(username, "UTF-8")
48-
, URLEncoder.encode(password, "UTF-8"))
63+
, URLEncoder.encode(password, "UTF-8"))
4964
.getBytes("UTF-8");
50-
;
5165
} catch (UnsupportedEncodingException e) {
5266
//all JVMs should support UTF-8, so this should not happen
5367
throw new RuntimeException(e);

cloudant-http/src/main/java/com/cloudant/http/internal/interceptors/CookieInterceptorBase.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2017, 2019 IBM Corp. All rights reserved.
2+
* Copyright © 2017, 2021 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -59,13 +59,15 @@ public abstract class CookieInterceptorBase implements HttpConnectionRequestInte
5959
private final CookieManager cookieManager = new CookieManager();
6060
private final ReadWriteLock sessionLock = new ReentrantReadWriteLock(true);
6161
private volatile UUID sessionId = UUID.randomUUID();
62+
private final URL proxyURL;
6263

6364
/**
6465
* @param baseUrl the server URL to get cookies from
6566
* @param endpoint the server endpoint to get cookies from
6667
* @param requestMimeType the MIME Content-Type to use for the session request
68+
* @param proxyURL the proxy URL
6769
*/
68-
protected CookieInterceptorBase(String baseUrl, String endpoint, String requestMimeType) {
70+
protected CookieInterceptorBase(String baseUrl, String endpoint, String requestMimeType, URL proxyURL) {
6971
try {
7072
baseUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
7173
endpoint = endpoint.startsWith("/") ? endpoint : "/" + endpoint;
@@ -77,6 +79,16 @@ protected CookieInterceptorBase(String baseUrl, String endpoint, String requestM
7779
logger.log(Level.SEVERE, "Failed to create URL for session endpoint", e);
7880
throw new RuntimeException(e);
7981
}
82+
this.proxyURL = proxyURL;
83+
}
84+
85+
/**
86+
* @param baseUrl the server URL to get cookies from
87+
* @param endpoint the server endpoint to get cookies from
88+
* @param requestMimeType the MIME Content-Type to use for the session request
89+
*/
90+
protected CookieInterceptorBase(String baseUrl, String endpoint, String requestMimeType) {
91+
this(baseUrl, endpoint, requestMimeType, null);
8092
}
8193

8294
/**
@@ -129,6 +141,10 @@ HttpConnection makeSessionRequest(URL url, byte[] payload, String contentMimeTyp
129141
conn.requestProperties.put("accept", "application/json");
130142
conn.setRequestBody(payload);
131143

144+
if (proxyURL != null) {
145+
conn.connectionFactory.setProxy(this.proxyURL);
146+
}
147+
132148
//when we request the session we need all interceptors except this one
133149

134150
conn.requestInterceptors.addAll(context.connection.requestInterceptors);

cloudant-http/src/main/java/com/cloudant/http/internal/interceptors/IamCookieInterceptor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2019 IBM Corp. All rights reserved.
2+
* Copyright © 2019, 2021 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -34,7 +34,11 @@ public class IamCookieInterceptor extends CookieInterceptorBase {
3434
private final byte[] tokenRequestPayload;
3535

3636
public IamCookieInterceptor(String apiKey, String baseUrl) {
37-
super(baseUrl, "/_iam_session", null);
37+
this(apiKey, baseUrl, null);
38+
}
39+
40+
public IamCookieInterceptor(String apiKey, String baseUrl, URL proxyURL) {
41+
super(baseUrl, "/_iam_session", null, proxyURL);
3842

3943
// Read iamServer from system property, or set default
4044
try {

0 commit comments

Comments
 (0)