Pay off technical debt with ConsoleUiServlet

1. Turn ConsoleUiServlet into an action
2. Remove AbstractUiServlet, which fixes its threading bug
3. Use type-safe soy template parameters when rendering console

A follow-up change will add a new template parameter that renders the
payment page link on the navigation bar.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=117969638
This commit is contained in:
jart 2016-03-23 14:31:45 -07:00 committed by Justine Tunney
parent 79b2d5a990
commit b6b13333dd
22 changed files with 265 additions and 365 deletions

View file

@ -395,6 +395,12 @@ public final class ConfigModule {
}
}
@Provides
@Config("registrarConsoleEnabled")
public static boolean provideRegistrarConsoleEnabled() {
return true;
}
/** Maximum amount of time for syncing a spreadsheet, before killing. */
@Provides
@Config("sheetLockTimeout")

View file

@ -201,11 +201,6 @@ public interface RegistryConfig {
*/
public URL getRegistrarDefaultReferralUrl();
/**
* Returns whether the registrar console is enabled.
*/
public boolean isRegistrarConsoleEnabled();
/**
* Returns the title of the project used in generating documentation.
*/

View file

@ -149,11 +149,6 @@ public class TestRegistryConfig implements RegistryConfig {
return makeUrl("http://www.referral.example/path");
}
@Override
public boolean isRegistrarConsoleEnabled() {
return true;
}
@Override
public String getDocumentationProjectTitle() {
return "Domain Registry";

View file

@ -19,21 +19,6 @@
<url-pattern>/_dr/epp</url-pattern>
</servlet-mapping>
<!-- Registrar Console -->
<servlet>
<description>
Registrar Console UI servlet.
</description>
<display-name>Registrar Console UI</display-name>
<servlet-name>registrar-ui</servlet-name>
<servlet-class>com.google.domain.registry.ui.server.registrar.ConsoleUiServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>registrar-ui</servlet-name>
<url-pattern>/registrar</url-pattern>
</servlet-mapping>
<servlet>
<description>
Registrar Console XHR servlet. Accepts EPP XHRs from GAE GAIA-authenticated frontend sessions.
@ -67,6 +52,12 @@
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Registrar Console. -->
<servlet-mapping>
<servlet-name>frontend-servlet</servlet-name>
<url-pattern>/registrar</url-pattern>
</servlet-mapping>
<!-- Registrar Braintree payment form setup. -->
<servlet-mapping>
<servlet-name>frontend-servlet</servlet-name>

View file

@ -38,6 +38,6 @@ java_binary(
":frontend",
"//java/com/google/domain/registry/monitoring/whitebox", # MetricsTaskServlet
"//java/com/google/domain/registry/ui/server/api", # CheckApiServlet
"//java/com/google/domain/registry/ui/server/registrar", # ConsoleUiServlet, etc.
"//java/com/google/domain/registry/ui/server/registrar", # ResourceServlet
],
)

View file

@ -26,6 +26,7 @@ import com.google.domain.registry.rdap.RdapNameserverAction;
import com.google.domain.registry.rdap.RdapNameserverSearchAction;
import com.google.domain.registry.request.RequestModule;
import com.google.domain.registry.request.RequestScope;
import com.google.domain.registry.ui.server.registrar.ConsoleUiAction;
import com.google.domain.registry.ui.server.registrar.RegistrarPaymentAction;
import com.google.domain.registry.ui.server.registrar.RegistrarPaymentSetupAction;
import com.google.domain.registry.ui.server.registrar.RegistrarUserModule;
@ -45,6 +46,7 @@ import dagger.Subcomponent;
WhoisModule.class,
})
interface FrontendRequestComponent {
ConsoleUiAction consoleUiAction();
RdapAutnumAction rdapAutnumAction();
RegistrarPaymentAction registrarPaymentAction();
RegistrarPaymentSetupAction registrarPaymentSetupAction();

View file

@ -1,105 +0,0 @@
// Copyright 2016 Google Inc. All Rights Reserved.
//
// 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.domain.registry.ui.server;
import static com.google.domain.registry.security.XsrfTokenManager.generateToken;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.common.net.MediaType;
import com.google.template.soy.data.SoyMapData;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** Abstract servlet for serving HTML pages. */
public abstract class AbstractUiServlet extends HttpServlet {
protected String userId;
protected String userName;
protected String userActionName;
protected String userActionHref;
protected boolean userIsAdmin;
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp)
throws ServletException, IOException {
UserService userService = UserServiceFactory.getUserService();
if (userService.isUserLoggedIn()) {
User u = userService.getCurrentUser();
userId = u.getUserId();
userName = u.getNickname();
userActionName = "Sign out";
userActionHref = userService.createLogoutURL(req.getRequestURI());
userIsAdmin = userService.isUserAdmin();
} else {
userId = null;
userName = null;
userActionName = "Sign in";
userActionHref = userService.createLoginURL(req.getRequestURI());
userIsAdmin = false;
}
super.service(req, rsp);
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse rsp)
throws ServletException, IOException {
rsp.addHeader("X-Frame-Options", "SAMEORIGIN"); // Disallow iframing.
rsp.setHeader("X-Ui-Compatible", "IE=edge"); // Ask IE not to be silly.
rsp.setContentType(MediaType.HTML_UTF_8.toString());
UserService userService = UserServiceFactory.getUserService();
if (!userService.isUserLoggedIn()) {
rsp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
return;
}
rsp.getWriter().write(get(req));
}
/**
* Subclasses may override this method to access request params, or
* get() to simply return content.
*/
protected String get(@SuppressWarnings("unused") HttpServletRequest req) {
return get();
}
/** Override this to just return content. */
protected String get() {
throw new UnsupportedOperationException();
}
/**
* Returns a map with {@code (user: (id,name,actionName,actionHref), gaeUserId:, xsrfToken:)}
*/
protected SoyMapData getTemplateArgs(String xsrfToken) {
SoyMapData user = new SoyMapData();
user.put("id", userId);
user.put("name", userName);
user.put("actionName", userActionName);
user.put("actionHref", userActionHref);
user.put("isAdmin", userIsAdmin);
SoyMapData result = new SoyMapData();
result.put("user", user);
result.put("gaeUserId", userId);
result.put("xsrfToken", generateToken(xsrfToken));
return result;
}
}

