| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
package net.sourceforge.pebble.web.controller; |
| 33 |
|
|
| 34 |
|
import net.sourceforge.pebble.Constants; |
| 35 |
|
import net.sourceforge.pebble.domain.AbstractBlog; |
| 36 |
|
import net.sourceforge.pebble.domain.Blog; |
| 37 |
|
import net.sourceforge.pebble.util.SecurityUtils; |
| 38 |
|
import net.sourceforge.pebble.util.Utilities; |
| 39 |
|
import net.sourceforge.pebble.web.action.Action; |
| 40 |
|
import net.sourceforge.pebble.web.action.ActionFactory; |
| 41 |
|
import net.sourceforge.pebble.web.action.ActionNotFoundException; |
| 42 |
|
import net.sourceforge.pebble.web.action.SecureAction; |
| 43 |
|
import net.sourceforge.pebble.web.model.Model; |
| 44 |
|
import net.sourceforge.pebble.web.view.View; |
| 45 |
|
import org.apache.commons.logging.Log; |
| 46 |
|
import org.apache.commons.logging.LogFactory; |
| 47 |
|
|
| 48 |
|
import javax.servlet.ServletException; |
| 49 |
|
import javax.servlet.http.*; |
| 50 |
|
import java.io.IOException; |
| 51 |
|
import java.security.SecureRandom; |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
@author |
| 58 |
|
|
|
|
|
| 0% |
Uncovered Elements: 129 (129) |
Complexity: 28 |
Complexity Density: 0,32 |
|
| 59 |
|
public class HttpController extends HttpServlet { |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
private static Log log = LogFactory.getLog(HttpController.class); |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
public static final String PEBBLE_SECURITY_TOKEN_PARAMETER = "pebbleSecurityToken"; |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
private static final String PEBBLE_SECURITY_TOKEN_HEADER = "X-Pebble-Token"; |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
private static final String PEBBLE_SECURITY_TOKEN_HEADER_NOCHECK = "nocheck"; |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
private ActionFactory actionFactory; |
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
private String actionExtension = ".action"; |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
private static final SecureRandom random = new SecureRandom(); |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 99 |
0
|
public void init() {... |
| 100 |
0
|
String actions = getServletConfig().getInitParameter("actions"); |
| 101 |
0
|
this.actionExtension = getServletConfig().getInitParameter("actionExtension"); |
| 102 |
0
|
this.actionFactory = new ActionFactory(actions); |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
@param |
| 109 |
|
@param |
| 110 |
|
|
|
|
|
| 0% |
Uncovered Elements: 44 (44) |
Complexity: 7 |
Complexity Density: 0,19 |
|
| 111 |
0
|
protected void processRequest(HttpServletRequest request,... |
| 112 |
|
HttpServletResponse response) |
| 113 |
|
throws ServletException, IOException { |
| 114 |
|
|
| 115 |
0
|
AbstractBlog blog = (AbstractBlog) request.getAttribute(Constants.BLOG_KEY); |
| 116 |
|
|
| 117 |
|
|
| 118 |
0
|
String actionName = request.getRequestURI(); |
| 119 |
0
|
if (actionName.indexOf("?") > -1) { |
| 120 |
|
|
| 121 |
0
|
actionName = actionName.substring(0, actionName.indexOf("?")); |
| 122 |
|
} |
| 123 |
0
|
int index = actionName.lastIndexOf("/"); |
| 124 |
0
|
actionName = actionName.substring(index + 1, (actionName.length() - actionExtension.length())); |
| 125 |
0
|
Action action; |
| 126 |
|
|
| 127 |
0
|
try { |
| 128 |
0
|
log.debug("Action is " + actionName); |
| 129 |
0
|
action = actionFactory.getAction(actionName); |
| 130 |
|
} catch (ActionNotFoundException anfe) { |
| 131 |
0
|
log.warn(anfe.getMessage()); |
| 132 |
0
|
response.sendError(HttpServletResponse.SC_NOT_FOUND); |
| 133 |
0
|
return; |
| 134 |
|
} |
| 135 |
|
|
| 136 |
0
|
boolean authorised = isAuthorised(request, action); |
| 137 |
0
|
if (!authorised) { |
| 138 |
0
|
response.sendError(HttpServletResponse.SC_FORBIDDEN); |
| 139 |
|
} else { |
| 140 |
0
|
boolean validated = validateSecurityToken(request, response, action); |
| 141 |
0
|
if (!validated) { |
| 142 |
0
|
response.sendError(HttpServletResponse.SC_FORBIDDEN, "No security token"); |
| 143 |
|
} else { |
| 144 |
0
|
try { |
| 145 |
0
|
Model model = new Model(); |
| 146 |
0
|
model.put(Constants.BLOG_KEY, blog); |
| 147 |
0
|
String calculatedBaseUrl = Utilities.calcBaseUrl(request.getScheme(), blog.getUrl()); |
| 148 |
0
|
model.put(Constants.BLOG_URL, blog.getUrl()); |
| 149 |
0
|
action.setModel(model); |
| 150 |
0
|
View view = action.process(request, response); |
| 151 |
0
|
if (view != null) { |
| 152 |
|
|
| 153 |
0
|
view.setModel(model); |
| 154 |
0
|
view.setServletContext(this.getServletContext()); |
| 155 |
|
|
| 156 |
0
|
view.prepare(); |
| 157 |
|
|
| 158 |
0
|
for (Object key : model.keySet()) { |
| 159 |
0
|
request.setAttribute(key.toString(), model.get(key.toString())); |
| 160 |
|
} |
| 161 |
|
|
| 162 |
0
|
response.setContentType(view.getContentType()); |
| 163 |
0
|
view.dispatch(request, response, getServletContext()); |
| 164 |
|
|
| 165 |
|
} |
| 166 |
|
} catch (Exception e) { |
| 167 |
0
|
request.setAttribute("exception", e); |
| 168 |
0
|
throw new ServletException(e); |
| 169 |
|
} |
| 170 |
|
} |
| 171 |
|
} |
| 172 |
|
} |
| 173 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
| 174 |
0
|
private boolean isAuthorised(HttpServletRequest request, Action action) {... |
| 175 |
0
|
if (action instanceof SecureAction) { |
| 176 |
0
|
SecureAction secureAction = (SecureAction) action; |
| 177 |
0
|
return isUserInRole(request, secureAction); |
| 178 |
|
} else { |
| 179 |
0
|
return true; |
| 180 |
|
} |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
@param |
| 188 |
|
@param |
| 189 |
|
@return |
| 190 |
|
|
|
|
|
| 0% |
Uncovered Elements: 21 (21) |
Complexity: 5 |
Complexity Density: 0,38 |
|
| 191 |
0
|
private boolean isUserInRole(HttpServletRequest request, SecureAction action) {... |
| 192 |
0
|
AbstractBlog ab = (AbstractBlog) request.getAttribute(Constants.BLOG_KEY); |
| 193 |
0
|
String currentUser = SecurityUtils.getUsername(); |
| 194 |
0
|
String roles[] = action.getRoles(request); |
| 195 |
0
|
for (String role : roles) { |
| 196 |
0
|
if (role.equals(Constants.ANY_ROLE)) { |
| 197 |
0
|
return true; |
| 198 |
0
|
} else if (SecurityUtils.isUserInRole(role)) { |
| 199 |
0
|
if (ab instanceof Blog) { |
| 200 |
0
|
Blog blog = (Blog) ab; |
| 201 |
0
|
if (blog.isUserInRole(role, currentUser)) { |
| 202 |
0
|
return true; |
| 203 |
|
} |
| 204 |
|
} else { |
| 205 |
0
|
return true; |
| 206 |
|
} |
| 207 |
|
} |
| 208 |
|
} |
| 209 |
0
|
return false; |
| 210 |
|
} |
| 211 |
|
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0,43 |
|
| 212 |
0
|
private boolean validateSecurityToken(HttpServletRequest request, HttpServletResponse response, Action action) {... |
| 213 |
|
|
| 214 |
0
|
String token = ensureSecurityTokenExists(request, response); |
| 215 |
0
|
if (action.getClass().getAnnotation(RequireSecurityToken.class) != null) { |
| 216 |
|
|
| 217 |
|
|
| 218 |
0
|
if (PEBBLE_SECURITY_TOKEN_HEADER_NOCHECK.equals(request.getHeader(PEBBLE_SECURITY_TOKEN_HEADER))) { |
| 219 |
0
|
return true; |
| 220 |
|
} |
| 221 |
|
|
| 222 |
0
|
String requestToken = request.getParameter(PEBBLE_SECURITY_TOKEN_PARAMETER); |
| 223 |
|
|
| 224 |
0
|
return token.equals(requestToken); |
| 225 |
|
} else { |
| 226 |
0
|
return true; |
| 227 |
|
} |
| 228 |
|
} |
| 229 |
|
|
|
|
|
| 0% |
Uncovered Elements: 34 (34) |
Complexity: 8 |
Complexity Density: 0,36 |
|
| 230 |
0
|
private String ensureSecurityTokenExists(HttpServletRequest request, HttpServletResponse response) {... |
| 231 |
0
|
String token = (String) request.getAttribute(PEBBLE_SECURITY_TOKEN_PARAMETER); |
| 232 |
0
|
if (token != null) { |
| 233 |
|
|
| 234 |
0
|
return token; |
| 235 |
|
} |
| 236 |
0
|
String contextPath = request.getContextPath(); |
| 237 |
0
|
for (Cookie cookie : request.getCookies()) { |
| 238 |
0
|
if (HttpController.PEBBLE_SECURITY_TOKEN_PARAMETER.equals(cookie.getName())) { |
| 239 |
0
|
if (contextPath != null && contextPath.length() > 0) { |
| 240 |
0
|
if (contextPath.equals(cookie.getPath())) { |
| 241 |
0
|
token = cookie.getValue(); |
| 242 |
0
|
break; |
| 243 |
|
} |
| 244 |
|
} else { |
| 245 |
0
|
token = cookie.getValue(); |
| 246 |
0
|
break; |
| 247 |
|
} |
| 248 |
|
} |
| 249 |
|
} |
| 250 |
|
|
| 251 |
0
|
if (token == null) { |
| 252 |
0
|
token = ""; |
| 253 |
0
|
while (token.length() < 12) { |
| 254 |
0
|
token += Long.toHexString(random.nextLong()); |
| 255 |
|
} |
| 256 |
|
|
| 257 |
0
|
Cookie cookie = new Cookie(HttpController.PEBBLE_SECURITY_TOKEN_PARAMETER, token); |
| 258 |
|
|
| 259 |
0
|
cookie.setMaxAge(-1); |
| 260 |
0
|
cookie.setPath(contextPath); |
| 261 |
0
|
response.addCookie(cookie); |
| 262 |
|
} |
| 263 |
|
|
| 264 |
0
|
request.setAttribute(PEBBLE_SECURITY_TOKEN_PARAMETER, token); |
| 265 |
0
|
return token; |
| 266 |
|
} |
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
|
| 271 |
|
@param |
| 272 |
|
@param |
| 273 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 274 |
0
|
protected void doGet(HttpServletRequest req, HttpServletResponse res)... |
| 275 |
|
throws ServletException, IOException { |
| 276 |
0
|
processRequest(req, res); |
| 277 |
|
} |
| 278 |
|
|
| 279 |
|
|
| 280 |
|
|
| 281 |
|
|
| 282 |
|
@param |
| 283 |
|
@param |
| 284 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 285 |
0
|
protected void doPost(HttpServletRequest req, HttpServletResponse res)... |
| 286 |
|
throws ServletException, IOException { |
| 287 |
0
|
processRequest(req, res); |
| 288 |
|
} |
| 289 |
|
|
| 290 |
|
} |