use of com.novell.ldapchai.cr.Challenge in project pwm by pwm-project.
the class ChallengeProfile method readChallengeSet.
private static ChallengeSet readChallengeSet(final String profileID, final Locale locale, final StoredConfiguration storedConfiguration, final PwmSetting requiredChallenges, final PwmSetting randomChallenges, final int minimumRands) throws PwmOperationalException {
final List<ChallengeItemConfiguration> requiredQuestions = valueToChallengeItemArray(storedConfiguration.readSetting(requiredChallenges, profileID), locale);
final List<ChallengeItemConfiguration> randomQuestions = valueToChallengeItemArray(storedConfiguration.readSetting(randomChallenges, profileID), locale);
final List<Challenge> challenges = new ArrayList<>();
int randoms = minimumRands;
if (requiredQuestions != null) {
for (final ChallengeItemConfiguration item : requiredQuestions) {
if (item != null) {
final Challenge chaiChallenge = new ChaiChallenge(true, item.getText(), item.getMinLength(), item.getMaxLength(), item.isAdminDefined(), item.getMaxQuestionCharsInAnswer(), item.isEnforceWordlist());
challenges.add(chaiChallenge);
}
}
}
if (randomQuestions != null) {
for (final ChallengeItemConfiguration item : randomQuestions) {
if (item != null) {
final Challenge chaiChallenge = new ChaiChallenge(false, item.getText(), item.getMinLength(), item.getMaxLength(), item.isAdminDefined(), item.getMaxQuestionCharsInAnswer(), item.isEnforceWordlist());
challenges.add(chaiChallenge);
}
}
if (randoms > randomQuestions.size()) {
randoms = randomQuestions.size();
}
} else {
randoms = 0;
}
try {
return new ChaiChallengeSet(challenges, randoms, locale, PwmConstants.PWM_APP_NAME + "-defined " + PwmConstants.SERVLET_VERSION);
} catch (ChaiValidationException e) {
throw new PwmOperationalException(new ErrorInformation(PwmError.CONFIG_FORMAT_ERROR, "invalid challenge set configuration: " + e.getMessage()));
}
}
use of com.novell.ldapchai.cr.Challenge in project pwm by pwm-project.
the class SetupResponsesServlet method populateSetupData.
private static SetupResponsesBean.SetupData populateSetupData(final ChallengeSet challengeSet, final int minRandomSetup) {
boolean useSimple = true;
final Map<String, Challenge> indexedChallenges = new LinkedHashMap<>();
int minRandom = minRandomSetup;
{
if (minRandom != 0 && minRandom < challengeSet.getMinRandomRequired()) {
minRandom = challengeSet.getMinRandomRequired();
}
if (minRandom > challengeSet.getRandomChallenges().size()) {
minRandom = 0;
}
}
{
{
if (minRandom == 0) {
useSimple = false;
}
for (final Challenge challenge : challengeSet.getChallenges()) {
if (!challenge.isRequired() && !challenge.isAdminDefined()) {
useSimple = false;
}
}
if (challengeSet.getRandomChallenges().size() == challengeSet.getMinRandomRequired()) {
useSimple = false;
}
}
}
{
int index = 0;
for (final Challenge loopChallenge : challengeSet.getChallenges()) {
indexedChallenges.put(String.valueOf(index), loopChallenge);
index++;
}
}
final SetupResponsesBean.SetupData setupData = new SetupResponsesBean.SetupData();
setupData.setChallengeSet(challengeSet);
setupData.setSimpleMode(useSimple);
setupData.setIndexedChallenges(indexedChallenges);
setupData.setMinRandomSetup(minRandom);
return setupData;
}
use of com.novell.ldapchai.cr.Challenge in project ldapchai by ldapchai.
the class NmasResponseSet method parseNmasUserResponseXML.
static ChallengeSet parseNmasUserResponseXML(final String str) throws IOException, JDOMException, ChaiValidationException {
final List<Challenge> returnList = new ArrayList<Challenge>();
final Reader xmlreader = new StringReader(str);
final SAXBuilder builder = new SAXBuilder();
final Document doc = builder.build(xmlreader);
final Element rootElement = doc.getRootElement();
final int minRandom = StringHelper.convertStrToInt(rootElement.getAttributeValue("RandomQuestions"), 0);
final String guidValue;
{
final Attribute guidAttribute = rootElement.getAttribute("GUID");
guidValue = guidAttribute == null ? null : guidAttribute.getValue();
}
for (Iterator iter = doc.getDescendants(new ElementFilter("Challenge")); iter.hasNext(); ) {
final Element loopQ = (Element) iter.next();
final int maxLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MaxLength"), 255);
final int minLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MinLength"), 2);
final String defineStrValue = loopQ.getAttributeValue("Define");
final boolean adminDefined = "Admin".equalsIgnoreCase(defineStrValue);
final String typeStrValue = loopQ.getAttributeValue("Type");
final boolean required = "Required".equalsIgnoreCase(typeStrValue);
final String challengeText = loopQ.getText();
final Challenge challenge = new ChaiChallenge(required, challengeText, minLength, maxLength, adminDefined, 0, false);
returnList.add(challenge);
}
return new ChaiChallengeSet(returnList, minRandom, null, guidValue);
}
use of com.novell.ldapchai.cr.Challenge in project ldapchai by ldapchai.
the class NmasResponseSet method write.
boolean write() throws ChaiUnavailableException, ChaiOperationException {
if (this.state != STATE.NEW) {
throw new IllegalStateException("RepsonseSet not suitable for writing (not in NEW state)");
}
// write challenge set questions to Nmas Login Config
try {
final PutLoginConfigRequest request = new PutLoginConfigRequest();
request.setObjectDN(user.getEntryDN());
final byte[] data = csToNmasXML(getChallengeSet(), this.csIdentifier).getBytes("UTF8");
request.setData(data);
request.setDataLen(data.length);
request.setTag("ChallengeResponseQuestions");
request.setMethodID(NMASChallengeResponse.METHOD_ID);
request.setMethodIDLen(NMASChallengeResponse.METHOD_ID.length * 4);
final ExtendedResponse response = user.getChaiProvider().extendedOperation(request);
if (response != null && ((PutLoginConfigResponse) response).getNmasRetCode() != 0) {
LOGGER.debug("nmas error writing question: " + ((PutLoginConfigResponse) response).getNmasRetCode());
return false;
}
} catch (UnsupportedEncodingException e) {
LOGGER.error("error while writing nmas questions: " + e.getMessage());
return false;
} catch (ChaiOperationException e) {
LOGGER.error("error while writing nmas questions: " + e.getMessage());
throw e;
} catch (ChaiValidationException e) {
LOGGER.error("error while writing nmas questions: " + e.getMessage());
throw ChaiOperationException.forErrorMessage(e.getMessage());
}
boolean success = true;
// write responses
for (final Map.Entry<Challenge, Answer> entry : crMap.entrySet()) {
final Challenge loopChallenge = entry.getKey();
try {
final byte[] data = ((NmasAnswer) entry.getValue()).getAnswerText().getBytes("UTF8");
final PutLoginSecretRequest request = new PutLoginSecretRequest();
request.setObjectDN(user.getEntryDN());
request.setData(data);
request.setDataLen(data.length);
request.setTag(loopChallenge.getChallengeText());
request.setMethodID(NMASChallengeResponse.METHOD_ID);
request.setMethodIDLen(NMASChallengeResponse.METHOD_ID.length * 4);
final ExtendedResponse response = user.getChaiProvider().extendedOperation(request);
if (response != null && ((PutLoginSecretResponse) response).getNmasRetCode() != 0) {
LOGGER.debug("nmas error writing answer: " + ((PutLoginSecretResponse) response).getNmasRetCode());
success = false;
}
} catch (Exception e) {
LOGGER.error("error while writing nmas answer: " + e.getMessage());
}
}
if (success) {
LOGGER.info("successfully wrote NMAS challenge/response set for user " + user.getEntryDN());
this.state = STATE.WRITTEN;
}
return success;
}
use of com.novell.ldapchai.cr.Challenge in project ldapchai by ldapchai.
the class NmasResponseSet method csToNmasXML.
static String csToNmasXML(final ChallengeSet cs, final String guidValue) {
final Element rootElement = new Element(NMAS_XML_ROOTNODE);
rootElement.setAttribute(NMAS_XML_ATTR_RANDOM_COUNT, String.valueOf(cs.getMinRandomRequired()));
if (guidValue != null) {
rootElement.setAttribute("GUID", guidValue);
} else {
rootElement.setAttribute("GUID", "0");
}
for (final Challenge challenge : cs.getChallenges()) {
final Element loopElement = new Element(NMAS_XML_NODE_CHALLENGE);
if (challenge.getChallengeText() != null) {
loopElement.setText(challenge.getChallengeText());
}
if (challenge.isAdminDefined()) {
loopElement.setAttribute(NMAS_XML_ATTR_DEFINE, "Admin");
} else {
loopElement.setAttribute(NMAS_XML_ATTR_DEFINE, "User");
}
if (challenge.isRequired()) {
loopElement.setAttribute(NMAS_XML_ATTR_TYPE, "Required");
} else {
loopElement.setAttribute(NMAS_XML_ATTR_TYPE, "Random");
}
loopElement.setAttribute(NMAS_XML_ATTR_MIN_LENGTH, String.valueOf(challenge.getMinLength()));
loopElement.setAttribute(NMAS_XML_ATTR_MAX_LENGTH, String.valueOf(challenge.getMaxLength()));
rootElement.addContent(loopElement);
}
final XMLOutputter outputter = new XMLOutputter();
final Format format = Format.getRawFormat();
format.setTextMode(Format.TextMode.PRESERVE);
format.setLineSeparator("");
outputter.setFormat(format);
return outputter.outputString(rootElement);
}
Aggregations