View file

@ -0,0 +1,106 @@
// Copyright 2016 Google Inc. All Rights Reserved.
//
// 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.domain.registry.ui.server.registrar;
import static com.google.common.net.HttpHeaders.X_FRAME_OPTIONS;
import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
import com.google.appengine.api.users.UserService;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Supplier;
import com.google.common.io.Resources;
import com.google.common.net.MediaType;
import com.google.domain.registry.config.ConfigModule.Config;
import com.google.domain.registry.flows.EppConsoleServlet;
import com.google.domain.registry.model.registrar.Registrar;
import com.google.domain.registry.request.Action;
import com.google.domain.registry.request.Response;
import com.google.domain.registry.security.XsrfTokenManager;
import com.google.domain.registry.ui.server.SoyTemplateUtils;
import com.google.domain.registry.ui.soy.registrar.ConsoleSoyInfo;
import com.google.template.soy.data.SoyMapData;
import com.google.template.soy.shared.SoyCssRenamingMap;
import com.google.template.soy.tofu.SoyTofu;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
/** Action that serves Registrar Console single HTML page (SPA). */
@Action(path = ConsoleUiAction.PATH, requireLogin = true, xsrfProtection = false)
public final class ConsoleUiAction implements Runnable {
public static final String PATH = "/registrar";
private static final Supplier<SoyTofu> TOFU_SUPPLIER =
SoyTemplateUtils.createTofuSupplier(
com.google.domain.registry.ui.soy.ConsoleSoyInfo.getInstance(),
com.google.domain.registry.ui.soy.registrar.ConsoleSoyInfo.getInstance());
@VisibleForTesting // webdriver and screenshot tests need this
public static final Supplier<SoyCssRenamingMap> CSS_RENAMING_MAP_SUPPLIER =
SoyTemplateUtils.createCssRenamingMapSupplier(
Resources.getResource("com/google/domain/registry/ui/css/registrar_bin.css.js"),
Resources.getResource("com/google/domain/registry/ui/css/registrar_dbg.css.js"));
@Inject HttpServletRequest req;
@Inject Response response;
@Inject SessionUtils sessionUtils;
@Inject UserService userService;
@Inject @Config("registrarConsoleEnabled") boolean enabled;
@Inject ConsoleUiAction() {}
@Override
public void run() {
response.setContentType(MediaType.HTML_UTF_8);
response.setHeader(X_FRAME_OPTIONS, "SAMEORIGIN"); // Disallow iframing.
response.setHeader("X-Ui-Compatible", "IE=edge"); // Ask IE not to be silly.
if (!enabled) {
response.setStatus(SC_SERVICE_UNAVAILABLE);
response.setPayload(
TOFU_SUPPLIER.get()
.newRenderer(ConsoleSoyInfo.DISABLED)
.setCssRenamingMap(CSS_RENAMING_MAP_SUPPLIER.get())
.render());
return;
}
if (!sessionUtils.checkRegistrarConsoleLogin(req)) {
SoyMapData data = new SoyMapData();
data.put("username", userService.getCurrentUser().getNickname());
data.put("logoutUrl", userService.createLogoutURL(PATH));
response.setStatus(SC_FORBIDDEN);
response.setPayload(
TOFU_SUPPLIER.get()
.newRenderer(ConsoleSoyInfo.WHOAREYOU)
.setCssRenamingMap(CSS_RENAMING_MAP_SUPPLIER.get())
.setData(data)
.render());
return;
}
Registrar registrar = Registrar.loadByClientId(sessionUtils.getRegistrarClientId(req));
SoyMapData data = new SoyMapData();
data.put("xsrfToken", XsrfTokenManager.generateToken(EppConsoleServlet.XSRF_SCOPE));
data.put("clientId", registrar.getClientIdentifier());
data.put("username", userService.getCurrentUser().getNickname());
data.put("isAdmin", userService.isUserAdmin());
data.put("logoutUrl", userService.createLogoutURL(PATH));
response.setPayload(
TOFU_SUPPLIER.get()
.newRenderer(ConsoleSoyInfo.MAIN)
.setCssRenamingMap(CSS_RENAMING_MAP_SUPPLIER.get())
.setData(data)
.render());
}
}

