mirror of
https://github.com/google/nomulus.git
synced 2025-05-15 00:47:11 +02:00
Change registrar console login code in preparation for removing requireLogin
We are going to remove the requireLogin attribute from the action attribute, because it is specific to the UserService API. This is used by four actions: ConsoleUIAction RegistrarSettingsAction RegistrarPaymentSetupAction RegistrarPaymentAction Instead, these four actions will now check the login status directly. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=159562335
This commit is contained in:
parent
17697388b8
commit
2b7f78db98
17 changed files with 151 additions and 112 deletions
|
@ -41,7 +41,6 @@ import google.registry.ui.server.registrar.ConsoleUiAction;
|
|||
import google.registry.ui.server.registrar.RegistrarPaymentAction;
|
||||
import google.registry.ui.server.registrar.RegistrarPaymentSetupAction;
|
||||
import google.registry.ui.server.registrar.RegistrarSettingsAction;
|
||||
import google.registry.ui.server.registrar.RegistrarUserModule;
|
||||
import google.registry.whois.WhoisHttpServer;
|
||||
import google.registry.whois.WhoisModule;
|
||||
import google.registry.whois.WhoisServer;
|
||||
|
@ -54,7 +53,6 @@ import google.registry.whois.WhoisServer;
|
|||
DnsModule.class,
|
||||
EppTlsModule.class,
|
||||
RdapModule.class,
|
||||
RegistrarUserModule.class,
|
||||
RequestModule.class,
|
||||
WhiteboxModule.class,
|
||||
WhoisModule.class,
|
||||
|
|
|
@ -36,11 +36,11 @@ public abstract class AuthResult {
|
|||
return authLevel() != AuthLevel.NONE;
|
||||
}
|
||||
|
||||
static AuthResult create(AuthLevel authLevel) {
|
||||
public static AuthResult create(AuthLevel authLevel) {
|
||||
return new AutoValue_AuthResult(authLevel, Optional.<UserAuthInfo>absent());
|
||||
}
|
||||
|
||||
static AuthResult create(AuthLevel authLevel, @Nullable UserAuthInfo userAuthInfo) {
|
||||
public static AuthResult create(AuthLevel authLevel, @Nullable UserAuthInfo userAuthInfo) {
|
||||
if (authLevel == AuthLevel.USER) {
|
||||
checkNotNull(userAuthInfo);
|
||||
}
|
||||
|
|
|
@ -37,12 +37,12 @@ public abstract class UserAuthInfo {
|
|||
/** Used by the OAuth authentication mechanism (only) to return information about the session. */
|
||||
public abstract Optional<OAuthTokenInfo> oauthTokenInfo();
|
||||
|
||||
static UserAuthInfo create(
|
||||
public static UserAuthInfo create(
|
||||
User user, boolean isUserAdmin) {
|
||||
return new AutoValue_UserAuthInfo(user, isUserAdmin, Optional.<OAuthTokenInfo>absent());
|
||||
}
|
||||
|
||||
static UserAuthInfo create(
|
||||
public static UserAuthInfo create(
|
||||
User user, boolean isUserAdmin, OAuthTokenInfo oauthTokenInfo) {
|
||||
return new AutoValue_UserAuthInfo(user, isUserAdmin, Optional.of(oauthTokenInfo));
|
||||
}
|
||||
|
|
|
@ -14,10 +14,13 @@
|
|||
|
||||
package google.registry.ui.server.registrar;
|
||||
|
||||
import static com.google.common.net.HttpHeaders.LOCATION;
|
||||
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_MOVED_TEMPORARILY;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
|
||||
|
||||
import com.google.appengine.api.users.User;
|
||||
import com.google.appengine.api.users.UserService;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Supplier;
|
||||
|
@ -32,6 +35,7 @@ import google.registry.request.Action;
|
|||
import google.registry.request.Response;
|
||||
import google.registry.request.auth.Auth;
|
||||
import google.registry.request.auth.AuthLevel;
|
||||
import google.registry.request.auth.AuthResult;
|
||||
import google.registry.security.XsrfTokenManager;
|
||||
import google.registry.ui.server.SoyTemplateUtils;
|
||||
import google.registry.ui.soy.registrar.ConsoleSoyInfo;
|
||||
|
@ -69,6 +73,7 @@ public final class ConsoleUiAction implements Runnable {
|
|||
@Inject SessionUtils sessionUtils;
|
||||
@Inject UserService userService;
|
||||
@Inject XsrfTokenManager xsrfTokenManager;
|
||||
@Inject AuthResult authResult;
|
||||
@Inject @Config("logoFilename") String logoFilename;
|
||||
@Inject @Config("productName") String productName;
|
||||
@Inject @Config("integrationEmail") String integrationEmail;
|
||||
|
@ -81,6 +86,12 @@ public final class ConsoleUiAction implements Runnable {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!authResult.userAuthInfo().isPresent()) {
|
||||
response.setStatus(SC_MOVED_TEMPORARILY);
|
||||
response.setHeader(LOCATION, userService.createLoginURL(req.getRequestURI()));
|
||||
return;
|
||||
}
|
||||
User user = authResult.userAuthInfo().get().user();
|
||||
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.
|
||||
|
@ -102,9 +113,9 @@ public final class ConsoleUiAction implements Runnable {
|
|||
.render());
|
||||
return;
|
||||
}
|
||||
data.put("username", userService.getCurrentUser().getNickname());
|
||||
data.put("username", user.getNickname());
|
||||
data.put("logoutUrl", userService.createLogoutURL(PATH));
|
||||
if (!sessionUtils.checkRegistrarConsoleLogin(req)) {
|
||||
if (!sessionUtils.checkRegistrarConsoleLogin(req, user)) {
|
||||
response.setStatus(SC_FORBIDDEN);
|
||||
response.setPayload(
|
||||
TOFU_SUPPLIER.get()
|
||||
|
@ -115,9 +126,7 @@ public final class ConsoleUiAction implements Runnable {
|
|||
return;
|
||||
}
|
||||
Registrar registrar = Registrar.loadByClientIdCached(sessionUtils.getRegistrarClientId(req));
|
||||
data.put(
|
||||
"xsrfToken",
|
||||
xsrfTokenManager.generateToken(userService.getCurrentUser().getEmail()));
|
||||
data.put("xsrfToken", xsrfTokenManager.generateToken(user.getEmail()));
|
||||
data.put("clientId", registrar.getClientId());
|
||||
data.put("showPaymentLink", registrar.getBillingMethod() == Registrar.BillingMethod.BRAINTREE);
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ import google.registry.request.JsonActionRunner;
|
|||
import google.registry.request.JsonActionRunner.JsonAction;
|
||||
import google.registry.request.auth.Auth;
|
||||
import google.registry.request.auth.AuthLevel;
|
||||
import google.registry.request.auth.AuthResult;
|
||||
import google.registry.security.JsonResponseHelper;
|
||||
import google.registry.ui.forms.FormField;
|
||||
import google.registry.ui.forms.FormFieldException;
|
||||
|
@ -46,6 +47,7 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import javax.inject.Inject;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.joda.money.CurrencyUnit;
|
||||
import org.joda.money.IllegalCurrencyException;
|
||||
import org.joda.money.Money;
|
||||
|
@ -148,9 +150,11 @@ public final class RegistrarPaymentAction implements Runnable, JsonAction {
|
|||
.required()
|
||||
.build();
|
||||
|
||||
@Inject HttpServletRequest request;
|
||||
@Inject BraintreeGateway braintreeGateway;
|
||||
@Inject JsonActionRunner jsonActionRunner;
|
||||
@Inject Registrar registrar;
|
||||
@Inject AuthResult authResult;
|
||||
@Inject SessionUtils sessionUtils;
|
||||
@Inject @Config("braintreeMerchantAccountIds") ImmutableMap<CurrencyUnit, String> accountIds;
|
||||
@Inject RegistrarPaymentAction() {}
|
||||
|
||||
|
@ -161,6 +165,7 @@ public final class RegistrarPaymentAction implements Runnable, JsonAction {
|
|||
|
||||
@Override
|
||||
public Map<String, Object> handleJsonRequest(Map<String, ?> json) {
|
||||
Registrar registrar = sessionUtils.getRegistrarForAuthResult(request, authResult);
|
||||
logger.infofmt("Processing payment: %s", json);
|
||||
String paymentMethodNonce;
|
||||
Money amount;
|
||||
|
|
|
@ -30,9 +30,11 @@ import google.registry.request.JsonActionRunner;
|
|||
import google.registry.request.JsonActionRunner.JsonAction;
|
||||
import google.registry.request.auth.Auth;
|
||||
import google.registry.request.auth.AuthLevel;
|
||||
import google.registry.request.auth.AuthResult;
|
||||
import google.registry.security.JsonResponseHelper;
|
||||
import java.util.Map;
|
||||
import javax.inject.Inject;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.joda.money.CurrencyUnit;
|
||||
|
||||
/**
|
||||
|
@ -80,10 +82,12 @@ import org.joda.money.CurrencyUnit;
|
|||
)
|
||||
public final class RegistrarPaymentSetupAction implements Runnable, JsonAction {
|
||||
|
||||
@Inject HttpServletRequest request;
|
||||
@Inject BraintreeGateway braintreeGateway;
|
||||
@Inject BraintreeRegistrarSyncer customerSyncer;
|
||||
@Inject JsonActionRunner jsonActionRunner;
|
||||
@Inject Registrar registrar;
|
||||
@Inject AuthResult authResult;
|
||||
@Inject SessionUtils sessionUtils;
|
||||
@Inject @Config("brainframe") String brainframe;
|
||||
@Inject @Config("braintreeMerchantAccountIds") ImmutableMap<CurrencyUnit, String> accountIds;
|
||||
@Inject RegistrarPaymentSetupAction() {}
|
||||
|
@ -95,6 +99,8 @@ public final class RegistrarPaymentSetupAction implements Runnable, JsonAction {
|
|||
|
||||
@Override
|
||||
public Map<String, Object> handleJsonRequest(Map<String, ?> json) {
|
||||
Registrar registrar = sessionUtils.getRegistrarForAuthResult(request, authResult);
|
||||
|
||||
if (!json.isEmpty()) {
|
||||
return JsonResponseHelper.create(ERROR, "JSON request object must be empty");
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ import google.registry.request.HttpException.BadRequestException;
|
|||
import google.registry.request.JsonActionRunner;
|
||||
import google.registry.request.auth.Auth;
|
||||
import google.registry.request.auth.AuthLevel;
|
||||
import google.registry.request.auth.AuthResult;
|
||||
import google.registry.security.JsonResponseHelper;
|
||||
import google.registry.ui.forms.FormException;
|
||||
import google.registry.ui.forms.FormFieldException;
|
||||
|
@ -81,7 +82,7 @@ public class RegistrarSettingsAction implements Runnable, JsonActionRunner.JsonA
|
|||
|
||||
@Inject HttpServletRequest request;
|
||||
@Inject JsonActionRunner jsonActionRunner;
|
||||
@Inject Registrar initialRegistrar;
|
||||
@Inject AuthResult authResult;
|
||||
@Inject SendEmailUtils sendEmailUtils;
|
||||
@Inject SessionUtils sessionUtils;
|
||||
@Inject @Config("registrarChangesNotificationEmailAddresses") ImmutableList<String>
|
||||
|
@ -105,12 +106,9 @@ public class RegistrarSettingsAction implements Runnable, JsonActionRunner.JsonA
|
|||
throw new BadRequestException("Malformed JSON");
|
||||
}
|
||||
|
||||
if (!sessionUtils.checkRegistrarConsoleLogin(request)) {
|
||||
return JsonResponseHelper.create(ERROR, "Not authorized to access Registrar Console");
|
||||
}
|
||||
|
||||
Registrar initialRegistrar = sessionUtils.getRegistrarForAuthResult(request, authResult);
|
||||
// Process the operation. Though originally derived from a CRUD
|
||||
// handlder, registrar-settings really only supports read and update.
|
||||
// handler, registrar-settings really only supports read and update.
|
||||
String op = Optional.fromNullable((String) input.get(OP_PARAM)).or("read");
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, ?> args = (Map<String, Object>)
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
// Copyright 2017 The Nomulus Authors. 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 google.registry.ui.server.registrar;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import google.registry.model.registrar.Registrar;
|
||||
import google.registry.request.HttpException.ForbiddenException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/** Registrar Console module providing reference to logged-in {@link Registrar}. */
|
||||
@Module
|
||||
public final class RegistrarUserModule {
|
||||
|
||||
@Provides
|
||||
static Registrar provideRegistrarUser(SessionUtils sessionUtils, HttpServletRequest req) {
|
||||
if (!sessionUtils.checkRegistrarConsoleLogin(req)) {
|
||||
throw new ForbiddenException("Not authorized to access Registrar Console");
|
||||
}
|
||||
return Registrar.loadByClientId(sessionUtils.getRegistrarClientId(req));
|
||||
}
|
||||
}
|
|
@ -14,19 +14,19 @@
|
|||
|
||||
package google.registry.ui.server.registrar;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static com.google.common.base.Verify.verify;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
|
||||
import com.google.appengine.api.users.User;
|
||||
import com.google.appengine.api.users.UserService;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.FluentIterable;
|
||||
import com.googlecode.objectify.Key;
|
||||
import google.registry.model.registrar.Registrar;
|
||||
import google.registry.model.registrar.RegistrarContact;
|
||||
import google.registry.request.HttpException.ForbiddenException;
|
||||
import google.registry.request.auth.AuthResult;
|
||||
import google.registry.util.FormattingLogger;
|
||||
import javax.annotation.CheckReturnValue;
|
||||
import javax.annotation.Nonnull;
|
||||
|
@ -43,38 +43,49 @@ public class SessionUtils {
|
|||
|
||||
public static final String CLIENT_ID_ATTRIBUTE = "clientId";
|
||||
|
||||
private final UserService userService;
|
||||
@Inject public SessionUtils() {}
|
||||
|
||||
@Inject
|
||||
public SessionUtils(UserService userService) {
|
||||
this.userService = checkNotNull(userService);
|
||||
/**
|
||||
* Checks that the authentication result indicates a user that has access to the registrar
|
||||
* console, then gets the associated registrar.
|
||||
*
|
||||
* <p>Throws a {@link ForbiddenException} if the user is not logged in, or not authorized to use
|
||||
* the registrar console.
|
||||
*/
|
||||
@CheckReturnValue
|
||||
Registrar getRegistrarForAuthResult(HttpServletRequest request, AuthResult authResult) {
|
||||
if (!authResult.userAuthInfo().isPresent()) {
|
||||
throw new ForbiddenException("Not logged in");
|
||||
}
|
||||
if (!checkRegistrarConsoleLogin(request, authResult.userAuthInfo().get().user())) {
|
||||
throw new ForbiddenException("Not authorized to access Registrar Console");
|
||||
}
|
||||
return Registrar.loadByClientIdCached(getRegistrarClientId(request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks GAE user has access to Registrar Console.
|
||||
* Checks that the specified user has access to the Registrar Console.
|
||||
*
|
||||
* <p>This routine will first check the HTTP session (creating one if it doesn't exist) for the
|
||||
* {@code clientId} attribute:
|
||||
*
|
||||
* <ul>
|
||||
* <li>If it does not exist, then we will attempt to guess the {@link Registrar} with which the
|
||||
* user's GAIA ID is associated. The {@code clientId} of the first matching {@code Registrar} will
|
||||
* then be stored to the HTTP session.
|
||||
* user is associated. The {@code clientId} of the first matching {@code Registrar} will then
|
||||
* be stored to the HTTP session.
|
||||
* <li>If it does exist, then we'll fetch the Registrar from Datastore to make sure access
|
||||
* wasn't revoked.
|
||||
* </ul>
|
||||
*
|
||||
* <p><b>Note:</b> You must ensure the user has logged in before calling this method, for example
|
||||
* by setting {@code @Action(requireLogin = true)}.
|
||||
* <p><b>Note:</b> You must ensure the user has logged in before calling this method.
|
||||
*
|
||||
* @return {@code false} if user does not have access, in which case the caller should write an
|
||||
* error response and abort the request.
|
||||
*/
|
||||
@CheckReturnValue
|
||||
public boolean checkRegistrarConsoleLogin(HttpServletRequest req) {
|
||||
HttpSession session = req.getSession();
|
||||
User user = userService.getCurrentUser();
|
||||
public boolean checkRegistrarConsoleLogin(HttpServletRequest req, User user) {
|
||||
checkState(user != null, "No logged in user found");
|
||||
HttpSession session = req.getSession();
|
||||
String clientId = (String) session.getAttribute(CLIENT_ID_ATTRIBUTE);
|
||||
if (clientId == null) {
|
||||
Optional<Registrar> registrar = guessRegistrar(user.getUserId());
|
||||
|
@ -108,11 +119,6 @@ public class SessionUtils {
|
|||
return clientId;
|
||||
}
|
||||
|
||||
/** @see UserService#isUserLoggedIn() */
|
||||
public boolean isLoggedIn() {
|
||||
return userService.isUserLoggedIn();
|
||||
}
|
||||
|
||||
/** Returns first {@link Registrar} that {@code gaeUserId} is authorized to administer. */
|
||||
private static Optional<Registrar> guessRegistrar(String gaeUserId) {
|
||||
RegistrarContact contact = ofy().load()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue