use of javax.security.auth.callback.ConfirmationCallback in project OpenAM by OpenRock.
the class RestAuthConfirmationCallbackHandlerTest method shouldConvertFromJson.
@Test
public void shouldConvertFromJson() throws RestAuthException {
//Given
ConfirmationCallback confirmationCallback = new ConfirmationCallback("Select confirmation:", ConfirmationCallback.INFORMATION, new String[] { "OK", "NO", "CANCEL" }, 0);
JsonValue jsonConfirmationCallback = JsonValueBuilder.jsonValue().array("input").addLast(JsonValueBuilder.jsonValue().put("value", 2).build()).array("output").add(JsonValueBuilder.jsonValue().put("value", "Select confirmation:").build()).add(JsonValueBuilder.jsonValue().put("value", 0).build()).add(JsonValueBuilder.jsonValue().put("value", new String[] { "OK", "NO", "CANCEL" }).build()).add(JsonValueBuilder.jsonValue().put("value", -1).build()).addLast(JsonValueBuilder.jsonValue().put("value", 0).build()).put("type", "ConfirmationCallback").build();
//When
ConfirmationCallback convertedConfirmationCallback = restAuthConfirmationCallbackHandler.convertFromJson(confirmationCallback, jsonConfirmationCallback);
//Then
assertEquals(confirmationCallback, convertedConfirmationCallback);
assertEquals("Select confirmation:", convertedConfirmationCallback.getPrompt());
assertEquals(ConfirmationCallback.INFORMATION, convertedConfirmationCallback.getMessageType());
assertEquals("OK", convertedConfirmationCallback.getOptions()[0]);
assertEquals("NO", convertedConfirmationCallback.getOptions()[1]);
assertEquals("CANCEL", convertedConfirmationCallback.getOptions()[2]);
assertEquals(-1, convertedConfirmationCallback.getOptionType());
assertEquals(0, convertedConfirmationCallback.getDefaultOption());
assertEquals(2, convertedConfirmationCallback.getSelectedIndex());
}
use of javax.security.auth.callback.ConfirmationCallback in project OpenAM by OpenRock.
the class RestAuthConfirmationCallbackHandlerTest method shouldConvertToJson.
@Test
public void shouldConvertToJson() throws RestAuthException {
//Given
ConfirmationCallback confirmationCallback = new ConfirmationCallback("Select confirmation:", ConfirmationCallback.INFORMATION, new String[] { "OK", "NO", "CANCEL" }, 0);
//When
JsonValue jsonObject = restAuthConfirmationCallbackHandler.convertToJson(confirmationCallback, 1);
//Then
assertEquals("ConfirmationCallback", jsonObject.get("type").asString());
assertNotNull(jsonObject.get("output"));
assertEquals(5, jsonObject.get("output").size());
assertEquals("Select confirmation:", jsonObject.get("output").get(0).get("value").asString());
assertEquals(ConfirmationCallback.INFORMATION, (int) jsonObject.get("output").get(1).get("value").asInteger());
assertEquals("OK", jsonObject.get("output").get(2).get("value").get(0).asString());
assertEquals("NO", jsonObject.get("output").get(2).get("value").get(1).asString());
assertEquals("CANCEL", jsonObject.get("output").get(2).get("value").get(2).asString());
assertEquals(-1, (int) jsonObject.get("output").get(3).get("value").asInteger());
assertEquals(0, (int) jsonObject.get("output").get(4).get("value").asInteger());
assertNotNull(jsonObject.get("input"));
assertEquals(1, jsonObject.get("input").size());
assertEquals(0, (int) jsonObject.get("input").get(0).get("value").asInteger());
}
use of javax.security.auth.callback.ConfirmationCallback in project OpenAM by OpenRock.
the class OpenAMAuthHandler method injectAnswerForCallback.
/**
* Injects the user's answer into the callback currently waiting for one with proper handling for the type of
* callback. Increments the index of the current callback and returns true if the value was successly injected or
* false if it failed and terminated authentication.
*
* @param respHandler
* @param holder
* @param answer
*/
private boolean injectAnswerForCallback(RadiusResponse response, ContextHolder holder, String answer) {
final Callback[] callbacks = holder.getCallbacks();
if (callbacks == null) {
return false;
}
final Callback cb = callbacks[holder.getIdxOfCurrentCallback()];
// so that we are sitting on that callback in the next call
holder.incrementIdxOfCurrentCallback();
if (cb instanceof NameCallback) {
final NameCallback nc = (NameCallback) cb;
((NameCallback) cb).setName(answer);
// cLog.warning("--- set NameCallback=" + answer);
} else if (cb instanceof PasswordCallback) {
final PasswordCallback pc = (PasswordCallback) cb;
pc.setPassword(answer.toCharArray());
// cLog.warning("--- set PasswordCallback=" + answer);
} else if (cb instanceof ChoiceCallback) {
final ChoiceCallback cc = (ChoiceCallback) cb;
final int maxIdx = cc.getChoices().length - 1;
if ("".equals(answer)) {
// user didn't provide an answer so accept default
cc.setSelectedIndex(cc.getDefaultChoice());
// cLog.warning("--- set ChoiceCallback=default(" + cc.getDefaultChoice() + ")");
return true;
}
final boolean answerContainsSeparator = answer.indexOf(' ') != -1;
if (cc.allowMultipleSelections() && answerContainsSeparator) {
// may need to parse answer
if (answerContainsSeparator) {
final String[] answers = answer.split(" ");
final List<Integer> idxs = new ArrayList<Integer>();
for (final String ans : answers) {
if (!"".equals(ans)) {
final int idx = parseInt(response, ans, answer, maxIdx, holder, cb);
if (idx == -1) {
// cLog.warning("--- ChoiceCallback failed parsing mult");
return false;
}
idxs.add(idx);
}
}
final int[] selected = new int[idxs.size()];
for (int i = 0; i < selected.length; i++) {
selected[i] = idxs.get(i);
}
cc.setSelectedIndexes(selected);
// cLog.warning("--- set ChoiceCallback=" + Arrays.asList(selected));
}
} else {
final int idx = parseInt(response, answer, answer, maxIdx, holder, cb);
if (idx == -1) {
// cLog.warning("--- ChoiceCallback failed parsing");
return false;
}
cc.setSelectedIndex(idx);
// cLog.warning("--- set ChoiceCallback=" + idx);
}
} else if (cb instanceof ConfirmationCallback) {
final ConfirmationCallback cc = (ConfirmationCallback) cb;
final int maxIdx = cc.getOptions().length - 1;
if ("".equals(answer)) {
// user didn't provide an answer so accept default
cc.setSelectedIndex(cc.getDefaultOption());
// cLog.warning("--- set ConfirmationCallback=default(" + cc.getDefaultOption() + ")");
return true;
}
final int idx = parseInt(response, answer, answer, maxIdx, holder, cb);
if (idx == -1) {
// cLog.warning("--- ConfirmationCallback failed parsing");
return false;
}
cc.setSelectedIndex(idx);
// cLog.warning("--- set ConfirmationCallback=" + idx);
} else {
LOG.error("Unrecognized callback type '" + cb.getClass().getSimpleName() + "' while processing challenge response. Unable to submit answer. Denying Access.");
rejectAccessAndTerminateProcess(response, holder);
return false;
}
// reset the timeout since we just received confirmation that the user is still there.
holder.setMillisExpiryPoint(System.currentTimeMillis() + holder.getMillisExpiryForCurrentCallbacks());
return true;
}
use of javax.security.auth.callback.ConfirmationCallback in project OpenAM by OpenRock.
the class LoginViewBean method addLoginCallbackMessage.
// Method to generate HTML page from Callback objects
protected void addLoginCallbackMessage(Callback[] callbacks) throws Exception {
loginDebug.message("In addLoginCallbackMessage()");
buttonOptions = null;
pageState = null;
for (int i = 0; i < callbacks.length; i++) {
if (loginDebug.messageEnabled()) {
loginDebug.message("In addLoginCallbackMessage() callback : " + callbacks[i]);
}
if (callbacks[i] instanceof ConfirmationCallback) {
ConfirmationCallback conc = (ConfirmationCallback) callbacks[i];
buttonOptions = conc.getOptions();
defaultButtonIndex = conc.getDefaultOption();
String defaultButton = buttonOptions[defaultButtonIndex];
} else if (callbacks[i] instanceof PagePropertiesCallback) {
PagePropertiesCallback ppc = (PagePropertiesCallback) callbacks[i];
TextHeaderVal = ppc.getHeader();
pageTemplate = ppc.getTemplateName();
pageImage = ppc.getImage();
requiredList = ppc.getRequire();
pageState = ppc.getPageState();
infoText = ppc.getInfoText();
int lsize = 0;
if ((requiredList != null) && (!requiredList.isEmpty())) {
loginDebug.message("PPC - list not null & not empty");
lsize = requiredList.size();
}
if (loginDebug.messageEnabled()) {
loginDebug.message("PagePropertiesCallback - header : " + TextHeaderVal + " template : " + pageTemplate + " image : " + pageImage + " Required list : " + requiredList + " List size : " + lsize + "Info Text : " + infoText + " Page State : " + pageState);
}
// empty callback processing
if (callbacks.length == 1) {
onePageLogin = true;
processLoginDisplay();
break;
}
}
}
}
use of javax.security.auth.callback.ConfirmationCallback in project OpenAM by OpenRock.
the class LoginViewBean method processLoginDisplay.
protected void processLoginDisplay() throws Exception {
loginDebug.message("In processLoginDisplay()");
String tmp = "";
try {
if (!onePageLogin) {
if (AuthUtils.isNewRequest(ac)) {
loginDebug.message("In processLoginDisplay() : Session New ");
getLoginDisplay();
return;
}
}
String page_state = request.getParameter("page_state");
if (loginDebug.messageEnabled()) {
loginDebug.message("Submit with Page State : " + page_state);
}
if ((page_state != null) && (page_state.length() != 0)) {
callbacks = AuthUtils.getCallbacksPerState(ac, page_state);
if (callbacks == null) {
errorCode = AMAuthErrorCode.AUTH_TIMEOUT;
ErrorMessage = AuthUtils.getErrorVal(AMAuthErrorCode.AUTH_TIMEOUT, AuthUtils.ERROR_MESSAGE);
errorTemplate = AuthUtils.getErrorVal(AMAuthErrorCode.AUTH_TIMEOUT, AuthUtils.ERROR_TEMPLATE);
return;
}
//Get Callbacks in order to set the page state
Callback[] callbacksForPageState = AuthUtils.getRecdCallback(ac);
for (int i = 0; i < callbacksForPageState.length; i++) {
if (loginDebug.messageEnabled()) {
loginDebug.message("In processLoginDisplay() callbacksForPageState : " + callbacksForPageState[i]);
}
if (callbacksForPageState[i] instanceof PagePropertiesCallback) {
PagePropertiesCallback ppc = (PagePropertiesCallback) callbacksForPageState[i];
if (loginDebug.messageEnabled()) {
loginDebug.message("setPageState in PPC to : " + page_state);
}
ppc.setPageState(page_state);
break;
}
}
} else {
callbacks = AuthUtils.getRecdCallback(ac);
}
indexType = AuthUtils.getIndexType(ac);
// Assign user specified values
for (int i = 0; i < callbacks.length; i++) {
if (loginDebug.messageEnabled()) {
loginDebug.message("In processLoginDisplay() callback : " + callbacks[i]);
}
if (callbacks[i] instanceof NameCallback) {
NameCallback nc = (NameCallback) callbacks[i];
tmp = (String) reqDataHash.get(TOKEN + Integer.toString(i));
if (tmp == null) {
tmp = (String) reqDataHash.get(TOKEN_OLD + Integer.toString(i));
}
if ((bAuthLevel) || (tmp == null)) {
tmp = "";
}
nc.setName(tmp.trim());
} else if (callbacks[i] instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback) callbacks[i];
tmp = (String) reqDataHash.get(TOKEN + Integer.toString(i));
if (tmp == null) {
tmp = (String) reqDataHash.get(TOKEN_OLD + Integer.toString(i));
}
if (tmp == null) {
tmp = "";
}
pc.setPassword(tmp.toCharArray());
} else if (callbacks[i] instanceof ChoiceCallback) {
ChoiceCallback cc = (ChoiceCallback) callbacks[i];
choice = (String) reqDataHash.get(TOKEN + Integer.toString(i));
if (choice == null) {
choice = (String) reqDataHash.get(TOKEN_OLD + Integer.toString(i));
}
if (loginDebug.messageEnabled()) {
loginDebug.message("choice : " + choice);
}
String[] choices = cc.getChoices();
if (choice == null) {
if (loginDebug.messageEnabled()) {
loginDebug.message("No selected choice.");
}
} else if (choice.indexOf("|") != -1) {
StringTokenizer st = new StringTokenizer(choice, "|");
int cnt = st.countTokens();
int[] selectIndexs = new int[cnt];
int j = 0;
if (loginDebug.messageEnabled()) {
loginDebug.message("No of tokens : " + Integer.toString(cnt));
}
while (st.hasMoreTokens()) {
choice = st.nextToken();
if (choice != null && choice.length() != 0) {
int selected = Integer.parseInt(choice);
choice = choices[selected];
selectIndexs[j++] = selected;
if (loginDebug.messageEnabled()) {
loginDebug.message("selected choice : " + choice + " & selected index : " + selected);
}
}
}
cc.setSelectedIndexes(selectIndexs);
if (loginDebug.messageEnabled()) {
loginDebug.message("Selected indexes : " + selectIndexs);
}
} else {
int selected = Integer.parseInt(choice);
cc.setSelectedIndex(selected);
choice = choices[selected];
if (loginDebug.messageEnabled()) {
loginDebug.message("selected ONE choice : " + choice + " & selected ONE index : " + selected);
}
}
} else if (callbacks[i] instanceof ConfirmationCallback) {
ConfirmationCallback conc = (ConfirmationCallback) callbacks[i];
buttonOptions = conc.getOptions();
tmp = (String) reqDataHash.get(BUTTON);
if (tmp == null) {
tmp = (String) reqDataHash.get(BUTTON_OLD);
}
if (tmp == null) {
tmp = "";
}
int selectedIndex = 0;
for (int j = 0; j < buttonOptions.length; j++) {
if ((buttonOptions[j].trim()).equals(tmp.trim())) {
selectedIndex = j;
}
}
conc.setSelectedIndex(selectedIndex);
if (loginDebug.messageEnabled()) {
loginDebug.message("selected button : " + buttonOptions[selectedIndex] + " & selected button index : " + selectedIndex);
}
} else if (callbacks[i] instanceof RedirectCallback) {
RedirectCallback rc = (RedirectCallback) callbacks[i];
String status = request.getParameter(rc.getStatusParameter());
clearCookie(rc.getRedirectBackUrlCookieName());
loginDebug.message("Redirect callback : set status");
rc.setStatus(status);
}
}
// testing
if (loginDebug.messageEnabled()) {
loginDebug.message(" length 0f callbacks : " + callbacks.length);
loginDebug.message(" Index type : " + indexType + " Index name : " + indexName);
}
if ((indexType == AuthContext.IndexType.LEVEL) || (indexType == AuthContext.IndexType.COMPOSITE_ADVICE)) {
if (loginDebug.messageEnabled()) {
loginDebug.message("In processLoginDisplay(), Index type" + " is Auth Level or Composite Advice and selected Module " + "or Service is : " + choice);
}
indexName = AMAuthUtils.getDataFromRealmQualifiedData(choice);
String qualifiedRealm = AMAuthUtils.getRealmFromRealmQualifiedData(choice);
String orgDN = null;
if ((qualifiedRealm != null) && (qualifiedRealm.length() != 0)) {
orgDN = DNMapper.orgNameToDN(qualifiedRealm);
ac.setOrgDN(orgDN);
}
int type = AuthUtils.getCompositeAdviceType(ac);
if (type == AuthUtils.MODULE) {
indexType = AuthContext.IndexType.MODULE_INSTANCE;
} else if (type == AuthUtils.SERVICE) {
indexType = AuthContext.IndexType.SERVICE;
} else if (type == AuthUtils.REALM) {
indexType = AuthContext.IndexType.SERVICE;
orgDN = DNMapper.orgNameToDN(choice);
indexName = AuthUtils.getOrgConfiguredAuthenticationChain(orgDN);
ac.setOrgDN(orgDN);
} else {
indexType = AuthContext.IndexType.MODULE_INSTANCE;
}
bAuthLevel = true;
if ((indexName != null) && (indexType == AuthContext.IndexType.MODULE_INSTANCE)) {
if (indexName.equalsIgnoreCase("Application")) {
onePageLogin = true;
}
}
if (loginDebug.messageEnabled()) {
loginDebug.message("Index type : " + indexType);
loginDebug.message("Index name : " + indexName);
loginDebug.message("qualified orgDN : " + orgDN);
}
getLoginDisplay();
} else {
// Submit the information to auth module
ac.submitRequirements(callbacks);
// Check if more information is required
if (loginDebug.messageEnabled()) {
loginDebug.message("before hasMoreRequirements: Status is: " + ac.getStatus());
}
if (ac.hasMoreRequirements()) {
loginDebug.message("Has more requirements after Submit ");
callbacks = ac.getRequirements();
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof HttpCallback) {
processHttpCallback((HttpCallback) callbacks[i]);
return;
} else if (callbacks[i] instanceof RedirectCallback) {
processRedirectCallback((RedirectCallback) callbacks[i]);
return;
}
}
addLoginCallbackMessage(callbacks);
if (!LoginFail) {
//if the login already failed, then LoginState is already
//nullified, hence any attempt of calling this method
//the errormessage/code/template should be already set
//so a proper error page is shown.
AuthUtils.setCallbacksPerState(ac, pageState, callbacks);
}
} else {
if (loginDebug.messageEnabled()) {
loginDebug.message("No more Requirements : Status is : " + ac.getStatus());
}
if (ac.getStatus() == AuthContext.Status.SUCCESS) {
LoginSuccess = true;
ResultVal = rb.getString("authentication.successful");
/*
* redirect to 'goto' parameter or SPI hook or default
* redirect URL.
*/
redirect_url = AuthUtils.getLoginSuccessURL(ac);
if ((redirect_url != null) && (redirect_url.length() != 0)) {
if (loginDebug.messageEnabled()) {
loginDebug.message("LoginSuccessURL (in case of " + " successful auth) : " + redirect_url);
}
}
} else if (ac.getStatus() == AuthContext.Status.FAILED) {
handleAuthLoginException(null);
/*
* redirect to 'goto' parameter or SPI hook or default
* redirect URL.
*/
redirect_url = AuthUtils.getLoginFailedURL(ac);
if ((redirect_url != null) && (redirect_url.length() != 0)) {
if (loginDebug.messageEnabled()) {
loginDebug.message("LoginFailedURL : " + redirect_url);
}
}
} else {
/*
* redirect to 'goto' parameter or SPI hook or default
* redirect URL.
*/
redirect_url = AuthUtils.getLoginFailedURL(ac);
if (loginDebug.warningEnabled()) {
loginDebug.warning("Login Status is " + ac.getStatus() + " - redirect to loginFailedURL : " + redirect_url);
}
setErrorMessage(null);
}
}
}
} catch (Exception e) {
if (loginDebug.messageEnabled()) {
loginDebug.message("Error in processing LoginDisplay : ", e);
}
setErrorMessage(e);
throw new L10NMessageImpl(bundleName, "loginDisplay.process", new Object[] { e.getMessage() });
}
}
Aggregations