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.core.api.NuxeoException;
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:tdelprat@nuxeo.com">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    public static LoginScreenConfig getConfig() {
054        PluggableAuthenticationService authService = (PluggableAuthenticationService) Framework.getRuntime()
055                                                                                               .getComponent(
056                                                                                                       PluggableAuthenticationService.NAME);
057        if (authService != null) {
058            return authService.getLoginScreenConfig();
059        }
060        return null;
061    }
062
063    public static void registerLoginProvider(String name, String iconUrl, String link, String label, String description,
064            LoginProviderLinkComputer computer) {
065
066        LoginScreenConfig config = getConfig();
067        if (config != null) {
068            config.registerLoginProvider(name, iconUrl, link, label, description, computer);
069        } else {
070            throw new NuxeoException("There is no available LoginScreen config");
071        }
072    }
073
074    public static String getValueWithDefault(String value, String defaultValue) {
075        if (value == null || value.isEmpty()) {
076            return defaultValue;
077        }
078        return value;
079    }
080
081    /**
082     * Returns the startup page URL according to the path returned by {@link #getStartupPagePath()}.
083     *
084     * @since 8.4
085     */
086    public static String getStartupPageURL(HttpServletRequest request) {
087        return VirtualHostHelper.getBaseURL(request) + getStartupPagePath();
088    }
089
090    /**
091     * Returns the path of the startup page depending on the {@link LoginScreenConfig}/{@link LoginStartupPage}
092     * contributions to the {@code loginScreen} extension point.
093     *
094     * @since 8.4
095     */
096    public static String getStartupPagePath() {
097        LoginScreenConfig config = getConfig();
098        if (config == null) {
099            log.debug("No <loginScreenConfig> contribution found, startup page path = " + DEFAULT_STARTUP_PAGE_PATH);
100            return DEFAULT_STARTUP_PAGE_PATH;
101        }
102        LoginStartupPage defaultStartupPage = getDefaultStartupPage(config);
103        log.debug("Default <startupPage> contribution: " + defaultStartupPage);
104        // No <startupPage> contributions, return home.html
105        if (defaultStartupPage == null) {
106            log.debug("No <startupPage> contribution found, startup page path = " + DEFAULT_STARTUP_PAGE_PATH);
107            return DEFAULT_STARTUP_PAGE_PATH;
108        }
109        // Return the path of the <startupPage> contribution with the highest priority
110        String startupPagePath = defaultStartupPage.getPath();
111        if (startupPagePath.startsWith("/")) {
112            startupPagePath = startupPagePath.substring(1);
113        }
114        log.debug("Startup page path = " + startupPagePath);
115        return startupPagePath;
116    }
117
118    /**
119     * Returns the paths of the startup pages coming from the {@link LoginScreenConfig}/{@link LoginStartupPage}
120     * contributions to the {@code loginScreen} extension point.
121     *
122     * @since 8.10
123     */
124    public static List<String> getStartupPagePaths() {
125        LoginScreenConfig config = getConfig();
126        if (config == null) {
127            return Collections.emptyList();
128        }
129        return config.getStartupPages()
130                     .values()
131                     .stream()
132                     .sorted((p1, p2) -> p2.compareTo(p1))
133                     .map(startupPage -> startupPage.getPath())
134                     .collect(Collectors.toList());
135    }
136
137    /**
138     * Returns the {@link LoginStartupPage} contribution with the highest priority or {@code null} if none is
139     * contributed.
140     *
141     * @since 8.4
142     */
143    protected static LoginStartupPage getDefaultStartupPage(LoginScreenConfig config) {
144        if (config.getStartupPages().isEmpty()) {
145            return null;
146        }
147        return Collections.max(config.getStartupPages().values());
148    }
149
150}