/*******************************************************************************
* Copyright (c) 2007, Dave Whitla
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package au.net.ocean.httpclient.auth.spnego;
import au.net.ocean.util.StringUtil;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.ConfirmationCallback;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.TextOutputCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by dwhitla at Apr 19, 2007 9:40:15 AM
*
* @author <a href="mailto:dave.whitla@ocean.net.au">Dave Whitla</a>
* @version $Id: SPNegoCallbackHandler.java 0 Apr 19, 2007 9:40:15 AM dwhitla $
*/
public class SPNegoCallbackHandler implements javax.security.auth.callback.CallbackHandler {
private static final Logger LOGGER = Logger.getLogger(SPNegoCallbackHandler.class.getName());
private SPNegoCredentials credentials;
public SPNegoCallbackHandler(SPNegoCredentials credentials) {
if (credentials == null) {
throw new IllegalArgumentException("Non-null credentials must be supplied");
}
this.credentials = credentials;
}
/**
* @InheritDoc
*/
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
ConfirmationCallback confirmation = null;
for (Callback callback : callbacks) {
if (callback instanceof TextOutputCallback) {
handleOutputCallback((TextOutputCallback) callback);
} else if (callback instanceof NameCallback) {
((NameCallback) callback).setName(credentials.getPrincipal());
} else if (callback instanceof PasswordCallback) {
handlePasswordCallback((PasswordCallback) callback);
} else if (callback instanceof ConfirmationCallback) {
confirmation = (ConfirmationCallback) callback;
} else {
throw new UnsupportedCallbackException(callback);
}
}
if (confirmation != null) {
handleConfirmationCallback(confirmation);
}
}
private void handlePasswordCallback(PasswordCallback callback) throws IOException {
if (StringUtil.isEmpty(credentials.getPassword())) {
LOGGER.log(Level.INFO, "Empty password");
callback.clearPassword();
} else {
callback.setPassword(credentials.getPassword().toCharArray());
}
}
private void handleOutputCallback(TextOutputCallback textOutputCallback) throws UnsupportedCallbackException {
StringBuilder text = new StringBuilder();
switch (textOutputCallback.getMessageType()) {
case TextOutputCallback.WARNING:
text.append("Warning: ");
break;
case TextOutputCallback.ERROR:
text.append("Error: ");
break;
case TextOutputCallback.INFORMATION:
default:
break;
}
String message = textOutputCallback.getMessage();
if (message != null) {
text.append(message);
}
LOGGER.log(Level.INFO, "TextOutput: {0}", message);
}
private void handleConfirmationCallback(ConfirmationCallback confirmation) throws IOException, UnsupportedCallbackException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
PrintWriter out = new PrintWriter(stream);
String message = getConfirmationMessage(confirmation);
if (!StringUtil.isEmpty(message)) {
out.println(message);
}
int defaultOption = confirmation.getDefaultOption();
Option[] options = getOptions(confirmation);
for (int i = 0; i < options.length; i++) {
out.println(i + ". " + options[i].name + (options[i].value == defaultOption ? " [default]" : ""));
}
out.print("Enter a number: ");
out.flush();
LOGGER.log(Level.INFO, "Confirmation: {0}", stream.toString());
int selectedIndex = select(options, defaultOption);
confirmation.setSelectedIndex(selectedIndex);
LOGGER.log(Level.INFO, "Selected {0}", selectedIndex);
}
private int select(Option[] options, int defaultOption) {
// todo: use constructed config to determine an option
return options[defaultOption].value;
}
private String getConfirmationMessage(ConfirmationCallback confirmation) throws UnsupportedCallbackException {
StringBuilder message = new StringBuilder();
int messageType = confirmation.getMessageType();
switch (messageType) {
case ConfirmationCallback.WARNING:
message.append("Warning: ");
break;
case ConfirmationCallback.ERROR:
message.append("Error: ");
break;
case ConfirmationCallback.INFORMATION:
break;
default:
throw new UnsupportedCallbackException(confirmation, "Unrecognized message type: " + messageType);
}
String prompt = confirmation.getPrompt();
if (prompt == null) {
message.append(prompt);
}
return message.toString();
}
private Option[] getOptions(ConfirmationCallback confirmationCallback) throws UnsupportedCallbackException {
Option[] options;
int optionType = confirmationCallback.getOptionType();
switch (optionType) {
case ConfirmationCallback.YES_NO_OPTION:
options = new Option[]{
new Option("Yes", ConfirmationCallback.YES),
new Option("No", ConfirmationCallback.NO)
};
break;
case ConfirmationCallback.YES_NO_CANCEL_OPTION:
options = new Option[]{
new Option("Yes", ConfirmationCallback.YES),
new Option("No", ConfirmationCallback.NO),
new Option("Cancel", ConfirmationCallback.CANCEL)
};
break;
case ConfirmationCallback.OK_CANCEL_OPTION:
options = new Option[]{
new Option("OK", ConfirmationCallback.OK),
new Option("Cancel", ConfirmationCallback.CANCEL)
};
break;
case ConfirmationCallback.UNSPECIFIED_OPTION:
String[] optionStrings = confirmationCallback.getOptions();
options = new Option[optionStrings.length];
for (int i = 0; i < options.length; i++) {
options[i] = new Option(optionStrings[i], i);
}
break;
default:
throw new UnsupportedCallbackException(confirmationCallback, "Unrecognized option type: " + optionType);
}
return options;
}
private static class Option {
private String name;
private int value;
Option(String name, int value) {
this.name = name;
this.value = value;
}
}
}