Package org.apache.roller.weblogger.ui.core.filters

Source Code of org.apache.roller.weblogger.ui.core.filters.ValidateSaltFilter

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  The ASF licenses this file to You
* under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.  For additional information regarding
* copyright in this work, please see the NOTICE file in the top level
* directory of this distribution.
*/

package org.apache.roller.weblogger.ui.core.filters;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.WebloggerException;
import org.apache.roller.weblogger.business.UserManager;
import org.apache.roller.weblogger.business.WebloggerFactory;
import org.apache.roller.weblogger.pojos.User;
import org.apache.roller.weblogger.ui.rendering.util.cache.SaltCache;

/**
* Filter checks all POST request for presence of valid salt value and rejects
* those without a salt value or with a salt value not generated by this Roller
* instance.
*/
public class ValidateSaltFilter implements Filter  {
    private static Log log = LogFactory.getLog(ValidateSaltFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    try {
      HttpServletRequest httpReq = (HttpServletRequest) request;
      final User authenticUser;
      UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
      if (httpReq.getUserPrincipal() != null) {
        try {
          authenticUser = umgr.getUserByUserName(httpReq.getUserPrincipal().getName(), Boolean.TRUE);
        } catch (WebloggerException ex) {
          log.error("ERROR checking user rile", ex);
          throw new ServletException("Security Violation");
        }
      } else {
        authenticUser = null;
      }

      if (httpReq.getMethod().equals("POST") && authenticUser != null) {
        String salt = (String) httpReq.getParameter("salt");
        SaltCache saltCache = SaltCache.getInstance();
        if (salt == null || saltCache.get(salt) == null || !saltCache.get(salt).equals(authenticUser.getId())) {
          throw new ServletException("Security Violation");
        }
      }
   
    } catch (Exception e) {
      log.error("Error validating salt", e);
    }

        chain.doFilter(request, response);
    }
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void destroy() {
    }
}
TOP

Related Classes of org.apache.roller.weblogger.ui.core.filters.ValidateSaltFilter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.