use of javax.naming.directory.BasicAttribute 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();
}
use of javax.naming.directory.BasicAttribute 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));
}
use of javax.naming.directory.BasicAttribute in project spring-security by spring-projects.
the class PasswordComparisonAuthenticatorMockTests method ldapCompareOperationIsUsedWhenPasswordIsNotRetrieved.
// ~ Methods
// ========================================================================================================
@Test
public void ldapCompareOperationIsUsedWhenPasswordIsNotRetrieved() throws Exception {
final DirContext dirCtx = mock(DirContext.class);
final BaseLdapPathContextSource source = mock(BaseLdapPathContextSource.class);
final BasicAttributes attrs = new BasicAttributes();
attrs.put(new BasicAttribute("uid", "bob"));
PasswordComparisonAuthenticator authenticator = new PasswordComparisonAuthenticator(source);
authenticator.setUserDnPatterns(new String[] { "cn={0},ou=people" });
// Get the mock to return an empty attribute set
when(source.getReadOnlyContext()).thenReturn(dirCtx);
when(dirCtx.getAttributes(eq("cn=Bob,ou=people"), any(String[].class))).thenReturn(attrs);
when(dirCtx.getNameInNamespace()).thenReturn("dc=springframework,dc=org");
// Setup a single return value (i.e. success)
final NamingEnumeration searchResults = new BasicAttributes("", null).getAll();
when(dirCtx.search(eq("cn=Bob,ou=people"), eq("(userPassword={0})"), any(Object[].class), any(SearchControls.class))).thenReturn(searchResults);
authenticator.authenticate(new UsernamePasswordAuthenticationToken("Bob", "bobspassword"));
}
use of javax.naming.directory.BasicAttribute in project spring-security by spring-projects.
the class JndiDnsResolverTests method createSrvRecords.
private BasicAttributes createSrvRecords() {
BasicAttributes records = new BasicAttributes();
BasicAttribute record = new BasicAttribute("SRV");
// the structure of the service records is:
// priority weight port hostname
// for more information: http://en.wikipedia.org/wiki/SRV_record
record.add("20 80 389 kdc3.springsource.com.");
record.add("10 70 389 kdc.springsource.com.");
record.add("20 20 389 kdc4.springsource.com.");
record.add("10 30 389 kdc2.springsource.com");
records.put(record);
return records;
}
use of javax.naming.directory.BasicAttribute 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;
}
});
}
Aggregations