001    /**
002     * Copyright (c) 2000-2010 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.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                    return _instance._manager;
037            }
038    
039            public static String getScreenName(String openId) {
040                    String result = normalize(openId);
041    
042                    if (result.startsWith(Http.HTTP_WITH_SLASH)) {
043                            result = result.substring(Http.HTTP_WITH_SLASH.length());
044                    }
045    
046                    if (result.startsWith(Http.HTTPS_WITH_SLASH)) {
047                            result = result.substring(Http.HTTPS_WITH_SLASH.length());
048                    }
049    
050                    result = StringUtil.replace(
051                            result,
052                            new String[] {StringPool.SLASH, StringPool.UNDERLINE},
053                            new String[] {StringPool.PERIOD, StringPool.PERIOD});
054    
055                    return result;
056            }
057    
058            public static boolean isEnabled(long companyId) throws SystemException {
059                    return PrefsPropsUtil.getBoolean(
060                            companyId, PropsKeys.OPEN_ID_AUTH_ENABLED,
061                            PropsValues.OPEN_ID_AUTH_ENABLED);
062            }
063    
064            public static String normalize(String identity) {
065                    String result = identity;
066    
067                    if (result.endsWith(StringPool.SLASH)) {
068                            result = result.substring(0, result.length() - 1);
069                    }
070    
071                    return result;
072            }
073    
074            private OpenIdUtil() {
075                    try {
076                            _manager = new ConsumerManager();
077    
078                            _manager.setAssociations(new InMemoryConsumerAssociationStore());
079                            _manager.setNonceVerifier(new InMemoryNonceVerifier(5000));
080                    }
081                    catch (ConsumerException ce) {
082                            _log.error(ce.getMessage());
083                    }
084            }
085    
086            private static Log _log = LogFactoryUtil.getLog(OpenIdUtil.class);
087    
088            private static OpenIdUtil _instance = new OpenIdUtil();
089    
090            private ConsumerManager _manager;
091    
092    }