Example usage for javax.naming.directory BasicAttribute BasicAttribute

List of usage examples for javax.naming.directory BasicAttribute BasicAttribute

Introduction

In this page you can find the example usage for javax.naming.directory BasicAttribute BasicAttribute.

Prototype

public BasicAttribute(String id, boolean ordered) 

Source Link

Document

Constructs a new instance of a possibly ordered attribute with no value.

Usage

From source file:de.fiz.ddb.aas.utils.LDAPEngineUtilityOrganisation.java

/**
 * Checks attribute if it has to be written to LDAP or removed from LDAP
 * /*from   ww  w  .  ja v  a 2  s.  c om*/
 * @param pOrganisationAtt
 * @param pOldOrganisationAtt
 * @param ldapAttributeName
 * @param vOrgAttributes
 * @param vOrgRemoveAttributes
 * @param isUpdate
 */
private boolean checkAttribute(String pOrganisationAtt, String pOldOrganisationAtt, String ldapAttributeName,
        Attributes vOrgAttributes, Attributes vOrgRemoveAttributes, boolean isUpdate) {
    boolean hasChanged = false;
    if (!isUpdate) {
        if ((pOrganisationAtt != null && !pOrganisationAtt.isEmpty())
                && (pOrganisationAtt.trim().length() > 0)) {
            vOrgAttributes.put(new BasicAttribute(ldapAttributeName, pOrganisationAtt));
        }
    } else {
        if ((pOrganisationAtt != null && !pOrganisationAtt.isEmpty())
                && ((pOldOrganisationAtt == null) || (!pOldOrganisationAtt.equals(pOrganisationAtt)))) {
            vOrgAttributes.put(ldapAttributeName, pOrganisationAtt);
            hasChanged = true;
        } else if ((pOrganisationAtt == null || pOrganisationAtt.isEmpty())
                && (pOldOrganisationAtt != null && !pOldOrganisationAtt.isEmpty())) {
            vOrgRemoveAttributes.put(new BasicAttribute(ldapAttributeName));
            hasChanged = true;
        }
    }
    return hasChanged;
}

From source file:org.kitodo.services.data.LdapServerService.java

/**
 * change password of given user, needs old password for authentication.
 *
 * @param user// w  ww  .  jav  a2  s  .c  om
 *            User object
 * @param inNewPassword
 *            String
 * @return boolean about result of change
 */
public boolean changeUserPassword(User user, String inNewPassword) throws NoSuchAlgorithmException {
    JDKMessageDigest.MD4 digester = new JDKMessageDigest.MD4();
    PasswordEncryption passwordEncryption = user.getLdapGroup().getLdapServer().getPasswordEncryptionEnum();
    Hashtable<String, String> env = initializeWithLdapConnectionSettings(user.getLdapGroup().getLdapServer());
    if (!user.getLdapGroup().getLdapServer().isReadOnly()) {
        try {
            // encryption of password and Base64-Encoding
            MessageDigest md = MessageDigest.getInstance(passwordEncryption.getTitle());
            md.update(inNewPassword.getBytes(StandardCharsets.UTF_8));
            String encryptedPassword = new String(Base64.encodeBase64(md.digest()), StandardCharsets.UTF_8);

            // change attribute userPassword
            BasicAttribute userPassword = new BasicAttribute("userPassword",
                    "{" + passwordEncryption + "}" + encryptedPassword);

            // change attribute lanmgrPassword
            BasicAttribute lanmgrPassword = proceedPassword("sambaLMPassword", inNewPassword, null);

            // change attribute ntlmPassword
            BasicAttribute ntlmPassword = proceedPassword("sambaNTPassword", inNewPassword, digester);

            BasicAttribute sambaPwdLastSet = new BasicAttribute("sambaPwdLastSet",
                    String.valueOf(System.currentTimeMillis() / 1000L));

            ModificationItem[] mods = new ModificationItem[4];
            mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, userPassword);
            mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, lanmgrPassword);
            mods[2] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, ntlmPassword);
            mods[3] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, sambaPwdLastSet);

            DirContext ctx = new InitialDirContext(env);
            ctx.modifyAttributes(buildUserDN(user), mods);

            // Close the context when we're done
            ctx.close();
            return true;
        } catch (NamingException e) {
            logger.debug("Benutzeranmeldung nicht korrekt oder Passwortnderung nicht mglich", e);
            return false;
        }
    }
    return false;
}

From source file:de.fiz.ddb.aas.utils.LDAPEngineUtilityOrganisation.java

/**
 * Checks attribute if it has to be written to LDAP or removed from LDAP if attribute = 0, its like not set.
 * /*from   ww  w .j av  a2s  .  co m*/
 * @param pOrganisationAtt
 * @param pOldOrganisationAtt
 * @param ldapAttributeName
 * @param vOrgAttributes
 * @param vOrgRemoveAttributes
 * @param isUpdate
 */
