27
27
import static java .net .HttpURLConnection .HTTP_NOT_FOUND ;
28
28
import static java .net .HttpURLConnection .HTTP_OK ;
29
29
import static java .util .logging .Level .INFO ;
30
+ import static org .apache .commons .lang .StringUtils .isNotBlank ;
30
31
31
32
import java .io .BufferedReader ;
32
33
import java .io .File ;
114
115
*
115
116
*/
116
117
public class OpenShiftOAuth2SecurityRealm extends SecurityRealm {
118
+ private static final String EMPTY_STRING = "" ;
119
+
117
120
static final Logger LOGGER = Logger .getLogger (OpenShiftOAuth2SecurityRealm .class .getName ());
118
121
119
122
/**
@@ -144,6 +147,11 @@ public class OpenShiftOAuth2SecurityRealm extends SecurityRealm {
144
147
145
148
static final String LOGGING_OUT = "loggingOut" ;
146
149
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" ;
147
155
/**
148
156
* Global instance of the JSON factory.
149
157
*/
@@ -161,7 +169,7 @@ public class OpenShiftOAuth2SecurityRealm extends SecurityRealm {
161
169
/**
162
170
* Control the redirection URL for this realm. Exposed for testing.
163
171
*/
164
- static String redirectUrl ;
172
+ String redirectUrl ;
165
173
/**
166
174
* Allow a custom transport to be injected. Exposed for testing.
167
175
*/
@@ -958,7 +966,7 @@ public UsernamePasswordAuthenticationToken updateAuthorizationStrategy(Credentia
958
966
UsernamePasswordAuthenticationToken token = null ;
959
967
if (suffix != null ) {
960
968
String matrixKey = info .getName () + suffix ;
961
- token = new UsernamePasswordAuthenticationToken (matrixKey , "" , authorities );
969
+ token = new UsernamePasswordAuthenticationToken (matrixKey , EMPTY_STRING , authorities );
962
970
SecurityContextHolder .getContext ().setAuthentication (token );
963
971
964
972
User u = User .get (token .getName ());
@@ -1105,29 +1113,32 @@ public HttpResponse doCommenceLogin(@QueryParameter String from, @Header("Refere
1105
1113
return newOAuthSession (from , redirectOnFinish ).doCommenceLogin ();
1106
1114
}
1107
1115
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 ;
1111
1119
URL url = null ;
1112
1120
try {
1113
1121
url = new URL (redirect );
1114
1122
// just in case, strip redirect to a "root" url before appending the
1115
1123
// finishLogin path
1116
1124
// 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.
1122
1129
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 ();
1131
1142
}
1132
1143
} catch (MalformedURLException e ) {
1133
1144
throw e ;
@@ -1180,7 +1191,7 @@ protected String getPostLogOutUrl(StaplerRequest req, Authentication auth) {
1180
1191
// avoid the need for the
1181
1192
// 2 login attempts after logout when jenkins is recycled in the
1182
1193
// interim.
1183
- return req .getRequestURL ().toString ().replace (LOGOUT , "" );
1194
+ return req .getRequestURL ().toString ().replace (LOGOUT , EMPTY_STRING );
1184
1195
}
1185
1196
1186
1197
@ Extension
0 commit comments