Search in sources :

Example 1 with ModificationItem

use of javax.naming.directory.ModificationItem in project neo4j by neo4j.

the class LdapAuthIT method modifyLDAPAttribute.

private void modifyLDAPAttribute(String username, Object credentials, String attribute, Object value) throws Throwable {
    String principal = String.format("cn=%s,ou=users,dc=example,dc=com", username);
    String principal1 = String.format("cn=%s,ou=users,dc=example,dc=com", username);
    JndiLdapContextFactory contextFactory = new JndiLdapContextFactory();
    contextFactory.setUrl("ldaps://localhost:10636");
    LdapContext ctx = contextFactory.getLdapContext(principal1, credentials);
    ModificationItem[] mods = new ModificationItem[1];
    mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(attribute, value));
    // Perform the update
    ctx.modifyAttributes(principal, mods);
    ctx.close();
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ModificationItem(javax.naming.directory.ModificationItem) LdapContext(javax.naming.ldap.LdapContext) JndiLdapContextFactory(org.apache.shiro.realm.ldap.JndiLdapContextFactory)

Example 2 with ModificationItem

use of javax.naming.directory.ModificationItem in project camel by apache.

the class SpringLdapProducerTest method testModifyAttributes.

@Test
public void testModifyAttributes() throws Exception {
    String dn = "cn=dn";
    ModificationItem[] modificationItems = new ModificationItem[] { new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("key", "value")) };
    Exchange exchange = new DefaultExchange(context);
    Message in = new DefaultMessage();
    Map<String, Object> body = new HashMap<String, Object>();
    body.put(SpringLdapProducer.DN, dn);
    body.put(SpringLdapProducer.MODIFICATION_ITEMS, modificationItems);
    when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.MODIFY_ATTRIBUTES);
    processBody(exchange, in, body);
    verify(ldapTemplate).modifyAttributes(eq(dn), eq(modificationItems));
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultMessage(org.apache.camel.impl.DefaultMessage) ModificationItem(javax.naming.directory.ModificationItem) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 3 with ModificationItem

use of javax.naming.directory.ModificationItem in project spring-security by spring-projects.

the class LdapUserDetailsManager method updateUser.

public void updateUser(UserDetails user) {
    DistinguishedName dn = usernameMapper.buildDn(user.getUsername());
    logger.debug("Updating user '" + user.getUsername() + "' with DN '" + dn + "'");
    List<GrantedAuthority> authorities = getUserAuthorities(dn, user.getUsername());
    DirContextAdapter ctx = loadUserAsContext(dn, user.getUsername());
    ctx.setUpdateMode(true);
    copyToContext(user, ctx);
    // Remove the objectclass attribute from the list of mods (if present).
    List<ModificationItem> mods = new LinkedList<ModificationItem>(Arrays.asList(ctx.getModificationItems()));
    ListIterator<ModificationItem> modIt = mods.listIterator();
    while (modIt.hasNext()) {
        ModificationItem mod = (ModificationItem) modIt.next();
        Attribute a = mod.getAttribute();
        if ("objectclass".equalsIgnoreCase(a.getID())) {
            modIt.remove();
        }
    }
    template.modifyAttributes(dn, mods.toArray(new ModificationItem[mods.size()]));
    // template.rebind(dn, ctx, null);
    // Remove the old authorities and replace them with the new one
    removeAuthorities(dn, authorities);
    addAuthorities(dn, user.getAuthorities());
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) DistinguishedName(org.springframework.ldap.core.DistinguishedName) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) LinkedList(java.util.LinkedList)

Example 4 with ModificationItem

use of javax.naming.directory.ModificationItem in project spring-security by spring-projects.

the class LdapUserDetailsManager method changePassword.

/**
	 * Changes the password for the current user. The username is obtained from the
	 * security context.
	 * <p>
	 * If the old password is supplied, the update will be made by rebinding as the user,
	 * thus modifying the password using the user's permissions. If
	 * <code>oldPassword</code> is null, the update will be attempted using a standard
	 * read/write context supplied by the context source.
	 * </p>
	 *
	 * @param oldPassword the old password
	 * @param newPassword the new value of the password.
	 */