View file

@ -1,76 +0,0 @@
// Copyright 2016 Google Inc. All Rights Reserved.
//
// 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.domain.registry.ui.server.registrar;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Supplier;
import com.google.common.io.Resources;
import com.google.domain.registry.config.RegistryEnvironment;
import com.google.domain.registry.flows.EppConsoleServlet;
import com.google.domain.registry.ui.server.AbstractUiServlet;
import com.google.domain.registry.ui.server.SoyTemplateUtils;
import com.google.domain.registry.ui.soy.registrar.ConsoleSoyInfo;
import com.google.domain.registry.util.NonFinalForTesting;
import com.google.template.soy.data.SoyMapData;
import com.google.template.soy.shared.SoyCssRenamingMap;
import com.google.template.soy.tofu.SoyTofu;
import javax.servlet.http.HttpServletRequest;
/** Main registrar console servlet that serves the client code. */
public final class ConsoleUiServlet extends AbstractUiServlet {
@VisibleForTesting
static final Supplier<SoyTofu> TOFU_SUPPLIER =
SoyTemplateUtils.createTofuSupplier(
com.google.domain.registry.ui.soy.ConsoleSoyInfo.getInstance(),
com.google.domain.registry.ui.soy.registrar.ConsoleSoyInfo.getInstance());
@VisibleForTesting
public static final Supplier<SoyCssRenamingMap> CSS_RENAMING_MAP_SUPPLIER =
SoyTemplateUtils.createCssRenamingMapSupplier(
Resources.getResource("com/google/domain/registry/ui/css/registrar_bin.css.js"),
Resources.getResource("com/google/domain/registry/ui/css/registrar_dbg.css.js"));
@NonFinalForTesting
private static SessionUtils sessionUtils = new SessionUtils(UserServiceFactory.getUserService());
@Override
protected String get(HttpServletRequest req) {
if (!RegistryEnvironment.get().config().isRegistrarConsoleEnabled()) {
return TOFU_SUPPLIER.get()
.newRenderer(ConsoleSoyInfo.DISABLED)
.setCssRenamingMap(CSS_RENAMING_MAP_SUPPLIER.get())
.render();
}
SoyMapData data = getTemplateArgs(EppConsoleServlet.XSRF_SCOPE);
if (!sessionUtils.checkRegistrarConsoleLogin(req)) {
data.getMapData("user").put("actionName", "Logout and switch to another account");
return TOFU_SUPPLIER.get()
.newRenderer(ConsoleSoyInfo.WHOAREYOU)
.setCssRenamingMap(CSS_RENAMING_MAP_SUPPLIER.get())
.setData(data)
.render();
}
data.put("clientId", req.getSession().getAttribute(SessionUtils.CLIENT_ID_ATTRIBUTE));
return TOFU_SUPPLIER.get()
.newRenderer(ConsoleSoyInfo.MAIN)
.setCssRenamingMap(CSS_RENAMING_MAP_SUPPLIER.get())
.setData(data)
.render();
}
}

