Search in sources :

Example 6 with ThreePidMapping

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;
}
Also used : ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) RecursiveTask(java.util.concurrent.RecursiveTask) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 7 with ThreePidMapping

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);
    }
}
Also used : ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) ArrayList(java.util.ArrayList)

Example 8 with ThreePidMapping

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);
}
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) RestThreePidProvider(io.kamax.mxisd.backend.rest.RestThreePidProvider) Before(org.junit.Before)

Example 9 with ThreePidMapping

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;
}
Also used : ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) HttpPost(org.apache.http.client.methods.HttpPost) InvalidResponseJsonException(io.kamax.mxisd.exception.InvalidResponseJsonException) ArrayList(java.util.ArrayList) ClientBulkLookupRequest(io.kamax.mxisd.http.io.identity.ClientBulkLookupRequest) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 10 with ThreePidMapping

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();
}
Also used : BackendAuthResult(io.kamax.mxisd.auth.provider.BackendAuthResult) ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) AuthenticatorProvider(io.kamax.mxisd.auth.provider.AuthenticatorProvider) io.kamax.matrix._ThreePid(io.kamax.matrix._ThreePid) io.kamax.matrix._ThreePid(io.kamax.matrix._ThreePid) ThreePid(io.kamax.matrix.ThreePid) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID)

Aggregations

ThreePidMapping (io.kamax.mxisd.lookup.ThreePidMapping)12 ArrayList (java.util.ArrayList)7 MatrixConfig (io.kamax.mxisd.config.MatrixConfig)4 SingleLookupRequest (io.kamax.mxisd.lookup.SingleLookupRequest)4 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)3 RestBackendConfig (io.kamax.mxisd.config.rest.RestBackendConfig)3 IOException (java.io.IOException)3 MatrixID (io.kamax.matrix.MatrixID)2 ThreePid (io.kamax.matrix.ThreePid)2 UserID (io.kamax.mxisd.UserID)2 UserIdType (io.kamax.mxisd.UserIdType)2 InternalServerError (io.kamax.mxisd.exception.InternalServerError)2 ClientBulkLookupRequest (io.kamax.mxisd.http.io.identity.ClientBulkLookupRequest)2 SingleLookupReply (io.kamax.mxisd.lookup.SingleLookupReply)2 IThreePidProvider (io.kamax.mxisd.lookup.provider.IThreePidProvider)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 Before (org.junit.Before)2