01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.jetspeed.security.impl;
18:
19: import javax.security.auth.callback.*;
20:
21: /**
22: * <p>PassiveCallbackHandler has constructor that takes
23: * a username and password so its handle() method does
24: * not have to prompt the user for input.</p>
25: * <p>Useful for server-side applications.</p>
26: *
27: * <p>This code was inspired from an article from:<p>
28: * <ul>
29: * <li><a href="http://www.javaworld.com/javaworld/jw-09-2002/jw-0913-jaas.html">
30: * All that JAAS</a></li>
31: * </ul> *
32: * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
33: */
34:
35: public class PassiveCallbackHandler implements CallbackHandler {
36:
37: private String username;
38: char[] password;
39:
40: /**
41: * <p>Creates a callback handler with the give username
42: * and password.</p>
43: * @param username The username.
44: * @param password The password.
45: */
46: public PassiveCallbackHandler(String username, String password) {
47: this .username = username;
48: this .password = password.toCharArray();
49: }
50:
51: /**
52: * <p>Handles the specified set of Callbacks. Uses the
53: * username and password that were supplied to our
54: * constructor to popluate the Callbacks.</p>
55: * <p>This class supports NameCallback and PasswordCallback.</p>
56: *
57: * @param callbacks the callbacks to handle
58: * @throws IOException if an input or output error occurs.
59: * @throws UnsupportedCallbackException if the callback is not an
60: * instance of NameCallback or PasswordCallback
61: */
62: public void handle(Callback[] callbacks)
63: throws java.io.IOException, UnsupportedCallbackException {
64: for (int i = 0; i < callbacks.length; i++) {
65: if (callbacks[i] instanceof NameCallback) {
66: ((NameCallback) callbacks[i]).setName(username);
67: } else if (callbacks[i] instanceof PasswordCallback) {
68: ((PasswordCallback) callbacks[i]).setPassword(password);
69: } else {
70: throw new UnsupportedCallbackException(callbacks[i],
71: "Callback class not supported");
72: }
73: }
74: }
75:
76: /**
77: * <p>Clears out password state.</p>
78: */
79: public void clearPassword() {
80: if (password != null) {
81: for (int i = 0; i < password.length; i++) {
82: password[i] = ' ';
83: }
84: password = null;
85: }
86: }
87:
88: }
|