Skip to content

Commit 2d4f07f

Browse files
akramopenshift-merge-robot
authored andcommitted
Fix: RedirectUrl omits server-port
1 parent 8aab931 commit 2d4f07f

File tree

2 files changed

+260
-169
lines changed

2 files changed

+260
-169
lines changed

src/main/java/org/openshift/jenkins/plugins/openshiftlogin/OpenShiftOAuth2SecurityRealm.java

+30-19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
2828
import static java.net.HttpURLConnection.HTTP_OK;
2929
import static java.util.logging.Level.INFO;
30+
import static org.apache.commons.lang.StringUtils.isNotBlank;
3031

3132
import java.io.BufferedReader;
3233
import java.io.File;
@@ -114,6 +115,8 @@
114115
*
115116
*/
116117
public class OpenShiftOAuth2SecurityRealm extends SecurityRealm {
118+
private static final String EMPTY_STRING = "";
119+
117120
static final Logger LOGGER = Logger.getLogger(OpenShiftOAuth2SecurityRealm.class.getName());
118121

119122
/**
@@ -144,6 +147,11 @@ public class OpenShiftOAuth2SecurityRealm extends SecurityRealm {
144147

145148
static final String LOGGING_OUT = "loggingOut";
146149

150+
private static final String HTTPS_SCHEME = "https";
151+
private static final String HTTP_SCHEME = "http";
152+
private static final String SCHEME_SEPARATOR = "://";
153+
private static final String PORT_SEPARATOR = ":";
154+
public static final String SECURITY_REALM_FINISH_LOGIN = "/securityRealm/finishLogin";
147155
/**
148156
* Global instance of the JSON factory.
149157
*/
@@ -161,7 +169,7 @@ public class OpenShiftOAuth2SecurityRealm extends SecurityRealm {
161169
/**
162170
* Control the redirection URL for this realm. Exposed for testing.
163171
*/
164-
static String redirectUrl;
172+
String redirectUrl;
165173
/**
166174
* Allow a custom transport to be injected. Exposed for testing.
167175
*/
@@ -958,7 +966,7 @@ public UsernamePasswordAuthenticationToken updateAuthorizationStrategy(Credentia
958966
UsernamePasswordAuthenticationToken token = null;
959967
if (suffix != null) {
960968
String matrixKey = info.getName() + suffix;
961-
token = new UsernamePasswordAuthenticationToken(matrixKey, "", authorities);
969+
token = new UsernamePasswordAuthenticationToken(matrixKey, EMPTY_STRING, authorities);
962970
SecurityContextHolder.getContext().setAuthentication(token);
963971

964972
User u = User.get(token.getName());
@@ -1105,29 +1113,32 @@ public HttpResponse doCommenceLogin(@QueryParameter String from, @Header("Refere
11051113
return newOAuthSession(from, redirectOnFinish).doCommenceLogin();
11061114
}
11071115

1108-
private String buildOAuthRedirectUrl(String redirect) throws MalformedURLException {
1109-
if (redirectUrl != null)
1110-
return redirectUrl;
1116+
public String buildOAuthRedirectUrl(String redirect) throws MalformedURLException {
1117+
if (this.redirectUrl != null)
1118+
return this.redirectUrl;
11111119
URL url = null;
11121120
try {
11131121
url = new URL(redirect);
11141122
// just in case, strip redirect to a "root" url before appending the
11151123
// finishLogin path
11161124
// also validate the protocol as a sanity check
1117-
if (url != null
1118-
&& (url.getProtocol().equalsIgnoreCase("http") || url.getProtocol().equalsIgnoreCase("https"))) {
1119-
// Get the current request to check if Jenkins was launched with
1120-
// a prefix set and append it after the URL Host.
1121-
final String prefix;
1125+
String protocol = url.getProtocol();
1126+
if (url != null && (protocol.equalsIgnoreCase(HTTP_SCHEME) || protocol.equalsIgnoreCase(HTTPS_SCHEME))) {
1127+
// Get the current request to check if Jenkins was launched with a prefix set
1128+
// and append it after the URL Host.
11221129
StaplerRequest req = Stapler.getCurrentRequest();
1123-
1124-
if (req != null) {
1125-
prefix = req.getContextPath();
1126-
} else {
1127-
prefix = "";
1128-
}
1129-
1130-
return url.getProtocol() + "://" + url.getHost() + prefix + "/securityRealm/finishLogin";
1130+
String contextPath = req != null ? req.getContextPath().trim() : EMPTY_STRING;
1131+
String prefix = isNotBlank(contextPath.trim()) ? contextPath : EMPTY_STRING;
1132+
1133+
// if a port is specified, it is appended, unless it is the default port for the
1134+
// given protocol e.g: http://host:80/ => http://host/
1135+
// https://host:8443/ => https://host:8443
1136+
int defaultPort = url.getDefaultPort();
1137+
int port = url.getPort();
1138+
String redirectPort = (port > 0 && port != defaultPort) ? PORT_SEPARATOR + port : EMPTY_STRING;
1139+
StringBuilder sb = new StringBuilder(protocol).append(SCHEME_SEPARATOR).append(url.getHost());
1140+
sb.append(redirectPort).append(prefix).append(SECURITY_REALM_FINISH_LOGIN);
1141+
return sb.toString();
11311142
}
11321143
} catch (MalformedURLException e) {
11331144
throw e;
@@ -1180,7 +1191,7 @@ protected String getPostLogOutUrl(StaplerRequest req, Authentication auth) {
11801191
// avoid the need for the
11811192
// 2 login attempts after logout when jenkins is recycled in the
11821193
// interim.
1183-
return req.getRequestURL().toString().replace(LOGOUT, "");
1194+
return req.getRequestURL().toString().replace(LOGOUT, EMPTY_STRING);
11841195
}
11851196

11861197
@Extension

0 commit comments

Comments
 (0)