001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.sso;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.security.sso.OpenSSO;
020    import com.liferay.registry.Registry;
021    import com.liferay.registry.RegistryUtil;
022    import com.liferay.registry.ServiceTracker;
023    
024    import java.io.IOException;
025    
026    import java.util.Map;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * <p>
032     * See https://issues.liferay.com/browse/LEP-5943.
033     * </p>
034     *
035     * @author Prashant Dighe
036     * @author Brian Wing Shun Chan
037     * @author Wesley Gong
038     */
039    @ProviderType
040    public class OpenSSOUtil {
041    
042            public static Map<String, String> getAttributes(
043                    HttpServletRequest request, String serviceUrl) {
044    
045                    return _getOpenSSO().getAttributes(request, serviceUrl);
046            }
047    
048            public static String getSubjectId(
049                    HttpServletRequest request, String serviceUrl) {
050    
051                    return _getOpenSSO().getSubjectId(request, serviceUrl);
052            }
053    
054            public static boolean isAuthenticated(
055                            HttpServletRequest request, String serviceUrl)
056                    throws IOException {
057    
058                    return _getOpenSSO().isAuthenticated(request, serviceUrl);
059            }
060    
061            public static boolean isValidServiceUrl(String serviceUrl) {
062                    return _getOpenSSO().isValidServiceUrl(serviceUrl);
063            }
064    
065            public static boolean isValidUrl(String url) {
066                    return _getOpenSSO().isValidUrl(url);
067            }
068    
069            public static boolean isValidUrls(String[] urls) {
070                    return _getOpenSSO().isValidUrls(urls);
071            }
072    
073            private static OpenSSO _getOpenSSO() {
074                    return _instance._serviceTracker.getService();
075            }
076    
077            private OpenSSOUtil() {
078                    Registry registry = RegistryUtil.getRegistry();
079    
080                    _serviceTracker = registry.trackServices(OpenSSO.class);
081    
082                    _serviceTracker.open();
083            }
084    
085            private static final OpenSSOUtil _instance = new OpenSSOUtil();
086    
087            private final ServiceTracker<OpenSSO, OpenSSO> _serviceTracker;
088    
089    }