From e916ecd08bce34879949c96379292c4e8a47afd6 Mon Sep 17 00:00:00 2001 From: Maxime Dor Date: Mon, 30 Oct 2017 17:43:22 +0100 Subject: [PATCH] Properly handle Synapse as an Identity provider --- ...vider.java => GenericSqlAuthProvider.java} | 8 +-- ....java => GenericSqlDirectoryProvider.java} | 10 ++-- .../sql/GenericSqlThreePidProvider.java | 36 ++++++++++++++ .../backend/sql/SqlThreePidProvider.java | 12 ++--- .../sql/SynapseSqlThreePidProvider.java | 36 ++++++++++++++ .../sql/SynapseSqliteDirectoryProvider.java | 10 ++-- ...fig.java => GenericSqlProviderConfig.java} | 11 +---- .../io/kamax/mxisd/config/sql/SqlConfig.java | 49 +++++++++++-------- .../sql/synapse/SynapseSqlProviderConfig.java | 18 ++++++- .../RecursivePriorityLookupStrategy.java | 5 +- 10 files changed, 139 insertions(+), 56 deletions(-) rename src/main/java/io/kamax/mxisd/backend/sql/{SqlAuthProvider.java => GenericSqlAuthProvider.java} (87%) rename src/main/java/io/kamax/mxisd/backend/sql/{SqlDirectoryProvider.java => GenericSqlDirectoryProvider.java} (90%) create mode 100644 src/main/java/io/kamax/mxisd/backend/sql/GenericSqlThreePidProvider.java create mode 100644 src/main/java/io/kamax/mxisd/backend/sql/SynapseSqlThreePidProvider.java rename src/main/java/io/kamax/mxisd/config/sql/{SqlProviderConfig.java => GenericSqlProviderConfig.java} (87%) diff --git a/src/main/java/io/kamax/mxisd/backend/sql/SqlAuthProvider.java b/src/main/java/io/kamax/mxisd/backend/sql/GenericSqlAuthProvider.java similarity index 87% rename from src/main/java/io/kamax/mxisd/backend/sql/SqlAuthProvider.java rename to src/main/java/io/kamax/mxisd/backend/sql/GenericSqlAuthProvider.java index 089f4ee7..a44fa323 100644 --- a/src/main/java/io/kamax/mxisd/backend/sql/SqlAuthProvider.java +++ b/src/main/java/io/kamax/mxisd/backend/sql/GenericSqlAuthProvider.java @@ -24,7 +24,7 @@ import io.kamax.mxisd.auth.provider.AuthenticatorProvider; import io.kamax.mxisd.auth.provider.BackendAuthResult; import io.kamax.mxisd.config.ServerConfig; -import io.kamax.mxisd.config.sql.SqlProviderConfig; +import io.kamax.mxisd.config.sql.GenericSqlProviderConfig; import io.kamax.mxisd.invitation.InvitationManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,15 +32,15 @@ import org.springframework.stereotype.Component; @Component -public class SqlAuthProvider implements AuthenticatorProvider { +public class GenericSqlAuthProvider implements AuthenticatorProvider { - private Logger log = LoggerFactory.getLogger(SqlAuthProvider.class); + private Logger log = LoggerFactory.getLogger(GenericSqlAuthProvider.class); @Autowired private ServerConfig srvCfg; @Autowired - private SqlProviderConfig cfg; + private GenericSqlProviderConfig cfg; @Autowired private InvitationManager invMgr; diff --git a/src/main/java/io/kamax/mxisd/backend/sql/SqlDirectoryProvider.java b/src/main/java/io/kamax/mxisd/backend/sql/GenericSqlDirectoryProvider.java similarity index 90% rename from src/main/java/io/kamax/mxisd/backend/sql/SqlDirectoryProvider.java rename to src/main/java/io/kamax/mxisd/backend/sql/GenericSqlDirectoryProvider.java index 97a36f0a..463f28e4 100644 --- a/src/main/java/io/kamax/mxisd/backend/sql/SqlDirectoryProvider.java +++ b/src/main/java/io/kamax/mxisd/backend/sql/GenericSqlDirectoryProvider.java @@ -22,8 +22,8 @@ import io.kamax.matrix.MatrixID; import io.kamax.mxisd.config.MatrixConfig; +import io.kamax.mxisd.config.sql.GenericSqlProviderConfig; import io.kamax.mxisd.config.sql.SqlConfig; -import io.kamax.mxisd.config.sql.SqlProviderConfig; import io.kamax.mxisd.controller.directory.v1.io.UserDirectorySearchResult; import io.kamax.mxisd.directory.IDirectoryProvider; import io.kamax.mxisd.exception.InternalServerError; @@ -39,16 +39,16 @@ import static io.kamax.mxisd.controller.directory.v1.io.UserDirectorySearchResult.Result; -public abstract class SqlDirectoryProvider implements IDirectoryProvider { +public abstract class GenericSqlDirectoryProvider implements IDirectoryProvider { - private Logger log = LoggerFactory.getLogger(SqlDirectoryProvider.class); + private Logger log = LoggerFactory.getLogger(GenericSqlDirectoryProvider.class); protected SqlConfig cfg; private MatrixConfig mxCfg; private SqlConnectionPool pool; - public SqlDirectoryProvider(SqlConfig cfg, MatrixConfig mxCfg) { + public GenericSqlDirectoryProvider(SqlConfig cfg, MatrixConfig mxCfg) { this.cfg = cfg; this.pool = new SqlConnectionPool(cfg); this.mxCfg = mxCfg; @@ -72,7 +72,7 @@ protected Optional processRow(ResultSet rSet) throws SQLException { return Optional.of(item); } - public UserDirectorySearchResult search(String searchTerm, SqlProviderConfig.Query query) { + public UserDirectorySearchResult search(String searchTerm, GenericSqlProviderConfig.Query query) { try (Connection conn = pool.get()) { log.info("Will execute query: {}", query.getValue()); try (PreparedStatement stmt = conn.prepareStatement(query.getValue())) { diff --git a/src/main/java/io/kamax/mxisd/backend/sql/GenericSqlThreePidProvider.java b/src/main/java/io/kamax/mxisd/backend/sql/GenericSqlThreePidProvider.java new file mode 100644 index 00000000..095e910a --- /dev/null +++ b/src/main/java/io/kamax/mxisd/backend/sql/GenericSqlThreePidProvider.java @@ -0,0 +1,36 @@ +/* + * mxisd - Matrix Identity Server Daemon + * Copyright (C) 2017 Maxime Dor + * + * https://max.kamax.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package io.kamax.mxisd.backend.sql; + +import io.kamax.mxisd.config.MatrixConfig; +import io.kamax.mxisd.config.sql.GenericSqlProviderConfig; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class GenericSqlThreePidProvider extends SqlThreePidProvider { + + @Autowired + public GenericSqlThreePidProvider(GenericSqlProviderConfig cfg, MatrixConfig mxCfg) { + super(cfg, mxCfg); + } + +} diff --git a/src/main/java/io/kamax/mxisd/backend/sql/SqlThreePidProvider.java b/src/main/java/io/kamax/mxisd/backend/sql/SqlThreePidProvider.java index 2c62acd7..ff1b9ed3 100644 --- a/src/main/java/io/kamax/mxisd/backend/sql/SqlThreePidProvider.java +++ b/src/main/java/io/kamax/mxisd/backend/sql/SqlThreePidProvider.java @@ -22,7 +22,7 @@ import io.kamax.matrix.MatrixID; import io.kamax.mxisd.config.MatrixConfig; -import io.kamax.mxisd.config.sql.SqlProviderConfig; +import io.kamax.mxisd.config.sql.SqlConfig; import io.kamax.mxisd.lookup.SingleLookupReply; import io.kamax.mxisd.lookup.SingleLookupRequest; import io.kamax.mxisd.lookup.ThreePidMapping; @@ -30,8 +30,6 @@ import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import java.sql.Connection; import java.sql.PreparedStatement; @@ -41,18 +39,16 @@ import java.util.List; import java.util.Optional; -@Component -public class SqlThreePidProvider implements IThreePidProvider { +public abstract class SqlThreePidProvider implements IThreePidProvider { private Logger log = LoggerFactory.getLogger(SqlThreePidProvider.class); - private SqlProviderConfig cfg; + private SqlConfig cfg; private MatrixConfig mxCfg; private SqlConnectionPool pool; - @Autowired - public SqlThreePidProvider(SqlProviderConfig cfg, MatrixConfig mxCfg) { + public SqlThreePidProvider(SqlConfig cfg, MatrixConfig mxCfg) { this.cfg = cfg; this.pool = new SqlConnectionPool(cfg); this.mxCfg = mxCfg; diff --git a/src/main/java/io/kamax/mxisd/backend/sql/SynapseSqlThreePidProvider.java b/src/main/java/io/kamax/mxisd/backend/sql/SynapseSqlThreePidProvider.java new file mode 100644 index 00000000..ffb1fd1d --- /dev/null +++ b/src/main/java/io/kamax/mxisd/backend/sql/SynapseSqlThreePidProvider.java @@ -0,0 +1,36 @@ +/* + * mxisd - Matrix Identity Server Daemon + * Copyright (C) 2017 Maxime Dor + * + * https://max.kamax.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package io.kamax.mxisd.backend.sql; + +import io.kamax.mxisd.config.MatrixConfig; +import io.kamax.mxisd.config.sql.synapse.SynapseSqlProviderConfig; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class SynapseSqlThreePidProvider extends SqlThreePidProvider { + + @Autowired + public SynapseSqlThreePidProvider(SynapseSqlProviderConfig cfg, MatrixConfig mxCfg) { + super(cfg, mxCfg); + } + +} diff --git a/src/main/java/io/kamax/mxisd/backend/sql/SynapseSqliteDirectoryProvider.java b/src/main/java/io/kamax/mxisd/backend/sql/SynapseSqliteDirectoryProvider.java index 2d51c2c8..43d4546e 100644 --- a/src/main/java/io/kamax/mxisd/backend/sql/SynapseSqliteDirectoryProvider.java +++ b/src/main/java/io/kamax/mxisd/backend/sql/SynapseSqliteDirectoryProvider.java @@ -21,7 +21,7 @@ package io.kamax.mxisd.backend.sql; import io.kamax.mxisd.config.MatrixConfig; -import io.kamax.mxisd.config.sql.SqlProviderConfig; +import io.kamax.mxisd.config.sql.GenericSqlProviderConfig; import io.kamax.mxisd.config.sql.synapse.SynapseSqlProviderConfig; import io.kamax.mxisd.exception.ConfigurationException; import org.apache.commons.lang.StringUtils; @@ -32,9 +32,7 @@ import java.sql.SQLException; @Component -public class SynapseSqliteDirectoryProvider extends SqlDirectoryProvider { - - private SynapseSqlProviderConfig cfg; +public class SynapseSqliteDirectoryProvider extends GenericSqlDirectoryProvider { @Autowired public SynapseSqliteDirectoryProvider(SynapseSqlProviderConfig cfg, MatrixConfig mxCfg) { @@ -42,7 +40,7 @@ public SynapseSqliteDirectoryProvider(SynapseSqlProviderConfig cfg, MatrixConfig if (StringUtils.equals("sqlite", cfg.getType())) { String userId = "'@' || p.user_id || ':" + mxCfg.getDomain() + "'"; - SqlProviderConfig.Type queries = cfg.getDirectory().getQuery(); + GenericSqlProviderConfig.Type queries = cfg.getDirectory().getQuery(); queries.getName().setValue( "select " + userId + ", displayname from profiles p where displayname like ?"); queries.getThreepid().setValue( @@ -51,7 +49,7 @@ public SynapseSqliteDirectoryProvider(SynapseSqlProviderConfig cfg, MatrixConfig "where t.address like ?"); } else if (StringUtils.equals("postgresql", cfg.getType())) { String userId = "concat('@',p.user_id,':" + mxCfg.getDomain() + "')"; - SqlProviderConfig.Type queries = cfg.getDirectory().getQuery(); + GenericSqlProviderConfig.Type queries = cfg.getDirectory().getQuery(); queries.getName().setValue( "select " + userId + ", displayname from profiles p where displayname ilike ?"); queries.getThreepid().setValue( diff --git a/src/main/java/io/kamax/mxisd/config/sql/SqlProviderConfig.java b/src/main/java/io/kamax/mxisd/config/sql/GenericSqlProviderConfig.java similarity index 87% rename from src/main/java/io/kamax/mxisd/config/sql/SqlProviderConfig.java rename to src/main/java/io/kamax/mxisd/config/sql/GenericSqlProviderConfig.java index 4aa88310..1106f3e7 100644 --- a/src/main/java/io/kamax/mxisd/config/sql/SqlProviderConfig.java +++ b/src/main/java/io/kamax/mxisd/config/sql/GenericSqlProviderConfig.java @@ -24,21 +24,14 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; -import javax.annotation.PostConstruct; - @Configuration @ConfigurationProperties("sql") @Primary -public class SqlProviderConfig extends SqlConfig { +public class GenericSqlProviderConfig extends SqlConfig { @Override protected String getProviderName() { return "Generic SQL"; } - @PostConstruct - public void build() { - super.build(); - } - -} \ No newline at end of file +} diff --git a/src/main/java/io/kamax/mxisd/config/sql/SqlConfig.java b/src/main/java/io/kamax/mxisd/config/sql/SqlConfig.java index 793226af..a70d4c5c 100644 --- a/src/main/java/io/kamax/mxisd/config/sql/SqlConfig.java +++ b/src/main/java/io/kamax/mxisd/config/sql/SqlConfig.java @@ -4,6 +4,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.PostConstruct; import java.util.HashMap; import java.util.Map; @@ -36,22 +37,22 @@ public void setValue(String value) { public static class Type { - private SqlProviderConfig.Query name = new SqlProviderConfig.Query(); - private SqlProviderConfig.Query threepid = new SqlProviderConfig.Query(); + private GenericSqlProviderConfig.Query name = new GenericSqlProviderConfig.Query(); + private GenericSqlProviderConfig.Query threepid = new GenericSqlProviderConfig.Query(); - public SqlProviderConfig.Query getName() { + public GenericSqlProviderConfig.Query getName() { return name; } - public void setName(SqlProviderConfig.Query name) { + public void setName(GenericSqlProviderConfig.Query name) { this.name = name; } - public SqlProviderConfig.Query getThreepid() { + public GenericSqlProviderConfig.Query getThreepid() { return threepid; } - public void setThreepid(SqlProviderConfig.Query threepid) { + public void setThreepid(GenericSqlProviderConfig.Query threepid) { this.threepid = threepid; } @@ -74,7 +75,7 @@ public void setEnabled(Boolean enabled) { public static class Directory { private Boolean enabled; - private SqlProviderConfig.Type query = new SqlProviderConfig.Type(); + private GenericSqlProviderConfig.Type query = new GenericSqlProviderConfig.Type(); public Boolean isEnabled() { return enabled; @@ -84,11 +85,11 @@ public void setEnabled(Boolean enabled) { this.enabled = enabled; } - public SqlProviderConfig.Type getQuery() { + public GenericSqlProviderConfig.Type getQuery() { return query; } - public void setQuery(SqlProviderConfig.Type query) { + public void setQuery(GenericSqlProviderConfig.Type query) { this.query = query; } @@ -138,9 +139,9 @@ public void setMedium(Map medium) { private boolean enabled; private String type; private String connection; - private SqlProviderConfig.Auth auth = new SqlProviderConfig.Auth(); - private SqlProviderConfig.Directory directory = new SqlProviderConfig.Directory(); - private SqlProviderConfig.Identity identity = new SqlProviderConfig.Identity(); + private GenericSqlProviderConfig.Auth auth = new GenericSqlProviderConfig.Auth(); + private GenericSqlProviderConfig.Directory directory = new GenericSqlProviderConfig.Directory(); + private GenericSqlProviderConfig.Identity identity = new GenericSqlProviderConfig.Identity(); public boolean isEnabled() { return enabled; @@ -166,35 +167,33 @@ public void setConnection(String connection) { this.connection = connection; } - public SqlProviderConfig.Auth getAuth() { + public GenericSqlProviderConfig.Auth getAuth() { return auth; } - public void setAuth(SqlProviderConfig.Auth auth) { + public void setAuth(GenericSqlProviderConfig.Auth auth) { this.auth = auth; } - public SqlProviderConfig.Directory getDirectory() { + public GenericSqlProviderConfig.Directory getDirectory() { return directory; } - public void setDirectory(SqlProviderConfig.Directory directory) { + public void setDirectory(GenericSqlProviderConfig.Directory directory) { this.directory = directory; } - public SqlProviderConfig.Identity getIdentity() { + public GenericSqlProviderConfig.Identity getIdentity() { return identity; } - public void setIdentity(SqlProviderConfig.Identity identity) { + public void setIdentity(GenericSqlProviderConfig.Identity identity) { this.identity = identity; } protected abstract String getProviderName(); - public void build() { - log.info("--- " + getProviderName() + " Provider config ---"); - + protected void doBuild() { if (getAuth().isEnabled() == null) { getAuth().setEnabled(isEnabled()); } @@ -206,6 +205,13 @@ public void build() { if (getIdentity().isEnabled() == null) { getIdentity().setEnabled(isEnabled()); } + } + + @PostConstruct + public void build() { + log.info("--- " + getProviderName() + " Provider config ---"); + + doBuild(); log.info("Enabled: {}", isEnabled()); if (isEnabled()) { @@ -214,6 +220,7 @@ public void build() { log.info("Auth enabled: {}", getAuth().isEnabled()); log.info("Directory queries: {}", GsonUtil.build().toJson(getDirectory().getQuery())); log.info("Identity type: {}", getIdentity().getType()); + log.info("3PID mapping query: {}", getIdentity().getQuery()); log.info("Identity medium queries: {}", GsonUtil.build().toJson(getIdentity().getMedium())); } } diff --git a/src/main/java/io/kamax/mxisd/config/sql/synapse/SynapseSqlProviderConfig.java b/src/main/java/io/kamax/mxisd/config/sql/synapse/SynapseSqlProviderConfig.java index ea6ca3c1..81bd1bba 100644 --- a/src/main/java/io/kamax/mxisd/config/sql/synapse/SynapseSqlProviderConfig.java +++ b/src/main/java/io/kamax/mxisd/config/sql/synapse/SynapseSqlProviderConfig.java @@ -21,6 +21,7 @@ package io.kamax.mxisd.config.sql.synapse; import io.kamax.mxisd.config.sql.SqlConfig; +import org.apache.commons.lang.StringUtils; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; @@ -36,10 +37,23 @@ protected String getProviderName() { } @PostConstruct - public void build() { - super.build(); + public void doBuild() { + super.doBuild(); // FIXME check that the DB is not the mxisd one // See https://matrix.to/#/!NPRUEisLjcaMtHIzDr:kamax.io/$1509377583327omXkC:kamax.io + + getAuth().setEnabled(false); // Synapse does the auth, we only act as a directory/identity service. + + if (getDirectory().isEnabled()) { + //FIXME set default queries for name and threepid + } + + if (getIdentity().isEnabled()) { + if (StringUtils.isBlank(getIdentity().getType())) { + getIdentity().setType("mxid"); + getIdentity().setQuery("SELECT user_id AS uid FROM user_threepids WHERE medium = ? AND address = ?"); + } + } } } diff --git a/src/main/java/io/kamax/mxisd/lookup/strategy/RecursivePriorityLookupStrategy.java b/src/main/java/io/kamax/mxisd/lookup/strategy/RecursivePriorityLookupStrategy.java index a5d23f76..98dbf293 100644 --- a/src/main/java/io/kamax/mxisd/lookup/strategy/RecursivePriorityLookupStrategy.java +++ b/src/main/java/io/kamax/mxisd/lookup/strategy/RecursivePriorityLookupStrategy.java @@ -53,7 +53,10 @@ public class RecursivePriorityLookupStrategy implements LookupStrategy { public RecursivePriorityLookupStrategy(RecursiveLookupConfig cfg, List providers, IBridgeFetcher bridge) { this.cfg = cfg; this.bridge = bridge; - this.providers = providers.stream().filter(IThreePidProvider::isEnabled).collect(Collectors.toList()); + this.providers = providers.stream().filter(p -> { + log.info("3PID Provider {} is enabled: {}", p.getClass().getSimpleName(), p.isEnabled()); + return p.isEnabled(); + }).collect(Collectors.toList()); } @PostConstruct