Skip to content

Instantly share code, notes, and snippets.

@bertold
Last active October 13, 2023 09:29
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bertold/002491a2630a98d80733b8228f75b75c to your computer and use it in GitHub Desktop.
Save bertold/002491a2630a98d80733b8228f75b75c to your computer and use it in GitHub Desktop.
Changing password using the UnboundID LDAP SDK
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
import com.unboundid.ldap.listener.InMemoryListenerConfig;
import com.unboundid.ldap.sdk.*;
import com.unboundid.ldif.LDIFException;
/**
* Sample code to demonstrate password change.
* Error handling is not demonstrated here.
*/
public class PasswordChange {
// Fields
private InMemoryDirectoryServer server;
private void setupServer() throws LDAPException, LDIFException {
//
// Setup/configure an in-memory LDAP server
//
InMemoryDirectoryServerConfig config =
new InMemoryDirectoryServerConfig("dc=example,dc=com");
// Configure admin user credentials
config.addAdditionalBindCredentials("cn=Directory Manager", "password");
// Configure an LDAP port
config.setListenerConfigs(
InMemoryListenerConfig.createLDAPConfig(
"LDAP", // Listener name
1389 // Listen port
)
);
// Create and start the LDAP server
server = new InMemoryDirectoryServer(config);
server.startListening();
// Populate the LDAP server with some data
server.add(
"dn: dc=example,dc=com",
"objectClass: domain",
"dc: example");
server.add(
"dn: uid=testuser,dc=example,dc=com",
"objectClass: inetOrgPerson",
"uid: testuser",
"cn: Test User",
"sn: User",
"userPassword: abc123"
);
}
public void changePassword(String user, String password) throws LDAPException {
final LDAPConnection connection = server.getConnection();
final String userDN;
// Connect as the admin user
connection.bind("cn=Directory Manager", "password");
// The password is replaced with the new value
Modification modification = new Modification(
ModificationType.REPLACE,
"userPassword",
password
);
// The DN of the user
userDN = "uid=" + user + ",dc=example,dc=com";
// Build the modification request
ModifyRequest modifyRequest = new ModifyRequest(
userDN,
modification
);
// Execute the modification
connection.modify(modifyRequest);
// Test the new password
final LDAPConnection userConnection = server.getConnection();
// Test the bind - if this fails, an exception is thrown
userConnection.bind(userDN, password);
}
public void tearDown()
{
server.shutDown(true);
}
public static void main(String[] args) throws LDAPException, LDIFException {
PasswordChange passwordChange = new PasswordChange();
passwordChange.setupServer();
passwordChange.changePassword("testuser", "testpassword");
passwordChange.tearDown();
}
}
@prashant7526
Copy link

prashant7526 commented Feb 4, 2021

I am getting the following error (am I missing ObjectClass?):

Exception in thread "main" LDAPException(resultCode=32 (no such object), diagnosticMessage='Unable to modify entry 'uid=prashantssadmin,cn=support-staff-admin,ou=canavans,dc=canavans,dc=local' because it does not exist in the server.', ldapSDKVersion=5.1.0, revision=89705d759f7c1ab3bccb2870f8c2e7d529ed231b)

@DeadlyCrush
Copy link

because it does not exist in the server;;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment