MSC2140 Add populating hashes via sql and memory stores.
This commit is contained in:
@@ -48,6 +48,7 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class MemoryIdentityStore implements AuthenticatorProvider, DirectoryProvider, IThreePidProvider, ProfileProvider {
|
public class MemoryIdentityStore implements AuthenticatorProvider, DirectoryProvider, IThreePidProvider, ProfileProvider {
|
||||||
|
|
||||||
@@ -171,4 +172,11 @@ public class MemoryIdentityStore implements AuthenticatorProvider, DirectoryProv
|
|||||||
}).orElseGet(BackendAuthResult::failure);
|
}).orElseGet(BackendAuthResult::failure);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<ThreePidMapping> populateHashes() {
|
||||||
|
return cfg.getIdentities().stream()
|
||||||
|
.map(mic -> mic.getThreepids().stream().map(mtp -> new ThreePidMapping(mtp.getMedium(), mtp.getAddress(), mic.getUsername())))
|
||||||
|
.flatMap(s -> s).collect(
|
||||||
|
Collectors.toList());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -36,6 +36,7 @@ import java.sql.PreparedStatement;
|
|||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -104,4 +105,27 @@ public abstract class SqlThreePidProvider implements IThreePidProvider {
|
|||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<ThreePidMapping> populateHashes() {
|
||||||
|
if (StringUtils.isBlank(cfg.getLookup().getQuery())) {
|
||||||
|
log.warn("Lookup query not configured, skip.");
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<ThreePidMapping> result = new ArrayList<>();
|
||||||
|
try (Connection connection = pool.get()) {
|
||||||
|
PreparedStatement statement = connection.prepareStatement(cfg.getLookup().getQuery());
|
||||||
|
try (ResultSet resultSet = statement.executeQuery()) {
|
||||||
|
while (resultSet.next()) {
|
||||||
|
String mxid = resultSet.getString("mxid");
|
||||||
|
String medium = resultSet.getString("medium");
|
||||||
|
String address = resultSet.getString("address");
|
||||||
|
result.add(new ThreePidMapping(medium, address, mxid));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -124,6 +124,18 @@ public abstract class SqlConfig {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Lookup {
|
||||||
|
private String query;
|
||||||
|
|
||||||
|
public String getQuery() {
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQuery(String query) {
|
||||||
|
this.query = query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class Identity {
|
public static class Identity {
|
||||||
|
|
||||||
private Boolean enabled;
|
private Boolean enabled;
|
||||||
@@ -264,6 +276,7 @@ public abstract class SqlConfig {
|
|||||||
private Directory directory = new Directory();
|
private Directory directory = new Directory();
|
||||||
private Identity identity = new Identity();
|
private Identity identity = new Identity();
|
||||||
private Profile profile = new Profile();
|
private Profile profile = new Profile();
|
||||||
|
private Lookup lookup = new Lookup();
|
||||||
|
|
||||||
public boolean isEnabled() {
|
public boolean isEnabled() {
|
||||||
return enabled;
|
return enabled;
|
||||||
@@ -321,6 +334,14 @@ public abstract class SqlConfig {
|
|||||||
this.profile = profile;
|
this.profile = profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Lookup getLookup() {
|
||||||
|
return lookup;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLookup(Lookup lookup) {
|
||||||
|
this.lookup = lookup;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract String getProviderName();
|
protected abstract String getProviderName();
|
||||||
|
|
||||||
public void build() {
|
public void build() {
|
||||||
@@ -354,6 +375,7 @@ public abstract class SqlConfig {
|
|||||||
log.info("Identity type: {}", getIdentity().getType());
|
log.info("Identity type: {}", getIdentity().getType());
|
||||||
log.info("3PID mapping query: {}", getIdentity().getQuery());
|
log.info("3PID mapping query: {}", getIdentity().getQuery());
|
||||||
log.info("Identity medium queries: {}", GsonUtil.build().toJson(getIdentity().getMedium()));
|
log.info("Identity medium queries: {}", GsonUtil.build().toJson(getIdentity().getMedium()));
|
||||||
|
log.info("Lookup query: {}", getLookup().getQuery());
|
||||||
log.info("Profile:");
|
log.info("Profile:");
|
||||||
log.info(" Enabled: {}", getProfile().isEnabled());
|
log.info(" Enabled: {}", getProfile().isEnabled());
|
||||||
if (getProfile().isEnabled()) {
|
if (getProfile().isEnabled()) {
|
||||||
|
Reference in New Issue
Block a user