Skip to content
Permalink
Browse files
CXF-8641
NPE on NamePasswordCallbackHandler
  • Loading branch information
Jan Filipski authored and Jan Filipski committed Jan 18, 2022
1 parent 22beef1 commit b4f45e7ece7d5f6bdda6df6284decbccc00817af
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
@@ -72,7 +72,7 @@ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallback
((NameCallback) callback).setName(username);
} else if (callback instanceof PasswordCallback) {
PasswordCallback pwCallback = (PasswordCallback) callback;
pwCallback.setPassword(password.toCharArray());
pwCallback.setPassword(password == null ? null : password.toCharArray());
} else if (!invokePasswordCallback(callback)) {
org.apache.cxf.common.i18n.Message errorMsg =
new org.apache.cxf.common.i18n.Message("UNSUPPORTED_CALLBACK_TYPE",
@@ -64,7 +64,7 @@ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallback
((NameCallback) callback).setName(username);
} else if (callback instanceof PasswordCallback) {
PasswordCallback pwCallback = (PasswordCallback) callback;
pwCallback.setPassword(password.toCharArray());
pwCallback.setPassword(password == null ? null : password.toCharArray());
} else if (!invokePasswordCallback(callback)) {
org.apache.cxf.common.i18n.Message errorMsg =
new org.apache.cxf.common.i18n.Message("UNSUPPORTED_CALLBACK_TYPE",
@@ -83,7 +83,7 @@ protected boolean handleCallback(Callback callback) {
/*
* This method is called from the handle(Callback[]) method when the specified callback
* did not match any of the known callback classes. It looks for the callback method
* having the specified method name with one of the suppported parameter types.
* having the specified method name with one of the supported parameter types.
* If found, it invokes the callback method on the object and returns true.
* If not, it returns false.
*/
@@ -26,6 +26,7 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
@@ -75,6 +76,16 @@ public void testHandleCallback4() throws Exception {
assertEquals("dog", new String(((CharArrayCallback)callbacks[1]).getValue()));
}

@Test
public void testHandleCallbackNullPassword() throws Exception {
NamePasswordCallbackHandler handler = new NamePasswordCallbackHandler("Barry", null);
Callback[] callbacks =
new Callback[]{new NameCallback("name"), new PasswordCallback("password", false)};
handler.handle(callbacks);
assertEquals("Barry", ((NameCallback)callbacks[0]).getName());
assertNull(((PasswordCallback)callbacks[1]).getPassword());
}

static class ObjectCallback implements Callback {
private Object obj;

@@ -72,12 +72,13 @@ public Subject doAuthenticate(final String username, final String password) {
try {
Subject subject = new Subject();
LoginContext loginContext = new LoginContext(realm, subject, new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
((NameCallback)callbacks[i]).setName(username);
} else if (callbacks[i] instanceof PasswordCallback) {
((PasswordCallback)callbacks[i]).setPassword(password.toCharArray());
((PasswordCallback)callbacks[i]).setPassword(
password == null ? null : password.toCharArray());
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}

0 comments on commit b4f45e7

Please sign in to comment.