<commit_before>package com.hpcloud.middleware;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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 com.google.common.util.concurrent.UncheckedExecutionException;
import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A token-based authentication filter. This filter uses Thrift protocol to
 * communicate with the CS server. The token to validate is set via the header
 * {@link #TOKEN}.
 * <p>
 * A token is required to validate. However, if no token is presented, the
 * filter will set the {@link #AUTH_IDENTITY_STATUS} request parameter to
 * <code>Invalid</code> and let any other filter downstream to decide what to
 * do. For instance, if a downstream filter knows how to deal with signature
 * rather than tokens, then it will go ahead and validate with signatures.
 * <p>
 * Upon successful validation, all the Auth request parameters will be
 * populated, including information such as tenant, user and user roles, and
 * passed down to the next filter downstream.
 * <p>
 * Upon unsuccessful validation, this filter will terminate the request by
 * returning a 401 (unauthorized).
 * 
 * @author liemmn
 * 
 */
public class TokenAuth implements Filter, com.hpcloud.middleware.AuthConstants {

	private static final String TOKEN_NOTFOUND = "Bad Request: Token not found in the request";
	private static final String SERVICE_IDS_PARAM = "serviceIds";
	private static final String ENDPOINT_IDS_PARAM = "endpointIds";
	private static final String SERVICE_CATALOG_PARAM = "includeCatalog";
	private static final String API_VERSION_PARAM = "apiVersion";

  private final com.hpcloud.middleware.Config appConfig = Config.getInstance();
	
	private FilterConfig filterConfig;

	// Thee faithful logger
	private static final Logger logger = LoggerFactory
			.getLogger(TokenAuth.class);

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;		
	}


	/**
	 * {@inheritDoc}
	 */
	public void destroy() {
		FilterUtils.destroyFilter();
	}

	/**
	 * {@inheritDoc}
	 */
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		Object auth = null;
		int numberOfTries = 0;		
		if (!appConfig.isInitialized()) {
			appConfig.initialize(filterConfig,req,getInputParams());
		}		
		int retries = appConfig.getRetries();
		long pauseTime = appConfig.getPauseTime();
    AuthClientFactory factory = appConfig.getFactory();

		// Extract credential
    String token = ((HttpServletRequest) req).getHeader(TOKEN);

		if (token == null) {
			if (!appConfig.isDelayAuthDecision()) {
				logger.error(HttpServletResponse.SC_UNAUTHORIZED
						+ " No token found.");
				((HttpServletResponse) resp).sendError(
						HttpServletResponse.SC_UNAUTHORIZED, TOKEN_NOTFOUND);
				return;
			} else {
				logger.info("No token found...Skipping");
			}
    } else {
      do {
        try {
          auth = FilterUtils.getCachedToken(token);
        }catch(ServiceUnavailableException e) {
          if (numberOfTries < retries) {
            FilterUtils.pause(pauseTime);
            logger.debug("Retrying connection after "
              + pauseTime + " seconds.");
            numberOfTries++;
            continue;
          } else {
            logger.debug("Exhausted retries..");
            TokenExceptionHandler handler = TokenExceptionHandler
              .valueOf("ServiceUnavailableException");
            handler.onException(e, resp, token);
          }
          return;
        } catch(ClientProtocolException e) {
          if (numberOfTries < retries) {
            FilterUtils.pause(pauseTime);
            logger.debug("Retrying connection after "
              + pauseTime + " seconds.");
            numberOfTries++;
            continue;
          } else {
            logger.debug("Exhausted retries..");
            TokenExceptionHandler handler = TokenExceptionHandler
              .valueOf("ClientProtocolException");
            handler.onException(e, resp, token);
          }
          return;
        } catch (UncheckedExecutionException e) {
          TokenExceptionHandler handler = TokenExceptionHandler
            .valueOf("UncheckedExecutionException");
          handler.onException(e, resp, token);
          return;
        }


      }while(auth==null && numberOfTries<=retries);
    }
		req = FilterUtils.wrapRequest(req, auth);
		logger.debug("TokenAuth: Forwarding down stream to next filter/servlet");
		// Forward downstream...
		chain.doFilter(req, resp);
	}
	
	private Map<String, String> getInputParams() {
		Map<String, String> inputParams = new HashMap<String, String>();
		if (appConfig.getServiceIds() != null) {
			inputParams.put(SERVICE_IDS_PARAM, appConfig.getServiceIds());
		}
		if (appConfig.getEndpointIds() != null) {
			inputParams.put(ENDPOINT_IDS_PARAM, appConfig.getEndpointIds());
		}
		inputParams.put(SERVICE_CATALOG_PARAM, String.valueOf(appConfig.isIncludeCatalog()));
		inputParams.put(API_VERSION_PARAM, appConfig.getAuthVersion());
		return inputParams;
	}


}
<commit_msg>Add CORS support to auth middleware<commit_after>package com.hpcloud.middleware;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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 com.google.common.util.concurrent.UncheckedExecutionException;
import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A token-based authentication filter. This filter uses Thrift protocol to
 * communicate with the CS server. The token to validate is set via the header
 * {@link #TOKEN}.
 * <p/>
 * A token is required to validate. However, if no token is presented, the
 * filter will set the {@link #AUTH_IDENTITY_STATUS} request parameter to
 * <code>Invalid</code> and let any other filter downstream to decide what to
 * do. For instance, if a downstream filter knows how to deal with signature
 * rather than tokens, then it will go ahead and validate with signatures.
 * <p/>
 * Upon successful validation, all the Auth request parameters will be
 * populated, including information such as tenant, user and user roles, and
 * passed down to the next filter downstream.
 * <p/>
 * Upon unsuccessful validation, this filter will terminate the request by
 * returning a 401 (unauthorized).
 *
 * @author liemmn
 */
public class TokenAuth implements Filter, com.hpcloud.middleware.AuthConstants {

  private static final String TOKEN_NOTFOUND = "Bad Request: Token not found in the request";
  private static final String SERVICE_IDS_PARAM = "serviceIds";
  private static final String ENDPOINT_IDS_PARAM = "endpointIds";
  private static final String SERVICE_CATALOG_PARAM = "includeCatalog";
  private static final String API_VERSION_PARAM = "apiVersion";

  private final com.hpcloud.middleware.Config appConfig = Config.getInstance();

  private FilterConfig filterConfig;

  // Thee faithful logger
  private static final Logger logger = LoggerFactory
          .getLogger(TokenAuth.class);

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
  }


  /**
   * {@inheritDoc}
   */
  public void destroy() {
    FilterUtils.destroyFilter();
  }

  /**
   * {@inheritDoc}
   */
  public void doFilter(ServletRequest req, ServletResponse resp,
                       FilterChain chain) throws IOException, ServletException {
    // According to CORS spec OPTIONS method does not pass auth info
    if (((HttpServletRequest) req).getMethod().equals("OPTIONS")) {
      chain.doFilter(req, resp);
      return;
    }
    Object auth = null;
    int numberOfTries = 0;
    if (!appConfig.isInitialized()) {
      appConfig.initialize(filterConfig, req, getInputParams());
    }
    int retries = appConfig.getRetries();
    long pauseTime = appConfig.getPauseTime();
    AuthClientFactory factory = appConfig.getFactory();

    // Extract credential
    String token = ((HttpServletRequest) req).getHeader(TOKEN);

    if (token == null) {
      if (!appConfig.isDelayAuthDecision()) {
        logger.error(HttpServletResponse.SC_UNAUTHORIZED
                + " No token found.");
        ((HttpServletResponse) resp).sendError(
                HttpServletResponse.SC_UNAUTHORIZED, TOKEN_NOTFOUND);
        return;
      } else {
        logger.info("No token found...Skipping");
      }
    } else {
      do {
        try {
          auth = FilterUtils.getCachedToken(token);
        } catch (ServiceUnavailableException e) {
          if (numberOfTries < retries) {
            FilterUtils.pause(pauseTime);
            logger.debug("Retrying connection after "
                    + pauseTime + " seconds.");
            numberOfTries++;
            continue;
          } else {
            logger.debug("Exhausted retries..");
            TokenExceptionHandler handler = TokenExceptionHandler
                    .valueOf("ServiceUnavailableException");
            handler.onException(e, resp, token);
          }
          return;
        } catch (ClientProtocolException e) {
          if (numberOfTries < retries) {
            FilterUtils.pause(pauseTime);
            logger.debug("Retrying connection after "
                    + pauseTime + " seconds.");
            numberOfTries++;
            continue;
          } else {
            logger.debug("Exhausted retries..");
            TokenExceptionHandler handler = TokenExceptionHandler
                    .valueOf("ClientProtocolException");
            handler.onException(e, resp, token);
          }
          return;
        } catch (UncheckedExecutionException e) {
          TokenExceptionHandler handler = TokenExceptionHandler
                  .valueOf("UncheckedExecutionException");
          handler.onException(e, resp, token);
          return;
        }


      } while (auth == null && numberOfTries <= retries);
    }
    req = FilterUtils.wrapRequest(req, auth);
    logger.debug("TokenAuth: Forwarding down stream to next filter/servlet");
    // Forward downstream...
    chain.doFilter(req, resp);
  }

  private Map<String, String> getInputParams() {
    Map<String, String> inputParams = new HashMap<String, String>();
    if (appConfig.getServiceIds() != null) {
      inputParams.put(SERVICE_IDS_PARAM, appConfig.getServiceIds());
    }
    if (appConfig.getEndpointIds() != null) {
      inputParams.put(ENDPOINT_IDS_PARAM, appConfig.getEndpointIds());
    }
    inputParams.put(SERVICE_CATALOG_PARAM, String.valueOf(appConfig.isIncludeCatalog()));
    inputParams.put(API_VERSION_PARAM, appConfig.getAuthVersion());
    return inputParams;
  }


}

{'fraction_non_alphanumeric': 0.0968112357565586, 'fraction_numerical': 0.0010599770338309336, 'mean_word_length': 3.210487169951655, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6745154', 'n_tokens_mistral': 3094, 'n_tokens_neox': 2987, 'n_words': 825}