// Copyright 2016 The Domain Registry 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 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 com.google.common.net.HttpHeaders.LOCATION; import static google.registry.model.ofy.ObjectifyService.ofy; import static javax.servlet.http.HttpServletResponse.SC_MOVED_TEMPORARILY; 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 google.registry.model.registrar.Registrar; import google.registry.model.registrar.RegistrarContact; import google.registry.util.FormattingLogger; import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; import javax.annotation.concurrent.Immutable; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** HTTP session management helper class. */ @Immutable public class SessionUtils { private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass(); public static final String CLIENT_ID_ATTRIBUTE = "clientId"; private final UserService userService; @Inject public SessionUtils(UserService userService) { this.userService = checkNotNull(userService); } /** * Redirects client to login URL if they aren't authenticated with the App Engine user service. * * @return {@code false} if request should abort. */ @CheckReturnValue public boolean redirectIfNotLoggedIn(HttpServletRequest req, HttpServletResponse rsp) { if (!isLoggedIn()) { logger.info("User not logged in to App Engine UserService."); rsp.setStatus(SC_MOVED_TEMPORARILY); rsp.setHeader(LOCATION, userService.createLoginURL(req.getRequestURI())); return false; } return true; } /** * Checks GAE user has access to Registrar Console. * *
This routine will first check the HTTP session (creating one if it doesn't exist) for the * {@code clientId} attribute: * *
Note: You should call {@link #redirectIfNotLoggedIn} 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();
checkState(user != null, "You forgot to call redirectIfNotLoggedIn()");
String clientId = (String) session.getAttribute(CLIENT_ID_ATTRIBUTE);
if (clientId == null) {
Optional