Search in sources :

Example 1 with SingleLookupRequest

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);
}
Also used : ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) MatrixConfig(io.kamax.mxisd.config.MatrixConfig) SingleLookupRequest(io.kamax.mxisd.lookup.SingleLookupRequest) RestBackendConfig(io.kamax.mxisd.config.rest.RestBackendConfig) Before(org.junit.Before)

Example 2 with SingleLookupRequest

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();
}
Also used : JsonParseException(com.google.gson.JsonParseException) GsonUtil(io.kamax.matrix.json.GsonUtil) LookupBulkResponseJson(io.kamax.mxisd.backend.rest.LookupBulkResponseJson) MatrixConfig(io.kamax.mxisd.config.MatrixConfig) LoggerFactory(org.slf4j.LoggerFactory) MatrixID(io.kamax.matrix.MatrixID) StringUtils(org.apache.commons.lang3.StringUtils) IThreePidProvider(io.kamax.mxisd.lookup.provider.IThreePidProvider) SingleLookupRequest(io.kamax.mxisd.lookup.SingleLookupRequest) UserIdType(io.kamax.mxisd.UserIdType) LookupSingleResponseJson(io.kamax.mxisd.backend.rest.LookupSingleResponseJson) SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) Logger(org.slf4j.Logger) ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID) Collectors(java.util.stream.Collectors) ThreePid(io.kamax.matrix.ThreePid) Objects(java.util.Objects) JsonArray(com.google.gson.JsonArray) InternalServerError(io.kamax.mxisd.exception.InternalServerError) List(java.util.List) ExecConfig(io.kamax.mxisd.config.ExecConfig) UserID(io.kamax.mxisd.UserID) Optional(java.util.Optional) Collections(java.util.Collections) SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) Optional(java.util.Optional) LookupSingleResponseJson(io.kamax.mxisd.backend.rest.LookupSingleResponseJson) ThreePid(io.kamax.matrix.ThreePid) InternalServerError(io.kamax.mxisd.exception.InternalServerError)

Example 3 with SingleLookupRequest

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());
}
Also used : SingleLookupRequest(io.kamax.mxisd.lookup.SingleLookupRequest) SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) ExecIdentityStore(io.kamax.mxisd.backend.exec.ExecIdentityStore) Test(org.junit.Test)

Example 4 with SingleLookupRequest

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());
}
Also used : SingleLookupRequest(io.kamax.mxisd.lookup.SingleLookupRequest) SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) Test(org.junit.Test)

Example 5 with SingleLookupRequest

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());
}
Also used : SingleLookupRequest(io.kamax.mxisd.lookup.SingleLookupRequest) SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) Test(org.junit.Test)

Aggregations

SingleLookupRequest (io.kamax.mxisd.lookup.SingleLookupRequest)10 SingleLookupReply (io.kamax.mxisd.lookup.SingleLookupReply)7 Test (org.junit.Test)5 MatrixConfig (io.kamax.mxisd.config.MatrixConfig)3 ThreePidMapping (io.kamax.mxisd.lookup.ThreePidMapping)3 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)2 ExecIdentityStore (io.kamax.mxisd.backend.exec.ExecIdentityStore)2 RestBackendConfig (io.kamax.mxisd.config.rest.RestBackendConfig)2 Before (org.junit.Before)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 MatrixID (io.kamax.matrix.MatrixID)1 ThreePid (io.kamax.matrix.ThreePid)1 GsonUtil (io.kamax.matrix.json.GsonUtil)1 UserID (io.kamax.mxisd.UserID)1 UserIdType (io.kamax.mxisd.UserIdType)1 LookupBulkResponseJson (io.kamax.mxisd.backend.rest.LookupBulkResponseJson)1 LookupSingleResponseJson (io.kamax.mxisd.backend.rest.LookupSingleResponseJson)1 RestThreePidProvider (io.kamax.mxisd.backend.rest.RestThreePidProvider)1