use of javax.security.auth.callback.ChoiceCallback in project OpenAM by OpenRock.
the class RestAuthChoiceCallbackHandlerTest method shouldNotFailToConvertFromJsonWithIntValueAsString.
@Test
public void shouldNotFailToConvertFromJsonWithIntValueAsString() throws RestAuthException {
//Given
ChoiceCallback choiceCallback = new ChoiceCallback("Select choice:", new String[] { "1", "34", "66", "93" }, 0, true);
JsonValue jsonNameCallback = json(object(field("input", array(object(field("value", "1")))), field("output", array(object(field("value", "Select choice:")), object(field("value", array("1", "34", "66", "93"))), object(field("value", "0")))), field("type", "choicecallback")));
//When
ChoiceCallback convertedChoiceCallback = restAuthChoiceCallbackHandler.convertFromJson(choiceCallback, jsonNameCallback);
//Then
assertEquals(choiceCallback, convertedChoiceCallback);
assertEquals("Select choice:", convertedChoiceCallback.getPrompt());
assertEquals(new String[] { "1", "34", "66", "93" }, convertedChoiceCallback.getChoices());
assertEquals(0, convertedChoiceCallback.getDefaultChoice());
assertEquals(new int[] { 1 }, convertedChoiceCallback.getSelectedIndexes());
}
use of javax.security.auth.callback.ChoiceCallback in project OpenAM by OpenRock.
the class RestAuthChoiceCallbackHandlerTest method shouldHandleCallback.
@Test
public void shouldHandleCallback() {
//Given
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
JsonValue jsonPostBody = mock(JsonValue.class);
ChoiceCallback originalChoiceCallback = mock(ChoiceCallback.class);
//When
ChoiceCallback choiceCallback = restAuthChoiceCallbackHandler.handle(request, response, jsonPostBody, originalChoiceCallback);
//Then
assertEquals(originalChoiceCallback, choiceCallback);
}
use of javax.security.auth.callback.ChoiceCallback in project OpenAM by OpenRock.
the class AMLoginModule method cloneCallbacks.
/**
* Clone Callback[], and save it in the internal/external
* callbacks list. External callback contains all user defined
* Callbacks in the xml module configuration (property file),
* internal callback contains the external callbacks plus the
* PagePropertiesCallback. Note here, although
* Callback[] in internal/external are different, the Callback
* instance they pointed are actually same instance
* @param index indicates state of callback
* @param original original array of callback to be cloned
* @return Callback[] returns cloned callback
* @exception AuthLoginException if callback can not be cloned
*/
private Callback[] cloneCallbacks(int index, Callback[] original) throws AuthLoginException {
// check if there is any callbacks in original
if (original == null || original.length == 0) {
// this is the error case where there is no Callbacks
// defined for a state
debug.error("cloneCallbacks, no callbacks in state " + (index + 1));
throw new AuthLoginException(bundleName, "noCallbackState", new Object[] { new Integer(index + 1) });
}
int len = original.length;
// Callback array which hold the cloned Callbacks
Callback[] copy = new Callback[len];
// List which contains the external callbacks only
List extCallbacks = new ArrayList();
// if it is an external Callback, add to the extCallback list
for (int i = 0; i < len; i++) {
if (original[i] instanceof HiddenValueCallback) {
final HiddenValueCallback hiddenValueCallback = (HiddenValueCallback) original[i];
String defaultValue = hiddenValueCallback.getDefaultValue();
if (defaultValue != null && defaultValue.length() != 0) {
copy[i] = new HiddenValueCallback(hiddenValueCallback.getId(), defaultValue);
} else {
copy[i] = new HiddenValueCallback(hiddenValueCallback.getId());
}
extCallbacks.add(copy[i]);
if (debug.messageEnabled()) {
debug.message("clone #" + i + " is HiddenValueCallback");
}
} else if (original[i] instanceof NameCallback) {
String dftName = ((NameCallback) original[i]).getDefaultName();
if (dftName != null && dftName.length() != 0) {
copy[i] = new NameCallback(((NameCallback) original[i]).getPrompt(), dftName);
} else {
copy[i] = new NameCallback(((NameCallback) original[i]).getPrompt());
}
extCallbacks.add(copy[i]);
if (debug.messageEnabled()) {
debug.message("clone #" + i + " is NameCallback");
}
} else if (original[i] instanceof PasswordCallback) {
copy[i] = new PasswordCallback(((PasswordCallback) original[i]).getPrompt(), ((PasswordCallback) original[i]).isEchoOn());
extCallbacks.add(copy[i]);
if (debug.messageEnabled()) {
debug.message("clone #" + i + " is PasswordCallback");
}
} else if (original[i] instanceof ScriptTextOutputCallback) {
copy[i] = new ScriptTextOutputCallback(((TextOutputCallback) original[i]).getMessage());
extCallbacks.add(copy[i]);
if (debug.messageEnabled()) {
debug.message("clone #" + i + " is ScriptTextOutputCallback");
}
} else if (original[i] instanceof TextOutputCallback) {
copy[i] = new TextOutputCallback(((TextOutputCallback) original[i]).getMessageType(), ((TextOutputCallback) original[i]).getMessage());
extCallbacks.add(copy[i]);
if (debug.messageEnabled()) {
debug.message("clone #" + i + " is TextOutputCallback");
}
} else if (original[i] instanceof PagePropertiesCallback) {
// PagePropertiesCallback, no need to add to external callbacks
copy[i] = new PagePropertiesCallback(((PagePropertiesCallback) original[i]).getModuleName(), ((PagePropertiesCallback) original[i]).getHeader(), ((PagePropertiesCallback) original[i]).getImage(), ((PagePropertiesCallback) original[i]).getTimeOutValue(), ((PagePropertiesCallback) original[i]).getTemplateName(), ((PagePropertiesCallback) original[i]).getErrorState(), ((PagePropertiesCallback) original[i]).getPageState());
((PagePropertiesCallback) copy[i]).setRequire(((PagePropertiesCallback) original[i]).getRequire());
((PagePropertiesCallback) copy[i]).setAttribute(((PagePropertiesCallback) original[i]).getAttribute());
((PagePropertiesCallback) copy[i]).setInfoText(((PagePropertiesCallback) original[i]).getInfoText());
if (debug.messageEnabled()) {
debug.message("clone #" + i + " is PagePropertiesCallback");
}
} else if (original[i] instanceof ChoiceCallback) {
ChoiceCallback originalChoiceCallback = (ChoiceCallback) original[i];
ChoiceCallback clone = new ChoiceCallback(originalChoiceCallback.getPrompt(), originalChoiceCallback.getChoices(), originalChoiceCallback.getDefaultChoice(), originalChoiceCallback.allowMultipleSelections());
if (originalChoiceCallback.getSelectedIndexes() != null && originalChoiceCallback.getSelectedIndexes().length > 0) {
if (originalChoiceCallback.allowMultipleSelections()) {
clone.setSelectedIndexes(originalChoiceCallback.getSelectedIndexes());
} else {
clone.setSelectedIndex(originalChoiceCallback.getSelectedIndexes()[0]);
}
}
copy[i] = clone;
extCallbacks.add(copy[i]);
if (debug.messageEnabled()) {
debug.message("clone #" + i + " is ChoiceCallback");
}
} else if (original[i] instanceof ConfirmationCallback) {
ConfirmationCallback temp = (ConfirmationCallback) original[i];
String prompt = temp.getPrompt();
String[] options = temp.getOptions();
if (prompt == null) {
// no prompt
if (options == null) {
// no options
copy[i] = new ConfirmationCallback(temp.getMessageType(), temp.getOptionType(), temp.getDefaultOption());
} else {
copy[i] = new ConfirmationCallback(temp.getMessageType(), options, temp.getDefaultOption());
}
} else {
// has prompt
if (options == null) {
// no options
copy[i] = new ConfirmationCallback(prompt, temp.getMessageType(), temp.getOptionType(), temp.getDefaultOption());
} else {
copy[i] = new ConfirmationCallback(prompt, temp.getMessageType(), options, temp.getDefaultOption());
}
}
extCallbacks.add(copy[i]);
if (debug.messageEnabled()) {
debug.message("clone #" + i + " is ConfirmationCallback");
}
} else if (original[i] instanceof TextInputCallback) {
copy[i] = new TextInputCallback(((TextInputCallback) original[i]).getPrompt());
extCallbacks.add(copy[i]);
if (debug.messageEnabled()) {
debug.message("clone #" + i + " is TextInputCallback");
}
} else if (original[i] instanceof HttpCallback) {
HttpCallback hc = (HttpCallback) original[i];
copy[i] = new HttpCallback(hc.getAuthorizationHeader(), hc.getNegotiationHeaderName(), hc.getNegotiationHeaderValue(), hc.getNegotiationCode());
extCallbacks.add(copy[i]);
} else if (original[i] instanceof RedirectCallback) {
RedirectCallback rc = (RedirectCallback) original[i];
copy[i] = new RedirectCallback(rc.getRedirectUrl(), rc.getRedirectData(), rc.getMethod(), rc.getStatusParameter(), rc.getRedirectBackUrlCookieName());
extCallbacks.add(copy[i]);
} else {
debug.error("unknown callback " + original[i]);
}
// more callbacks need to be handled here if ...
}
// construct external Callback[]
Callback[] ext = new Callback[extCallbacks.size()];
if (!extCallbacks.isEmpty()) {
Iterator it = extCallbacks.iterator();
int i = 0;
while (it.hasNext()) {
ext[i++] = (Callback) it.next();
}
}
// set external/internal callbacks
internal.set(index, copy);
external.set(index, ext);
return ext;
}
use of javax.security.auth.callback.ChoiceCallback in project OpenAM by OpenRock.
the class AMModuleProperties method walk.
//walk the DOM tree and create the callback array
private void walk(Node node) {
int type = node.getNodeType();
String nodeName = node.getNodeName();
debug.message("Callback type: " + nodeName);
String tmp;
Node sub;
switch(type) {
case Node.ELEMENT_NODE:
{
if (nodeName.equals("ModuleProperties")) {
moduleName = getAttribute(node, "moduleName");
} else if (nodeName.equals("Callbacks")) {
p = 0;
String timeout = getAttribute(node, "timeout");
String template = getAttribute(node, "template");
if (template == null) {
template = "";
}
String image = getAttribute(node, "image");
if (image == null) {
image = "";
}
String header = getAttribute(node, "header");
boolean error = Boolean.valueOf(getAttribute(node, "error")).booleanValue();
int size = Integer.parseInt(getAttribute(node, "length")) + 1;
int t = 0;
if (timeout != null) {
t = Integer.parseInt(timeout);
}
order = getAttribute(node, "order");
callbacks = new Callback[size];
callbacks[p] = new PagePropertiesCallback(moduleName, header, image, t, template, error, order);
p++;
attribute = new ArrayList();
require = new ArrayList();
infoText = new ArrayList<String>();
} else if (nodeName.equals("NameCallback")) {
sub = node.getFirstChild();
sub = sub.getNextSibling();
String prompt = sub.getFirstChild().getNodeValue();
String dftName = null;
sub = sub.getNextSibling().getNextSibling();
if (sub != null) {
sub = sub.getFirstChild();
dftName = sub.getNodeValue();
callbacks[p] = new NameCallback(prompt, dftName);
} else {
callbacks[p] = new NameCallback(prompt);
}
tmp = getAttribute(node, "isRequired");
if (tmp != null) {
if (tmp.equals("true")) {
require.add("true");
} else {
require.add("");
}
} else {
require.add("");
}
tmp = getAttribute(node, "attribute");
if (tmp != null) {
attribute.add(tmp);
} else {
attribute.add("");
}
tmp = getAttribute(node, "infoText");
if (tmp != null) {
infoText.add(tmp);
} else {
infoText.add("");
}
p++;
} else if (nodeName.equals("HiddenValueCallback")) {
sub = node.getFirstChild();
sub = sub.getNextSibling();
String id = sub.getFirstChild().getNodeValue();
String dftName = null;
sub = sub.getNextSibling().getNextSibling();
if (sub != null) {
sub = sub.getFirstChild();
dftName = sub.getNodeValue();
callbacks[p] = new HiddenValueCallback(id, dftName);
} else {
callbacks[p] = new HiddenValueCallback(id);
}
tmp = getAttribute(node, "isRequired");
if (Boolean.parseBoolean(tmp)) {
require.add("true");
} else {
require.add("");
}
tmp = getAttribute(node, "attribute");
if (tmp != null) {
attribute.add(tmp);
} else {
attribute.add("");
}
tmp = getAttribute(node, "infoText");
if (tmp != null) {
infoText.add(tmp);
} else {
infoText.add("");
}
p++;
} else if (nodeName.equals("PasswordCallback")) {
String echo = getAttribute(node, "echoPassword");
sub = node.getFirstChild();
sub = sub.getNextSibling().getFirstChild();
String prompt = sub.getNodeValue();
boolean b = Boolean.valueOf(echo).booleanValue();
callbacks[p] = new PasswordCallback(prompt, b);
tmp = getAttribute(node, "isRequired");
if (tmp != null) {
if (tmp.equals("true")) {
require.add("true");
} else {
require.add("");
}
} else {
require.add("");
}
tmp = getAttribute(node, "attribute");
if (tmp != null) {
attribute.add(tmp);
} else {
attribute.add("");
}
tmp = getAttribute(node, "infoText");
if (tmp != null) {
infoText.add(tmp);
} else {
infoText.add("");
}
p++;
} else if (nodeName.equals("ChoiceCallback")) {
String multiple = getAttribute(node, "multipleSelectionsAllowed");
String prompt = null;
String[] choices = null;
int defaultChoice = 0;
boolean mch = Boolean.valueOf(multiple).booleanValue();
for (sub = node.getFirstChild(); sub != null; sub = sub.getNextSibling()) {
if (sub.getNodeName().equals("Prompt")) {
prompt = sub.getFirstChild().getNodeValue();
} else if (sub.getNodeName().equals("ChoiceValues")) {
int len = 0;
Node ss = sub.getFirstChild().getNextSibling();
for (Node count = ss; count != null; count = count.getNextSibling().getNextSibling()) {
len++;
}
choices = new String[len];
for (int i = 0; i < len; i++) {
choices[i] = ss.getFirstChild().getNextSibling().getFirstChild().getNodeValue();
if (Boolean.valueOf(getAttribute(ss, "isDefault")).booleanValue()) {
defaultChoice = i;
}
ss = ss.getNextSibling().getNextSibling();
}
break;
}
}
callbacks[p] = new ChoiceCallback(prompt, choices, defaultChoice, mch);
tmp = getAttribute(node, "isRequired");
if (tmp != null) {
if (tmp.equals("true")) {
require.add("true");
} else {
require.add("");
}
} else {
require.add("");
}
tmp = getAttribute(node, "attribute");
if (tmp != null) {
attribute.add(tmp);
} else {
attribute.add("");
}
tmp = getAttribute(node, "infoText");
if (tmp != null) {
infoText.add(tmp);
} else {
infoText.add("");
}
p++;
} else if (nodeName.equals("ConfirmationCallback")) {
int messageType = ConfirmationCallback.INFORMATION;
int defaultOption = 0;
String[] options = null;
for (sub = node.getFirstChild(); sub != null; sub = sub.getNextSibling()) {
if (sub.getNodeName().equals("OptionValues")) {
int len = 0;
Node ss = sub.getFirstChild().getNextSibling();
for (Node count = ss; count != null; count = count.getNextSibling().getNextSibling()) {
len++;
}
options = new String[len];
for (int i = 0; i < len; i++) {
options[i] = ss.getFirstChild().getNextSibling().getFirstChild().getNodeValue();
ss = ss.getNextSibling().getNextSibling();
}
break;
}
}
callbacks[p] = new ConfirmationCallback(messageType, options, defaultOption);
p++;
} else if (nodeName.equals("TextInputCallback")) {
sub = node.getFirstChild();
sub = sub.getNextSibling().getFirstChild();
String prompt = sub.getNodeValue();
callbacks[p] = new TextInputCallback(prompt);
p++;
} else if (nodeName.equals("TextOutputCallback")) {
int messageType = TextOutputCallback.ERROR;
String s = getAttribute(node, "messageType");
String value = node.getFirstChild().getNodeValue();
if (s.equals("script")) {
callbacks[p] = new ScriptTextOutputCallback(value);
} else {
if (s.equals("error")) {
messageType = TextOutputCallback.ERROR;
} else if (s.equals("warning")) {
messageType = TextOutputCallback.WARNING;
} else {
//default to information
messageType = TextOutputCallback.INFORMATION;
}
callbacks[p] = new TextOutputCallback(messageType, value);
}
p++;
} else if (nodeName.equals("LanguageCallback")) {
for (sub = node.getFirstChild(); sub != null; sub = sub.getNextSibling()) {
if (sub.getNodeName().equals("ChoiceValue")) {
String isdefault = getAttribute(sub, "isDefault");
}
}
callbacks[p] = new LanguageCallback();
p++;
} else if (nodeName.equals(AuthXMLTags.HTTP_CALLBACK)) {
String header = null;
String negotiation = null;
String code = null;
sub = node.getFirstChild();
for (; sub != null; sub = sub.getNextSibling()) {
String tmpStr = sub.getNodeName();
if (tmpStr.equals(AuthXMLTags.HTTP_HEADER)) {
header = sub.getFirstChild().getNodeValue();
} else if (tmpStr.equals(AuthXMLTags.HTTP_NEGO)) {
negotiation = sub.getFirstChild().getNodeValue();
} else if (tmpStr.equals(AuthXMLTags.HTTP_CODE)) {
code = sub.getFirstChild().getNodeValue();
}
}
callbacks[p] = new HttpCallback(header, negotiation, code);
p++;
} else if (nodeName.equals(AuthXMLTags.REDIRECT_CALLBACK)) {
String redirectUrl = null;
String statusParameter = null;
String redirectBackUrlCookie = null;
Map redirectData = new HashMap();
String method = getAttribute(node, AuthXMLTags.REDIRECT_METHOD);
sub = node.getFirstChild();
for (; sub != null; sub = sub.getNextSibling()) {
String tmpStr = sub.getNodeName();
if (tmpStr.equals(AuthXMLTags.REDIRECT_URL)) {
redirectUrl = sub.getFirstChild().getNodeValue();
} else if (tmpStr.equals(AuthXMLTags.REDIRECT_STATUS_PARAM)) {
statusParameter = sub.getFirstChild().getNodeValue();
} else if (tmpStr.equals(AuthXMLTags.REDIRECT_BACK_URL_COOKIE)) {
redirectBackUrlCookie = sub.getFirstChild().getNodeValue();
} else if (tmpStr.equals(AuthXMLTags.REDIRECT_DATA)) {
String name = null;
String value = null;
Node ss = sub.getFirstChild().getNextSibling();
String tmpStrName = ss.getNodeName();
if (tmpStrName.equals("Name")) {
name = ss.getFirstChild().getNodeValue();
}
ss = ss.getNextSibling().getNextSibling();
String tmpStrValue = ss.getNodeName();
if (tmpStrValue.equals("Value")) {
value = ss.getFirstChild().getNodeValue();
}
redirectData.put(name, value);
}
}
if (debug.messageEnabled()) {
debug.message("redirectUrl : " + redirectUrl);
debug.message("statusParameter : " + statusParameter);
debug.message("redirectBackUrlCookie : " + redirectBackUrlCookie);
debug.message("redirectData : " + redirectData);
debug.message("method : " + method);
}
callbacks[p] = new RedirectCallback(redirectUrl, redirectData, method);
p++;
}
break;
}
//end of element
default:
break;
}
for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
walk(child);
}
//without this the ending tags will miss
if (type == Node.ELEMENT_NODE) {
if (nodeName.equals("Callbacks")) {
((PagePropertiesCallback) callbacks[0]).setAttribute(attribute);
((PagePropertiesCallback) callbacks[0]).setRequire(require);
((PagePropertiesCallback) callbacks[0]).setInfoText(infoText);
rtable.put(order, callbacks);
//attrtable.put(order, subAttr);
//requiretable.put(order, subRequire);
}
}
}
use of javax.security.auth.callback.ChoiceCallback in project OpenAM by OpenRock.
the class AMLoginModule method resetCallback.
/**
* Reset a Callback instance to the original Callback for the specified
* state and the specified index. This will override change to the Callback
* instance by the <code>replaceCallback()</code> method.
* @param state state in which the Callback[] to be reset
* @param index index order of the Callback in the Callback[], index starts
* with 0, i.e. 0 means first callback instance, 1 means
* the second callback instance.
* @throws AuthLoginException if state or index is out of bound.
* @supported.api
*/
public void resetCallback(int state, int index) throws AuthLoginException {
if (debug.messageEnabled()) {
debug.message("resetCallback: state=" + state + ",index=" + index);
}
// check state length
if (state > stateLength) {
throw new AuthLoginException(bundleName, "invalidState", new Object[] { new Integer(state) });
}
// check callback length for the state
Callback[] ext = getCallback(state);
if (index < 0 || index >= ext.length) {
throw new AuthLoginException(bundleName, "invalidCallbackIndex", new Object[] { new Integer(index) });
}
// get the Callback from AMModuleProperties
// add one to index here since first one is the PagePropertiesCallback
Callback callback = ((Callback[]) origList.get(state - 1))[index + 1];
Callback newCallback = null;
if (callback instanceof NameCallback) {
newCallback = new NameCallback(((NameCallback) callback).getPrompt());
} else if (callback instanceof PasswordCallback) {
newCallback = new PasswordCallback(((PasswordCallback) callback).getPrompt(), ((PasswordCallback) callback).isEchoOn());
} else if (callback instanceof ChoiceCallback) {
int selection = ((ChoiceCallback) callback).getDefaultChoice();
newCallback = new ChoiceCallback(((ChoiceCallback) callback).getPrompt(), ((ChoiceCallback) callback).getChoices(), selection, ((ChoiceCallback) callback).allowMultipleSelections());
} else {
// should never come here since only above three will be supported
debug.error("Unsupported call back instance " + callback);
throw new AuthLoginException(bundleName, "unknownCallback", null);
}
if (debug.messageEnabled()) {
debug.message("original=" + callback + ",new=" + newCallback);
}
// set external & internal callback instance
((Callback[]) internal.get(state - 1))[index + 1] = newCallback;
((Callback[]) external.get(state - 1))[index] = newCallback;
}
Aggregations