1 19 20 package com.sslexplorer.core.actions; 21 22 import java.io.IOException ; 23 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.struts.Globals; 31 import org.apache.struts.action.ActionForm; 32 import org.apache.struts.action.ActionForward; 33 import org.apache.struts.action.ActionMapping; 34 import org.apache.struts.action.ActionMessage; 35 import org.apache.struts.action.ActionMessages; 36 37 import com.sslexplorer.boot.Util; 38 import com.sslexplorer.core.CoreUtil; 39 import com.sslexplorer.core.ServletRequestAdapter; 40 import com.sslexplorer.core.ServletResponseAdapter; 41 import com.sslexplorer.policyframework.NoPermissionException; 42 import com.sslexplorer.policyframework.Permission; 43 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 44 import com.sslexplorer.policyframework.ResourceType; 45 import com.sslexplorer.properties.PropertyProfile; 46 import com.sslexplorer.security.Constants; 47 import com.sslexplorer.security.LogonController; 48 import com.sslexplorer.security.LogonControllerFactory; 49 import com.sslexplorer.security.SessionInfo; 50 import com.sslexplorer.security.SystemDatabaseFactory; 51 import com.sslexplorer.security.User; 52 53 65 public abstract class AuthenticatedAction extends DefaultAction implements CoreAction { 66 67 static Log log = LogFactory.getLog(AuthenticatedAction.class); 68 69 71 private boolean requiresAdministrator; 72 private ResourceType resourceType; 73 private Permission[] permissions; 74 75 79 public AuthenticatedAction() { 80 } 81 82 83 90 public AuthenticatedAction(ResourceType resourceType, Permission permissions[]) { 91 if (resourceType == null || permissions == null || permissions.length < 1) { 92 throw new IllegalArgumentException ("Must provide a resource type and at least 1 permission."); 93 } 94 this.resourceType = resourceType; 95 this.permissions = permissions; 96 } 97 98 113 public SessionInfo getSessionInfo(HttpServletRequest request) { 114 return LogonControllerFactory.getInstance().getSessionInfo(request); 115 116 } 117 118 public final ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, 119 HttpServletResponse response) throws Exception { 120 boolean installMode = isInstallMode(); 122 if (installMode) { 123 if ((getNavigationContext(mapping, form, request, response) & SessionInfo.SETUP_CONSOLE_CONTEXT) == 0) { 124 return mapping.findForward("setup"); 125 } else { 126 130 request.setAttribute(Constants.REQ_ATTR_ACTION_MAPPING, mapping); 131 request.setAttribute(Constants.REQ_ATTR_FORM, form); 132 133 CoreUtil.checkNavigationContext(this, mapping, form, request, response); 134 return onExecute(mapping, form, request, response); 135 } 136 } 137 138 try { 139 try { 140 if (!SystemDatabaseFactory.getInstance().verifyIPAddress(request.getRemoteAddr())) { 141 String link = null; 142 log.error(request.getRemoteHost() + " is not authorized"); 143 if (log.isInfoEnabled()) 144 log.info("Logging off, IP address verification failed."); 145 if(LogonControllerFactory.getInstance().hasClientLoggedOn(request, response) == LogonController.LOGGED_ON) { 146 LogonControllerFactory.getInstance().logoffSession(request, response); 147 } 148 149 if (link != null) { 150 return new ActionForward(link, true); 151 } else { 152 if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { 154 response.setStatus(HttpServletResponse.SC_FORBIDDEN); 155 return null; 156 } 157 return mapping.findForward("logon"); 158 } 159 160 } else { 161 162 int logonStatus = LogonControllerFactory.getInstance().hasClientLoggedOn(request, response); 163 if (logonStatus == LogonController.INVALID_TICKET) { 164 ActionMessages msgs = new ActionMessages(); 165 msgs.add(Globals.ERROR_KEY, new ActionMessage("login.invalidTicket")); 166 saveErrors(request, msgs); 167 } else if (logonStatus == LogonController.LOGGED_ON) { 168 169 User currentUser = LogonControllerFactory.getInstance().getUser(request); 170 171 LogonControllerFactory.getInstance().addCookies(new ServletRequestAdapter(request), 173 new ServletResponseAdapter(response), 174 (String ) request.getSession().getAttribute(Constants.LOGON_TICKET), getSessionInfo(request)); 175 176 if (!LogonControllerFactory.getInstance().isAdministrator(getSessionInfo(request).getUser()) 177 && requiresAdministrator) { 178 response.sendError(403, "You do not have permission to access this area"); 179 return null; 180 } else { 181 185 request.setAttribute(Constants.REQ_ATTR_ACTION_MAPPING, mapping); 186 request.setAttribute(Constants.REQ_ATTR_FORM, form); 187 188 191 ActionForward fwd = checkIntercept(mapping, request, response); 192 if(fwd != null) { 193 return fwd; 194 } 195 196 201 CoreUtil.checkNavigationContext(this, mapping, form, request, response); 202 203 if (resourceType != null) { 206 if (!PolicyDatabaseFactory.getInstance().isPermitted(resourceType, permissions, currentUser, false)) { 207 throw new NoPermissionException("Action denied for current user"); 208 } 209 } 210 211 if (request.getSession().getAttribute(Constants.SESSION_LOCKED) == null || isIgnoreSessionLock()) { 212 if (requiresProfile()) { 213 PropertyProfile profile = (PropertyProfile) request.getSession().getAttribute( 214 Constants.SELECTED_PROFILE); 215 if (profile == null) { 216 request.getSession().setAttribute(Constants.ORIGINAL_REQUEST, 217 Util.getOriginalRequest(request)); 218 return mapping.findForward("selectPropertyProfile"); 219 } 220 } 221 return onExecute(mapping, form, request, response); 222 } 223 } 224 } 225 } 226 } catch (NoPermissionException e) { 227 if (log.isDebugEnabled()) 228 log.debug("User attempted to access page they do have have permission for. Resource type = " 229 + resourceType 230 + ". Now attempting to find the first valid item in the current menu tree to display.", e); 231 response.sendError(HttpServletResponse.SC_FORBIDDEN); 232 return null; 233 } catch (SecurityException ex) { 234 } catch (ServletException ex) { 236 throw ex; 237 } 238 239 if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { 241 response.setStatus(HttpServletResponse.SC_FORBIDDEN); 242 return null; 243 } 244 245 return gotoLogon(mapping, form, request, response); 246 } catch (Throwable t) { 247 log.error("Failed to process authenticated request.", t); 248 throw t instanceof Exception ? (Exception ) t : new Exception (t); 249 } 250 } 251 252 263 protected ActionForward gotoLogon(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 264 request.getSession().setAttribute(Constants.ORIGINAL_REQUEST, Util.getOriginalRequest(request)); 265 return mapping.findForward("logon"); 266 } 267 268 276 public ResourceType getResourceType() { 277 return resourceType; 278 } 279 280 289 290 protected boolean requiresProfile() { 291 return true; 292 } 293 294 299 protected boolean requiresAuthentication() { 300 return true; 301 } 302 303 308 protected boolean isIgnoreSessionLock() { 309 return false; 310 } 311 312 318 void sendAuthorizationError(HttpServletResponse response) throws IOException { 319 response.setHeader("WWW-Authenticate", "Basic realm=\"SSL-Explorer\""); 320 response.sendError(HttpServletResponse.SC_UNAUTHORIZED); 321 } 322 323 334 protected ActionForward onExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 335 return mapping.findForward("success"); 336 } 337 338 352 public abstract int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response); 353 354 protected void saveError(HttpServletRequest request, String message ) { 355 saveMessage(request, message, ""); 356 } 357 358 protected void saveError(HttpServletRequest request, String message, Object ... objects) { 359 ActionMessages actionMessages = new ActionMessages(); 360 actionMessages.add(Globals.ERROR_KEY, new ActionMessage(message, objects)); 361 saveErrors(request, actionMessages); 362 } 363 364 protected void saveMessage(HttpServletRequest request, String message ) { 365 saveMessage(request, message, ""); 366 } 367 368 protected void saveMessage(HttpServletRequest request, String message, Object ... objects) { 369 ActionMessages actionMessages = new ActionMessages(); 370 actionMessages.add(Globals.MESSAGE_KEY, new ActionMessage(message, objects)); 371 saveErrors(request, actionMessages); 372 } 373 } | Popular Tags |