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.util.Http;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    
023    import org.openid4java.consumer.ConsumerManager;
024    import org.openid4java.consumer.InMemoryConsumerAssociationStore;
025    import org.openid4java.consumer.InMemoryNonceVerifier;
026    
027    /**
028     * @author Jorge Ferrer
029     */
030    public class OpenIdUtil {
031    
032            public static ConsumerManager getConsumerManager() {
033                    _instance._initialize();
034    
035                    return _instance._manager;
036            }
037    
038            public static String getScreenName(String openId) {
039                    String result = normalize(openId);
040    
041                    if (result.startsWith(Http.HTTP_WITH_SLASH)) {
042                            result = result.substring(Http.HTTP_WITH_SLASH.length());
043                    }
044    
045                    if (result.startsWith(Http.HTTPS_WITH_SLASH)) {
046                            result = result.substring(Http.HTTPS_WITH_SLASH.length());
047                    }
048    
049                    result = StringUtil.replace(
050                            result, new String[] {StringPool.SLASH, StringPool.UNDERLINE},
051                            new String[] {StringPool.PERIOD, StringPool.PERIOD});
052    
053                    return result;
054            }
055    
056            public static boolean isEnabled(long companyId) throws SystemException {
057                    return PrefsPropsUtil.getBoolean(
058                            companyId, PropsKeys.OPEN_ID_AUTH_ENABLED,
059                            PropsValues.OPEN_ID_AUTH_ENABLED);
060            }
061    
062            public static String normalize(String identity) {
063                    String result = identity;
064    
065                    if (result.endsWith(StringPool.SLASH)) {
066                            result = result.substring(0, result.length() - 1);
067                    }
068    
069                    return result;
070            }
071    
072            private void _initialize() {
073                    if (_manager != null) {
074                            return;
075                    }
076    
077                    _manager = new ConsumerManager();
078    
079                    _manager.setAssociations(new InMemoryConsumerAssociationStore());
080                    _manager.setNonceVerifier(new InMemoryNonceVerifier(5000));
081            }
082    
083            private static OpenIdUtil _instance = new OpenIdUtil();
084    
085            private ConsumerManager _manager;
086    
087    }