Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# UNRELEASED
- [FIXED] Enable proxy support for session cookie and IAM authentication.

# 2.20.0 (2021-08-26)
- [FIXED] Type of `sinceSeq` can be also a `String` besides an `Integer`.
- [DEPRECATED] This library is now deprecated and will be EOL on Dec 31 2021.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2015, 2020 IBM Corp. All rights reserved.
* Copyright © 2015, 2021 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -257,7 +257,7 @@ public CloudantClient build() {

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

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

//Create cookie interceptor and set in HttpConnection interceptors
CookieInterceptor cookieInterceptor = new CookieInterceptor(username, password, this.url.toString());
CookieInterceptor cookieInterceptor = new CookieInterceptor(username, password,
this.url.toString(), this.proxyURL);


props.addRequestInterceptors(cookieInterceptor);
props.addResponseInterceptors(cookieInterceptor);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2015, 2019 IBM Corp. All rights reserved.
* Copyright © 2015, 2021 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand All @@ -21,6 +21,7 @@
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Locale;
import java.util.logging.Level;
Expand All @@ -41,13 +42,26 @@ public class CookieInterceptor extends CookieInterceptorBase {
* @param baseURL The base URL to use when constructing an `_session` request.
*/
public CookieInterceptor(String username, String password, String baseURL) {
this(username, password, baseURL, null);
}

/**
* Constructs a cookie interceptor with proxy url.
* Credentials should be supplied not URL encoded, this class
* will perform the necessary URL encoding.
*
* @param username The username to use when getting the cookie (not URL encoded)
* @param password The password to use when getting the cookie (not URL encoded)
* @param baseURL The base URL to use when constructing an `_session` request.
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.

@param proxyURL is missing

* @param proxyURL The URL of the proxy server
*/
public CookieInterceptor(String username, String password, String baseURL, URL proxyURL) {
// Use form encoding for the user/pass submission
super(baseURL, "/_session", "application/x-www-form-urlencoded");
super(baseURL, "/_session", "application/x-www-form-urlencoded", proxyURL);
try {
this.auth = String.format("name=%s&password=%s", URLEncoder.encode(username, "UTF-8")
, URLEncoder.encode(password, "UTF-8"))
, URLEncoder.encode(password, "UTF-8"))
.getBytes("UTF-8");
;
} catch (UnsupportedEncodingException e) {
//all JVMs should support UTF-8, so this should not happen
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2017, 2019 IBM Corp. All rights reserved.
* Copyright © 2017, 2021 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -59,13 +59,15 @@ public abstract class CookieInterceptorBase implements HttpConnectionRequestInte
private final CookieManager cookieManager = new CookieManager();
private final ReadWriteLock sessionLock = new ReentrantReadWriteLock(true);
private volatile UUID sessionId = UUID.randomUUID();
private final URL proxyURL;

/**
* @param baseUrl the server URL to get cookies from
* @param endpoint the server endpoint to get cookies from
* @param requestMimeType the MIME Content-Type to use for the session request
* @param proxyURL the proxy URL
*/
protected CookieInterceptorBase(String baseUrl, String endpoint, String requestMimeType) {
protected CookieInterceptorBase(String baseUrl, String endpoint, String requestMimeType, URL proxyURL) {
try {
baseUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
endpoint = endpoint.startsWith("/") ? endpoint : "/" + endpoint;
Expand All @@ -77,6 +79,16 @@ protected CookieInterceptorBase(String baseUrl, String endpoint, String requestM
logger.log(Level.SEVERE, "Failed to create URL for session endpoint", e);
throw new RuntimeException(e);
}
this.proxyURL = proxyURL;
}

/**
* @param baseUrl the server URL to get cookies from
* @param endpoint the server endpoint to get cookies from
* @param requestMimeType the MIME Content-Type to use for the session request
*/
protected CookieInterceptorBase(String baseUrl, String endpoint, String requestMimeType) {
this(baseUrl, endpoint, requestMimeType, null);
}

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

if (proxyURL != null) {
conn.connectionFactory.setProxy(this.proxyURL);
}

//when we request the session we need all interceptors except this one

conn.requestInterceptors.addAll(context.connection.requestInterceptors);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2019 IBM Corp. All rights reserved.
* Copyright © 2019, 2021 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -34,7 +34,11 @@ public class IamCookieInterceptor extends CookieInterceptorBase {
private final byte[] tokenRequestPayload;

public IamCookieInterceptor(String apiKey, String baseUrl) {
super(baseUrl, "/_iam_session", null);
this(apiKey, baseUrl, null);
}

public IamCookieInterceptor(String apiKey, String baseUrl, URL proxyURL) {
super(baseUrl, "/_iam_session", null, proxyURL);

// Read iamServer from system property, or set default
try {
Expand Down