1 20 package org.apache.cactus.client.authentication; 21 22 import java.net.HttpURLConnection ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 26 import org.apache.cactus.Cookie; 27 import org.apache.cactus.WebRequest; 28 import org.apache.cactus.internal.WebRequestImpl; 29 import org.apache.cactus.internal.client.connector.http.HttpClientConnectionHelper; 30 import org.apache.cactus.internal.configuration.Configuration; 31 import org.apache.cactus.internal.configuration.WebConfiguration; 32 import org.apache.cactus.util.ChainedRuntimeException; 33 import org.apache.commons.httpclient.HttpMethod; 34 import org.apache.commons.httpclient.HttpState; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 51 public class FormAuthentication extends AbstractAuthentication 52 { 53 56 private static final Log LOGGER = 57 LogFactory.getLog(FormAuthentication.class); 58 59 63 private int expectedAuthResponse = HttpURLConnection.HTTP_MOVED_TEMP; 64 65 69 private URL securityCheckURL; 70 71 74 private String sessionCookieName = "JSESSIONID"; 75 76 79 private Cookie jsessionCookie; 80 81 85 private WebRequest securityRequest = new WebRequestImpl(); 86 87 91 public FormAuthentication(String theName, String thePassword) 92 { 93 super(theName, thePassword); 94 } 95 96 99 public void configure(HttpState theState, HttpMethod theMethod, 100 WebRequest theRequest, Configuration theConfiguration) 101 { 102 if (this.jsessionCookie == null) 104 { 105 authenticate(theRequest, theConfiguration); 106 } 107 108 if (this.jsessionCookie != null) 110 { 111 theRequest.addCookie(this.jsessionCookie); 112 } 113 } 114 115 120 public WebRequest getSecurityRequest() 121 { 122 return this.securityRequest; 123 } 124 125 131 public void setSecurityCheckURL(URL theUrl) 132 { 133 this.securityCheckURL = theUrl; 134 } 135 136 144 public URL getSecurityCheckURL(Configuration theConfiguration) 145 { 146 if (this.securityCheckURL == null) 147 { 148 String stringUrl = 150 ((WebConfiguration) theConfiguration).getContextURL() 151 + "/j_security_check"; 152 153 try 154 { 155 this.securityCheckURL = new URL (stringUrl); 156 } 157 catch (MalformedURLException e) 158 { 159 throw new ChainedRuntimeException( 160 "Unable to create default Security Check URL [" 161 + stringUrl + "]"); 162 } 163 } 164 165 LOGGER.debug("Using security check URL [" + this.securityCheckURL 166 + "]"); 167 168 return securityCheckURL; 169 } 170 171 172 176 private String getSessionCookieName() 177 { 178 return this.sessionCookieName; 179 } 180 181 187 public void setSessionCookieName(String theName) 188 { 189 if (theName != null) 190 { 191 this.sessionCookieName = theName; 192 } 193 } 194 195 196 201 protected int getExpectedAuthResponse() 202 { 203 return this.expectedAuthResponse; 204 } 205 206 212 public void setExpectedAuthResponse(int theExpectedCode) 213 { 214 this.expectedAuthResponse = theExpectedCode; 215 } 216 217 218 224 private Cookie getCookie(HttpURLConnection theConnection, String theTarget) 225 { 226 int i = 1; 228 String key = theConnection.getHeaderFieldKey(i); 229 while (key != null) 230 { 231 if (key.equalsIgnoreCase("set-cookie")) 232 { 233 String cookiestr = theConnection.getHeaderField(i); 239 String nameValue = cookiestr.substring(0, 240 cookiestr.indexOf(";")); 241 int equalsChar = nameValue.indexOf("="); 242 String name = nameValue.substring(0, equalsChar); 243 String value = nameValue.substring(equalsChar + 1); 244 if (name.equalsIgnoreCase(theTarget)) 245 { 246 return new Cookie(theConnection.getURL().getHost(), 247 name, value); 248 } 249 } 250 key = theConnection.getHeaderFieldKey(++i); 251 } 252 return null; 253 } 254 255 256 265 protected void checkPreAuthResponse(HttpURLConnection theConnection) 266 throws Exception 267 { 268 if (theConnection.getResponseCode() >= 400) 269 { 270 throw new Exception ("Received a status code [" 271 + theConnection.getResponseCode() 272 + "] and was expecting less than 400"); 273 } 274 } 275 276 277 290 private Cookie getSecureSessionIdCookie(WebRequest theRequest, 291 Configuration theConfiguration) 292 { 293 HttpURLConnection connection; 294 String resource = null; 295 296 try 297 { 298 WebConfiguration webConfig = (WebConfiguration) theConfiguration; 300 resource = webConfig.getRedirectorURL(theRequest); 301 302 HttpClientConnectionHelper helper = 303 new HttpClientConnectionHelper(resource); 304 305 WebRequest request = 306 new WebRequestImpl((WebConfiguration) theConfiguration); 307 308 connection = helper.connect(request, theConfiguration); 310 311 checkPreAuthResponse(connection); 312 } 313 catch (Throwable e) 314 { 315 throw new ChainedRuntimeException( 316 "Failed to connect to the secured redirector: " + resource, e); 317 } 318 319 return getCookie(connection, getSessionCookieName()); 320 } 321 322 323 332 protected void checkAuthResponse(HttpURLConnection theConnection) 333 throws Exception 334 { 335 if (theConnection.getResponseCode() != getExpectedAuthResponse()) 336 { 337 throw new Exception ("Received a status code [" 338 + theConnection.getResponseCode() 339 + "] and was expecting a [" 340 + getExpectedAuthResponse() + "]"); 341 } 342 } 343 344 345 351 public void authenticate(WebRequest theRequest, 352 Configuration theConfiguration) 353 { 354 this.jsessionCookie = getSecureSessionIdCookie(theRequest, 355 theConfiguration); 356 357 try 358 { 359 HttpClientConnectionHelper helper = 361 new HttpClientConnectionHelper( 362 getSecurityCheckURL(theConfiguration).toString()); 363 364 WebRequest request = getSecurityRequest(); 367 ((WebRequestImpl) request).setConfiguration(theConfiguration); 368 request.addCookie(this.jsessionCookie); 369 request.addParameter("j_username", getName(), 370 WebRequest.POST_METHOD); 371 request.addParameter("j_password", getPassword(), 372 WebRequest.POST_METHOD); 373 374 HttpURLConnection connection = helper.connect(request, 376 theConfiguration); 377 378 checkAuthResponse(connection); 379 } 380 catch (Throwable e) 381 { 382 this.jsessionCookie = null; 383 throw new ChainedRuntimeException( 384 "Failed to authenticate the principal", e); 385 } 386 } 387 } 388 | Popular Tags |