]> source.dussan.org Git - gitblit.git/commitdiff
Implemented custom request handling for (un)authenticated sessions to workaround...
authorJames Moger <james.moger@gitblit.com>
Fri, 24 Aug 2012 17:32:44 +0000 (13:32 -0400)
committerJames Moger <james.moger@gitblit.com>
Fri, 24 Aug 2012 17:32:44 +0000 (13:32 -0400)
docs/04_releases.mkd
src/com/gitblit/wicket/AuthorizationStrategy.java
src/com/gitblit/wicket/GitBlitWebSession.java
src/com/gitblit/wicket/pages/BasePage.java
src/com/gitblit/wicket/pages/ChangePasswordPage.java
src/com/gitblit/wicket/pages/RepositoryPage.java
src/com/gitblit/wicket/pages/RootPage.java

index 074396c706551d83208f0ca6f028499d2f349d04..21b47bab9a43568077cea098967403821d8ff7ea 100644 (file)
@@ -11,6 +11,7 @@ If you are updating from an earlier release AND you have indexed branches with t
 \r
 #### fixes\r
 \r
+- Bypass Wicket's inability to handle direct url addressing of a view-restricted, grouped repository for new, unauthenticated sessions (e.g. click link from email or rss feed without having an active Wicket session)\r
 - Fixed MailExecutor's failure to cope with mail server connection troubles resulting in 100% CPU usage\r
 - Fixed generated urls in Groovy *sendmail* hook script for grouped repositories\r
 - Fixed generated urls in RSS feeds for grouped repositories\r
index 452215a71102c10498237d329a2c37698bbb0e5e..16a4ec80b5bc04fe18cde36cdd10afec3201b2ab 100644 (file)
@@ -16,7 +16,7 @@
 package com.gitblit.wicket;\r
 \r
 import org.apache.wicket.Component;\r
-import org.apache.wicket.RestartResponseAtInterceptPageException;\r
+import org.apache.wicket.RestartResponseException;\r
 import org.apache.wicket.authorization.IUnauthorizedComponentInstantiationListener;\r
 import org.apache.wicket.authorization.strategies.page.AbstractPageAuthorizationStrategy;\r
 \r
@@ -49,6 +49,7 @@ public class AuthorizationStrategy extends AbstractPageAuthorizationStrategy imp
                        GitBlitWebSession session = GitBlitWebSession.get();\r
                        if (authenticateView && !session.isLoggedIn()) {\r
                                // authentication required\r
+                               session.cacheRequest(pageClass);\r
                                return false;\r
                        }\r
 \r
@@ -78,7 +79,7 @@ public class AuthorizationStrategy extends AbstractPageAuthorizationStrategy imp
        @Override\r
        public void onUnauthorizedInstantiation(Component component) {\r
                if (component instanceof BasePage) {\r
-                       throw new RestartResponseAtInterceptPageException(RepositoriesPage.class);\r
+                       throw new RestartResponseException(RepositoriesPage.class);\r
                }\r
        }\r
 }\r
index 2238660f22953ee63f5ea154c591d9d83aea6499..7ecc05b2ab497f9e2c57d8cc98d2a76efb9578e1 100644 (file)
  */\r
 package com.gitblit.wicket;\r
 \r
+import java.util.Map;\r
 import java.util.TimeZone;\r
 \r
+import org.apache.wicket.Page;\r
+import org.apache.wicket.PageParameters;\r
+import org.apache.wicket.RedirectToUrlException;\r
 import org.apache.wicket.Request;\r
 import org.apache.wicket.Session;\r
+import org.apache.wicket.protocol.http.RequestUtils;\r
+import org.apache.wicket.protocol.http.WebRequestCycle;\r
 import org.apache.wicket.protocol.http.WebSession;\r
 import org.apache.wicket.protocol.http.request.WebClientInfo;\r
 \r
