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.server;
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  
32  
33  /**
34   * @version $Revision: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
35   */
36  public class WrappingLoginModuleProxy implements LoginModule {
37      private final LoginModule source;
38      private final String loginDomainName;
39      private final String realmName;
40      private final Subject localSubject = new Subject();
41      private Subject subject;
42  
43      public WrappingLoginModuleProxy(LoginModule source, String loginDomainName, String realmName) {
44          this.source = source;
45          this.loginDomainName = loginDomainName;
46          this.realmName = realmName;
47      }
48  
49      public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
50          this.subject = subject;
51          source.initialize(localSubject, callbackHandler, sharedState, options);
52      }
53  
54      public boolean login() throws LoginException {
55          return source.login();
56      }
57  
58      public boolean abort() throws LoginException {
59          return source.abort();
60      }
61  
62      public boolean commit() throws LoginException {
63          boolean result = source.commit();
64  
65          Set wrapped = new HashSet();
66          for (Iterator iter = localSubject.getPrincipals().iterator(); iter.hasNext();) {
67              Principal principal = (Principal) iter.next();
68  
69              wrapped.add(new DomainPrincipal(loginDomainName, principal));
70              wrapped.add(new RealmPrincipal(realmName, loginDomainName, principal));
71          }
72          localSubject.getPrincipals().addAll(wrapped);
73          subject.getPrincipals().addAll(localSubject.getPrincipals());
74  
75          return result;
76      }
77  
78      public boolean logout() throws LoginException {
79          boolean result = source.logout();
80  
81          subject.getPrincipals().removeAll(localSubject.getPrincipals());
82          localSubject.getPrincipals().clear();
83  
84          return result;
85      }
86  }