View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.security.jaas;
18  
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.geronimo.gbean.GBeanInfo;
25  import org.apache.geronimo.gbean.GBeanInfoBuilder;
26  import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
27  import org.apache.geronimo.kernel.Kernel;
28  import org.apache.geronimo.system.serverinfo.ServerInfo;
29  import org.apache.geronimo.security.jaas.server.JaasLoginModuleConfiguration;
30  
31  
32  /**
33   * Holds a reference to a login module and the control flag.  A linked list of these forms the list of login modules
34   * in a GenericSecurityRealm.
35   *
36   * @version $Rev: 391894 $ $Date: 2006-04-05 21:00:33 -0700 (Wed, 05 Apr 2006) $
37   */
38  public class JaasLoginModuleUse implements JaasLoginModuleChain {
39      // See also http://java.sun.com/j2se/1.4.2/docs/guide/security/jaas/JAASLMDevGuide.html for more standard login module option keys
40      public final static String KERNEL_NAME_LM_OPTION = "org.apache.geronimo.security.realm.GenericSecurityRealm.KERNEL";
41      public final static String SERVERINFO_LM_OPTION = "org.apache.geronimo.security.realm.GenericSecurityRealm.SERVERINFO";
42      public final static String CLASSLOADER_LM_OPTION = "org.apache.geronimo.security.realm.GenericSecurityRealm.CLASSLOADER";
43  
44      private final LoginModuleSettings loginModule;
45      private final JaasLoginModuleUse next;
46      private LoginModuleControlFlag controlFlag;
47      private final Kernel kernel;
48  
49      //for reference.
50      public JaasLoginModuleUse() {
51          loginModule = null;
52          next = null;
53          controlFlag = null;
54          kernel = null;
55      }
56  
57      public JaasLoginModuleUse(LoginModuleSettings loginModule, JaasLoginModuleUse next, String controlFlag, Kernel kernel) {
58          this.loginModule = loginModule;
59          this.next = next;
60          LoginModuleControlFlagEditor editor = new LoginModuleControlFlagEditor();
61          editor.setAsText(controlFlag);
62          this.controlFlag = (LoginModuleControlFlag) editor.getValue();
63          this.kernel = kernel;
64      }
65  
66      public LoginModuleSettings getLoginModule() {
67          return loginModule;
68      }
69  
70      public JaasLoginModuleChain getNext() {
71          return next;
72      }
73  
74      public String getLoginModuleName() {
75          //TODO configId which is correct?
76  //        return kernel.getAbstractNameFor(loginModule).getObjectName().getCanonicalName();
77          return kernel.getAbstractNameFor(loginModule).toURI().toString();
78      }
79  
80      public String getNextName() {
81          if(next == null) {
82              return null;
83          }
84          //TODO configId which is correct?
85  //        return kernel.getAbstractNameFor(next).getObjectName().getCanonicalName();
86          return kernel.getAbstractNameFor(next).toURI().toString();
87      }
88  
89      public String getControlFlag() {
90          return controlFlag.toString();
91      }
92  
93      public void setControlFlag(String controlFlag) {
94          LoginModuleControlFlagEditor ed = new LoginModuleControlFlagEditor();
95          ed.setAsText(controlFlag);
96          this.controlFlag = (LoginModuleControlFlag) ed.getValue();
97      }
98  
99      public void configure(Set domainNames, List loginModuleConfigurations, Kernel kernel, ServerInfo serverInfo, ClassLoader classLoader) {
100         Map options = loginModule.getOptions();
101         if (options != null) {
102             options = new HashMap(options);
103         } else {
104             options = new HashMap();
105         }
106         if (kernel != null && !options.containsKey(KERNEL_NAME_LM_OPTION)) {
107             options.put(KERNEL_NAME_LM_OPTION, kernel.getKernelName());
108         }
109         if (serverInfo != null && !options.containsKey(SERVERINFO_LM_OPTION)) {
110             options.put(SERVERINFO_LM_OPTION, serverInfo);
111         }
112         if (classLoader != null && !options.containsKey(CLASSLOADER_LM_OPTION)) {
113             options.put(CLASSLOADER_LM_OPTION, classLoader);
114         }
115         if (loginModule.getLoginDomainName() != null) {
116             if (domainNames.contains(loginModule.getLoginDomainName())) {
117                 throw new IllegalStateException("Error in realm: one security realm cannot contain multiple login modules for the same login domain");
118             } else {
119                 domainNames.add(loginModule.getLoginDomainName());
120             }
121         }
122         JaasLoginModuleConfiguration config = new JaasLoginModuleConfiguration(loginModule.getLoginModuleClass(), controlFlag, options, loginModule.isServerSide(), loginModule.getLoginDomainName(), loginModule.isWrapPrincipals(), loginModule.getClassLoader());
123         loginModuleConfigurations.add(config);
124 
125         if (next != null) {
126             next.configure(domainNames, loginModuleConfigurations, kernel, serverInfo, classLoader);
127         }
128     }
129 
130     public static final GBeanInfo GBEAN_INFO;
131 
132     static {
133         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(JaasLoginModuleUse.class, "LoginModuleUse");
134         infoBuilder.addAttribute("controlFlag", String.class, true);
135         infoBuilder.addAttribute("kernel", Kernel.class, false, false);
136         infoBuilder.addReference("LoginModule", LoginModuleSettings.class, NameFactory.LOGIN_MODULE);
137         infoBuilder.addReference("Next", JaasLoginModuleUse.class);
138 
139         infoBuilder.addOperation("configure", new Class[]{Set.class, List.class, Kernel.class, ServerInfo.class, ClassLoader.class});
140         infoBuilder.addInterface(JaasLoginModuleChain.class);
141         infoBuilder.setConstructor(new String[]{"LoginModule", "Next", "controlFlag", "kernel"});
142         GBEAN_INFO = infoBuilder.getBeanInfo();
143     }
144 
145     public static GBeanInfo getGBeanInfo() {
146         return GBEAN_INFO;
147     }
148 }