diff --git a/src/main/java/io/kamax/mxisd/backend/google/GoogleProviderBackend.java b/src/main/java/io/kamax/mxisd/backend/google/GoogleProviderBackend.java index 1d6b53c..7d7bf62 100644 --- a/src/main/java/io/kamax/mxisd/backend/google/GoogleProviderBackend.java +++ b/src/main/java/io/kamax/mxisd/backend/google/GoogleProviderBackend.java @@ -26,8 +26,8 @@ import com.google.api.client.http.HttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import io.kamax.matrix.MatrixID; +import io.kamax.matrix.ThreePid; import io.kamax.matrix._MatrixID; -import io.kamax.mxisd.ThreePid; import io.kamax.mxisd.UserIdType; import io.kamax.mxisd.auth.provider.AuthenticatorProvider; import io.kamax.mxisd.auth.provider.BackendAuthResult; 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 9ee66b2..39d74e6 100644 --- a/src/main/java/io/kamax/mxisd/backend/sql/SqlThreePidProvider.java +++ b/src/main/java/io/kamax/mxisd/backend/sql/SqlThreePidProvider.java @@ -59,6 +59,10 @@ public abstract class SqlThreePidProvider implements IThreePidProvider, ProfileP this.mxCfg = mxCfg; } + protected Connection getConnection() throws SQLException { + return pool.get(); + } + @Override public boolean isEnabled() { return cfg.isEnabled(); @@ -119,7 +123,7 @@ public abstract class SqlThreePidProvider implements IThreePidProvider, ProfileP List<_ThreePid> threepids = new ArrayList<>(); String stmtSql = cfg.getProfile().getThreepid().getQuery(); - try (Connection conn = pool.get()) { + try (Connection conn = getConnection()) { PreparedStatement stmt = conn.prepareStatement(stmtSql); stmt.setString(1, mxid.getId()); diff --git a/src/main/java/io/kamax/mxisd/backend/sql/SynapseSqlThreePidProvider.java b/src/main/java/io/kamax/mxisd/backend/sql/SynapseSqlThreePidProvider.java index ffb1fd1..1ec6fe0 100644 --- a/src/main/java/io/kamax/mxisd/backend/sql/SynapseSqlThreePidProvider.java +++ b/src/main/java/io/kamax/mxisd/backend/sql/SynapseSqlThreePidProvider.java @@ -20,17 +20,50 @@ package io.kamax.mxisd.backend.sql; +import io.kamax.matrix.ThreePid; +import io.kamax.matrix._MatrixID; import io.kamax.mxisd.config.MatrixConfig; import io.kamax.mxisd.config.sql.synapse.SynapseSqlProviderConfig; +import io.kamax.mxisd.profile.ProfileWriter; +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; +import java.sql.SQLException; +import java.time.Instant; + @Component -public class SynapseSqlThreePidProvider extends SqlThreePidProvider { +public class SynapseSqlThreePidProvider extends SqlThreePidProvider implements ProfileWriter { + + private final Logger log = LoggerFactory.getLogger(SynapseSqlThreePidProvider.class); @Autowired public SynapseSqlThreePidProvider(SynapseSqlProviderConfig cfg, MatrixConfig mxCfg) { super(cfg, mxCfg); } + @Override + public boolean addThreepid(_MatrixID mxid, ThreePid tpid) { + try (Connection conn = getConnection()) { + PreparedStatement stmt = conn.prepareStatement("INSERT INTO user_threepids (user_id, medium, address, validated_at, added_at) values (?,?,?,?,?)"); + stmt.setString(1, mxid.getId()); + stmt.setString(2, tpid.getMedium()); + stmt.setString(3, tpid.getAddress()); + stmt.setLong(4, Instant.now().toEpochMilli()); + stmt.setLong(5, Instant.now().toEpochMilli()); + + int rows = stmt.executeUpdate(); + if (rows != 1) { + log.error("Unable to update 3PID info. Modified row(s): {}", rows); + } + + return true; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } diff --git a/src/main/java/io/kamax/mxisd/controller/auth/v1/RegistrationController.java b/src/main/java/io/kamax/mxisd/controller/auth/v1/RegistrationController.java index 7d53d99..8f2ab10 100644 --- a/src/main/java/io/kamax/mxisd/controller/auth/v1/RegistrationController.java +++ b/src/main/java/io/kamax/mxisd/controller/auth/v1/RegistrationController.java @@ -1,8 +1,32 @@ +/* + * mxisd - Matrix Identity Server Daemon + * Copyright (C) 2018 Kamax Sàrl + * + * https://www.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.controller.auth.v1; import com.google.gson.Gson; import com.google.gson.JsonObject; +import io.kamax.matrix.MatrixID; +import io.kamax.matrix.ThreePid; +import io.kamax.matrix._MatrixID; import io.kamax.mxisd.dns.ClientDnsOverwrite; +import io.kamax.mxisd.profile.ProfileManager; import io.kamax.mxisd.util.GsonParser; import io.kamax.mxisd.util.GsonUtil; import io.kamax.mxisd.util.RestClientUtils; @@ -26,6 +50,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URI; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; @RestController @@ -37,13 +63,15 @@ public class RegistrationController { private final String registerV1Url = "/_matrix/client/r0/register"; + private ProfileManager pMgr; private ClientDnsOverwrite dns; private CloseableHttpClient client; private Gson gson; private GsonParser parser; @Autowired - public RegistrationController(ClientDnsOverwrite dns, CloseableHttpClient client) { + public RegistrationController(ProfileManager pMgr, ClientDnsOverwrite dns, CloseableHttpClient client) { + this.pMgr = pMgr; this.dns = dns; this.client = client; this.gson = GsonUtil.build(); @@ -70,6 +98,7 @@ public class RegistrationController { @RequestMapping(path = registerV1Url, method = RequestMethod.POST) public String register(HttpServletRequest req, HttpServletResponse res) { + List ids = new ArrayList<>(); try { JsonObject reqJsonObject = parser.parse(req.getInputStream()); GsonUtil.findObj(reqJsonObject, "auth").ifPresent(auth -> { @@ -82,6 +111,7 @@ public class RegistrationController { String gId = auth.get("googleId").getAsString(); log.info("Google ID: {}", gId); + ids.add(gId); auth.addProperty("type", "m.login.dummy"); auth.remove("googleId"); reqJsonObject.addProperty("password", UUID.randomUUID().toString()); @@ -97,8 +127,8 @@ public class RegistrationController { JsonObject json = parser.parse(body); if (sc == 200 && json.has("user_id")) { log.info("User was registered, adding 3PID"); - String userId = json.get("user_id").getAsString(); - + _MatrixID mxid = new MatrixID(json.get("user_id").getAsString()); + pMgr.addThreepid(mxid, new ThreePid("io.kamax.google.id", ids.get(0))); } res.setStatus(sc); return body; diff --git a/src/main/java/io/kamax/mxisd/profile/ProfileManager.java b/src/main/java/io/kamax/mxisd/profile/ProfileManager.java index 17a6935..4d3b83f 100644 --- a/src/main/java/io/kamax/mxisd/profile/ProfileManager.java +++ b/src/main/java/io/kamax/mxisd/profile/ProfileManager.java @@ -20,6 +20,7 @@ package io.kamax.mxisd.profile; +import io.kamax.matrix.ThreePid; import io.kamax.matrix._MatrixID; import io.kamax.matrix._ThreePid; import org.springframework.stereotype.Component; @@ -32,16 +33,21 @@ import java.util.stream.Collectors; @Component public class ProfileManager { - private List providers; + private List readers; + private List writers; - public ProfileManager(List providers) { - this.providers = providers.stream() + public ProfileManager(List providers, List writers) { + this.readers = providers.stream() .filter(ProfileProvider::isEnabled) .collect(Collectors.toList()); + + this.writers = writers.stream() + .filter(ProfileWriter::isEnabled) + .collect(Collectors.toList()); } public List get(Function> function) { - return providers.stream() + return readers.stream() .map(function) .flatMap(Collection::stream) .collect(Collectors.toList()); @@ -55,4 +61,8 @@ public class ProfileManager { return get(p -> p.getRoles(mxid)); } + public void addThreepid(_MatrixID mxid, ThreePid tpid) { + writers.forEach(w -> w.addThreepid(mxid, tpid)); + } + } diff --git a/src/main/java/io/kamax/mxisd/profile/ProfileWriter.java b/src/main/java/io/kamax/mxisd/profile/ProfileWriter.java new file mode 100644 index 0000000..95b68e9 --- /dev/null +++ b/src/main/java/io/kamax/mxisd/profile/ProfileWriter.java @@ -0,0 +1,32 @@ +/* + * mxisd - Matrix Identity Server Daemon + * Copyright (C) 2018 Kamax Sàrl + * + * https://www.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.profile; + +import io.kamax.matrix.ThreePid; +import io.kamax.matrix._MatrixID; + +public interface ProfileWriter { + + boolean isEnabled(); + + boolean addThreepid(_MatrixID mxid, ThreePid tpid); + +}