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    
018    package org.apache.geronimo.security.realm.providers;
019    
020    import java.util.Map;
021    import javax.security.auth.Subject;
022    import javax.security.auth.callback.Callback;
023    import javax.security.auth.callback.CallbackHandler;
024    import javax.security.auth.callback.NameCallback;
025    import javax.security.auth.callback.PasswordCallback;
026    import javax.security.auth.callback.UnsupportedCallbackException;
027    import javax.security.auth.login.LoginException;
028    import javax.security.auth.spi.LoginModule;
029    
030    
031    /**
032     * @version $Rev: 487175 $ $Date: 2006-12-14 03:10:31 -0800 (Thu, 14 Dec 2006) $
033     */
034    public class GeronimoPasswordCredentialLoginModule implements LoginModule {
035    
036        private Subject subject;
037        private CallbackHandler callbackHandler;
038    
039        private GeronimoPasswordCredential geronimoPasswordCredential;
040    
041        public void initialize(Subject subject, CallbackHandler callbackHandler,
042                               Map sharedState, Map options) {
043            this.subject = subject;
044            this.callbackHandler = callbackHandler;
045        }
046    
047        public boolean login() throws LoginException {
048            Callback[] callbacks = new Callback[2];
049            callbacks[0] = new NameCallback("");
050            callbacks[1] = new PasswordCallback("", false);
051            try {
052                callbackHandler.handle(callbacks);
053            } catch (java.io.IOException e) {
054            } catch (UnsupportedCallbackException e) {
055                throw (LoginException) new LoginException("Unlikely UnsupportedCallbackException").initCause(e);
056            }
057            geronimoPasswordCredential = new GeronimoPasswordCredential(((NameCallback) callbacks[0]).getName(),
058                                                                        ((PasswordCallback) callbacks[1]).getPassword());
059            return true;
060        }
061    
062        public boolean commit() throws LoginException {
063            subject.getPrivateCredentials().add(geronimoPasswordCredential);
064            return true;
065        }
066    
067        public boolean abort() throws LoginException {
068            geronimoPasswordCredential = null;
069            return true;
070        }
071    
072        public boolean logout() throws LoginException {
073            geronimoPasswordCredential = null;
074            return true;
075        }
076    }