01: package org.jacorb.demo.sas;
02:
03: import javax.security.auth.callback.Callback;
04: import javax.security.auth.callback.CallbackHandler;
05: import javax.security.auth.callback.NameCallback;
06: import javax.security.auth.callback.PasswordCallback;
07: import javax.security.auth.callback.TextOutputCallback;
08: import javax.security.auth.callback.UnsupportedCallbackException;
09:
10: public class JaasTxtCalbackHandler implements CallbackHandler {
11:
12: private String myUsername = "";
13: private char[] myPassword = null;
14:
15: /**
16: * <p>Creates a callback handler that prompts and reads from the
17: * command line for answers to authentication questions.
18: * This can be used by JAAS applications to instantiate a
19: * CallbackHandler.
20:
21: */
22: public JaasTxtCalbackHandler() {
23: }
24:
25: public void setMyUsername(String username) {
26: myUsername = username;
27: }
28:
29: public void setMyPassword(char[] password) {
30: myPassword = password;
31: }
32:
33: /**
34: * Handles the specified set of callbacks.
35: *
36: * @param callbacks the callbacks to handle
37: * @throws IOException if an input or output error occurs.
38: * @throws UnsupportedCallbackException if the callback is not an
39: * instance of NameCallback or PasswordCallback
40: */
41: public void handle(Callback[] callbacks)
42: throws UnsupportedCallbackException {
43:
44: for (int i = 0; i < callbacks.length; i++) {
45: if (callbacks[i] instanceof TextOutputCallback) {
46: TextOutputCallback tc = (TextOutputCallback) callbacks[i];
47:
48: String text;
49: switch (tc.getMessageType()) {
50: case TextOutputCallback.INFORMATION:
51: text = "";
52: break;
53: case TextOutputCallback.WARNING:
54: text = "Warning: ";
55: break;
56: case TextOutputCallback.ERROR:
57: text = "Error: ";
58: break;
59: default:
60: throw new UnsupportedCallbackException(
61: callbacks[i], "Unrecognized message type");
62: }
63:
64: String message = tc.getMessage();
65: if (message != null) {
66: text += message;
67: }
68: if (text != null) {
69: System.err.println(text);
70: }
71:
72: } else if (callbacks[i] instanceof NameCallback) {
73: NameCallback nc = (NameCallback) callbacks[i];
74: nc.setName(myUsername);
75:
76: } else if (callbacks[i] instanceof PasswordCallback) {
77: PasswordCallback pc = (PasswordCallback) callbacks[i];
78: pc.setPassword(myPassword);
79:
80: } else {
81: throw new UnsupportedCallbackException(callbacks[i],
82: "Unrecognized Callback");
83: }
84: }
85: }
86:
87: }
|