public void changePassword(final String oldPassword, final String newPassword) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Assert.notNull(authentication, "No authentication object found in security context. Can't change current user's password!");
    String username = authentication.getName();
    logger.debug("Changing password for user '" + username);
    final DistinguishedName dn = usernameMapper.buildDn(username);
    final ModificationItem[] passwordChange = new ModificationItem[] { new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(passwordAttributeName, newPassword)) };
    if (oldPassword == null) {
        template.modifyAttributes(dn, passwordChange);
        return;
    }
    template.executeReadWrite(new ContextExecutor() {

        public Object executeWithContext(DirContext dirCtx) throws NamingException {
            LdapContext ctx = (LdapContext) dirCtx;
            ctx.removeFromEnvironment("com.sun.jndi.ldap.connect.pool");
            ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, LdapUtils.getFullDn(dn, ctx).toString());
            ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, oldPassword);
            // TODO: reconnect doesn't appear to actually change the credentials
            try {
                ctx.reconnect(null);
            } catch (javax.naming.AuthenticationException e) {
                throw new BadCredentialsException("Authentication for password change failed.");
            }
            ctx.modifyAttributes(dn, passwordChange);
            return null;
        }
    });
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContext(javax.naming.directory.DirContext) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) ContextExecutor(org.springframework.ldap.core.ContextExecutor) ModificationItem(javax.naming.directory.ModificationItem) Authentication(org.springframework.security.core.Authentication) NamingException(javax.naming.NamingException) LdapContext(javax.naming.ldap.LdapContext)

Example 5 with ModificationItem

use of javax.naming.directory.ModificationItem in project OpenAM by OpenRock.

the class SMSEntry method setAttribute.

/**
     * Set the attribute values. <code>save()</code> must be called to make
     * the changes persistant
     */
public void setAttribute(String attrName, String[] attrValues) {
    // Attribute Values to be Set and BasicAttribute
    Set attrs = new HashSet();
    BasicAttribute ba = new BasicAttribute(attrName);
    for (int i = 0; attrValues != null && i < attrValues.length; i++) {
        attrs.add(attrValues[i]);
        ba.add(attrValues[i]);
    }
    // Check if attrSet, modSet is present, if not create
    attrSet = (attrSet == null) ? (new CaseInsensitiveHashMap()) : attrSet;
    modSet = (modSet == null) ? (new HashSet()) : modSet;
    // Check if the attribute exists, if not present add, else replace
    if (!attrSet.containsKey(attrName)) {
        // Not present: add it, update modset
        modSet.add(new ModificationItem(DirContext.ADD_ATTRIBUTE, ba));
    } else {
        // Remove old attrbute and add the new attribute, update modset
        modSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, ba));
    }
    // Update attrset
    attrSet.put(attrName, attrs);
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ModificationItem(javax.naming.directory.ModificationItem) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) OrderedSet(com.sun.identity.shared.datastruct.OrderedSet) CaseInsensitiveHashSet(com.sun.identity.common.CaseInsensitiveHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) CaseInsensitiveHashMap(com.sun.identity.common.CaseInsensitiveHashMap)

Aggregations

ModificationItem (javax.naming.directory.ModificationItem)23 BasicAttribute (javax.naming.directory.BasicAttribute)19 Attribute (javax.naming.directory.Attribute)12 ArrayList (java.util.ArrayList)5 NamingException (javax.naming.NamingException)5 HashSet (java.util.HashSet)4 Set (java.util.Set)4 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)3 OrderedSet (com.sun.identity.shared.datastruct.OrderedSet)3 SMSException (com.sun.identity.sm.SMSException)3 Hashtable (java.util.Hashtable)3 LinkedHashSet (java.util.LinkedHashSet)3 Attributes (javax.naming.directory.Attributes)3 DirContext (javax.naming.directory.DirContext)3 LdapOperationException (org.codelibs.fess.exception.LdapOperationException)3 Base64 (java.util.Base64)2 Collections (java.util.Collections)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Locale (java.util.Locale)2