Search in sources :

Example 11 with Challenge

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()));
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) ChaiValidationException(com.novell.ldapchai.exception.ChaiValidationException) ChallengeItemConfiguration(password.pwm.config.value.data.ChallengeItemConfiguration) ArrayList(java.util.ArrayList) ChaiChallengeSet(com.novell.ldapchai.cr.ChaiChallengeSet) Challenge(com.novell.ldapchai.cr.Challenge) ChaiChallenge(com.novell.ldapchai.cr.ChaiChallenge) ChaiChallenge(com.novell.ldapchai.cr.ChaiChallenge) PwmOperationalException(password.pwm.error.PwmOperationalException)

Example 12 with Challenge

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;
}
Also used : SetupResponsesBean(password.pwm.http.bean.SetupResponsesBean) Challenge(com.novell.ldapchai.cr.Challenge) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with Challenge

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);
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) Document(org.jdom2.Document) Challenge(com.novell.ldapchai.cr.Challenge) ChaiChallenge(com.novell.ldapchai.cr.ChaiChallenge) ElementFilter(org.jdom2.filter.ElementFilter) StringReader(java.io.StringReader) Iterator(java.util.Iterator) ChaiChallengeSet(com.novell.ldapchai.cr.ChaiChallengeSet) ChaiChallenge(com.novell.ldapchai.cr.ChaiChallenge)

Example 14 with Challenge

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;
}
Also used : PutLoginConfigRequest(com.novell.security.nmas.jndi.ldap.ext.PutLoginConfigRequest) PutLoginConfigResponse(com.novell.security.nmas.jndi.ldap.ext.PutLoginConfigResponse) PutLoginSecretResponse(com.novell.security.nmas.jndi.ldap.ext.PutLoginSecretResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JDOMException(org.jdom2.JDOMException) ChaiValidationException(com.novell.ldapchai.exception.ChaiValidationException) ChaiOperationException(com.novell.ldapchai.exception.ChaiOperationException) IOException(java.io.IOException) ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Challenge(com.novell.ldapchai.cr.Challenge) ChaiChallenge(com.novell.ldapchai.cr.ChaiChallenge) Answer(com.novell.ldapchai.cr.Answer) ChaiValidationException(com.novell.ldapchai.exception.ChaiValidationException) PutLoginSecretRequest(com.novell.security.nmas.jndi.ldap.ext.PutLoginSecretRequest) ExtendedResponse(javax.naming.ldap.ExtendedResponse) ChaiOperationException(com.novell.ldapchai.exception.ChaiOperationException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 15 with Challenge

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);
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Format(org.jdom2.output.Format) Element(org.jdom2.Element) Challenge(com.novell.ldapchai.cr.Challenge) ChaiChallenge(com.novell.ldapchai.cr.ChaiChallenge)

Aggregations

Challenge (com.novell.ldapchai.cr.Challenge)16 ChaiChallenge (com.novell.ldapchai.cr.ChaiChallenge)9 ChaiChallengeSet (com.novell.ldapchai.cr.ChaiChallengeSet)6 ArrayList (java.util.ArrayList)6 LinkedHashMap (java.util.LinkedHashMap)5 ChallengeSet (com.novell.ldapchai.cr.ChallengeSet)4 ChaiValidationException (com.novell.ldapchai.exception.ChaiValidationException)4 ErrorInformation (password.pwm.error.ErrorInformation)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Element (org.jdom2.Element)3 JDOMException (org.jdom2.JDOMException)3 PwmDataValidationException (password.pwm.error.PwmDataValidationException)3 SetupResponsesBean (password.pwm.http.bean.SetupResponsesBean)3 Answer (com.novell.ldapchai.cr.Answer)2 ResponseSet (com.novell.ldapchai.cr.ResponseSet)2 ChaiOperationException (com.novell.ldapchai.exception.ChaiOperationException)2 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2