Skip to content

Commit

Permalink
Properly handle Synapse as an Identity provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Dor committed Oct 30, 2017
1 parent 1461d8e commit e916ecd
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@
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;
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -72,7 +72,7 @@ protected Optional<Result> 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())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@

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;
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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,17 +32,15 @@
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) {
super(cfg, mxCfg);

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(
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
}
49 changes: 28 additions & 21 deletions src/main/java/io/kamax/mxisd/config/sql/SqlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -138,9 +139,9 @@ public void setMedium(Map<String, String> 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;
Expand All @@ -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());
}
Expand All @@ -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()) {
Expand All @@ -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()));
}
}
Expand Down
Loading

0 comments on commit e916ecd

Please sign in to comment.