View Javadoc

1   /**
2    *
3    * Copyright 2005 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.client;
18  
19  import java.security.Principal;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  import javax.security.auth.Subject;
25  import javax.security.auth.callback.CallbackHandler;
26  import javax.security.auth.login.LoginException;
27  import javax.security.auth.spi.LoginModule;
28  
29  import org.apache.geronimo.security.DomainPrincipal;
30  import org.apache.geronimo.security.RealmPrincipal;
31  import org.apache.geronimo.security.jaas.LoginModuleControlFlag;
32  
33  
34  /**
35   * @version $Revision: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
36   */
37  public class WrappingClientLoginModuleProxy extends ClientLoginModuleProxy {
38      private final String loginDomainName;
39      private final String realmName;
40      private final Subject localSubject = new Subject();
41  
42      public WrappingClientLoginModuleProxy(LoginModuleControlFlag controlFlag, Subject subject, LoginModule source,
43                                            String loginDomainName, String realmName)
44      {
45          super(controlFlag, subject, source);
46          this.loginDomainName = loginDomainName;
47          this.realmName = realmName;
48      }
49  
50      public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
51          super.initialize(localSubject, callbackHandler, sharedState, options);
52      }
53  
54      public boolean commit() throws LoginException {
55          boolean result = super.commit();
56  
57          Set wrapped = new HashSet();
58          for (Iterator iter = subject.getPrincipals().iterator(); iter.hasNext();) {
59              Principal principal = (Principal) iter.next();
60  
61              wrapped.add(new DomainPrincipal(loginDomainName, principal));
62              wrapped.add(new RealmPrincipal(realmName, loginDomainName, principal));
63          }
64          localSubject.getPrincipals().addAll(wrapped);
65          subject.getPrincipals().addAll(localSubject.getPrincipals());
66  
67          return result;
68      }
69  
70      public boolean logout() throws LoginException {
71          boolean result = super.logout();
72  
73          subject.getPrincipals().removeAll(localSubject.getPrincipals());
74          localSubject.getPrincipals().clear();
75  
76          return result;
77      }
78  }