Search in sources :

Example 1 with ChaiPasswordPolicy

use of com.novell.ldapchai.ChaiPasswordPolicy in project pwm by pwm-project.

the class PwmPasswordPolicy method merge.

public PwmPasswordPolicy merge(final PwmPasswordPolicy otherPolicy) {
    if (otherPolicy == null) {
        return this;
    }
    final Map<String, String> newPasswordPolicies = new HashMap<>();
    for (final PwmPasswordRule rule : PwmPasswordRule.values()) {
        final String ruleKey = rule.getKey();
        if (this.policyMap.containsKey(ruleKey) || otherPolicy.policyMap.containsKey(ruleKey)) {
            switch(rule) {
                case DisallowedValues:
                case DisallowedAttributes:
                case RegExMatch:
                case RegExNoMatch:
                case CharGroupsValues:
                    final String seperator = (rule == PwmPasswordRule.RegExMatch || rule == PwmPasswordRule.RegExNoMatch) ? ";;;" : "\n";
                    final Set<String> combinedSet = new HashSet<>();
                    combinedSet.addAll(StringHelper.tokenizeString(this.policyMap.get(rule.getKey()), seperator));
                    combinedSet.addAll(StringHelper.tokenizeString(otherPolicy.policyMap.get(rule.getKey()), seperator));
                    newPasswordPolicies.put(ruleKey, StringHelper.stringCollectionToString(combinedSet, seperator));
                    break;
                case ChangeMessage:
                    final String thisChangeMessage = getValue(PwmPasswordRule.ChangeMessage);
                    if (thisChangeMessage == null || thisChangeMessage.length() < 1) {
                        newPasswordPolicies.put(ruleKey, otherPolicy.getValue(PwmPasswordRule.ChangeMessage));
                    } else {
                        newPasswordPolicies.put(ruleKey, getValue(PwmPasswordRule.ChangeMessage));
                    }
                    break;
                case ExpirationInterval:
                    final String expirationIntervalLocalValue = StringUtil.defaultString(policyMap.get(ruleKey), rule.getDefaultValue());
                    final String expirationIntervalOtherValue = StringUtil.defaultString(otherPolicy.policyMap.get(ruleKey), rule.getDefaultValue());
                    newPasswordPolicies.put(ruleKey, mergeMin(expirationIntervalLocalValue, expirationIntervalOtherValue));
                    break;
                case MinimumLifetime:
                    final String minimumLifetimeLocalValue = StringUtil.defaultString(policyMap.get(ruleKey), rule.getDefaultValue());
                    final String minimumLifetimeOtherValue = StringUtil.defaultString(otherPolicy.policyMap.get(ruleKey), rule.getDefaultValue());
                    newPasswordPolicies.put(ruleKey, mergeMin(minimumLifetimeLocalValue, minimumLifetimeOtherValue));
                    break;
                default:
                    final String localValueString = StringUtil.defaultString(policyMap.get(ruleKey), rule.getDefaultValue());
                    final String otherValueString = StringUtil.defaultString(otherPolicy.policyMap.get(ruleKey), rule.getDefaultValue());
                    switch(rule.getRuleType()) {
                        case MIN:
                            newPasswordPolicies.put(ruleKey, mergeMin(localValueString, otherValueString));
                            break;
                        case MAX:
                            newPasswordPolicies.put(ruleKey, mergeMax(localValueString, otherValueString));
                            break;
                        case BOOLEAN:
                            final boolean localValue = StringHelper.convertStrToBoolean(localValueString);
                            final boolean otherValue = StringHelper.convertStrToBoolean(otherValueString);
                            if (rule.isPositiveBooleanMerge()) {
                                newPasswordPolicies.put(ruleKey, String.valueOf(localValue || otherValue));
                            } else {
                                newPasswordPolicies.put(ruleKey, String.valueOf(localValue && otherValue));
                            }
                            break;
                        default:
                            // continue processing
                            break;
                    }
            }
        }
    }
    final ChaiPasswordPolicy backingPolicy = this.chaiPasswordPolicy != null ? chaiPasswordPolicy : otherPolicy.chaiPasswordPolicy;
    final PwmPasswordPolicy returnPolicy = createPwmPasswordPolicy(newPasswordPolicies, backingPolicy);
    final String newRuleText = (ruleText != null && !ruleText.isEmpty()) ? ruleText : otherPolicy.ruleText;
    returnPolicy.setRuleText(newRuleText);
    return returnPolicy;
}
Also used : ChaiPasswordPolicy(com.novell.ldapchai.ChaiPasswordPolicy) DefaultChaiPasswordPolicy(com.novell.ldapchai.util.DefaultChaiPasswordPolicy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet)

Example 2 with ChaiPasswordPolicy

use of com.novell.ldapchai.ChaiPasswordPolicy in project pwm by pwm-project.

the class PasswordUtility method readLdapPasswordPolicy.

