Fix query generation

This commit is contained in:
Maxime Dor
2017-09-30 00:27:36 +02:00
parent 69ecef0155
commit 52e4a65c3c
4 changed files with 24 additions and 23 deletions

View File

@@ -72,7 +72,7 @@ public class LdapAuthProvider extends LdapGenericBackend implements Authenticato
return BackendAuthResult.failure();
}
String userFilter = "(" + getCfg().getAttribute().getUid().getValue() + "=" + userFilterValue + ")";
String userFilter = "(" + getUidAtt() + "=" + userFilterValue + ")";
userFilter = buildWithFilter(userFilter, getCfg().getAuth().getFilter());
try (EntryCursor cursor = conn.search(getBaseDn(), userFilter, SearchScope.SUBTREE, getUidAtt(), getAt().getName())) {
while (cursor.next()) {
@@ -80,15 +80,7 @@ public class LdapAuthProvider extends LdapGenericBackend implements Authenticato
String dn = entry.getDn().getName();
log.info("Checking possible match, DN: {}", dn);
Attribute attribute = entry.get(getUidAtt());
if (attribute == null) {
log.info("DN {}: no attribute {}, skpping", dn, getUidAtt());
continue;
}
String data = attribute.get().toString();
if (data.length() < 1) {
log.info("DN {}: empty attribute {}, skipping", getUidAtt());
if (!getAttribute(entry, getUidAtt()).isPresent()) {
continue;
}

View File

@@ -33,6 +33,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public abstract class LdapGenericBackend {
@@ -66,7 +67,7 @@ public abstract class LdapGenericBackend {
return getAt().getUid().getValue();
}
protected LdapConnection getConn() {
protected synchronized LdapConnection getConn() throws LdapException {
return new LdapNetworkConnection(cfg.getConn().getHost(), cfg.getConn().getPort(), cfg.getConn().isTls());
}
@@ -86,10 +87,14 @@ public abstract class LdapGenericBackend {
}
}
public static String buildOrQuery(String value, String... attributes) {
public static String buildOrQuery(String value, List<String> attributes) {
if (attributes.size() < 1) {
throw new IllegalArgumentException();
}
StringBuilder builder = new StringBuilder();
builder.append("(|");
Arrays.stream(attributes).forEach(s -> {
attributes.forEach(s -> {
builder.append("(");
builder.append(s).append("=").append(value).append(")");
});
@@ -97,6 +102,10 @@ public abstract class LdapGenericBackend {
return builder.toString();
}
public static String buildOrQuery(String value, String... attributes) {
return buildOrQuery(value, Arrays.asList(attributes));
}
public String buildOrQueryWithFilter(String filter, String value, String... attributes) {
return buildWithFilter(buildOrQuery(value, attributes), filter);
}

View File

@@ -80,10 +80,13 @@ public class LdapThreePidProvider extends LdapGenericBackend implements IThreePi
Entry entry = cursor.get();
log.info("Found possible match, DN: {}", entry.getDn().getName());
getAttribute(entry, getUidAtt()).map(uid -> {
Optional<String> data = getAttribute(entry, getUidAtt());
if (!data.isPresent()) {
continue;
}
log.info("DN {} is a valid match", entry.getDn().getName());
return buildMatrixIdFromUid(uid);
});
return Optional.of(buildMatrixIdFromUid(data.get()));
}
} catch (CursorLdapReferralException e) {
log.warn("3PID {} is only available via referral, skipping", value);
@@ -100,13 +103,10 @@ public class LdapThreePidProvider extends LdapGenericBackend implements IThreePi
try (LdapConnection conn = getConn()) {
bind(conn);
lookup(conn, request.getType(), request.getThreePid()).map(id -> new SingleLookupReply(request, id));
return lookup(conn, request.getType(), request.getThreePid()).map(id -> new SingleLookupReply(request, id));
} catch (LdapException | IOException e) {
throw new InternalServerError(e);
}
log.info("No match found");
return Optional.empty();
}
@Override

View File

@@ -159,9 +159,9 @@ public class LdapConfig {
attribute.getThreepid().forEach((k, v) -> {
if (StringUtils.isBlank(identity.getMedium().get(k))) {
if (ThreePidMedium.PhoneNumber.is(k)) {
identity.getMedium().put(k, LdapGenericBackend.buildOrQuery("+" + getIdentity().getToken()));
identity.getMedium().put(k, LdapGenericBackend.buildOrQuery("+" + getIdentity().getToken(), v));
} else {
identity.getMedium().put(k, LdapGenericBackend.buildOrQuery(getIdentity().getToken()));
identity.getMedium().put(k, LdapGenericBackend.buildOrQuery(getIdentity().getToken(), v));
}
}
});