1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.security.jaas;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.security.auth.login.AppConfigurationEntry;
29  import javax.security.auth.login.Configuration;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * <a href="PortalConfiguration.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Michael Weisser
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PortalConfiguration extends Configuration {
42  
43      public static final String REALM_NAME = "PortalRealm";
44  
45      public static final String JBOSS_LOGIN_MODULE = "client-login";
46  
47      public PortalConfiguration(Configuration config) {
48          _config = config;
49      }
50  
51      public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
52          AppConfigurationEntry[] aceArray =
53              _config.getAppConfigurationEntry(name);
54  
55          if ((name != null) && !name.equals(JBOSS_LOGIN_MODULE)) {
56              if (_log.isDebugEnabled()) {
57                  _log.debug(name);
58              }
59  
60              Map options = null;
61  
62              if (aceArray == null || aceArray.length == 0) {
63                  options = new HashMap();
64              }
65              else {
66                  options = aceArray[0].getOptions();
67              }
68  
69              AppConfigurationEntry ace = new AppConfigurationEntry(
70                  PortalLoginModule.class.getName(),
71                  AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT,
72                  options);
73  
74              if (aceArray == null || aceArray.length == 0) {
75                  aceArray = new AppConfigurationEntry[] {ace};
76              }
77              else {
78                  AppConfigurationEntry[] newAceArray =
79                      new AppConfigurationEntry[aceArray.length + 1];
80  
81                  if (name.equals(REALM_NAME)) {
82                      newAceArray[0] = ace;
83  
84                      System.arraycopy(
85                          aceArray, 0, newAceArray, 1, aceArray.length);
86                  }
87                  else {
88                      newAceArray[aceArray.length] = ace;
89  
90                      System.arraycopy(
91                          aceArray, 0, newAceArray, 0, aceArray.length);
92                  }
93  
94                  aceArray = newAceArray;
95  
96                  for (int i = 0; i < newAceArray.length; i++) {
97                      if (newAceArray[i].getControlFlag().equals(
98                              AppConfigurationEntry.LoginModuleControlFlag.
99                                  REQUIRED)) {
100 
101                         newAceArray[i] = new AppConfigurationEntry(
102                             newAceArray[i].getLoginModuleName(),
103                             AppConfigurationEntry.LoginModuleControlFlag.
104                                 SUFFICIENT,
105                             newAceArray[i].getOptions());
106                     }
107 
108                     if (_log.isDebugEnabled()) {
109                         _log.debug(
110                             newAceArray[i].getLoginModuleName() + " " +
111                                 newAceArray[i].getControlFlag());
112                     }
113                 }
114             }
115         }
116 
117         return aceArray;
118     }
119 
120     public void refresh() {
121         _config.refresh();
122     }
123 
124     private static Log _log = LogFactory.getLog(PortalConfiguration.class);
125 
126     private Configuration _config = null;
127 
128 }