use of io.kamax.mxisd.lookup.SingleLookupRequest 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.SingleLookupRequest in project mxisd by kamax-io.
the class ExecIdentityStore method find.
@Override
public Optional<SingleLookupReply> find(SingleLookupRequest request) {
Processor<Optional<SingleLookupReply>> p = new Processor<>();
p.withConfig(cfg.getLookup().getSingle());
p.addTokenMapper(getSingleCfg().getToken().getMedium(), request::getType);
p.addTokenMapper(getSingleCfg().getToken().getAddress(), request::getThreePid);
p.addJsonInputTemplate(tokens -> new ThreePid(tokens.getMedium(), tokens.getAddress()));
p.addInputTemplate(PlainType, tokens -> tokens.getMedium() + System.lineSeparator() + tokens.getAddress());
p.addSuccessMapper(JsonType, output -> {
if (StringUtils.isBlank(output)) {
return Optional.empty();
}
return GsonUtil.findObj(GsonUtil.parseObj(output), "lookup").filter(obj -> !obj.entrySet().isEmpty()).map(json -> GsonUtil.get().fromJson(json, LookupSingleResponseJson.class)).map(lookup -> getUserId(lookup.getId())).map(mxId -> new SingleLookupReply(request, mxId));
});
p.addSuccessMapper(PlainType, output -> {
String[] lines = output.split("\\R");
if (lines.length > 2) {
throw new InternalServerError("Exec auth command returned more than 2 lines (" + lines.length + ")");
}
if (lines.length == 1 && StringUtils.isBlank(lines[0])) {
return Optional.empty();
}
String type = StringUtils.trimToEmpty(lines.length == 1 ? UserIdType.Localpart.getId() : lines[0]);
String value = StringUtils.trimToEmpty(lines.length == 2 ? lines[1] : lines[0]);
if (UserIdType.Localpart.is(type)) {
return Optional.of(new SingleLookupReply(request, MatrixID.asAcceptable(value, mxCfg.getDomain())));
}
if (UserIdType.MatrixID.is(type)) {
return Optional.of(new SingleLookupReply(request, MatrixID.asAcceptable(value)));
}
throw new InternalServerError("Invalid user type: " + type);
});
p.withFailureDefault(o -> Optional.empty());
return p.execute();
}
use of io.kamax.mxisd.lookup.SingleLookupRequest in project mxisd by kamax-io.
the class ExecIdentityStoreTest method singleSuccessEmpty.
@Test
public void singleSuccessEmpty() {
ExecIdentityStore store = getStore("singleSuccessEmpty");
SingleLookupRequest req = new SingleLookupRequest();
req.setType(ThreePidMedium.Email.getId());
req.setThreePid(user1Email);
Optional<SingleLookupReply> lookup = store.find(req);
assertFalse(lookup.isPresent());
}
use of io.kamax.mxisd.lookup.SingleLookupRequest in project mxisd by kamax-io.
the class ExecIdentityStoreTest method singleSuccessData.
@Test
public void singleSuccessData() {
SingleLookupRequest req = new SingleLookupRequest();
req.setType(ThreePidMedium.Email.getId());
req.setThreePid(user1Email);
Optional<SingleLookupReply> lookup = getStore("singleSuccessData").find(req);
assertTrue(lookup.isPresent());
SingleLookupReply reply = lookup.get();
assertEquals(MatrixID.asAcceptable(user1Localpart, domain), reply.getMxid());
}
use of io.kamax.mxisd.lookup.SingleLookupRequest in project mxisd by kamax-io.
the class MxisdTest method singleLookup.
@Test
public void singleLookup() {
SingleLookupRequest req = new SingleLookupRequest();
req.setRecursive(false);
req.setType("email");
req.setThreePid("john@localhost");
Optional<SingleLookupReply> reply = m.getIdentity().find(req);
assertTrue(reply.isPresent());
assertEquals("@john:localhost", reply.get().getMxid().getId());
}
Aggregations