Handle multiple LDAP entries in results
This commit is contained in:
@@ -28,6 +28,7 @@ import org.apache.commons.lang.StringUtils
|
|||||||
import org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException
|
import org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException
|
||||||
import org.apache.directory.api.ldap.model.cursor.EntryCursor
|
import org.apache.directory.api.ldap.model.cursor.EntryCursor
|
||||||
import org.apache.directory.api.ldap.model.entry.Attribute
|
import org.apache.directory.api.ldap.model.entry.Attribute
|
||||||
|
import org.apache.directory.api.ldap.model.entry.Entry
|
||||||
import org.apache.directory.api.ldap.model.message.SearchScope
|
import org.apache.directory.api.ldap.model.message.SearchScope
|
||||||
import org.apache.directory.ldap.client.api.LdapConnection
|
import org.apache.directory.ldap.client.api.LdapConnection
|
||||||
import org.apache.directory.ldap.client.api.LdapNetworkConnection
|
import org.apache.directory.ldap.client.api.LdapNetworkConnection
|
||||||
@@ -75,13 +76,20 @@ class LdapProvider implements IThreePidProvider {
|
|||||||
String searchQuery = queryOpt.get().replaceAll("%3pid", value)
|
String searchQuery = queryOpt.get().replaceAll("%3pid", value)
|
||||||
EntryCursor cursor = conn.search(ldapCfg.getBaseDn(), searchQuery, SearchScope.SUBTREE, ldapCfg.getAttribute())
|
EntryCursor cursor = conn.search(ldapCfg.getBaseDn(), searchQuery, SearchScope.SUBTREE, ldapCfg.getAttribute())
|
||||||
try {
|
try {
|
||||||
if (cursor.next()) {
|
while (cursor.next()) {
|
||||||
Attribute attribute = cursor.get().get(ldapCfg.getAttribute())
|
Entry entry = cursor.get()
|
||||||
if (attribute != null) {
|
log.info("Found possible match, DN: {}", entry.getDn().getName())
|
||||||
|
|
||||||
|
Attribute attribute = entry.get(ldapCfg.getAttribute())
|
||||||
|
if (attribute == null) {
|
||||||
|
log.info("DN {}: no attribute {}, skpping", ldapCfg.getAttribute())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
String data = attribute.get().toString()
|
String data = attribute.get().toString()
|
||||||
if (data.length() < 1) {
|
if (data.length() < 1) {
|
||||||
log.warn("Bind was found but value is empty")
|
log.info("DN {}: empty attribute {}, skipping", ldapCfg.getAttribute())
|
||||||
return Optional.empty()
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder matrixId = new StringBuilder()
|
StringBuilder matrixId = new StringBuilder()
|
||||||
@@ -91,14 +99,13 @@ class LdapProvider implements IThreePidProvider {
|
|||||||
} else if (StringUtils.equals(MATRIX_ID, ldapCfg.getType())) {
|
} else if (StringUtils.equals(MATRIX_ID, ldapCfg.getType())) {
|
||||||
matrixId.append(data)
|
matrixId.append(data)
|
||||||
} else {
|
} else {
|
||||||
log.warn("Bind was found but type ${ldapCfg.getType()} is not supported")
|
log.warn("Bind was found but type {} is not supported", ldapCfg.getType())
|
||||||
return Optional.empty()
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("Found a match in LDAP")
|
log.info("DN {} is a valid match", entry.getDn().getName())
|
||||||
return Optional.of(matrixId.toString())
|
return Optional.of(matrixId.toString())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} catch (CursorLdapReferralException e) {
|
} catch (CursorLdapReferralException e) {
|
||||||
log.warn("3PID {} is only available via referral, skipping", value)
|
log.warn("3PID {} is only available via referral, skipping", value)
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user