1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.Http;
21  import com.liferay.portal.kernel.util.PropsKeys;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.StringUtil;
24  
25  import org.openid4java.consumer.ConsumerException;
26  import org.openid4java.consumer.ConsumerManager;
27  import org.openid4java.consumer.InMemoryConsumerAssociationStore;
28  import org.openid4java.consumer.InMemoryNonceVerifier;
29  
30  /**
31   * <a href="OpenIdUtil.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Jorge Ferrer
34   */
35  public class OpenIdUtil {
36  
37      public static ConsumerManager getConsumerManager() {
38          _instance._initialize();
39  
40          return _instance._manager;
41      }
42  
43      public static String getScreenName(String openId) {
44          String result = normalize(openId);
45  
46          if (result.startsWith(Http.HTTP_WITH_SLASH)) {
47              result = result.substring(Http.HTTP_WITH_SLASH.length());
48          }
49  
50          if (result.startsWith(Http.HTTPS_WITH_SLASH)) {
51              result = result.substring(Http.HTTPS_WITH_SLASH.length());
52          }
53  
54          result = StringUtil.replace(
55              result,
56              new String[] {StringPool.SLASH, StringPool.UNDERLINE},
57              new String[] {StringPool.PERIOD, StringPool.PERIOD});
58  
59          return result;
60      }
61  
62      public static boolean isEnabled(long companyId) throws SystemException {
63          return PrefsPropsUtil.getBoolean(
64              companyId, PropsKeys.OPEN_ID_AUTH_ENABLED,
65              PropsValues.OPEN_ID_AUTH_ENABLED);
66      }
67  
68      public static String normalize(String identity) {
69          String result = identity;
70  
71          if (result.endsWith(StringPool.SLASH)) {
72              result = result.substring(0, result.length() - 1);
73          }
74  
75          return result;
76      }
77  
78      private void _initialize() {
79          try {
80              if (_manager == null) {
81                  _manager = new ConsumerManager();
82  
83                  _manager.setAssociations(
84                      new InMemoryConsumerAssociationStore());
85                  _manager.setNonceVerifier(new InMemoryNonceVerifier(5000));
86              }
87          }
88          catch (ConsumerException ce) {
89              _log.error(ce.getMessage());
90          }
91      }
92  
93      private static Log _log = LogFactoryUtil.getLog(OpenIdUtil.class);
94  
95      private static OpenIdUtil _instance = new OpenIdUtil();
96  
97      private ConsumerManager _manager;
98  
99  }