use of io.kamax.mxisd.lookup.ThreePidMapping in project mxisd by kamax-io.
the class DnsLookupProvider method populate.
@Override
public List<ThreePidMapping> populate(List<ThreePidMapping> mappings) {
Map<String, List<ThreePidMapping>> domains = new HashMap<>();
for (ThreePidMapping mapping : mappings) {
if (!ThreePidMedium.Email.is(mapping.getMedium())) {
log.info("Skipping unsupported type {} for {}", mapping.getMedium(), mapping.getValue());
continue;
}
Optional<String> domainOpt = getDomain(mapping.getValue());
if (!domainOpt.isPresent()) {
log.warn("No domain for 3PID {}", mapping.getValue());
continue;
}
String domain = domainOpt.get();
List<ThreePidMapping> domainMappings = domains.computeIfAbsent(domain, s -> new ArrayList<>());
domainMappings.add(mapping);
}
log.info("Looking mappings across {} domains", domains.keySet().size());
ForkJoinPool pool = ForkJoinPool.commonPool();
RecursiveTask<List<ThreePidMapping>> task = new RecursiveTask<List<ThreePidMapping>>() {
@Override
protected List<ThreePidMapping> compute() {
List<ThreePidMapping> mappingsFound = new ArrayList<>();
List<DomainBulkLookupTask> tasks = new ArrayList<>();
for (String domain : domains.keySet()) {
DomainBulkLookupTask domainTask = new DomainBulkLookupTask(domain, domains.get(domain));
domainTask.fork();
tasks.add(domainTask);
}
for (DomainBulkLookupTask task : tasks) {
mappingsFound.addAll(task.join());
}
return mappingsFound;
}
};
pool.submit(task);
pool.shutdown();
List<ThreePidMapping> mappingsFound = task.join();
log.info("Found {} mappings overall", mappingsFound.size());
return mappingsFound;
}
use of io.kamax.mxisd.lookup.ThreePidMapping in project mxisd by kamax-io.
the class ClientBulkLookupRequest method setMappings.
public void setMappings(List<ThreePidMapping> mappings) {
for (ThreePidMapping mapping : mappings) {
List<String> threepid = new ArrayList<>();
threepid.add(mapping.getMedium());
threepid.add(mapping.getValue());
threepids.add(threepid);
}
}
use of io.kamax.mxisd.lookup.ThreePidMapping in project mxisd by kamax-io.
the class RestThreePidProviderTest method before.
@Before
public void before() {
MatrixConfig mxCfg = new MatrixConfig();
mxCfg.setDomain("example.org");
mxCfg.build();
RestBackendConfig cfg = new RestBackendConfig();
cfg.setEnabled(true);
cfg.setHost("http://localhost:65000");
cfg.getEndpoints().getIdentity().setSingle(lookupSinglePath);
cfg.getEndpoints().getIdentity().setBulk(lookupBulkPath);
cfg.build();
p = new RestThreePidProvider(cfg, mxCfg);
lookupSingleRequest = new SingleLookupRequest();
lookupSingleRequest.setType(ThreePidMedium.Email.getId());
lookupSingleRequest.setThreePid("john.doe@example.org");
ThreePidMapping m1 = new ThreePidMapping();
m1.setMedium(ThreePidMedium.Email.getId());
m1.setValue("john.doe@example.org");
ThreePidMapping m2 = new ThreePidMapping();
m1.setMedium(ThreePidMedium.PhoneNumber.getId());
m1.setValue("123456789");
lookupBulkList = new ArrayList<>();
lookupBulkList.add(m1);
lookupBulkList.add(m2);
}
use of io.kamax.mxisd.lookup.ThreePidMapping in project mxisd by kamax-io.
the class RemoteIdentityServerFetcher method find.
@Override
public List<ThreePidMapping> find(String remote, List<ThreePidMapping> mappings) {
List<ThreePidMapping> mappingsFound = new ArrayList<>();
ClientBulkLookupRequest mappingRequest = new ClientBulkLookupRequest();
mappingRequest.setMappings(mappings);
String url = remote + IsAPIv1.Base + "/bulk_lookup";
try {
HttpPost request = RestClientUtils.post(url, mappingRequest);
try (CloseableHttpResponse response = client.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
String body = EntityUtils.toString(response.getEntity());
if (statusCode != 200) {
log.warn("Could not perform lookup at {} due to HTTP return code: {}", url, statusCode);
log.warn("Body: {}", body);
return mappingsFound;
}
ClientBulkLookupRequest input = parser.parse(response, ClientBulkLookupRequest.class);
for (List<String> mappingRaw : input.getThreepids()) {
ThreePidMapping mapping = new ThreePidMapping();
mapping.setMedium(mappingRaw.get(0));
mapping.setValue(mappingRaw.get(1));
mapping.setMxid(mappingRaw.get(2));
mappingsFound.add(mapping);
}
}
} catch (IOException e) {
log.warn("Unable to fetch remote lookup data: {}", e.getMessage());
} catch (InvalidResponseJsonException e) {
log.info("HTTP response from {} was empty/invalid", remote);
}
return mappingsFound;
}
use of io.kamax.mxisd.lookup.ThreePidMapping in project mxisd by kamax-io.
the class AuthManager method authenticate.
public UserAuthResult authenticate(String id, String password) {
_MatrixID mxid = MatrixID.asAcceptable(id);
for (AuthenticatorProvider provider : providers) {
if (!provider.isEnabled()) {
continue;
}
log.info("Attempting authentication with store {}", provider.getClass().getSimpleName());
BackendAuthResult result = provider.authenticate(mxid, password);
if (result.isSuccess()) {
String mxId;
if (UserIdType.Localpart.is(result.getId().getType())) {
mxId = MatrixID.from(result.getId().getValue(), mxCfg.getDomain()).acceptable().getId();
} else if (UserIdType.MatrixID.is(result.getId().getType())) {
mxId = MatrixID.asAcceptable(result.getId().getValue()).getId();
} else {
log.warn("Unsupported User ID type {} for backend {}", result.getId().getType(), provider.getClass().getSimpleName());
continue;
}
UserAuthResult authResult = new UserAuthResult().success(result.getProfile().getDisplayName());
for (_ThreePid pid : result.getProfile().getThreePids()) {
authResult.withThreePid(pid.getMedium(), pid.getAddress());
}
log.info("{} was authenticated by {}, publishing 3PID mappings, if any", id, provider.getClass().getSimpleName());
for (ThreePid pid : authResult.getThreePids()) {
log.info("Processing {} for {}", pid, id);
invMgr.publishMappingIfInvited(new ThreePidMapping(pid, mxId));
}
try {
MatrixID.asValid(mxId);
} catch (IllegalArgumentException e) {
log.warn("The returned User ID {} is not a valid Matrix ID. Login might fail at the Homeserver level", mxId);
}
invMgr.lookupMappingsForInvites();
return authResult;
}
}
return new UserAuthResult().failure();
}
Aggregations