private boolean checkAttribute(Double pOrganisationAtt, Double pOldOrganisationAtt, String ldapAttributeName,
        Attributes vOrgAttributes, Attributes vOrgRemoveAttributes, boolean isUpdate) {
    boolean hasChanged = false;
    if (!isUpdate) {
        if (pOldOrganisationAtt != null && pOrganisationAtt != 0) {
            vOrgAttributes.put(new BasicAttribute(ldapAttributeName, String.valueOf(pOrganisationAtt)));
        }
    } else {
        if (pOrganisationAtt != null && pOrganisationAtt != 0 && (pOldOrganisationAtt == null
                || pOldOrganisationAtt == 0 || pOldOrganisationAtt != pOrganisationAtt)) {
            vOrgAttributes.put(ldapAttributeName, String.valueOf(pOrganisationAtt));
            hasChanged = true;
        } else if ((pOrganisationAtt == null || pOrganisationAtt == 0)
                && (pOldOrganisationAtt != null && pOldOrganisationAtt != 0)) {
            vOrgRemoveAttributes.put(new BasicAttribute(ldapAttributeName));
            hasChanged = true;
        }
    }
    return hasChanged;
}

From source file:org.kitodo.services.data.LdapServerService.java

private BasicAttribute proceedPassword(String identifier, String newPassword, JDKMessageDigest.MD4 digester) {
    try {/*w  w w.  j  a  va 2 s .co m*/
        byte[] hash;
        if (Objects.isNull(digester)) {
            hash = LdapUser.lmHash(newPassword);
        } else {
            hash = digester.digest(newPassword.getBytes("UnicodeLittleUnmarked"));
        }
        return new BasicAttribute(identifier, LdapUser.toHexString(hash));
        // TODO: Don't catch super class exception, make sure that
        // the password isn't logged here
    } catch (InvalidKeyException | UnsupportedEncodingException | NoSuchAlgorithmException
            | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | RuntimeException e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}

From source file:org.kitodo.production.services.data.LdapServerService.java

private BasicAttribute proceedPassword(String identifier, String newPassword, JDKMessageDigest.MD4 digester) {
    try {//from w ww. j a va  2  s .c  o  m
        byte[] hash;
        if (Objects.isNull(digester)) {
            hash = LdapUser.lmHash(newPassword);
        } else {
            hash = digester.digest(newPassword.getBytes(StandardCharsets.UTF_16LE));
        }
        return new BasicAttribute(identifier, LdapUser.toHexString(hash));
        // TODO: Don't catch super class exception, make sure that
        // the password isn't logged here
    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException
            | BadPaddingException | RuntimeException e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static boolean resetPassword(String cn, String newPassword) {
    DirContext ctx = null;// ww w.ja v  a 2s.  c o m
    try {
        ctx = getMainAuthContext();

        ModificationItem[] modItems = new ModificationItem[1];
        modItems[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("userPassword", newPassword));

        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        ctx.modifyAttributes("cn=" + cn + "," + rb.getString("peopleRoot"), modItems);
    } catch (NamingException ex) {
        _log.error(ex);
        return false;
    }

    return true;
}

From source file:org.apache.jmeter.protocol.ldap.sampler.LDAPExtSampler.java

/***************************************************************************
 * This will create the Basic Attribute for the give name value pair
 *
 * @return The BasicAttribute/*from   w w  w.j  a va  2 s  . c om*/
 **************************************************************************/
private BasicAttribute getBasicAttribute(String name, String value) {
    return new BasicAttribute(name, value);
}

From source file:org.springframework.ldap.core.DirContextAdapter.java

public void setAttributeValues(String name, Object[] values, boolean orderMatters) {
    Attribute a = new BasicAttribute(name, orderMatters);

    for (int i = 0; values != null && i < values.length; i++) {
        a.add(values[i]);//from   w  w w .ja v  a 2  s. co m
    }

    // only change the original attribute if not in update mode
    if (!updateMode && values != null && values.length > 0) {
        // don't save empty arrays
        originalAttrs.put(a);
    }

    // possible to set an already existing attribute to an empty array
    if (updateMode && isChanged(name, values, orderMatters)) {
        updatedAttrs.put(a);
    }
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static boolean updatePassword(LDAPUser user, String newPassword) {
    DirContext ctx = null;//from  ww w . j  a  v  a 2  s .c o m
    try {
        ctx = getAuthContext(user.getUsername(), user.getPassword());

        ModificationItem[] modItems = new ModificationItem[1];
        modItems[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("userPassword", newPassword));

        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        ctx.modifyAttributes("cn=" + user.getUsername() + "," + rb.getString("peopleRoot"), modItems);
    } catch (NamingException ex) {
        _log.error(ex);
        return false;
    }

    return true;
}

From source file:org.apache.syncope.fit.AbstractITCase.java

protected void updateLdapRemoteObject(final String bindDn, final String bindPwd, final String objectDn,
        final Pair<String, String> attribute) {

    InitialDirContext ctx = null;
    try {//from   w  ww . j  ava  2s .c o m
        ctx = getLdapResourceDirContext(bindDn, bindPwd);

        Attribute ldapAttribute = new BasicAttribute(attribute.getKey(), attribute.getValue());
        ModificationItem[] item = new ModificationItem[1];
        item[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, ldapAttribute);
        ctx.modifyAttributes(objectDn, item);
    } catch (Exception e) {
        // ignore
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
                // ignore
            }
        }
    }
}