Search in sources :

Example 21 with InternalServerError

use of io.kamax.mxisd.exception.InternalServerError in project mxisd by kamax-io.

the class LdapProfileProvider method getDisplayName.

@Override
public Optional<String> getDisplayName(_MatrixID userId) {
    String uid = buildUidFromMatrixId(userId);
    log.info("Searching for display name of {}:", uid);
    try (LdapConnection conn = getConn()) {
        bind(conn);
        String searchQuery = buildOrQueryWithFilter(getCfg().getProfile().getFilter(), uid, getUidAtt());
        log.debug("Query: {}", searchQuery);
        for (String baseDN : getBaseDNs()) {
            log.debug("Base DN: {}", baseDN);
            try (EntryCursor cursor = conn.search(baseDN, searchQuery, SearchScope.SUBTREE, getAt().getName())) {
                while (cursor.next()) {
                    Entry entry = cursor.get();
                    log.info("Found possible match, DN: {}", entry.getDn().getName());
                    Optional<String> v = getAttribute(entry, getAt().getName()).flatMap(id -> {
                        log.info("DN {} is a valid match", entry.getDn().getName());
                        try {
                            return getAttribute(entry, getAt().getName());
                        } catch (IllegalArgumentException e) {
                            log.warn("Bind was found but type {} is not supported", getAt().getUid().getType());
                            return Optional.empty();
                        }
                    });
                    if (v.isPresent()) {
                        log.info("DN {} is the final match", entry.getDn().getName());
                        return v;
                    }
                }
            } catch (CursorLdapReferralException e) {
                log.warn("An entry is only available via referral, skipping");
            }
        }
    } catch (IOException | LdapException | CursorException e) {
        throw new InternalServerError(e);
    }
    return Optional.empty();
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) Entry(org.apache.directory.api.ldap.model.entry.Entry) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException) 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 22 with InternalServerError

use of io.kamax.mxisd.exception.InternalServerError in project mxisd by kamax-io.

the class LdapThreePidProvider method lookup.

private Optional<String> lookup(LdapConnection conn, String medium, String value) {
    Optional<String> tPidQueryOpt = getCfg().getIdentity().getQuery(medium);
    if (!tPidQueryOpt.isPresent()) {
        log.warn("{} is not a configured 3PID type for LDAP lookup", medium);
        return Optional.empty();
    }
    // we merge 3PID specific query with global/specific filter, if one exists.
    String tPidQuery = tPidQueryOpt.get().replaceAll(getCfg().getIdentity().getToken(), value);
    String searchQuery = buildWithFilter(tPidQuery, getCfg().getIdentity().getFilter());
    log.debug("Query: {}", searchQuery);
    log.debug("Attributes: {}", GsonUtil.build().toJson(getUidAtt()));
    for (String baseDN : getBaseDNs()) {
        log.debug("Base DN: {}", baseDN);
        try (EntryCursor cursor = conn.search(baseDN, searchQuery, SearchScope.SUBTREE, getUidAtt())) {
            while (cursor.next()) {
                Entry entry = cursor.get();
                log.info("Found possible match, DN: {}", entry.getDn().getName());
                Optional<String> data = getAttribute(entry, getUidAtt());
                if (!data.isPresent()) {
                    continue;
                }
                log.info("DN {} is a valid match", entry.getDn().getName());
                return Optional.of(buildMatrixIdFromUid(data.get()));
            }
        } catch (CursorLdapReferralException e) {
            log.warn("3PID {} is only available via referral, skipping", value);
        } catch (IOException | LdapException | CursorException e) {
            throw new InternalServerError(e);
        }
    }
    return Optional.empty();
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) Entry(org.apache.directory.api.ldap.model.entry.Entry) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) InternalServerError(io.kamax.mxisd.exception.InternalServerError)

Example 23 with InternalServerError

use of io.kamax.mxisd.exception.InternalServerError in project mxisd by kamax-io.

the class GenericSqlDirectoryProvider method search.

public UserDirectorySearchResult search(String searchTerm, GenericSqlProviderConfig.Query query) {
    try (Connection conn = pool.get()) {
        log.info("Will execute query: {}", query.getValue());
        try (PreparedStatement stmt = conn.prepareStatement(query.getValue())) {
            setParameters(stmt, searchTerm);
            try (ResultSet rSet = stmt.executeQuery()) {
                UserDirectorySearchResult result = new UserDirectorySearchResult();
                result.setLimited(false);
                while (rSet.next()) {
                    processRow(rSet).ifPresent(e -> {
                        if (StringUtils.equalsIgnoreCase("localpart", query.getType())) {
                            e.setUserId(MatrixID.asAcceptable(e.getUserId(), mxCfg.getDomain()).getId());
                        }
                        result.addResult(e);
                    });
                }
                return result;
            }
        }
    } catch (SQLException e) {
        throw new InternalServerError(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult) InternalServerError(io.kamax.mxisd.exception.InternalServerError)

Example 24 with InternalServerError

use of io.kamax.mxisd.exception.InternalServerError in project mxisd by kamax-io.

the class WordpressDirectoryProvider method search.

public UserDirectorySearchResult search(String searchTerm, String query) {
    try (Connection conn = wordpress.getConnection()) {
        log.info("Will execute query: {}", query);
        try (PreparedStatement stmt = conn.prepareStatement(query)) {
            setParameters(stmt, searchTerm);
            try (ResultSet rSet = stmt.executeQuery()) {
                UserDirectorySearchResult result = new UserDirectorySearchResult();
                result.setLimited(false);
                while (rSet.next()) {
                    processRow(rSet).ifPresent(e -> {
                        try {
                            e.setUserId(MatrixID.from(e.getUserId(), mxCfg.getDomain()).valid().getId());
                            result.addResult(e);
                        } catch (IllegalArgumentException ex) {
                            log.warn("Ignoring result {} - Invalid characters for a Matrix ID", e.getUserId());
                        }
                    });
                }
                return result;
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
        throw new InternalServerError(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult) InternalServerError(io.kamax.mxisd.exception.InternalServerError)

Aggregations

InternalServerError (io.kamax.mxisd.exception.InternalServerError)24 IOException (java.io.IOException)13 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)5 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)5 CursorLdapReferralException (org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException)5 EntryCursor (org.apache.directory.api.ldap.model.cursor.EntryCursor)5 Entry (org.apache.directory.api.ldap.model.entry.Entry)5 JsonObject (com.google.gson.JsonObject)4 ThreePid (io.kamax.matrix.ThreePid)4 UserDirectorySearchResult (io.kamax.mxisd.http.io.UserDirectorySearchResult)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)4 MatrixID (io.kamax.matrix.MatrixID)3 GsonUtil (io.kamax.matrix.json.GsonUtil)3