Refactor authentication types to remove code duplication

The code to extract the redirect token is duplicated in several places.
Move the code into LoginUrlToken.

Change-Id: I8b8917f76d6fd0126f78fbb040594d0bb74aa628
This commit is contained in:
Simon Lei 2014-07-15 13:03:48 -04:00
parent 3ca59b803c
commit cdfbcbccf6
5 changed files with 44 additions and 47 deletions

View File

@ -0,0 +1,33 @@
// Copyright (C) 2014 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.google.gerrit.httpd;
import com.google.common.base.Strings;
import com.google.gerrit.common.PageLinks;
import javax.servlet.http.HttpServletRequest;
public class LoginUrlToken {
public static String getToken(HttpServletRequest req) {
String token = req.getPathInfo();
if (Strings.isNullOrEmpty(token)) {
token = PageLinks.MINE;
} else if (!token.startsWith("/")) {
token = "/" + token;
}
return token;
}
}

View File

@ -18,6 +18,7 @@ import com.google.gerrit.common.PageLinks;
import com.google.gerrit.extensions.registration.DynamicItem;
import com.google.gerrit.httpd.CanonicalWebUrl;
import com.google.gerrit.httpd.HtmlDomUtil;
import com.google.gerrit.httpd.LoginUrlToken;
import com.google.gerrit.httpd.WebSession;
import com.google.gerrit.server.account.AccountException;
import com.google.gerrit.server.account.AccountManager;
@ -78,7 +79,7 @@ class HttpLoginServlet extends HttpServlet {
@Override
protected void doGet(final HttpServletRequest req,
final HttpServletResponse rsp) throws ServletException, IOException {
final String token = getToken(req);
final String token = LoginUrlToken.getToken(req);
if ("/logout".equals(token) || "/signout".equals(token)) {
req.getRequestDispatcher("/logout").forward(req, rsp);
return;
@ -163,14 +164,4 @@ class HttpLoginServlet extends HttpServlet {
replaceByClass(n, name, value);
}
}
private String getToken(final HttpServletRequest req) {
String token = req.getPathInfo();
if (token == null || token.isEmpty()) {
token = PageLinks.MINE;
} else if (!token.startsWith("/")) {
token = "/" + token;
}
return token;
}
}

View File

@ -15,7 +15,7 @@
package com.google.gerrit.httpd.auth.container;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.httpd.LoginUrlToken;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gwtexpui.server.CacheHeaders;
import com.google.inject.Inject;
@ -54,17 +54,9 @@ public class HttpsClientSslCertLoginServlet extends HttpServlet {
final StringBuilder rdr = new StringBuilder();
rdr.append(urlProvider.get());
rdr.append('#');
rdr.append(getToken(req));
rdr.append(LoginUrlToken.getToken(req));
CacheHeaders.setNotCacheable(rsp);
rsp.sendRedirect(rdr.toString());
}
private String getToken(final HttpServletRequest req) {
String token = req.getPathInfo();
if (token == null || token.isEmpty()) {
token = PageLinks.MINE;
}
return token;
}
}

View File

@ -17,10 +17,10 @@ package com.google.gerrit.httpd.auth.ldap;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.extensions.registration.DynamicItem;
import com.google.gerrit.httpd.CanonicalWebUrl;
import com.google.gerrit.httpd.HtmlDomUtil;
import com.google.gerrit.httpd.LoginUrlToken;
import com.google.gerrit.httpd.WebSession;
import com.google.gerrit.httpd.template.SiteHeaderFooter;
import com.google.gerrit.server.account.AccountException;
@ -73,7 +73,7 @@ class LdapLoginServlet extends HttpServlet {
@Nullable String errorMessage) throws IOException {
String self = req.getRequestURI();
String cancel = Objects.firstNonNull(urlProvider.get(req), "/");
String token = getToken(req);
String token = LoginUrlToken.getToken(req);
if (!token.equals("/")) {
cancel += "#" + token;
}
@ -145,20 +145,10 @@ class LdapLoginServlet extends HttpServlet {
StringBuilder dest = new StringBuilder();
dest.append(urlProvider.get(req));
dest.append('#');
dest.append(getToken(req));
dest.append(LoginUrlToken.getToken(req));
CacheHeaders.setNotCacheable(res);
webSession.get().login(ares, "1".equals(remember));
res.sendRedirect(dest.toString());
}
private static String getToken(final HttpServletRequest req) {
String token = req.getPathInfo();
if (token == null || token.isEmpty()) {
token = PageLinks.MINE;
} else if (!token.startsWith("/")) {
token = "/" + token;
}
return token;
}
}

View File

@ -24,6 +24,7 @@ import com.google.gerrit.common.PageLinks;
import com.google.gerrit.common.auth.openid.OpenIdUrls;
import com.google.gerrit.extensions.restapi.Url;
import com.google.gerrit.httpd.HtmlDomUtil;
import com.google.gerrit.httpd.LoginUrlToken;
import com.google.gerrit.httpd.template.SiteHeaderFooter;
import com.google.gerrit.reviewdb.client.AuthType;
import com.google.gerrit.server.config.AuthConfig;
@ -102,7 +103,7 @@ class LoginForm extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
if (ssoUrl != null) {
String token = getToken(req);
String token = LoginUrlToken.getToken(req);
SignInMode mode;
if (PageLinks.REGISTER.equals(token)) {
mode = SignInMode.REGISTER;
@ -140,7 +141,7 @@ class LoginForm extends HttpServlet {
}
boolean remember = "1".equals(req.getParameter("rememberme"));
String token = getToken(req);
String token = LoginUrlToken.getToken(req);
SignInMode mode;
if (link) {
mode = SignInMode.LINK_IDENTIY;
@ -216,21 +217,11 @@ class LoginForm extends HttpServlet {
sendHtml(res, doc);
}
private static String getToken(HttpServletRequest req) {
String token = req.getPathInfo();
if (token == null || token.isEmpty()) {
token = PageLinks.MINE;
} else if (!token.startsWith("/")) {
token = "/" + token;
}
return token;
}
private void sendForm(HttpServletRequest req, HttpServletResponse res,
boolean link, @Nullable String errorMessage) throws IOException {
String self = req.getRequestURI();
String cancel = Objects.firstNonNull(urlProvider != null ? urlProvider.get() : "/", "/");
String token = getToken(req);
String token = LoginUrlToken.getToken(req);
if (!token.equals("/")) {
cancel += "#" + token;
}