public static PwmPasswordPolicy readLdapPasswordPolicy(final PwmApplication pwmApplication, final ChaiUser theUser) throws PwmUnrecoverableException {
    try {
        final Map<String, String> ruleMap = new HashMap<>();
        final ChaiPasswordPolicy chaiPolicy;
        try {
            chaiPolicy = theUser.getPasswordPolicy();
        } catch (ChaiUnavailableException e) {
            throw new PwmUnrecoverableException(PwmError.forChaiError(e.getErrorCode()));
        }
        if (chaiPolicy != null) {
            for (final String key : chaiPolicy.getKeys()) {
                ruleMap.put(key, chaiPolicy.getValue(key));
            }
            if (!"read".equals(pwmApplication.getConfig().readSettingAsString(PwmSetting.PASSWORD_POLICY_CASE_SENSITIVITY))) {
                ruleMap.put(PwmPasswordRule.CaseSensitive.getKey(), pwmApplication.getConfig().readSettingAsString(PwmSetting.PASSWORD_POLICY_CASE_SENSITIVITY));
            }
            return PwmPasswordPolicy.createPwmPasswordPolicy(ruleMap, chaiPolicy);
        }
    } catch (ChaiOperationException e) {
        LOGGER.warn("error reading password policy for user " + theUser.getEntryDN() + ", error: " + e.getMessage());
    }
    return PwmPasswordPolicy.defaultPolicy();
}
Also used : ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) ChaiPasswordPolicy(com.novell.ldapchai.ChaiPasswordPolicy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) ChaiOperationException(com.novell.ldapchai.exception.ChaiOperationException)

Example 3 with ChaiPasswordPolicy

use of com.novell.ldapchai.ChaiPasswordPolicy in project ldapchai by ldapchai.

the class ReadUserData method main.

public static void main(final String[] args) {
    String ldapURL = "ldap://ldaphost:389";
    String ldapBindDN = "cn=admin,ou=ou,o=o";
    String ldapBindPW = "password";
    if (args.length == 3) {
        ldapURL = args[0];
        ldapBindDN = args[1];
        ldapBindPW = args[2];
    }
    try {
        // create provider factory
        final ChaiProviderFactory chaiProviderFactory = ChaiProviderFactory.newProviderFactory();
        // create a provider using the standard JNDI factory.
        ChaiProvider chaiProvider = chaiProviderFactory.newProvider(ldapURL, ldapBindDN, ldapBindPW);
        ChaiUser user = chaiProvider.getEntryFactory().newChaiUser(ldapBindDN);
        // read the value of the bindDN's cn attribute, and print it to stdout.
        Map<String, String> allUserAttributes = user.readStringAttributes(null);
        System.out.println("UserDN: " + user.getEntryDN());
        // Output each of the user's attributes, and one value for each attribute:
        for (String key : allUserAttributes.keySet()) {
            String value = allUserAttributes.get(key);
            System.out.println(key + ": " + value);
        }
        // Detect the user's password and output the debug string
        ChaiPasswordPolicy pwdPolicy = user.getPasswordPolicy();
        System.out.println("PasswordPolicy = " + pwdPolicy);
        System.out.println("PasswordModificationDate = " + user.readPasswordModificationDate());
        System.out.println("PasswordExpirationDate = " + user.readPasswordExpirationDate());
        System.out.println("PasswordExpired = " + user.isPasswordExpired());
        System.out.println("PasswordLocked = " + user.isPasswordLocked());
        // Read the user's group membership, and output each group DN.
        System.out.println(user.getEntryDN() + " groups: ");
        for (ChaiGroup group : user.getGroups()) {
            System.out.println(group.getEntryDN());
        }
        System.out.println("");
    } catch (ChaiException e) {
        System.out.println("LDAP error: " + e.getMessage());
    }
}
Also used : ChaiPasswordPolicy(com.novell.ldapchai.ChaiPasswordPolicy) ChaiProvider(com.novell.ldapchai.provider.ChaiProvider) ChaiUser(com.novell.ldapchai.ChaiUser) ChaiProviderFactory(com.novell.ldapchai.provider.ChaiProviderFactory) ChaiGroup(com.novell.ldapchai.ChaiGroup) ChaiException(com.novell.ldapchai.exception.ChaiException)

Aggregations

ChaiPasswordPolicy (com.novell.ldapchai.ChaiPasswordPolicy)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ChaiGroup (com.novell.ldapchai.ChaiGroup)1 ChaiUser (com.novell.ldapchai.ChaiUser)1 ChaiException (com.novell.ldapchai.exception.ChaiException)1 ChaiOperationException (com.novell.ldapchai.exception.ChaiOperationException)1 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)1 ChaiProvider (com.novell.ldapchai.provider.ChaiProvider)1 ChaiProviderFactory (com.novell.ldapchai.provider.ChaiProviderFactory)1 DefaultChaiPasswordPolicy (com.novell.ldapchai.util.DefaultChaiPasswordPolicy)1 HashSet (java.util.HashSet)1 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)1