PoC of registration with Google 3PID
This commit is contained in:
@@ -26,8 +26,8 @@ import com.google.api.client.http.HttpTransport;
|
|||||||
import com.google.api.client.json.JsonFactory;
|
import com.google.api.client.json.JsonFactory;
|
||||||
import com.google.api.client.json.jackson2.JacksonFactory;
|
import com.google.api.client.json.jackson2.JacksonFactory;
|
||||||
import io.kamax.matrix.MatrixID;
|
import io.kamax.matrix.MatrixID;
|
||||||
|
import io.kamax.matrix.ThreePid;
|
||||||
import io.kamax.matrix._MatrixID;
|
import io.kamax.matrix._MatrixID;
|
||||||
import io.kamax.mxisd.ThreePid;
|
|
||||||
import io.kamax.mxisd.UserIdType;
|
import io.kamax.mxisd.UserIdType;
|
||||||
import io.kamax.mxisd.auth.provider.AuthenticatorProvider;
|
import io.kamax.mxisd.auth.provider.AuthenticatorProvider;
|
||||||
import io.kamax.mxisd.auth.provider.BackendAuthResult;
|
import io.kamax.mxisd.auth.provider.BackendAuthResult;
|
||||||
|
@@ -59,6 +59,10 @@ public abstract class SqlThreePidProvider implements IThreePidProvider, ProfileP
|
|||||||
this.mxCfg = mxCfg;
|
this.mxCfg = mxCfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Connection getConnection() throws SQLException {
|
||||||
|
return pool.get();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEnabled() {
|
public boolean isEnabled() {
|
||||||
return cfg.isEnabled();
|
return cfg.isEnabled();
|
||||||
@@ -119,7 +123,7 @@ public abstract class SqlThreePidProvider implements IThreePidProvider, ProfileP
|
|||||||
List<_ThreePid> threepids = new ArrayList<>();
|
List<_ThreePid> threepids = new ArrayList<>();
|
||||||
|
|
||||||
String stmtSql = cfg.getProfile().getThreepid().getQuery();
|
String stmtSql = cfg.getProfile().getThreepid().getQuery();
|
||||||
try (Connection conn = pool.get()) {
|
try (Connection conn = getConnection()) {
|
||||||
PreparedStatement stmt = conn.prepareStatement(stmtSql);
|
PreparedStatement stmt = conn.prepareStatement(stmtSql);
|
||||||
stmt.setString(1, mxid.getId());
|
stmt.setString(1, mxid.getId());
|
||||||
|
|
||||||
|
@@ -20,17 +20,50 @@
|
|||||||
|
|
||||||
package io.kamax.mxisd.backend.sql;
|
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.MatrixConfig;
|
||||||
import io.kamax.mxisd.config.sql.synapse.SynapseSqlProviderConfig;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class SynapseSqlThreePidProvider extends SqlThreePidProvider {
|
public class SynapseSqlThreePidProvider extends SqlThreePidProvider implements ProfileWriter {
|
||||||
|
|
||||||
|
private final Logger log = LoggerFactory.getLogger(SynapseSqlThreePidProvider.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public SynapseSqlThreePidProvider(SynapseSqlProviderConfig cfg, MatrixConfig mxCfg) {
|
public SynapseSqlThreePidProvider(SynapseSqlProviderConfig cfg, MatrixConfig mxCfg) {
|
||||||
super(cfg, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
package io.kamax.mxisd.controller.auth.v1;
|
package io.kamax.mxisd.controller.auth.v1;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
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.dns.ClientDnsOverwrite;
|
||||||
|
import io.kamax.mxisd.profile.ProfileManager;
|
||||||
import io.kamax.mxisd.util.GsonParser;
|
import io.kamax.mxisd.util.GsonParser;
|
||||||
import io.kamax.mxisd.util.GsonUtil;
|
import io.kamax.mxisd.util.GsonUtil;
|
||||||
import io.kamax.mxisd.util.RestClientUtils;
|
import io.kamax.mxisd.util.RestClientUtils;
|
||||||
@@ -26,6 +50,8 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -37,13 +63,15 @@ public class RegistrationController {
|
|||||||
|
|
||||||
private final String registerV1Url = "/_matrix/client/r0/register";
|
private final String registerV1Url = "/_matrix/client/r0/register";
|
||||||
|
|
||||||
|
private ProfileManager pMgr;
|
||||||
private ClientDnsOverwrite dns;
|
private ClientDnsOverwrite dns;
|
||||||
private CloseableHttpClient client;
|
private CloseableHttpClient client;
|
||||||
private Gson gson;
|
private Gson gson;
|
||||||
private GsonParser parser;
|
private GsonParser parser;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public RegistrationController(ClientDnsOverwrite dns, CloseableHttpClient client) {
|
public RegistrationController(ProfileManager pMgr, ClientDnsOverwrite dns, CloseableHttpClient client) {
|
||||||
|
this.pMgr = pMgr;
|
||||||
this.dns = dns;
|
this.dns = dns;
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.gson = GsonUtil.build();
|
this.gson = GsonUtil.build();
|
||||||
@@ -70,6 +98,7 @@ public class RegistrationController {
|
|||||||
|
|
||||||
@RequestMapping(path = registerV1Url, method = RequestMethod.POST)
|
@RequestMapping(path = registerV1Url, method = RequestMethod.POST)
|
||||||
public String register(HttpServletRequest req, HttpServletResponse res) {
|
public String register(HttpServletRequest req, HttpServletResponse res) {
|
||||||
|
List<String> ids = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
JsonObject reqJsonObject = parser.parse(req.getInputStream());
|
JsonObject reqJsonObject = parser.parse(req.getInputStream());
|
||||||
GsonUtil.findObj(reqJsonObject, "auth").ifPresent(auth -> {
|
GsonUtil.findObj(reqJsonObject, "auth").ifPresent(auth -> {
|
||||||
@@ -82,6 +111,7 @@ public class RegistrationController {
|
|||||||
|
|
||||||
String gId = auth.get("googleId").getAsString();
|
String gId = auth.get("googleId").getAsString();
|
||||||
log.info("Google ID: {}", gId);
|
log.info("Google ID: {}", gId);
|
||||||
|
ids.add(gId);
|
||||||
auth.addProperty("type", "m.login.dummy");
|
auth.addProperty("type", "m.login.dummy");
|
||||||
auth.remove("googleId");
|
auth.remove("googleId");
|
||||||
reqJsonObject.addProperty("password", UUID.randomUUID().toString());
|
reqJsonObject.addProperty("password", UUID.randomUUID().toString());
|
||||||
@@ -97,8 +127,8 @@ public class RegistrationController {
|
|||||||
JsonObject json = parser.parse(body);
|
JsonObject json = parser.parse(body);
|
||||||
if (sc == 200 && json.has("user_id")) {
|
if (sc == 200 && json.has("user_id")) {
|
||||||
log.info("User was registered, adding 3PID");
|
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);
|
res.setStatus(sc);
|
||||||
return body;
|
return body;
|
||||||
|
@@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
package io.kamax.mxisd.profile;
|
package io.kamax.mxisd.profile;
|
||||||
|
|
||||||
|
import io.kamax.matrix.ThreePid;
|
||||||
import io.kamax.matrix._MatrixID;
|
import io.kamax.matrix._MatrixID;
|
||||||
import io.kamax.matrix._ThreePid;
|
import io.kamax.matrix._ThreePid;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -32,16 +33,21 @@ import java.util.stream.Collectors;
|
|||||||
@Component
|
@Component
|
||||||
public class ProfileManager {
|
public class ProfileManager {
|
||||||
|
|
||||||
private List<ProfileProvider> providers;
|
private List<ProfileProvider> readers;
|
||||||
|
private List<ProfileWriter> writers;
|
||||||
|
|
||||||
public ProfileManager(List<ProfileProvider> providers) {
|
public ProfileManager(List<ProfileProvider> providers, List<ProfileWriter> writers) {
|
||||||
this.providers = providers.stream()
|
this.readers = providers.stream()
|
||||||
.filter(ProfileProvider::isEnabled)
|
.filter(ProfileProvider::isEnabled)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
this.writers = writers.stream()
|
||||||
|
.filter(ProfileWriter::isEnabled)
|
||||||
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> List<T> get(Function<ProfileProvider, List<T>> function) {
|
public <T> List<T> get(Function<ProfileProvider, List<T>> function) {
|
||||||
return providers.stream()
|
return readers.stream()
|
||||||
.map(function)
|
.map(function)
|
||||||
.flatMap(Collection::stream)
|
.flatMap(Collection::stream)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
@@ -55,4 +61,8 @@ public class ProfileManager {
|
|||||||
return get(p -> p.getRoles(mxid));
|
return get(p -> p.getRoles(mxid));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addThreepid(_MatrixID mxid, ThreePid tpid) {
|
||||||
|
writers.forEach(w -> w.addThreepid(mxid, tpid));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
32
src/main/java/io/kamax/mxisd/profile/ProfileWriter.java
Normal file
32
src/main/java/io/kamax/mxisd/profile/ProfileWriter.java
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user