KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > jaas > server > DecouplingCallbackHandler


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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.server;
18
19 import javax.security.auth.callback.Callback JavaDoc;
20 import javax.security.auth.callback.CallbackHandler JavaDoc;
21 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
22
23 /**
24  * This callback handler separates the process of obtaining callbacks from
25  * the user from the process of providing the user's values to the login
26  * module. This means the JaasLoginService can figure out what callbacks
27  * the module wants and prompt the user in advance, and then turn around
28  * and pass those values to the login module, instead of actually prompting
29  * the user at the mercy of the login module.
30  *
31  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
32  */

33 public class DecouplingCallbackHandler implements CallbackHandler JavaDoc {
34     private Callback JavaDoc[] source;
35     private boolean exploring = true;
36
37     public DecouplingCallbackHandler() {
38     }
39
40     public void handle(Callback JavaDoc[] callbacks) throws IllegalArgumentException JavaDoc, UnsupportedCallbackException JavaDoc {
41         if (exploring) {
42             source = callbacks;
43             throw new UnsupportedCallbackException JavaDoc(callbacks != null && callbacks.length > 0 ? callbacks[0] : null, "DO NOT PROCEED WITH THIS LOGIN");
44         } else {
45             if(callbacks.length != source.length) {
46                 throw new IllegalArgumentException JavaDoc("Mismatched callbacks");
47             }
48             for (int i = 0; i < callbacks.length; i++) {
49                 callbacks[i] = source[i];
50             }
51         }
52     }
53
54     /**
55      * Within the same VM, the client just populates the callbacks directly
56      * into the array held by this object. However, remote clients will
57      * serialize their responses, so they need to be manually set back on this
58      * object to take effect.
59      *
60      * @param callbacks The callbacks populated by the client
61      */

62     public void setClientResponse(Callback JavaDoc[] callbacks) throws IllegalArgumentException JavaDoc {
63         if(source == null && callbacks == null) {
64             return; // i.e. The Kerberos LM doesn't need callbacks
65
}
66         if(callbacks.length != source.length) {
67             throw new IllegalArgumentException JavaDoc("Mismatched callbacks");
68         }
69         for (int i = 0; i < callbacks.length; i++) {
70             source[i] = callbacks[i];
71         }
72     }
73
74
75     /**
76      * While we're exploring, we'll discover new callbacks that the server
77      * login module wants. While not exploring, we'll actually set
78      * values for the server callbacks.
79      */

80     public void setExploring() {
81         exploring = true;
82         source = null;
83     }
84
85     /**
86      * Indicates that the exploring phase is over.
87      */

88     public Callback JavaDoc[] finalizeCallbackList() {
89         exploring = false;
90         return source;
91     }
92 }
93
Popular Tags