Skip to content

Commit

Permalink
fix rundeck#1774 handle relative url redirect after authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
gschueler committed Apr 20, 2016
1 parent 07fea3c commit 181d057
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public boolean authenticate(final URL baseURL, final HttpClient client) throws H
login.releaseConnection();

Header locHeader = login.getResponseHeader("Location");
String location = null != locHeader ? locHeader.getValue() : null;
String location = absoluteUrl(baseURL, null != locHeader ? locHeader.getValue() : null);
if (isLoginError(login)) {
logger.error("Form-based auth failed");
return false;
Expand Down Expand Up @@ -302,6 +302,19 @@ public boolean authenticate(final URL baseURL, final HttpClient client) throws H
return true;
}

public static String absoluteUrl(final URL baseURL, String location) throws MalformedURLException {
if (null != location && location.startsWith("/")) {
//make absolute url
location = new URL(
baseURL.getProtocol(),
baseURL.getHost(),
baseURL.getPort(),
location
).toExternalForm();
}
return location;
}

/**
* Check the http state cookies to see if we have the proper session id cookie
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dtolabs.client.utils

import org.apache.commons.httpclient.HttpMethod
import spock.lang.Specification

/**
* Created by greg on 4/20/16.
*/
class BaseFormAuthenticatorSpec extends Specification {
def "absolute url"() {
given:
URL url = new URL("http", host, 8080, "/some/path")
when:
def result = BaseFormAuthenticator.absoluteUrl(url, location)
then:
result == expected

where:
host | location | expected
"ahost" | '/monkey' | 'http://ahost:8080/monkey'
"bhost" | '/monkey' | 'http://bhost:8080/monkey'
"ahost" | 'http://ahost2:8080/abc' | 'http://ahost2:8080/abc'

}
}

0 comments on commit 181d057

Please sign in to comment.