001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.Http;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    
025    import org.openid4java.consumer.ConsumerException;
026    import org.openid4java.consumer.ConsumerManager;
027    import org.openid4java.consumer.InMemoryConsumerAssociationStore;
028    import org.openid4java.consumer.InMemoryNonceVerifier;
029    
030    /**
031     * @author Jorge Ferrer
032     */
033    public class OpenIdUtil {
034    
035            public static ConsumerManager getConsumerManager() {
036                    _instance._initialize();
037    
038                    return _instance._manager;
039            }
040    
041            public static String getScreenName(String openId) {
042                    String result = normalize(openId);
043    
044                    if (result.startsWith(Http.HTTP_WITH_SLASH)) {
045                            result = result.substring(Http.HTTP_WITH_SLASH.length());
046                    }
047    
048                    if (result.startsWith(Http.HTTPS_WITH_SLASH)) {
049                            result = result.substring(Http.HTTPS_WITH_SLASH.length());
050                    }
051    
052                    result = StringUtil.replace(
053                            result,
054                            new String[] {StringPool.SLASH, StringPool.UNDERLINE},
055                            new String[] {StringPool.PERIOD, StringPool.PERIOD});
056    
057                    return result;
058            }
059    
060            public static boolean isEnabled(long companyId) throws SystemException {
061                    return PrefsPropsUtil.getBoolean(
062                            companyId, PropsKeys.OPEN_ID_AUTH_ENABLED,
063                            PropsValues.OPEN_ID_AUTH_ENABLED);
064            }
065    
066            public static String normalize(String identity) {
067                    String result = identity;
068    
069                    if (result.endsWith(StringPool.SLASH)) {
070                            result = result.substring(0, result.length() - 1);
071                    }
072    
073                    return result;
074            }
075    
076            private void _initialize() {
077                    try {
078                            if (_manager == null) {
079                                    _manager = new ConsumerManager();
080    
081                                    _manager.setAssociations(
082                                            new InMemoryConsumerAssociationStore());
083                                    _manager.setNonceVerifier(new InMemoryNonceVerifier(5000));
084                            }
085                    }
086                    catch (ConsumerException ce) {
087                            _log.error(ce.getMessage());
088                    }
089            }
090    
091            private static Log _log = LogFactoryUtil.getLog(OpenIdUtil.class);
092    
093            private static OpenIdUtil _instance = new OpenIdUtil();
094    
095            private ConsumerManager _manager;
096    
097    }