Last active
October 13, 2023 09:29
-
-
Save bertold/002491a2630a98d80733b8228f75b75c to your computer and use it in GitHub Desktop.
Changing password using the UnboundID LDAP SDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
because it does not exist in the server;;