Properly handle invites with LDAP backend
This commit is contained in:
116
src/main/groovy/io/kamax/mxisd/lookup/SingleLookupReply.java
Normal file
116
src/main/groovy/io/kamax/mxisd/lookup/SingleLookupReply.java
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* 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.lookup;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import io.kamax.matrix.MatrixID;
|
||||
import io.kamax.matrix._MatrixID;
|
||||
import io.kamax.mxisd.controller.v1.io.SingeLookupReplyJson;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public class SingleLookupReply {
|
||||
|
||||
private static Gson gson = new Gson();
|
||||
|
||||
private boolean isRecursive;
|
||||
private boolean isSigned;
|
||||
private String body;
|
||||
private SingleLookupRequest request;
|
||||
private _MatrixID mxid;
|
||||
private Instant notBefore;
|
||||
private Instant notAfter;
|
||||
private Instant timestamp;
|
||||
|
||||
public static SingleLookupReply fromRecursive(SingleLookupRequest request, String body) {
|
||||
SingleLookupReply reply = new SingleLookupReply();
|
||||
reply.isRecursive = true;
|
||||
reply.request = request;
|
||||
reply.body = body;
|
||||
|
||||
try {
|
||||
SingeLookupReplyJson json = gson.fromJson(body, SingeLookupReplyJson.class);
|
||||
reply.mxid = new MatrixID(json.getMxid());
|
||||
reply.notAfter = Instant.ofEpochMilli(json.getNot_after());
|
||||
reply.notBefore = Instant.ofEpochMilli(json.getNot_before());
|
||||
reply.timestamp = Instant.ofEpochMilli(json.getTs());
|
||||
reply.isSigned = json.isSigned();
|
||||
} catch (JsonSyntaxException e) {
|
||||
// stub - we only want to try, nothing more
|
||||
}
|
||||
|
||||
return reply;
|
||||
}
|
||||
|
||||
private SingleLookupReply() {
|
||||
// stub
|
||||
}
|
||||
|
||||
public SingleLookupReply(SingleLookupRequest request, String mxid) {
|
||||
this(request, new MatrixID(mxid));
|
||||
}
|
||||
|
||||
public SingleLookupReply(SingleLookupRequest request, _MatrixID mxid) {
|
||||
this(request, mxid, Instant.now(), Instant.ofEpochMilli(0), Instant.ofEpochMilli(253402300799000L));
|
||||
}
|
||||
|
||||
public SingleLookupReply(SingleLookupRequest request, _MatrixID mxid, Instant timestamp, Instant notBefore, Instant notAfter) {
|
||||
this.request = request;
|
||||
this.mxid = mxid;
|
||||
this.timestamp = timestamp;
|
||||
this.notBefore = notBefore;
|
||||
this.notAfter = notAfter;
|
||||
}
|
||||
|
||||
public boolean isRecursive() {
|
||||
return isRecursive;
|
||||
}
|
||||
|
||||
public boolean isSigned() {
|
||||
return isSigned;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public SingleLookupRequest getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
||||
public _MatrixID getMxid() {
|
||||
return mxid;
|
||||
}
|
||||
|
||||
public Instant getNotBefore() {
|
||||
return notBefore;
|
||||
}
|
||||
|
||||
public Instant getNotAfter() {
|
||||
return notAfter;
|
||||
}
|
||||
|
||||
public Instant getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,12 +20,13 @@
|
||||
|
||||
package io.kamax.mxisd.lookup.fetcher
|
||||
|
||||
import io.kamax.mxisd.lookup.SingleLookupReply
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping
|
||||
|
||||
interface IBridgeFetcher {
|
||||
|
||||
Optional<?> find(SingleLookupRequest request)
|
||||
Optional<SingleLookupReply> find(SingleLookupRequest request)
|
||||
|
||||
List<ThreePidMapping> populate(List<ThreePidMapping> mappings)
|
||||
|
||||
|
||||
@@ -20,13 +20,15 @@
|
||||
|
||||
package io.kamax.mxisd.lookup.fetcher
|
||||
|
||||
import io.kamax.mxisd.lookup.SingleLookupReply
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping
|
||||
|
||||
interface IRemoteIdentityServerFetcher {
|
||||
|
||||
boolean isUsable(String remote)
|
||||
|
||||
Optional<?> find(String remote, String type, String threePid)
|
||||
Optional<SingleLookupReply> find(String remote, SingleLookupRequest request)
|
||||
|
||||
List<ThreePidMapping> find(String remote, List<ThreePidMapping> mappings)
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
package io.kamax.mxisd.lookup.provider;
|
||||
|
||||
import io.kamax.mxisd.config.RecursiveLookupBridgeConfig;
|
||||
import io.kamax.mxisd.lookup.SingleLookupReply;
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest;
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping;
|
||||
import io.kamax.mxisd.lookup.fetcher.IBridgeFetcher;
|
||||
@@ -46,16 +47,16 @@ public class BridgeFetcher implements IBridgeFetcher {
|
||||
private RemoteIdentityServerFetcher fetcher;
|
||||
|
||||
@Override
|
||||
public Optional<?> find(SingleLookupRequest request) {
|
||||
public Optional<SingleLookupReply> find(SingleLookupRequest request) {
|
||||
Optional<String> mediumUrl = Optional.ofNullable(cfg.getMappings().get(request.getType()));
|
||||
if (mediumUrl.isPresent() && !StringUtils.isBlank(mediumUrl.get())) {
|
||||
log.info("Using specific medium bridge lookup URL {}", mediumUrl.get());
|
||||
|
||||
return fetcher.find(mediumUrl.get(), request.getType(), request.getThreePid());
|
||||
return fetcher.find(mediumUrl.get(), request);
|
||||
} else if (!StringUtils.isBlank(cfg.getServer())) {
|
||||
log.info("Using generic bridge lookup URL {}", cfg.getServer());
|
||||
|
||||
return fetcher.find(cfg.getServer(), request.getType(), request.getThreePid());
|
||||
return fetcher.find(cfg.getServer(), request);
|
||||
} else {
|
||||
log.info("No bridge lookup URL found/configured, skipping");
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
package io.kamax.mxisd.lookup.provider
|
||||
|
||||
import io.kamax.mxisd.config.ServerConfig
|
||||
import io.kamax.mxisd.lookup.SingleLookupReply
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping
|
||||
import io.kamax.mxisd.lookup.fetcher.IRemoteIdentityServerFetcher
|
||||
@@ -124,7 +125,7 @@ class DnsLookupProvider implements IThreePidProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
Optional<?> find(SingleLookupRequest request) {
|
||||
Optional<SingleLookupReply> find(SingleLookupRequest request) {
|
||||
if (!StringUtils.equals("email", request.getType())) { // TODO use enum
|
||||
log.info("Skipping unsupported type {} for {}", request.getType(), request.getThreePid())
|
||||
return Optional.empty()
|
||||
@@ -137,7 +138,7 @@ class DnsLookupProvider implements IThreePidProvider {
|
||||
Optional<String> baseUrl = findIdentityServerForDomain(domain)
|
||||
|
||||
if (baseUrl.isPresent()) {
|
||||
return fetcher.find(baseUrl.get(), request.getType().toString(), request.getThreePid())
|
||||
return fetcher.find(baseUrl.get(), request)
|
||||
}
|
||||
|
||||
return Optional.empty()
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
package io.kamax.mxisd.lookup.provider
|
||||
|
||||
import io.kamax.mxisd.config.ForwardConfig
|
||||
import io.kamax.mxisd.lookup.SingleLookupReply
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping
|
||||
import io.kamax.mxisd.lookup.fetcher.IRemoteIdentityServerFetcher
|
||||
@@ -56,9 +57,9 @@ class ForwarderProvider implements IThreePidProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
Optional<?> find(SingleLookupRequest request) {
|
||||
Optional<SingleLookupReply> find(SingleLookupRequest request) {
|
||||
for (String root : cfg.getServers()) {
|
||||
Optional<?> answer = fetcher.find(root, request.getType(), request.getThreePid())
|
||||
Optional<SingleLookupReply> answer = fetcher.find(root, request)
|
||||
if (answer.isPresent()) {
|
||||
return answer
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.google.firebase.internal.NonNull
|
||||
import com.google.firebase.tasks.OnFailureListener
|
||||
import com.google.firebase.tasks.OnSuccessListener
|
||||
import io.kamax.matrix.ThreePidMedium
|
||||
import io.kamax.mxisd.lookup.SingleLookupReply
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping
|
||||
import org.apache.commons.lang.StringUtils
|
||||
@@ -88,6 +89,10 @@ public class GoogleFirebaseProvider implements IThreePidProvider {
|
||||
.build();
|
||||
}
|
||||
|
||||
private String getMxid(UserRecord record) {
|
||||
return "@${record.getUid()}:${domain}";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return isEnabled;
|
||||
@@ -154,20 +159,13 @@ public class GoogleFirebaseProvider implements IThreePidProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<?> find(SingleLookupRequest request) {
|
||||
public Optional<SingleLookupReply> find(SingleLookupRequest request) {
|
||||
Optional<UserRecord> urOpt = findInternal(request.getType(), request.getThreePid())
|
||||
if (urOpt.isPresent()) {
|
||||
return [
|
||||
address : request.getThreePid(),
|
||||
medium : request.getType(),
|
||||
mxid : "@${urOpt.get().getUid()}:${domain}",
|
||||
not_before: 0,
|
||||
not_after : 9223372036854775807,
|
||||
ts : 0
|
||||
]
|
||||
} else {
|
||||
return Optional.empty();
|
||||
return Optional.of(new SingleLookupReply(request, getMxid(urOpt.get())));
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -181,7 +179,7 @@ public class GoogleFirebaseProvider implements IThreePidProvider {
|
||||
ThreePidMapping result = new ThreePidMapping();
|
||||
result.setMedium(o.getMedium())
|
||||
result.setValue(o.getValue())
|
||||
result.setMxid("@${urOpt.get().getUid()}:${domain}")
|
||||
result.setMxid(getMxid(urOpt.get()))
|
||||
results.add(result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
package io.kamax.mxisd.lookup.provider
|
||||
|
||||
import io.kamax.mxisd.lookup.SingleLookupReply
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping
|
||||
|
||||
@@ -34,7 +35,7 @@ interface IThreePidProvider {
|
||||
*/
|
||||
int getPriority() // Should not be here but let's KISS for now
|
||||
|
||||
Optional<?> find(SingleLookupRequest request)
|
||||
Optional<SingleLookupReply> find(SingleLookupRequest request)
|
||||
|
||||
List<ThreePidMapping> populate(List<ThreePidMapping> mappings)
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ package io.kamax.mxisd.lookup.provider
|
||||
|
||||
import io.kamax.mxisd.config.ServerConfig
|
||||
import io.kamax.mxisd.config.ldap.LdapConfig
|
||||
import io.kamax.mxisd.lookup.SingleLookupReply
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping
|
||||
import org.apache.commons.lang.StringUtils
|
||||
@@ -131,7 +132,7 @@ class LdapProvider implements IThreePidProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
Optional<?> find(SingleLookupRequest request) {
|
||||
Optional<SingleLookupReply> find(SingleLookupRequest request) {
|
||||
log.info("Performing LDAP lookup ${request.getThreePid()} of type ${request.getType()}")
|
||||
|
||||
LdapConnection conn = getConn()
|
||||
@@ -140,14 +141,7 @@ class LdapProvider implements IThreePidProvider {
|
||||
|
||||
Optional<String> mxid = lookup(conn, request.getType(), request.getThreePid())
|
||||
if (mxid.isPresent()) {
|
||||
return Optional.of([
|
||||
address : request.getThreePid(),
|
||||
medium : request.getType(),
|
||||
mxid : mxid.get(),
|
||||
not_before: 0,
|
||||
not_after : 9223372036854775807,
|
||||
ts : 0
|
||||
])
|
||||
return Optional.of(new SingleLookupReply(request, mxid.get()));
|
||||
}
|
||||
} finally {
|
||||
conn.close()
|
||||
|
||||
@@ -24,6 +24,8 @@ import groovy.json.JsonException
|
||||
import groovy.json.JsonOutput
|
||||
import groovy.json.JsonSlurper
|
||||
import io.kamax.mxisd.controller.v1.ClientBulkLookupRequest
|
||||
import io.kamax.mxisd.lookup.SingleLookupReply
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping
|
||||
import io.kamax.mxisd.lookup.fetcher.IRemoteIdentityServerFetcher
|
||||
import org.apache.http.HttpEntity
|
||||
@@ -77,24 +79,26 @@ public class RemoteIdentityServerFetcher implements IRemoteIdentityServerFetcher
|
||||
}
|
||||
|
||||
@Override
|
||||
Optional<?> find(String remote, String type, String threePid) {
|
||||
log.info("Looking up {} 3PID {} using {}", type, threePid, remote)
|
||||
Optional<SingleLookupReply> find(String remote, SingleLookupRequest request) {
|
||||
log.info("Looking up {} 3PID {} using {}", request.getType(), request.getThreePid(), remote)
|
||||
|
||||
HttpURLConnection rootSrvConn = (HttpURLConnection) new URL(
|
||||
"${remote}/_matrix/identity/api/v1/lookup?medium=${type}&address=${threePid}"
|
||||
"${remote}/_matrix/identity/api/v1/lookup?medium=${request.getType()}&address=${request.getThreePid()}"
|
||||
).openConnection()
|
||||
|
||||
try {
|
||||
def output = json.parseText(rootSrvConn.getInputStream().getText())
|
||||
String outputRaw = rootSrvConn.getInputStream().getText()
|
||||
def output = json.parseText(outputRaw)
|
||||
if (output['address']) {
|
||||
log.info("Found 3PID mapping: {}", output)
|
||||
return Optional.of(output)
|
||||
|
||||
return Optional.of(SingleLookupReply.fromRecursive(request, outputRaw))
|
||||
}
|
||||
|
||||
log.info("Empty 3PID mapping from {}", remote)
|
||||
return Optional.empty()
|
||||
} catch (IOException e) {
|
||||
log.warn("Error looking up 3PID mapping {}: {}", threePid, e.getMessage())
|
||||
log.warn("Error looking up 3PID mapping {}: {}", request.getThreePid(), e.getMessage())
|
||||
return Optional.empty()
|
||||
} catch (JsonException e) {
|
||||
log.warn("Invalid JSON answer from {}", remote)
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
package io.kamax.mxisd.lookup.strategy
|
||||
|
||||
import io.kamax.mxisd.lookup.BulkLookupRequest
|
||||
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
|
||||
@@ -29,11 +30,11 @@ interface LookupStrategy {
|
||||
|
||||
List<IThreePidProvider> getLocalProviders()
|
||||
|
||||
Optional<?> find(String medium, String address, boolean recursive)
|
||||
Optional<SingleLookupReply> find(String medium, String address, boolean recursive)
|
||||
|
||||
Optional<?> find(SingleLookupRequest request)
|
||||
Optional<SingleLookupReply> find(SingleLookupRequest request)
|
||||
|
||||
Optional<?> findRecursive(SingleLookupRequest request)
|
||||
Optional<SingleLookupReply> findRecursive(SingleLookupRequest request)
|
||||
|
||||
List<ThreePidMapping> find(BulkLookupRequest requests)
|
||||
|
||||
|
||||
@@ -22,10 +22,7 @@ package io.kamax.mxisd.lookup.strategy
|
||||
|
||||
import edazdarevic.commons.net.CIDRUtils
|
||||
import io.kamax.mxisd.config.RecursiveLookupConfig
|
||||
import io.kamax.mxisd.lookup.ALookupRequest
|
||||
import io.kamax.mxisd.lookup.BulkLookupRequest
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping
|
||||
import io.kamax.mxisd.lookup.*
|
||||
import io.kamax.mxisd.lookup.fetcher.IBridgeFetcher
|
||||
import io.kamax.mxisd.lookup.provider.IThreePidProvider
|
||||
import org.slf4j.Logger
|
||||
@@ -122,7 +119,7 @@ class RecursivePriorityLookupStrategy implements LookupStrategy, InitializingBea
|
||||
}
|
||||
|
||||
@Override
|
||||
Optional<?> find(String medium, String address, boolean recursive) {
|
||||
Optional<SingleLookupReply> find(String medium, String address, boolean recursive) {
|
||||
SingleLookupRequest req = new SingleLookupRequest();
|
||||
req.setType(medium)
|
||||
req.setThreePid(address)
|
||||
@@ -130,9 +127,9 @@ class RecursivePriorityLookupStrategy implements LookupStrategy, InitializingBea
|
||||
return find(req, recursive)
|
||||
}
|
||||
|
||||
Optional<?> find(SingleLookupRequest request, boolean forceRecursive) {
|
||||
Optional<SingleLookupReply> find(SingleLookupRequest request, boolean forceRecursive) {
|
||||
for (IThreePidProvider provider : listUsableProviders(request, forceRecursive)) {
|
||||
Optional<?> lookupDataOpt = provider.find(request)
|
||||
Optional<SingleLookupReply> lookupDataOpt = provider.find(request)
|
||||
if (lookupDataOpt.isPresent()) {
|
||||
return lookupDataOpt
|
||||
}
|
||||
@@ -151,12 +148,12 @@ class RecursivePriorityLookupStrategy implements LookupStrategy, InitializingBea
|
||||
}
|
||||
|
||||
@Override
|
||||
Optional<?> find(SingleLookupRequest request) {
|
||||
Optional<SingleLookupReply> find(SingleLookupRequest request) {
|
||||
return find(request, false)
|
||||
}
|
||||
|
||||
@Override
|
||||
Optional<?> findRecursive(SingleLookupRequest request) {
|
||||
Optional<SingleLookupReply> findRecursive(SingleLookupRequest request) {
|
||||
return find(request, true)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user