Coverage Report - org.kuali.rice.kew.web.UserLoginFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
UserLoginFilter
0%
0/44
0%
0/22
2.714
UserLoginFilter$1
0%
0/2
N/A
2.714
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.web;
 18  
 
 19  
 import java.io.IOException;
 20  
 
 21  
 import javax.servlet.Filter;
 22  
 import javax.servlet.FilterChain;
 23  
 import javax.servlet.FilterConfig;
 24  
 import javax.servlet.ServletException;
 25  
 import javax.servlet.ServletRequest;
 26  
 import javax.servlet.ServletResponse;
 27  
 import javax.servlet.http.HttpServletRequest;
 28  
 import javax.servlet.http.HttpServletRequestWrapper;
 29  
 import javax.servlet.http.HttpServletResponse;
 30  
 
 31  
 import org.apache.commons.lang.StringUtils;
 32  
 import org.apache.log4j.Logger;
 33  
 import org.apache.log4j.MDC;
 34  
 import org.kuali.rice.core.exception.RiceRuntimeException;
 35  
 import org.kuali.rice.kew.util.KEWConstants;
 36  
 import org.kuali.rice.kew.web.session.UserSession;
 37  
 import org.kuali.rice.kim.bo.entity.KimPrincipal;
 38  
 import org.kuali.rice.kim.service.IdentityManagementService;
 39  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 40  
 
 41  
 
 42  
 /**
 43  
  * A filter for processing user logins and creating a {@link UserSession}.
 44  
  *
 45  
  * @see UserSession
 46  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 47  
  */
 48  0
 public class UserLoginFilter implements Filter {
 49  0
     private static final Logger LOG = Logger.getLogger(UserLoginFilter.class);
 50  
 
 51  0
     public void init(FilterConfig config) throws ServletException {}
 52  
 
 53  
     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
 54  0
         if (!(req instanceof HttpServletRequest && res instanceof HttpServletResponse)) {
 55  0
             chain.doFilter(req, res);
 56  0
             return;
 57  
         }
 58  
 
 59  0
         LOG.debug("Begin UserLoginFilter...");
 60  
 
 61  0
         HttpServletRequest request = (HttpServletRequest) req;
 62  0
         HttpServletResponse response = (HttpServletResponse) res;
 63  
 
 64  
         final UserSession userSession;
 65  0
         if (!isUserSessionEstablished(request)) {
 66  0
             userSession = login(request);
 67  0
             if (userSession != null) {
 68  0
                 request.getSession().setAttribute(KEWConstants.USER_SESSION_KEY, userSession);
 69  
             }
 70  
         } else {
 71  0
             userSession = (UserSession) request.getSession().getAttribute(KEWConstants.USER_SESSION_KEY);
 72  
         }
 73  
 
 74  0
         if (userSession != null) {
 75  
             // Override the HttpServletRequest with one that provides
 76  
             // our logged-in user. This allows any engine-agnostic webapp code
 77  
             // that may be living in the context to obtain remote user traditionally
 78  0
             LOG.debug("Wrapping servlet request: " + userSession.getPrincipalName());
 79  0
             request = new HttpServletRequestWrapper(request) {
 80  
                 public String getRemoteUser() {
 81  0
                     return userSession.getPrincipalName();
 82  
                 }
 83  
             };
 84  
         }
 85  
 
 86  
         // set up the thread local reference to the current authenticated user
 87  
         // and then forward to next filter in the chain
 88  0
         MDC.put("user", userSession.getPrincipalName());
 89  
         try {
 90  0
             UserSession.setAuthenticatedUser(userSession);
 91  0
             LOG.debug("...end UserLoginFilter.");
 92  0
             chain.doFilter(request, response);
 93  0
         } finally {
 94  0
                 MDC.remove("user");
 95  0
             UserSession.setAuthenticatedUser(null);
 96  0
         }
 97  
 
 98  0
     }
 99  
 
 100  
     /**
 101  
      * Checks if the user who made the request has a UserSession established
 102  
      *
 103  
      * @param request
 104  
      *            the HTTPServletRequest object passed in
 105  
      * @return true if the user session has been established, false otherwise
 106  
      */
 107  
     public static boolean isUserSessionEstablished(HttpServletRequest request) {
 108  0
         return (request.getSession(false) != null && request.getSession(false).getAttribute(KEWConstants.USER_SESSION_KEY) != null);
 109  
     }
 110  
 
 111  
     /**
 112  
      * create a UserSession object for the workflow user
 113  
      *
 114  
      * @param request
 115  
      *            the servlet request
 116  
      * @return UserSession object if authentication was successful, null otherwise
 117  
      */
 118  
     protected UserSession login(HttpServletRequest request) {
 119  0
         LOG.info("performing user login: ");
 120  
 
 121  0
         String principalName = null;
 122  0
         KimPrincipal principal = null;
 123  
 
 124  0
         IdentityManagementService idmService = KIMServiceLocator.getIdentityManagementService();
 125  0
         principalName = idmService.getAuthenticatedPrincipalName(request);
 126  
                 
 127  0
         if ( LOG.isDebugEnabled() ) {
 128  0
                 LOG.debug("Looking up principal by name: " + principalName);
 129  
         }
 130  
         
 131  0
         principal = idmService.getPrincipalByPrincipalName(principalName);
 132  
 
 133  0
         if (StringUtils.isBlank(principalName) || principal == null) {
 134  0
                 throw new RiceRuntimeException("KIM could not identify an authenticated principal from incoming request.  The principal name was " + principalName);
 135  
         }
 136  
         
 137  0
         if ( LOG.isDebugEnabled() ) {
 138  0
                 LOG.debug("ending user lookup: " + principal);
 139  
         }
 140  
 
 141  0
         UserSession userSession = new UserSession(principal);
 142  0
         LOG.info("...finished performing user login.");
 143  0
         return userSession;
 144  
     }
 145  
 
 146  
     public static UserSession getUserSession(HttpServletRequest request) {
 147  0
         return (UserSession) request.getSession().getAttribute(KEWConstants.USER_SESSION_KEY);
 148  
     }
 149  
 
 150  0
     public void destroy() {}
 151  
 }