001/* 002 * (C) Copyright 2006-2013 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 */ 019 020package org.nuxeo.ecm.platform.ui.web.auth; 021 022import java.util.Collections; 023import java.util.List; 024import java.util.stream.Collectors; 025 026import javax.servlet.http.HttpServletRequest; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.ecm.platform.ui.web.auth.service.LoginProviderLink; 031import org.nuxeo.ecm.platform.ui.web.auth.service.LoginProviderLinkComputer; 032import org.nuxeo.ecm.platform.ui.web.auth.service.LoginScreenConfig; 033import org.nuxeo.ecm.platform.ui.web.auth.service.LoginStartupPage; 034import org.nuxeo.ecm.platform.ui.web.auth.service.PluggableAuthenticationService; 035import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 036import org.nuxeo.runtime.api.Framework; 037 038/** 039 * Simple helper class for easy access form the login.jsp page 040 * 041 * @author <a href="mailto:[email protected]">Tiry</a> 042 * @since 5.7 043 */ 044public class LoginScreenHelper { 045 046 protected static final Log log = LogFactory.getLog(LoginScreenHelper.class); 047 048 /** 049 * @since 8.4 050 */ 051 public static final String DEFAULT_STARTUP_PAGE_PATH = "home.html"; 052 053 private LoginScreenHelper() { 054 // helper class 055 } 056 057 public static LoginScreenConfig getConfig() { 058 PluggableAuthenticationService authenticationService = getPluggableAuthenticationService(); 059 return authenticationService == null ? null : authenticationService.getLoginScreenConfig(); 060 } 061 062 /** 063 * Registers and returns a login screen configuration including a single login provider described by the given 064 * parameters. 065 * 066 * @since 10.10 067 */ 068 public static LoginScreenConfig registerSingleProviderLoginScreenConfig(String name, String iconUrl, String link, 069 String label, String description, LoginProviderLinkComputer computer) { 070 LoginProviderLink provider = new LoginProviderLink(name, iconUrl, link, label, description, computer); 071 LoginScreenConfig config = new LoginScreenConfig(provider); 072 getPluggableAuthenticationService().registerLoginScreenConfig(config); 073 return config; 074 } 075 076 /** 077 * Unregisters the given login screen configuration. 078 * 079 * @since 10.10 080 */ 081 public static void unregisterLoginScreenConfig(LoginScreenConfig config) { 082 getPluggableAuthenticationService().unregisterLoginScreenConfig(config); 083 } 084 085 /** 086 * @deprecated since 10.10, use 087 * {@link #registerSingleProviderLoginScreenConfig(String, String, String, String, String, LoginProviderLinkComputer)} 088 * instead 089 */ 090 @Deprecated 091 public static void registerLoginProvider(String name, String iconUrl, String link, String label, String description, 092 LoginProviderLinkComputer computer) { 093 registerSingleProviderLoginScreenConfig(name, iconUrl, link, label, description, computer); 094 } 095 096 public static String getValueWithDefault(String value, String defaultValue) { 097 if (value == null || value.isEmpty()) { 098 return defaultValue; 099 } 100 return value; 101 } 102 103 /** 104 * Returns the startup page URL according to the path returned by {@link #getStartupPagePath()}. 105 * 106 * @since 8.4 107 */ 108 public static String getStartupPageURL(HttpServletRequest request) { 109 return VirtualHostHelper.getBaseURL(request) + getStartupPagePath(); 110 } 111 112 /** 113 * Returns the path of the startup page depending on the {@link LoginScreenConfig}/{@link LoginStartupPage} 114 * contributions to the {@code loginScreen} extension point. 115 * 116 * @since 8.4 117 */ 118 public static String getStartupPagePath() { 119 LoginScreenConfig config = getConfig(); 120 if (config == null) { 121 log.debug("No <loginScreenConfig> contribution found, startup page path = " + DEFAULT_STARTUP_PAGE_PATH); 122 return DEFAULT_STARTUP_PAGE_PATH; 123 } 124 LoginStartupPage defaultStartupPage = getDefaultStartupPage(config); 125 log.debug("Default <startupPage> contribution: " + defaultStartupPage); 126 // No <startupPage> contributions, return home.html 127 if (defaultStartupPage == null) { 128 log.debug("No <startupPage> contribution found, startup page path = " + DEFAULT_STARTUP_PAGE_PATH); 129 return DEFAULT_STARTUP_PAGE_PATH; 130 } 131 // Return the path of the <startupPage> contribution with the highest priority 132 String startupPagePath = defaultStartupPage.getPath(); 133 if (startupPagePath.startsWith("/")) { 134 startupPagePath = startupPagePath.substring(1); 135 } 136 log.debug("Startup page path = " + startupPagePath); 137 return startupPagePath; 138 } 139 140 /** 141 * Returns the paths of the startup pages coming from the {@link LoginScreenConfig}/{@link LoginStartupPage} 142 * contributions to the {@code loginScreen} extension point. 143 * 144 * @since 8.10 145 */ 146 public static List<String> getStartupPagePaths() { 147 LoginScreenConfig config = getConfig(); 148 if (config == null) { 149 return Collections.emptyList(); 150 } 151 return config.getStartupPages() 152 .values() 153 .stream() 154 .sorted((p1, p2) -> p2.compareTo(p1)) 155 .map(LoginStartupPage::getPath) 156 .collect(Collectors.toList()); 157 } 158 159 /** 160 * Returns the {@link LoginStartupPage} contribution with the highest priority or {@code null} if none is 161 * contributed. 162 * 163 * @since 8.4 164 */ 165 protected static LoginStartupPage getDefaultStartupPage(LoginScreenConfig config) { 166 if (config.getStartupPages().isEmpty()) { 167 return null; 168 } 169 return Collections.max(config.getStartupPages().values()); 170 } 171 172 protected static PluggableAuthenticationService getPluggableAuthenticationService() { 173 return (PluggableAuthenticationService) Framework.getRuntime() 174 .getComponent(PluggableAuthenticationService.NAME); 175 } 176 177}