Skip to content

Commit 3fd82ae

Browse files
author
taylor.smock
committed
See #22596: Some hosts redirect to another host for authentication (patch by hhtznr, modified)
This lets plugins do authentication with hosts that redirect to another host for authentication. git-svn-id: https://josm.openstreetmap.de/svn/trunk@18913 0c6e7542-c601-0410-84e7-c038aed88b3b
1 parent ba6ea49 commit 3fd82ae

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/org/openstreetmap/josm/tools/HttpClient.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.InputStream;
99
import java.net.CookieHandler;
1010
import java.net.CookieManager;
11+
import java.net.CookiePolicy;
1112
import java.net.HttpURLConnection;
1213
import java.net.MalformedURLException;
1314
import java.net.URL;
@@ -83,7 +84,7 @@ public interface HttpClientFactory {
8384

8485
static {
8586
try {
86-
CookieHandler.setDefault(new CookieManager());
87+
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
8788
} catch (SecurityException e) {
8889
Logging.log(Logging.LEVEL_ERROR, "Unable to set default cookie handler", e);
8990
}
@@ -132,6 +133,19 @@ public final Response connect() throws IOException {
132133
* @since 9179
133134
*/
134135
public final Response connect(ProgressMonitor progressMonitor) throws IOException {
136+
return connect(progressMonitor, null, null);
137+
}
138+
139+
/**
140+
* Opens the HTTP connection.
141+
* @param progressMonitor progress monitor
142+
* @param authRedirectLocation The location where we will be redirected for authentication
143+
* @param authRequestProperty The authorization header to set when being redirected to the auth location
144+
* @return HTTP response
145+
* @throws IOException if any I/O error occurs
146+
* @since 18913
147+
*/
148+
public final Response connect(ProgressMonitor progressMonitor, String authRedirectLocation, String authRequestProperty) throws IOException {
135149
if (progressMonitor == null) {
136150
progressMonitor = NullProgressMonitor.INSTANCE;
137151
}
@@ -183,8 +197,10 @@ public final Response connect(ProgressMonitor progressMonitor) throws IOExceptio
183197
url = new URL(url, redirectLocation);
184198
maxRedirects--;
185199
logRequest(tr("Download redirected to ''{0}''", redirectLocation));
186-
// Fix JOSM #21935: Avoid leaking `Authorization` header on redirects.
187-
if (!Objects.equals(oldUrl.getHost(), this.url.getHost()) && this.getRequestHeader("Authorization") != null) {
200+
if (authRedirectLocation != null && authRequestProperty != null && redirectLocation.startsWith(authRedirectLocation)) {
201+
setHeader("Authorization", authRequestProperty);
202+
} else if (!Objects.equals(oldUrl.getHost(), this.url.getHost()) && this.getRequestHeader("Authorization") != null) {
203+
// Fix JOSM #21935: Avoid leaking `Authorization` header on redirects.
188204
logRequest(tr("Download redirected to different host (''{0}'' -> ''{1}''), removing authorization headers",
189205
oldUrl.getHost(), url.getHost()));
190206
this.headers.remove("Authorization");

0 commit comments

Comments
 (0)