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.util.Map;
020    import javax.security.auth.Subject;
021    import javax.security.auth.callback.Callback;
022    import javax.security.auth.callback.CallbackHandler;
023    import javax.security.auth.login.LoginException;
024    import javax.security.auth.login.FailedLoginException;
025    
026    import org.apache.geronimo.security.jaas.server.JaasSessionId;
027    import org.apache.geronimo.security.jaas.server.JaasLoginServiceMBean;
028    import org.apache.geronimo.security.jaas.LoginModuleControlFlag;
029    
030    
031    /**
032     * @version $Revision: 472291 $ $Date: 2006-11-07 13:51:35 -0800 (Tue, 07 Nov 2006) $
033     */
034    public class ServerLoginProxy extends LoginModuleProxy {
035        CallbackHandler handler;
036        Callback[] callbacks;
037        private final int lmIndex;
038        private final JaasLoginServiceMBean service;
039        private final JaasSessionId sessionHandle;
040    
041        public ServerLoginProxy(LoginModuleControlFlag controlFlag, Subject subject, int lmIndex,
042                                JaasLoginServiceMBean service, JaasSessionId sessionHandle)
043        {
044            super(controlFlag, subject);
045            this.lmIndex = lmIndex;
046            this.service = service;
047            this.sessionHandle = sessionHandle;
048        }
049    
050        public void initialize(Subject subject, CallbackHandler handler, Map sharedState, Map options) {
051            this.handler = handler;
052        }
053    
054        /**
055         * Perform a login on the server side.
056         * <p/>
057         * Here we get the Callbacks from the server side, pass them to the
058         * local handler so that they may be filled.  We pass the resulting
059         * set of Callbacks back to the server.
060         *
061         * @return true if the authentication succeeded, or false if this
062         *         <code>LoginModule</code> should be ignored.
063         * @throws javax.security.auth.login.LoginException
064         *          if the authentication fails
065         */
066        public boolean login() throws LoginException {
067            try {
068                callbacks = service.getServerLoginCallbacks(sessionHandle, lmIndex);
069                if (handler != null) {
070                    handler.handle(callbacks);
071                } else if (callbacks != null && callbacks.length > 0) {
072                    System.err.println("No callback handler available for " + callbacks.length + " callbacks!");
073                }
074                return service.performLogin(sessionHandle, lmIndex, callbacks);
075            } catch (FailedLoginException e) {
076                throw e;
077            } catch (Exception e) {
078                LoginException le = new LoginException("Error filling callback list");
079                le.initCause(e);
080                throw le;
081            }
082        }
083    
084        public boolean commit() throws LoginException {
085            return service.performCommit(sessionHandle, lmIndex);
086        }
087    
088        public boolean abort() throws LoginException {
089            return service.performAbort(sessionHandle, lmIndex);
090        }
091    
092        public boolean logout() throws LoginException {
093            return false; // taken care of with a single call to the server
094        }
095    }