// Copyright (C) 2013 The Android Open Source Project
//
// Licensed 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.
package com.googlesource.gerrit.plugins.github.oauth;

import java.io.IOException;
import java.util.regex.Pattern;

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 org.slf4j.LoggerFactory;

import com.google.gerrit.httpd.GitOverHttpServlet;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;

@Singleton
public class OAuthFilter implements Filter {
  private static final org.slf4j.Logger log = LoggerFactory
      .getLogger(OAuthFilter.class);
  private static Pattern GIT_HTTP_REQUEST_PATTERN = Pattern
      .compile(GitOverHttpServlet.URL_REGEX);

  private final GitHubOAuthConfig config;
  private final OAuthGitFilter gitFilter;
  private final OAuthWebFilter webFilter;

  @Inject
  public OAuthFilter(GitHubOAuthConfig config, 
      OAuthWebFilter webFilter, Injector injector) {
    this.config = config;
    this.webFilter = webFilter;
    Injector childInjector = injector.createChildInjector(OAuthCache.module());
    this.gitFilter = childInjector.getInstance(OAuthGitFilter.class);
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    gitFilter.init(filterConfig);
    webFilter.init(filterConfig);
  }

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

    if (!config.enabled) {
      chain.doFilter(request, response);
      return;
    }

    String requestUrl = ((HttpServletRequest) request).getRequestURI();
    if (GIT_HTTP_REQUEST_PATTERN.matcher(requestUrl).matches()) {
      gitFilter.doFilter(request, response, chain);
    } else {
      webFilter.doFilter(request, response, chain);
    }
  }

  @Override
  public void destroy() {
    log.info("Destroy");
    gitFilter.destroy();
    webFilter.destroy();
  }
}

@@ -676,32 +676,79 @@
 io.IOException;%0A
+import java.util.Arrays;%0Aimport java.util.Set;%0A
 import java.util
@@ -1038,29 +1038,112 @@
 org.
-slf4j.LoggerFactory;%0A
+apache.commons.lang.StringUtils;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport com.google.common.collect.Sets;
 %0Aimp
@@ -1186,24 +1186,68 @@
 ttpServlet;%0A
+import com.google.gerrit.httpd.XGerritAuth;%0A
 import com.g
@@ -1597,16 +1597,208 @@
 _REGEX);
+%0A  private static final Set%3CString%3E GERRIT_STATIC_RESOURCES_EXTS = Sets%0A      .newHashSet(Arrays.asList(%22css%22, %22png%22, %22jpg%22, %22gif%22, %22woff%22, %22otf%22,%0A          %22ttf%22, %22map%22, %22js%22, %22swf%22, %22txt%22));
 %0A%0A  priv
@@ -1974,17 +1974,16 @@
  config,
- 
 %0A      O
@@ -2551,32 +2551,206 @@
 n %7B%0A
-%0A    if (!config.enabled
+    HttpServletRequest httpRequest = (HttpServletRequest) request;%0A%0A    if (!config.enabled %7C%7C isStaticResource(httpRequest)%0A        %7C%7C isRpcCall(httpRequest) %7C%7C isAuthenticatedRestCall(httpRequest)
 ) %7B%0A
@@ -2839,38 +2839,19 @@
 l = 
-((HttpServletRequest) request)
+httpRequest
 .get
@@ -3054,24 +3054,635 @@
 %0A    %7D%0A  %7D%0A%0A
+  private boolean isAuthenticatedRestCall(HttpServletRequest httpRequest) %7B%0A    return !StringUtils.isEmpty(httpRequest%0A        .getHeader(XGerritAuth.X_GERRIT_AUTH));%0A  %7D%0A%0A  private boolean isStaticResource(HttpServletRequest httpRequest) %7B%0A    String pathExt =%0A        StringUtils.substringAfterLast(httpRequest.getRequestURI(), %22.%22);%0A    if (StringUtils.isEmpty(pathExt)) %7B%0A      return false;%0A    %7D%0A%0A    return GERRIT_STATIC_RESOURCES_EXTS.contains(pathExt.toLowerCase());%0A  %7D%0A%0A  private boolean isRpcCall(HttpServletRequest httpRequest) %7B%0A    return httpRequest.getRequestURI().indexOf(%22/rpc/%22) %3E= 0;%0A  %7D%0A%0A
   @Override%0A

Avoid OAuth with REST and RPC calls and static resources