1 /**
2 *
3 * Copyright 2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.geronimo.security.jaas.client;
18
19 import java.util.Map;
20 import javax.security.auth.Subject;
21 import javax.security.auth.callback.Callback;
22 import javax.security.auth.callback.CallbackHandler;
23 import javax.security.auth.login.LoginException;
24 import javax.security.auth.login.FailedLoginException;
25
26 import org.apache.geronimo.security.jaas.server.JaasSessionId;
27 import org.apache.geronimo.security.jaas.server.JaasLoginServiceMBean;
28 import org.apache.geronimo.security.jaas.LoginModuleControlFlag;
29
30
31 /**
32 * @version $Revision: 472291 $ $Date: 2006-11-07 13:51:35 -0800 (Tue, 07 Nov 2006) $
33 */
34 public class ServerLoginProxy extends LoginModuleProxy {
35 CallbackHandler handler;
36 Callback[] callbacks;
37 private final int lmIndex;
38 private final JaasLoginServiceMBean service;
39 private final JaasSessionId sessionHandle;
40
41 public ServerLoginProxy(LoginModuleControlFlag controlFlag, Subject subject, int lmIndex,
42 JaasLoginServiceMBean service, JaasSessionId sessionHandle)
43 {
44 super(controlFlag, subject);
45 this.lmIndex = lmIndex;
46 this.service = service;
47 this.sessionHandle = sessionHandle;
48 }
49
50 public void initialize(Subject subject, CallbackHandler handler, Map sharedState, Map options) {
51 this.handler = handler;
52 }
53
54 /**
55 * Perform a login on the server side.
56 * <p/>
57 * Here we get the Callbacks from the server side, pass them to the
58 * local handler so that they may be filled. We pass the resulting
59 * set of Callbacks back to the server.
60 *
61 * @return true if the authentication succeeded, or false if this
62 * <code>LoginModule</code> should be ignored.
63 * @throws javax.security.auth.login.LoginException
64 * if the authentication fails
65 */
66 public boolean login() throws LoginException {
67 try {
68 callbacks = service.getServerLoginCallbacks(sessionHandle, lmIndex);
69 if (handler != null) {
70 handler.handle(callbacks);
71 } else if (callbacks != null && callbacks.length > 0) {
72 System.err.println("No callback handler available for " + callbacks.length + " callbacks!");
73 }
74 return service.performLogin(sessionHandle, lmIndex, callbacks);
75 } catch (FailedLoginException e) {
76 throw e;
77 } catch (Exception e) {
78 LoginException le = new LoginException("Error filling callback list");
79 le.initCause(e);
80 throw le;
81 }
82 }
83
84 public boolean commit() throws LoginException {
85 return service.performCommit(sessionHandle, lmIndex);
86 }
87
88 public boolean abort() throws LoginException {
89 return service.performAbort(sessionHandle, lmIndex);
90 }
91
92 public boolean logout() throws LoginException {
93 return false;
94 }
95 }