Properly handle Synapse as an Identity provider
This commit is contained in:
@@ -24,7 +24,7 @@ import io.kamax.matrix._MatrixID;
|
||||
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.beans.factory.annotation.Autowired;
|
||||
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;
|
@@ -22,8 +22,8 @@ package io.kamax.mxisd.backend.sql;
|
||||
|
||||
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 java.util.Optional;
|
||||
|
||||
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 @@ public abstract class SqlDirectoryProvider implements IDirectoryProvider {
|
||||
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())) {
|
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@@ -22,7 +22,7 @@ package io.kamax.mxisd.backend.sql;
|
||||
|
||||
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 io.kamax.mxisd.lookup.provider.IThreePidProvider;
|
||||
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.ArrayList;
|
||||
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;
|
||||
|
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@@ -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.PreparedStatement;
|
||||
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 class SynapseSqliteDirectoryProvider extends SqlDirectoryProvider {
|
||||
|
||||
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 class SynapseSqliteDirectoryProvider extends SqlDirectoryProvider {
|
||||
"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(
|
||||
|
@@ -24,21 +24,14 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -4,6 +4,7 @@ import io.kamax.mxisd.util.GsonUtil;
|
||||
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 abstract class SqlConfig {
|
||||
|
||||
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 abstract class SqlConfig {
|
||||
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 abstract class SqlConfig {
|
||||
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 abstract class SqlConfig {
|
||||
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 abstract class SqlConfig {
|
||||
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 abstract class SqlConfig {
|
||||
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 abstract class SqlConfig {
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
@@ -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 @@ public class SynapseSqlProviderConfig extends SqlConfig {
|
||||
}
|
||||
|
||||
@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 = ?");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -53,7 +53,10 @@ public class RecursivePriorityLookupStrategy implements LookupStrategy {
|
||||
public RecursivePriorityLookupStrategy(RecursiveLookupConfig cfg, List<IThreePidProvider> 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
|
||||
|
Reference in New Issue
Block a user