<commit_before>/*
 * This class has been written using code from cam.cl.raven.RavenValve, a Tomcat Valve, 
 * written by <NAME> (whb21 at cam.ac.uk).
 * 
 * The main body of the code remains unchanged. All that has been added in version 1 is 
 * additional error handling and a simplified configuration.
 * 
 * RavenValve and RavenFilter achieve the same end result. 
 * 
 * The advantages of RavenFilter are that it can be used on any servlet container without
 * code modification but the Principal object containing the id of the authenticated user 
 * has to be obtained via an HttpSession attribute. 
 * 
 * RavenValve, is only usable within Tomcat.  However, it does allow the Raven user id
 * to be fetched from the standard Principal object available through HttpServletRequest.getRemoteUser(). 
 * 
 * 
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package uk.ac.cam.ucs.webauth;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A Servlet Filter which ensures a user is Raven authenticated.
 * 
 * <h1>Quick Configuration</h2>
 * 
 * <h3>Install Webauth package</h3>
 * 
 * Ensure you have the Raven Java Toolkit classes installed.
 * 
 * <h3>Install the Raven public key certificate</h3><commit_msg>Removed unused import<commit_after>/*
 * This class has been written using code from cam.cl.raven.RavenValve, a Tomcat Valve, 
 * written by <NAME> (whb21 at cam.ac.uk).
 * 
 * The main body of the code remains unchanged. All that has been added in version 1 is 
 * additional error handling and a simplified configuration.
 * 
 * RavenValve and RavenFilter achieve the same end result. 
 * 
 * The advantages of RavenFilter are that it can be used on any servlet container without
 * code modification but the Principal object containing the id of the authenticated user 
 * has to be obtained via an HttpSession attribute. 
 * 
 * RavenValve, is only usable within Tomcat.  However, it does allow the Raven user id
 * to be fetched from the standard Principal object available through HttpServletRequest.getRemoteUser(). 
 * 
 * 
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package uk.ac.cam.ucs.webauth;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A Servlet Filter which ensures a user is Raven authenticated.
 * 
 * <h1>Quick Configuration</h2>
 * 
 * <h3>Install Webauth package</h3>
 * 
 * Ensure you have the Raven Java Toolkit classes installed.
 * 
 * <h3>Install the Raven public key certificate</h3>
{'fraction_non_alphanumeric': 0.07384672619047619, 'fraction_numerical': 0.003720238095238095, 'mean_word_length': 4.447821681864235, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6715271', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1386, 'n_words': 587}