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.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.Http;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import org.openid4java.consumer.ConsumerException;
025    import org.openid4java.consumer.ConsumerManager;
026    import org.openid4java.consumer.InMemoryConsumerAssociationStore;
027    import org.openid4java.consumer.InMemoryNonceVerifier;
028    
029    /**
030     * @author Jorge Ferrer
031     */
032    public class OpenIdUtil {
033    
034            public static ConsumerManager getConsumerManager() {
035                    _instance._initialize();
036    
037                    return _instance._manager;
038            }
039    
040            public static String getScreenName(String openId) {
041                    String result = normalize(openId);
042    
043                    if (result.startsWith(Http.HTTP_WITH_SLASH)) {
044                            result = result.substring(Http.HTTP_WITH_SLASH.length());
045                    }
046    
047                    if (result.startsWith(Http.HTTPS_WITH_SLASH)) {
048                            result = result.substring(Http.HTTPS_WITH_SLASH.length());
049                    }
050    
051                    result = StringUtil.replace(
052                            result, 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) {
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 void _initialize() {
075                    try {
076                            if (_manager == null) {
077                                    _manager = new ConsumerManager();
078    
079                                    _manager.setAssociations(
080                                            new InMemoryConsumerAssociationStore());
081                                    _manager.setNonceVerifier(new InMemoryNonceVerifier(5000));
082                            }
083                    }
084                    catch (ConsumerException ce) {
085                            _log.error(ce.getMessage());
086                    }
087            }
088    
089            private static final Log _log = LogFactoryUtil.getLog(OpenIdUtil.class);
090    
091            private static final OpenIdUtil _instance = new OpenIdUtil();
092    
093            private ConsumerManager _manager;
094    
095    }