001    /**
002     * Copyright (c) 2000-2013 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                    _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, new String[] {StringPool.SLASH, StringPool.UNDERLINE},
054                            new String[] {StringPool.PERIOD, StringPool.PERIOD});
055    
056                    return result;
057            }
058    
059            public static boolean isEnabled(long companyId) throws SystemException {
060                    return PrefsPropsUtil.getBoolean(
061                            companyId, PropsKeys.OPEN_ID_AUTH_ENABLED,
062                            PropsValues.OPEN_ID_AUTH_ENABLED);
063            }
064    
065            public static String normalize(String identity) {
066                    String result = identity;
067    
068                    if (result.endsWith(StringPool.SLASH)) {
069                            result = result.substring(0, result.length() - 1);
070                    }
071    
072                    return result;
073            }
074    
075            private void _initialize() {
076                    try {
077                            if (_manager == null) {
078                                    _manager = new ConsumerManager();
079    
080                                    _manager.setAssociations(
081                                            new InMemoryConsumerAssociationStore());
082                                    _manager.setNonceVerifier(new InMemoryNonceVerifier(5000));
083                            }
084                    }
085                    catch (ConsumerException ce) {
086                            _log.error(ce.getMessage());
087                    }
088            }
089    
090            private static Log _log = LogFactoryUtil.getLog(OpenIdUtil.class);
091    
092            private static OpenIdUtil _instance = new OpenIdUtil();
093    
094            private ConsumerManager _manager;
095    
096    }