Commits

Sebastian Sdorra committed 1caae45

improved git error messages for failed authentication and not enough permissions

Comments (0)

Files changed (4)

scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java

 //~--- non-JDK imports --------------------------------------------------------
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Strings;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.Multimap;
 
 import java.io.File;
 import java.io.IOException;
 
+import java.util.Locale;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
+import javax.servlet.http.HttpServletRequest;
+
 /**
  *
  * @author Sebastian Sdorra
   private static final String DIRECTORY_REFS = "refs";
 
   /** Field description */
+  private static final String HEADER_USERAGENT = "User-Agent";
+
+  /** Field description */
   private static final String PREFIX_HEADS = "refs/heads/";
 
   /** Field description */
   /** Field description */
   private static final int TIMEOUT = 5;
 
+  /** Field description */
+  private static final String USERAGENT_GIT = "git/";
+
   /** the logger for GitUtil */
   private static final Logger logger = LoggerFactory.getLogger(GitUtil.class);
 
   }
 
   /**
+   * Returns true if the request comes from a git client.
+   *
+   *
+   * @param request servlet request
+   *
+   * @return true if the client is git
+   */
+  public static boolean isGitClient(HttpServletRequest request)
+  {
+    return Strings.nullToEmpty(request.getHeader(HEADER_USERAGENT)).toLowerCase(
+      Locale.ENGLISH).startsWith(USERAGENT_GIT);
+  }
+
+  /**
    * Method description
    *
    *

scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitBasicAuthenticationFilter.java

+/**
+ * Copyright (c) 2010, Sebastian Sdorra All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer. 2. Redistributions in
+ * binary form must reproduce the above copyright notice, this list of
+ * conditions and the following disclaimer in the documentation and/or other
+ * materials provided with the distribution. 3. Neither the name of SCM-Manager;
+ * nor the names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * http://bitbucket.org/sdorra/scm-manager
+ *
+ */
+
+
+
+package sonia.scm.web;
+
+//~--- non-JDK imports --------------------------------------------------------
+
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+import org.eclipse.jgit.http.server.GitSmartHttpTools;
+
+import sonia.scm.config.ScmConfiguration;
+import sonia.scm.repository.GitUtil;
+import sonia.scm.web.filter.AutoLoginModule;
+import sonia.scm.web.filter.BasicAuthenticationFilter;
+
+//~--- JDK imports ------------------------------------------------------------
+
+import java.io.IOException;
+
+import java.util.Set;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ *
+ * @author Sebastian Sdorra
+ */
+@Singleton
+public class GitBasicAuthenticationFilter extends BasicAuthenticationFilter
+{
+
+  /** Field description */
+  private static final String MESSAGE_FAILED_AUTHENTICATION =
+    "Invalid username or password.";
+
+  //~--- constructors ---------------------------------------------------------
+
+  /**
+   * Constructs ...
+   *
+   *
+   * @param configuration
+   * @param autoLoginModules
+   */
+  @Inject
+  public GitBasicAuthenticationFilter(ScmConfiguration configuration,
+    Set<AutoLoginModule> autoLoginModules)
+  {
+    super(configuration, autoLoginModules);
+  }
+
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @param request
+   * @param response
+   *
+   * @throws IOException
+   */
+  @Override
+  protected void sendFailedAuthenticationError(HttpServletRequest request,
+    HttpServletResponse response)
+    throws IOException
+  {
+    if (GitUtil.isGitClient(request))
+    {
+      GitSmartHttpTools.sendError(request, response,
+        HttpServletResponse.SC_FORBIDDEN, MESSAGE_FAILED_AUTHENTICATION);
+    }
+    else
+    {
+      super.sendFailedAuthenticationError(request, response);
+    }
+  }
+}

scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilter.java

 import com.google.inject.Inject;
 import com.google.inject.Singleton;
 
+import org.eclipse.jgit.http.server.GitSmartHttpTools;
+
+import sonia.scm.config.ScmConfiguration;
+import sonia.scm.repository.GitUtil;
 import sonia.scm.repository.RepositoryProvider;
 import sonia.scm.web.filter.ProviderPermissionFilter;
 
 //~--- JDK imports ------------------------------------------------------------
 
+import java.io.IOException;
+
 import javax.servlet.http.HttpServletRequest;
-import sonia.scm.config.ScmConfiguration;
+import javax.servlet.http.HttpServletResponse;
 
 /**
  *
   /** Field description */
   public static final String URI_REF_INFO = "/info/refs";
 
+  /** Field description */
+  private static final String MESSAGE_NOT_ENOUGH_PRIVILEGES =
+    "You do not have enough access privileges for this operation.";
+
   //~--- constructors ---------------------------------------------------------
 
   /**
    * Constructs ...
    *
-   *
-   *
-   * @param securityContextProvider
+   * @param configuration
    * @param repositoryProvider
    */
   @Inject
-  public GitPermissionFilter(
-          ScmConfiguration configuration,
-          RepositoryProvider repositoryProvider)
+  public GitPermissionFilter(ScmConfiguration configuration,
+    RepositoryProvider repositoryProvider)
   {
     super(configuration, repositoryProvider);
   }
 
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @param request
+   * @param response
+   *
+   * @throws IOException
+   */
+  @Override
+  protected void sendNotEnoughPrivilegesError(HttpServletRequest request,
+    HttpServletResponse response)
+    throws IOException
+  {
+    if (GitUtil.isGitClient(request))
+    {
+      GitSmartHttpTools.sendError(request, response,
+        HttpServletResponse.SC_FORBIDDEN, MESSAGE_NOT_ENOUGH_PRIVILEGES);
+    }
+    else
+    {
+      super.sendNotEnoughPrivilegesError(request, response);
+    }
+  }
+
   //~--- get methods ----------------------------------------------------------
 
   /**
     String uri = request.getRequestURI();
 
     return uri.endsWith(URI_RECEIVE_PACK)
-           || (uri.endsWith(URI_REF_INFO)
-               && PARAMETER_VALUE_RECEIVE.equals(
-                   request.getParameter(PARAMETER_SERVICE)));
+      || (uri.endsWith(URI_REF_INFO)
+        && PARAMETER_VALUE_RECEIVE.equals(
+          request.getParameter(PARAMETER_SERVICE)));
   }
 }

scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitServletModule.java

 import org.eclipse.jgit.transport.ScmTransportProtocol;
 
 import sonia.scm.plugin.ext.Extension;
-import sonia.scm.web.filter.BasicAuthenticationFilter;
 
 /**
  *
     bind(ScmTransportProtocol.class);
 
     // serlvelts and filters
-    filter(PATTERN_GIT).through(BasicAuthenticationFilter.class);
+    filter(PATTERN_GIT).through(GitBasicAuthenticationFilter.class);
     filter(PATTERN_GIT).through(GitPermissionFilter.class);
     serve(PATTERN_GIT).with(ScmGitServlet.class);
   }