001    /**
002     *
003     * Copyright 2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.security.jaas.client;
018    
019    import java.security.Principal;
020    import java.util.HashSet;
021    import java.util.Iterator;
022    import java.util.Map;
023    import java.util.Set;
024    import javax.security.auth.Subject;
025    import javax.security.auth.callback.CallbackHandler;
026    import javax.security.auth.login.LoginException;
027    import javax.security.auth.spi.LoginModule;
028    
029    import org.apache.geronimo.security.DomainPrincipal;
030    import org.apache.geronimo.security.RealmPrincipal;
031    import org.apache.geronimo.security.jaas.LoginModuleControlFlag;
032    
033    
034    /**
035     * @version $Revision: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
036     */
037    public class WrappingClientLoginModuleProxy extends ClientLoginModuleProxy {
038        private final String loginDomainName;
039        private final String realmName;
040        private final Subject localSubject = new Subject();
041    
042        public WrappingClientLoginModuleProxy(LoginModuleControlFlag controlFlag, Subject subject, LoginModule source,
043                                              String loginDomainName, String realmName)
044        {
045            super(controlFlag, subject, source);
046            this.loginDomainName = loginDomainName;
047            this.realmName = realmName;
048        }
049    
050        public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
051            super.initialize(localSubject, callbackHandler, sharedState, options);
052        }
053    
054        public boolean commit() throws LoginException {
055            boolean result = super.commit();
056    
057            Set wrapped = new HashSet();
058            for (Iterator iter = subject.getPrincipals().iterator(); iter.hasNext();) {
059                Principal principal = (Principal) iter.next();
060    
061                wrapped.add(new DomainPrincipal(loginDomainName, principal));
062                wrapped.add(new RealmPrincipal(realmName, loginDomainName, principal));
063            }
064            localSubject.getPrincipals().addAll(wrapped);
065            subject.getPrincipals().addAll(localSubject.getPrincipals());
066    
067            return result;
068        }
069    
070        public boolean logout() throws LoginException {
071            boolean result = super.logout();
072    
073            subject.getPrincipals().removeAll(localSubject.getPrincipals());
074            localSubject.getPrincipals().clear();
075    
076            return result;
077        }
078    }