KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Copyright 2003-2004 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;
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: 46019 $ $Date: 2004-09-14 05:56:06 -0400 (Tue, 14 Sep 2004) $
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)
41             throws IllegalArgumentException JavaDoc, UnsupportedCallbackException JavaDoc {
42         if (exploring) {
43             source = callbacks;
44             throw new UnsupportedCallbackException JavaDoc(callbacks != null && callbacks.length > 0 ? callbacks[0] : null, "DO NOT PROCEED WITH THIS LOGIN");
45         } else {
46             if(callbacks.length != source.length) {
47                 throw new IllegalArgumentException JavaDoc("Mismatched callbacks");
48             }
49             for (int i = 0; i < callbacks.length; i++) {
50                 callbacks[i] = source[i];
51             }
52         }
53     }
54
55     /**
56      * Within the same VM, the client just populates the callbacks directly
57      * into the array held by this object. However, remote clients will
58      * serialize their responses, so they need to be manually set back on this
59      * object to take effect.
60      *
61      * @param callbacks The callbacks populated by the client
62      */

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

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

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