@@ -33,7 +39,9 @@ public final class GitBlitWebSession extends WebSession {
        private UserModel user;\r
 \r
        private String errorMessage;\r
-\r
+       \r
+       private String requestUrl;\r
+       \r
        public GitBlitWebSession(Request request) {\r
                super(request);\r
        }\r
@@ -42,6 +50,46 @@ public final class GitBlitWebSession extends WebSession {
                super.invalidate();\r
                user = null;\r
        }\r
+       \r
+       /**\r
+        * Cache the requested protected resource pending successful authentication.\r
+        * \r
+        * @param pageClass\r
+        */\r
+       public void cacheRequest(Class<? extends Page> pageClass) {\r
+               // build absolute url with correctly encoded parameters?!\r
+               Request req = WebRequestCycle.get().getRequest();\r
+               Map<String, ?> params = req.getRequestParameters().getParameters();\r
+               PageParameters pageParams = new PageParameters(params);\r
+               String relativeUrl = WebRequestCycle.get().urlFor(pageClass, pageParams).toString();\r
+               requestUrl = RequestUtils.toAbsolutePath(relativeUrl);\r
+               if (isTemporary())\r
+               {\r
+                       // we must bind the temporary session into the session store\r
+                       // so that we can re-use this session for reporting an error message\r
+                       // on the redirected page and continuing the request after\r
+                       // authentication.\r
+                       bind();\r
+               }\r
+       }\r
+       \r
+       /**\r
+        * Continue any cached request.  This is used when a request for a protected\r
+        * resource is aborted/redirected pending proper authentication.  Gitblit\r
+        * no longer uses Wicket's built-in mechanism for this because of Wicket's\r
+        * failure to properly handle parameters with forward-slashes.  This is a\r
+        * constant source of headaches with Wicket.\r
+        *  \r
+        * @return false if there is no cached request to process\r
+        */\r
+       public boolean continueRequest() {\r
+               if (requestUrl != null) {\r
+                       String url = requestUrl;\r
+                       requestUrl = null;\r
+                       throw new RedirectToUrlException(url);\r
+               }\r
+               return false;\r
+       }\r
 \r
        public boolean isLoggedIn() {\r
                return user != null;\r
@@ -53,6 +101,10 @@ public final class GitBlitWebSession extends WebSession {
                }\r
                return user.canAdmin;\r
        }\r
+       \r
+       public String getUsername() {\r
+               return user == null ? "anonymous" : user.username;\r
+       }\r
 \r
        public UserModel getUser() {\r
                return user;\r
index 82862ae927c72178523ee9a46a55a33b2bc1473e..234c2a94412e83b5479e62fa194db726d61f8a0b 100644 (file)
@@ -26,7 +26,7 @@ import javax.servlet.http.HttpServletRequest;
 import org.apache.wicket.Application;\r
 import org.apache.wicket.MarkupContainer;\r
 import org.apache.wicket.PageParameters;\r
-import org.apache.wicket.RestartResponseAtInterceptPageException;\r
+import org.apache.wicket.RedirectToUrlException;\r
 import org.apache.wicket.RestartResponseException;\r
 import org.apache.wicket.markup.html.CSSPackageResource;\r
 import org.apache.wicket.markup.html.WebPage;\r
@@ -35,9 +35,11 @@ import org.apache.wicket.markup.html.link.BookmarkablePageLink;
 import org.apache.wicket.markup.html.link.ExternalLink;\r
 import org.apache.wicket.markup.html.panel.FeedbackPanel;\r
 import org.apache.wicket.markup.html.panel.Fragment;\r
+import org.apache.wicket.protocol.http.RequestUtils;\r
 import org.apache.wicket.protocol.http.WebRequest;\r
 import org.apache.wicket.protocol.http.WebResponse;\r
 import org.apache.wicket.protocol.http.servlet.ServletWebRequest;\r
+import org.apache.wicket.request.RequestParameters;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
@@ -137,7 +139,8 @@ public abstract class BasePage extends WebPage {
                        // Set Cookie\r
                        WebResponse response = (WebResponse) getRequestCycle().getResponse();\r
                        GitBlit.self().setCookie(response, user);\r
-                       continueToOriginalDestination();\r
+                       \r
+                       session.continueRequest();\r
                }\r
        }\r
 \r
@@ -229,7 +232,7 @@ public abstract class BasePage extends WebPage {
                // inject username into repository url if authentication is required\r
                if (repository.accessRestriction.exceeds(AccessRestrictionType.NONE)\r
                                && GitBlitWebSession.get().isLoggedIn()) {\r
-                       String username = GitBlitWebSession.get().getUser().username;\r
+                       String username = GitBlitWebSession.get().getUsername();\r
                        sb.insert(sb.indexOf("://") + 3, username + "@");\r
                }\r
                return sb.toString();\r
@@ -240,10 +243,13 @@ public abstract class BasePage extends WebPage {
        }\r
 \r
        public void error(String message, boolean redirect) {\r
-               logger.error(message);\r
+               logger.error(message  + " for " + GitBlitWebSession.get().getUsername());\r
                if (redirect) {\r
                        GitBlitWebSession.get().cacheErrorMessage(message);\r
-                       throw new RestartResponseException(getApplication().getHomePage());\r
+                       RequestParameters params = getRequest().getRequestParameters();\r
+                       String relativeUrl = urlFor(RepositoriesPage.class, null).toString();\r
+                       String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl);\r
+                       throw new RedirectToUrlException(absoluteUrl);\r
                } else {\r
                        super.error(message);\r
                }\r
@@ -260,12 +266,13 @@ public abstract class BasePage extends WebPage {
        }\r
 \r
        public void authenticationError(String message) {\r
-               logger.error(message);\r
-               if (GitBlitWebSession.get().isLoggedIn()) {\r
-                       error(message, true);\r
-               } else {\r
-                       throw new RestartResponseAtInterceptPageException(RepositoriesPage.class);\r
+               logger.error(getRequest().getURL() + " for " + GitBlitWebSession.get().getUsername());\r
+               if (!GitBlitWebSession.get().isLoggedIn()) {\r
+                       // cache the request if we have not authenticated.\r
+                       // the request will continue after authentication.\r
+                       GitBlitWebSession.get().cacheRequest(getClass());\r
                }\r
+               error(message, true);\r
        }\r
 \r
        /**\r
index cbe732ffc6e6181559f8b61ff81ff9d48b3b5126..5e66300645e52abae968c6e34dac4f4351771e39 100644 (file)
@@ -56,7 +56,7 @@ public class ChangePasswordPage extends RootSubPage {
                                        GitBlit.getString(Keys.realm.userService, "users.conf")), true);\r
                }\r
                \r
-               setupPage(getString("gb.changePassword"), GitBlitWebSession.get().getUser().username);\r
+               setupPage(getString("gb.changePassword"), GitBlitWebSession.get().getUsername());\r
 \r
                StatelessForm<Void> form = new StatelessForm<Void>("passwordForm") {\r
 \r
index 6d33a147e7cd71c77b13e6dbe212a46e14d375a0..19a5de2c067e3ac30be2c605fe94a8cefafc269a 100644 (file)
@@ -151,7 +151,7 @@ public abstract class RepositoryPage extends BasePage {
                if (showAdmin\r
                                || GitBlitWebSession.get().isLoggedIn()\r
                                && (model.owner != null && model.owner.equalsIgnoreCase(GitBlitWebSession.get()\r
-                                               .getUser().username))) {\r
+                                               .getUsername()))) {\r
                        pages.put("edit", new PageRegistration("gb.edit", EditRepositoryPage.class, params));\r
                }\r
                return pages;\r
@@ -198,7 +198,13 @@ public abstract class RepositoryPage extends BasePage {
                        RepositoryModel model = GitBlit.self().getRepositoryModel(\r
                                        GitBlitWebSession.get().getUser(), repositoryName);\r
                        if (model == null) {\r
-                               authenticationError(getString("gb.unauthorizedAccessForRepository") + " " + repositoryName);\r
+                               if (GitBlit.self().hasRepository(repositoryName)) {\r
+                                       // has repository, but unauthorized\r
+                                       authenticationError(getString("gb.unauthorizedAccessForRepository") + " " + repositoryName);\r
+                               } else {\r
+                                       // does not have repository\r
+                                       error(getString("gb.canNotLoadRepository") + " " + repositoryName, true);\r
+                               }\r
                                return null;\r
                        }\r
                        m = model;\r
index eaa25425c940b88527d31be193f2faae82891981..40f7aec4ab857327f3325053e68fec64ac1bc74c 100644 (file)
@@ -210,7 +210,7 @@ public abstract class RootPage extends BasePage {
                                GitBlit.self().setCookie(response, user);\r
                        }\r
 \r
-                       if (!continueToOriginalDestination()) {\r
+                       if (!session.continueRequest()) {\r
                                PageParameters params = getPageParameters();\r
                                if (params == null) {\r
                                        // redirect to this page\r