1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.exception.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          return _instance._manager;
39      }
40  
41      public static String getScreenName(String openId) {
42          String result = normalize(openId);
43  
44          if (result.startsWith(Http.HTTP_WITH_SLASH)) {
45              result = result.substring(Http.HTTP_WITH_SLASH.length());
46          }
47  
48          if (result.startsWith(Http.HTTPS_WITH_SLASH)) {
49              result = result.substring(Http.HTTPS_WITH_SLASH.length());
50          }
51  
52          result = StringUtil.replace(
53              result,
54              new String[] {StringPool.SLASH, StringPool.UNDERLINE},
55              new String[] {StringPool.PERIOD, StringPool.PERIOD});
56  
57          return result;
58      }
59  
60      public static boolean isEnabled(long companyId) throws SystemException {
61          return PrefsPropsUtil.getBoolean(
62              companyId, PropsKeys.OPEN_ID_AUTH_ENABLED,
63              PropsValues.OPEN_ID_AUTH_ENABLED);
64      }
65  
66      public static String normalize(String identity) {
67          String result = identity;
68  
69          if (result.endsWith(StringPool.SLASH)) {
70              result = result.substring(0, result.length() - 1);
71          }
72  
73          return result;
74      }
75  
76      private OpenIdUtil() {
77          try {
78              _manager = new ConsumerManager();
79  
80              _manager.setAssociations(new InMemoryConsumerAssociationStore());
81              _manager.setNonceVerifier(new InMemoryNonceVerifier(5000));
82          }
83          catch (ConsumerException ce) {
84              _log.error(ce.getMessage());
85          }
86      }
87  
88      private static Log _log = LogFactoryUtil.getLog(OpenIdUtil.class);
89  
90      private static OpenIdUtil _instance = new OpenIdUtil();
91  
92      private ConsumerManager _manager;
93  
94  }