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: */package org.apache.openejb.core.security.jaas;
17:
18: import javax.security.auth.callback.Callback;
19: import javax.security.auth.callback.CallbackHandler;
20: import javax.security.auth.callback.NameCallback;
21: import javax.security.auth.callback.PasswordCallback;
22: import javax.security.auth.callback.UnsupportedCallbackException;
23: import java.io.IOException;
24:
25: /**
26: * A JAAS username password CallbackHandler.
27: */
28: public class UsernamePasswordCallbackHandler implements CallbackHandler {
29:
30: private final String username;
31: private final String password;
32:
33: public UsernamePasswordCallbackHandler(String username,
34: String password) {
35: this .username = username;
36: this .password = password;
37: }
38:
39: public void handle(Callback[] callbacks) throws IOException,
40: UnsupportedCallbackException {
41: for (int i = 0; i < callbacks.length; i++) {
42: Callback callback = callbacks[i];
43: if (callback instanceof PasswordCallback) {
44: PasswordCallback passwordCallback = (PasswordCallback) callback;
45: if (password == null) {
46: passwordCallback.setPassword(null);
47: } else {
48: passwordCallback
49: .setPassword(password.toCharArray());
50: }
51: } else if (callback instanceof NameCallback) {
52: NameCallback nameCallback = (NameCallback) callback;
53: if (username == null) {
54: nameCallback.setName(null);
55: } else {
56: nameCallback.setName(username);
57: }
58: }
59: }
60: }
61:
62: }
|