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;
018    
019    import java.io.IOException;
020    import java.util.Arrays;
021    import java.util.Collections;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.Set;
025    import javax.security.auth.DestroyFailedException;
026    import javax.security.auth.Subject;
027    import javax.security.auth.callback.Callback;
028    import javax.security.auth.callback.CallbackHandler;
029    import javax.security.auth.callback.NameCallback;
030    import javax.security.auth.callback.PasswordCallback;
031    import javax.security.auth.callback.UnsupportedCallbackException;
032    import javax.security.auth.login.LoginException;
033    import javax.security.auth.spi.LoginModule;
034    
035    import org.apache.commons.logging.Log;
036    import org.apache.commons.logging.LogFactory;
037    
038    
039    /**
040     * Inserts named Username/Password credential into private credentials of Subject.
041     * <p/>
042     * If either the username or password is not passed in the callback handler,
043     * then the credential is not placed into the Subject.
044     *
045     * This login module does not check credentials so it should never be able to cause a login to succeed.
046     * Therefore the lifecycle methods must return false to indicate success or throw a LoginException to indicate failure.
047     * 
048     * @deprecated As of release 2.0.3
049     * @see org.apache.geronimo.security.realm.providers.NamedUsernamePasswordCredentialLoginModule
050     *
051     * @version $Revision: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
052     */
053    @Deprecated
054    public class NamedUPCredentialLoginModule implements LoginModule {
055        private static Log log = LogFactory.getLog(NamedUPCredentialLoginModule.class);
056    
057        public static final String CREDENTIAL_NAME = "org.apache.geronimo.jaas.NamedUPCredentialLoginModule.Name";
058        public final static List<String> supportedOptions = Collections.unmodifiableList(Arrays.asList(CREDENTIAL_NAME));
059    
060        private String name;
061        private Subject subject;
062        private CallbackHandler callbackHandler;
063        private NamedUsernamePasswordCredential nupCredential;
064    
065        public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
066            this.subject = subject;
067            this.callbackHandler = callbackHandler;
068            for(Object option: options.keySet()) {
069                if(!supportedOptions.contains(option) && !JaasLoginModuleUse.supportedOptions.contains(option)
070                        && !WrappingLoginModule.supportedOptions.contains(option)) {
071                    log.warn("Ignoring option: "+option+". Not supported.");
072                }
073            }
074            this.name = (String) options.get(CREDENTIAL_NAME);
075        }
076    
077        public boolean login() throws LoginException {
078    
079            Callback[] callbacks = new Callback[2];
080    
081            callbacks[0] = new NameCallback("User name");
082            callbacks[1] = new PasswordCallback("Password", false);
083            try {
084                callbackHandler.handle(callbacks);
085            } catch (IOException ioe) {
086                throw (LoginException) new LoginException().initCause(ioe);
087            } catch (UnsupportedCallbackException uce) {
088                throw (LoginException) new LoginException().initCause(uce);
089            }
090    
091            String username = ((NameCallback) callbacks[0]).getName();
092            char[] password = ((PasswordCallback) callbacks[1]).getPassword();
093    
094            if (username == null || password == null) return false;
095    
096            nupCredential = new NamedUsernamePasswordCredential(username, password, name);
097    
098            return false;
099        }
100    
101        public boolean commit() throws LoginException {
102    
103            if (subject.isReadOnly()) {
104                throw new LoginException("Subject is ReadOnly");
105            }
106    
107            Set pvtCreds = subject.getPrivateCredentials();
108            if (nupCredential != null && !pvtCreds.contains(nupCredential)) {
109                pvtCreds.add(nupCredential);
110            }
111    
112            return false;
113        }
114    
115        public boolean abort() throws LoginException {
116    
117            return logout();
118        }
119    
120        public boolean logout() throws LoginException {
121    
122            if (nupCredential == null) return false;
123    
124            if(!subject.isReadOnly()) {
125                subject.getPrivateCredentials().remove(nupCredential);
126            }
127            
128            try {
129                nupCredential.destroy();
130            } catch (DestroyFailedException e) {
131                // do nothing
132            }
133            nupCredential = null;
134    
135            return false;
136        }
137    
138    }