View file

@ -54,7 +54,8 @@
* Happy little googley bar.
*/
{template .googlebar}
{@param user: map<string, ?>}
{@param username: string}
{@param logoutUrl: string}
<div id="kd-googlebar" role="banner">
<a class="{css logo}" href="/registrar">
<img src="/assets/images/glogo_black.png" alt="Google">Registry
@ -79,16 +80,10 @@
</p>
</div>
<div id="kd-social" class="{css kd-buttonbar} {css right}">
{if isNonnull($user['id'])}
<span class="{css kd-name} {css mobile-hide} {css x-crush-hide}">
{$user['name']}{sp}
<a href="{$user['actionHref']}" tabindex="-1">{$user['actionName']}</a>
</span>
{else}
<a href="{$user['actionHref']}"
class="{css kd-button} {css kd-button-submit}"
tabindex="-1">{$user['actionName']}</a>
{/if}
<span class="{css kd-name} {css mobile-hide} {css x-crush-hide}">
{$username}{sp}
<a href="{$logoutUrl}" tabindex="-1">Sign out</a>
</span>
</div>
</div>
{/template}

View file

@ -7,9 +7,11 @@
* and other templates within it.
*/
{template .main}
{@param user: map<string, ?>} /** Passed to googlebar. */
{@param xsrfToken: string} /** Security token. */
{@param clientId: string} /** App Engine user ID. */
{@param clientId: string} /** Registrar client identifier. */
{@param username: string} /** Arbitrary username to display. */
{@param isAdmin: bool} /** Is this user an App Engine account admin? */
{@param logoutUrl: string} /** Generated URL for logging out of Google. */
{call registry.soy.console.header}
{param app: 'registrar' /}
{param subtitle: 'Registrar Console' /}
@ -27,7 +29,7 @@
</div>
{switch DEBUG}
{case com.google.domain.registry.ui.ConsoleDebug.PRODUCTION}
{if $user['isAdmin']}
{if $isAdmin}
<script src="/assets/js/registrar_bin_map.js"></script>
{else}
<script src="/assets/js/registrar_bin.js"></script>
@ -92,11 +94,13 @@
</div>
{/template}
/**
* Who goes thar?!
*/
{template .whoareyou}
{@param user: map<string, string>}
{@param username: string} /** Arbitrary username to display. */
{@param logoutUrl: string} /** Generated URL for logging out of Google. */
{call registry.soy.console.header}
{param app: 'registrar' /}
{param subtitle: 'Please Login' /}
@ -110,13 +114,12 @@
The account you are logged in as is not associated with Google
Registry. Please contact your customer service representative or
switch to an account associated with Google Registry.
{if isNonnull($user['name'])}
<p>You are signed in as <strong>{$user['name']}</strong>.
{/if}
<p>
You are signed in as <strong>{$username}</strong>.
<div>
<a href="{$user['actionHref']}"
<a href="{$logoutUrl}"
class="{css kd-button} {css kd-button-submit}"
tabindex="-1">{$user['actionName']}</a>
tabindex="-1">Logout and switch to another account</a>
</div>
</div>
{/template}