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                  com.liferay.portal.kernel.security.jaas.PortalLoginModule.class.
71                      getName(),
72                  AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT,
73                  options);
74  
75              if (aceArray == null || aceArray.length == 0) {
76                  aceArray = new AppConfigurationEntry[] {ace};
77              }
78              else {
79                  AppConfigurationEntry[] newAceArray =
80                      new AppConfigurationEntry[aceArray.length + 1];
81  
82                  if (name.equals(REALM_NAME)) {
83                      newAceArray[0] = ace;
84  
85                      System.arraycopy(
86                          aceArray, 0, newAceArray, 1, aceArray.length);
87                  }
88                  else {
89                      newAceArray[aceArray.length] = ace;
90  
91                      System.arraycopy(
92                          aceArray, 0, newAceArray, 0, aceArray.length);
93                  }
94  
95                  aceArray = newAceArray;
96  
97                  for (int i = 0; i < newAceArray.length; i++) {
98                      if (newAceArray[i].getControlFlag().equals(
99                              AppConfigurationEntry.LoginModuleControlFlag.
100                                 REQUIRED)) {
101 
102                         newAceArray[i] = new AppConfigurationEntry(
103                             newAceArray[i].getLoginModuleName(),
104                             AppConfigurationEntry.LoginModuleControlFlag.
105                                 SUFFICIENT,
106                             newAceArray[i].getOptions());
107                     }
108 
109                     if (_log.isDebugEnabled()) {
110                         _log.debug(
111                             newAceArray[i].getLoginModuleName() + " " +
112                                 newAceArray[i].getControlFlag());
113                     }
114                 }
115             }
116         }
117 
118         return aceArray;
119     }
120 
121     public void refresh() {
122         _config.refresh();
123     }
124 
125     private static Log _log = LogFactory.getLog(PortalConfiguration.class);
126 
127     private Configuration _config = null;
128 
129 }