Search in sources :

Example 1 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 2 with ThreePidMapping

use of io.kamax.mxisd.lookup.ThreePidMapping in project mxisd by kamax-io.

the class LdapThreePidProvider method populate.

@Override
public List<ThreePidMapping> populate(List<ThreePidMapping> mappings) {
    log.info("Looking up {} mappings", mappings.size());
    List<ThreePidMapping> mappingsFound = new ArrayList<>();
    try (LdapConnection conn = getConn()) {
        bind(conn);
        for (ThreePidMapping mapping : mappings) {
            try {
                lookup(conn, mapping.getMedium(), mapping.getValue()).ifPresent(id -> {
                    mapping.setMxid(id);
                    mappingsFound.add(mapping);
                });
            } catch (IllegalArgumentException e) {
                log.warn("{} is not a supported 3PID type for LDAP lookup", mapping.getMedium());
            }
        }
    } catch (LdapException | IOException e) {
        throw new InternalServerError(e);
    }
    return mappingsFound;
}
Also used : ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) InternalServerError(io.kamax.mxisd.exception.InternalServerError) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 3 with ThreePidMapping

use of io.kamax.mxisd.lookup.ThreePidMapping in project mxisd by kamax-io.

the class RestThreePidProvider method populate.

// TODO refactor common code
@Override
public List<ThreePidMapping> populate(List<ThreePidMapping> mappings) {
    List<LookupSingleRequestJson> ioListRequest = mappings.stream().map(mapping -> new LookupSingleRequestJson(mapping.getMedium(), mapping.getValue())).collect(Collectors.toList());
    HttpUriRequest req = RestClientUtils.post(cfg.getEndpoints().getIdentity().getBulk(), gson, "lookup", ioListRequest);
    try (CloseableHttpResponse res = client.execute(req)) {
        mappings = new ArrayList<>();
        int status = res.getStatusLine().getStatusCode();
        if (status < 200 || status >= 300) {
            return mappings;
        }
        LookupBulkResponseJson listIo = parser.parse(res, LookupBulkResponseJson.class);
        return listIo.getLookup().stream().map(io -> new ThreePidMapping(io.getMedium(), io.getAddress(), getMxId(io.getId()).getId())).collect(Collectors.toList());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) Logger(org.slf4j.Logger) ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) MatrixConfig(io.kamax.mxisd.config.MatrixConfig) LoggerFactory(org.slf4j.LoggerFactory) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) MatrixID(io.kamax.matrix.MatrixID) RestBackendConfig(io.kamax.mxisd.config.rest.RestBackendConfig) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) RestClientUtils(io.kamax.mxisd.util.RestClientUtils) Component(org.springframework.stereotype.Component) List(java.util.List) IThreePidProvider(io.kamax.mxisd.lookup.provider.IThreePidProvider) SingleLookupRequest(io.kamax.mxisd.lookup.SingleLookupRequest) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) UserID(io.kamax.mxisd.UserID) Optional(java.util.Optional) UserIdType(io.kamax.mxisd.UserIdType) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 4 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) Before(org.junit.Before)

Example 5 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;
        }
        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));
            }
            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) ThreePid(io.kamax.matrix.ThreePid) io.kamax.matrix._ThreePid(io.kamax.matrix._ThreePid) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID)

Aggregations

ThreePidMapping (io.kamax.mxisd.lookup.ThreePidMapping)8 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)3 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)2 MatrixConfig (io.kamax.mxisd.config.MatrixConfig)2 RestBackendConfig (io.kamax.mxisd.config.rest.RestBackendConfig)2 SingleLookupRequest (io.kamax.mxisd.lookup.SingleLookupRequest)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 UserRecord (com.google.firebase.auth.UserRecord)1 MatrixID (io.kamax.matrix.MatrixID)1 ThreePid (io.kamax.matrix.ThreePid)1 io.kamax.matrix._ThreePid (io.kamax.matrix._ThreePid)1 UserID (io.kamax.mxisd.UserID)1 UserIdType (io.kamax.mxisd.UserIdType)1 AuthenticatorProvider (io.kamax.mxisd.auth.provider.AuthenticatorProvider)1 BackendAuthResult (io.kamax.mxisd.auth.provider.BackendAuthResult)1 ClientBulkLookupRequest (io.kamax.mxisd.controller.identity.v1.ClientBulkLookupRequest)1 InternalServerError (io.kamax.mxisd.exception.InternalServerError)1 InvalidResponseJsonException (io.kamax.mxisd.exception.InvalidResponseJsonException)1 SingleLookupReply (io.kamax.mxisd.lookup.SingleLookupReply)1