View Javadoc

1   package com.atlassian.plugins.rest.module.sal.websudo;
2   
3   import com.atlassian.plugins.rest.common.sal.websudo.WebSudoResourceContext;
4   import com.atlassian.plugins.rest.module.servlet.ServletUtils;
5   import com.atlassian.sal.api.websudo.WebSudoManager;
6   
7   import javax.servlet.http.HttpServletRequest;
8   
9   public class SalWebSudoResourceContext implements WebSudoResourceContext {
10      private static final String BASIC_AUTHZ_TYPE_PREFIX = "Basic ";
11  
12      private final WebSudoManager webSudoManager;
13  
14      public SalWebSudoResourceContext(final WebSudoManager webSudoManager) {
15          this.webSudoManager = webSudoManager;
16      }
17  
18      /**
19       * Checks if WebSudo protection is required.
20       * <p>
21       * <ul>
22       * <li>If clients authenticate using Basic-Auth WebSudo is not required.</li>
23       * <li>If the current request is already protected (or if WebSudo is disabled in the host application) WebSudo is not required.</li>
24       * </ul>
25       *
26       * @return true if resource need to be protected by WebSudo
27       */
28      public boolean shouldEnforceWebSudoProtection() {
29          final HttpServletRequest r = ServletUtils.getHttpServletRequest();
30          // If the servlet request is null (presumably because we are not running in a servlet container) there is no point in making use of WebSudo.
31          if (null == r) {
32              return false;
33          }
34  
35          // We can skip web sudo if this is a request authenticated by BASIC-AUTH
36          final String authHeader = r.getHeader("Authorization"); // as per RFC2616
37          if (null != authHeader && authHeader.startsWith(BASIC_AUTHZ_TYPE_PREFIX)) {
38              return false;
39          }
40  
41          return !webSudoManager.canExecuteRequest(r);
42      }
43  }