a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington/*
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * The contents of this file are subject to the terms of the Common Development and
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * Distribution License (the License). You may not use this file except in compliance with the
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * License.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington *
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * specific language governing permission and limitations under the License.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington *
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * When distributing Covered Software, include this CDDL Header Notice in each file and include
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * Header, with the fields enclosed by brackets [] replaced by your own identifying
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * information: "Portions copyright [year] [name of copyright owner]".
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington *
3a9e5adf8ed71f7841c483df0173964166267d9bJames Phillpotts * Copyright 2015-2016 ForgeRock AS.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington */
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington
a28658e7b50a29668499ee011576a857117fea4fPhill Cunningtonpackage com.sun.identity.console;
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington
a28658e7b50a29668499ee011576a857117fea4fPhill Cunningtonimport com.iplanet.jato.CompleteRequestException;
a28658e7b50a29668499ee011576a857117fea4fPhill Cunningtonimport com.iplanet.jato.RequestContext;
a28658e7b50a29668499ee011576a857117fea4fPhill Cunningtonimport com.iplanet.jato.RequestManager;
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunningtonimport com.iplanet.jato.view.ViewBeanBase;
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunningtonimport com.sun.identity.console.base.model.AMAdminConstants;
a28658e7b50a29668499ee011576a857117fea4fPhill Cunningtonimport org.forgerock.guice.core.InjectorHolder;
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunningtonimport org.forgerock.openam.services.baseurl.BaseURLProviderFactory;
a28658e7b50a29668499ee011576a857117fea4fPhill Cunningtonimport org.forgerock.openam.xui.XUIState;
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington
0e8bc8c8be7bf9b827d1d025079244092584f506Jaco Joosteimport javax.servlet.http.HttpServletRequest;
0e8bc8c8be7bf9b827d1d025079244092584f506Jaco Joosteimport java.io.IOException;
0e8bc8c8be7bf9b827d1d025079244092584f506Jaco Joosteimport java.text.MessageFormat;
0e8bc8c8be7bf9b827d1d025079244092584f506Jaco Jooste
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington/**
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * Helper for redirecting back to the XUI.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington *
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * @since 13.0.0
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington */
a28658e7b50a29668499ee011576a857117fea4fPhill Cunningtonpublic final class XuiRedirectHelper {
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington
8e31bd4d663551a2ea208cdf9894f388ea4fcca5sean oneill public static final String SERVER_DEFAULT_LOCATION = "configure/server-defaults/general";
84b168a45ee7013336cbc64ac025285206c5fa31Eugenia Sergueeva public static final String DEPLOYMENT_SERVERS = "deployment/servers";
41980fca86e3d5d58d8c687f7e844e5020f02f47Phill Cunnington public static final String GLOBAL_SERVICES = "configure/global-services";
f183bcdd3fb99ce20e1e2059714fd4422fc6fff3Phil Ostler public static final String TOP_LEVEL_REALM_SESSIONS = "realms/%2F/sessions";
8e31bd4d663551a2ea208cdf9894f388ea4fcca5sean oneill
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana private static final String XUI_CONSOLE_BASE_PAGE = "{0}/XUI?realm={1}#{2}";
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill private static final String DEFAULT_REALM = "/";
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington private XuiRedirectHelper() {
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington }
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington /**
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * Redirects to the XUI to the specified realm and hash.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington *
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington * @param request Used to determine the OpenAM deployment URI.
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana * @param administeredRealm The realm which is being administered.
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana * @param authenticationRealm The realm to which the user is authenticated.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * @param xuiHash The XUI location hash.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington */
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana public static void redirectToXui(HttpServletRequest request, String administeredRealm, String authenticationRealm,
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana String xuiHash) {
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana String deploymentUri = InjectorHolder.getInstance(BaseURLProviderFactory.class).get(administeredRealm)
1f2d552ab2fe7fc52b528ef4fef4136054b0871fJon Thomas .getContextPath();
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana String redirect = MessageFormat.format(XUI_CONSOLE_BASE_PAGE, deploymentUri, authenticationRealm, xuiHash);
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington RequestContext rc = RequestManager.getRequestContext();
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington try {
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington rc.getResponse().sendRedirect(redirect);
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington throw new CompleteRequestException();
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington } catch (IOException e) {
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington //never thrown, empty catch
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington }
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington }
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill /**
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill * Redirects to the XUI to the specified hash.
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill *
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill * @param request Used to determine the OpenAM deployment URI.
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill * @param xuiHash The XUI location hash.
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana * @param authenticationRealm The realm to which the user is authenticated.
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill */
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana public static void redirectToXui(HttpServletRequest request, String xuiHash, String authenticationRealm) {
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill String deploymentUri = InjectorHolder.getInstance(BaseURLProviderFactory.class).get(DEFAULT_REALM)
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill .getRootURL(request);
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana String redirect = MessageFormat.format(XUI_CONSOLE_BASE_PAGE, deploymentUri, authenticationRealm, xuiHash);
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill RequestContext rc = RequestManager.getRequestContext();
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill try {
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill rc.getResponse().sendRedirect(redirect);
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill throw new CompleteRequestException();
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill } catch (IOException e) {
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill //never thrown, empty catch
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill }
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill }
c3b0fb5f5fcfcf4947d168527ca89a8236ccf012sean oneill
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington /**
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana * Gets the administered realm to redirect to from the JATO page session.
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington *
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington * @param viewBean The view bean.
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana * @return The administered realm.
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington */
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana public static String getAdministeredRealm(ViewBeanBase viewBean) {
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington String redirectRealm = (String) viewBean.getPageSessionAttribute(AMAdminConstants.CURRENT_REALM);
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington if (redirectRealm == null) {
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington redirectRealm = (String) viewBean.getPageSessionAttribute(AMAdminConstants.CURRENT_PROFILE);
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington }
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington return redirectRealm;
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington }
6328925537fd7e5d853fe7540b10d6f909057805Phill Cunnington
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana /**
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana * Gets the authentication realm to redirect to from the JATO page session.
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana *
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana * @param viewBean The view bean.
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana * @return The authentication realm.
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana */
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana public static String getAuthenticationRealm(ViewBeanBase viewBean) {
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana String authenticationRealm = (String) viewBean.getPageSessionAttribute(AMAdminConstants.CURRENT_PROFILE);
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana if (authenticationRealm == "") {
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana authenticationRealm = DEFAULT_REALM;
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana }
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana return authenticationRealm;
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana }
93a5a3a88524fc81a9db1f999dae72316a21bfccJulian Kigwana
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington /**
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * Determines if request is from XUI to get a JATO page session.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington *
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * @param request The request.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * @return {@code true} if the request is from the XUI, {@code false} otherwise.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington */
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington public static boolean isJatoSessionRequestFromXUI(HttpServletRequest request) {
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington return "XUI".equals(request.getParameter("requester"));
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington }
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington /**
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * Determines if the XUI admin console is enabled.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington *
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington * @return {@code true} if the XUI admin console is enabled, {@code false} otherwise.
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington */
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington public static boolean isXuiAdminConsoleEnabled() {
0fa457ad5568b6445c5fd4537bc22392f68e157ePhill Cunnington return InjectorHolder.getInstance(XUIState.class).isXUIAdminEnabled();
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington }
a28658e7b50a29668499ee011576a857117fea4fPhill Cunnington}