001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. 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.server;
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
032
033 /**
034 * @version $Revision: 487175 $ $Date: 2006-12-14 03:10:31 -0800 (Thu, 14 Dec 2006) $
035 */
036 public class WrappingLoginModuleProxy implements LoginModule {
037 private final LoginModule source;
038 private final String loginDomainName;
039 private final String realmName;
040 private final Subject localSubject = new Subject();
041 private Subject subject;
042
043 public WrappingLoginModuleProxy(LoginModule source, String loginDomainName, String realmName) {
044 this.source = source;
045 this.loginDomainName = loginDomainName;
046 this.realmName = realmName;
047 }
048
049 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
050 this.subject = subject;
051 source.initialize(localSubject, callbackHandler, sharedState, options);
052 }
053
054 public boolean login() throws LoginException {
055 return source.login();
056 }
057
058 public boolean abort() throws LoginException {
059 return source.abort();
060 }
061
062 public boolean commit() throws LoginException {
063 boolean result = source.commit();
064
065 Set wrapped = new HashSet();
066 for (Iterator iter = localSubject.getPrincipals().iterator(); iter.hasNext();) {
067 Principal principal = (Principal) iter.next();
068
069 wrapped.add(new DomainPrincipal(loginDomainName, principal));
070 wrapped.add(new RealmPrincipal(realmName, loginDomainName, principal));
071 }
072 localSubject.getPrincipals().addAll(wrapped);
073 subject.getPrincipals().addAll(localSubject.getPrincipals());
074
075 return result;
076 }
077
078 public boolean logout() throws LoginException {
079 boolean result = source.logout();
080
081 subject.getPrincipals().removeAll(localSubject.getPrincipals());
082 localSubject.getPrincipals().clear();
083
084 return result;
085 }
086 }