Search in sources :

Example 1 with UserDirectorySearchResult

use of io.kamax.mxisd.http.io.UserDirectorySearchResult in project mxisd by kamax-io.

the class ExecDirectoryStore method search.

private UserDirectorySearchResult search(ExecConfig.Process cfg, UserDirectorySearchRequest request) {
    if (StringUtils.isEmpty(cfg.getCommand())) {
        return UserDirectorySearchResult.empty();
    }
    Processor<UserDirectorySearchResult> p = new Processor<>(cfg);
    p.addJsonInputTemplate(tokens -> new UserDirectorySearchRequest(tokens.getType(), tokens.getQuery()));
    p.addInputTemplate(PlainType, tokens -> tokens.getType() + System.lineSeparator() + tokens.getQuery());
    p.addTokenMapper(cfg.getToken().getType(), request::getBy);
    p.addTokenMapper(cfg.getToken().getQuery(), request::getSearchTerm);
    p.addSuccessMapper(JsonType, output -> {
        if (StringUtils.isBlank(output)) {
            return UserDirectorySearchResult.empty();
        }
        UserDirectorySearchResult response = GsonUtil.get().fromJson(output, UserDirectorySearchResult.class);
        for (UserDirectorySearchResult.Result result : response.getResults()) {
            result.setUserId(MatrixID.asAcceptable(result.getUserId(), mxCfg.getDomain()).getId());
        }
        return response;
    });
    p.withFailureDefault(output -> new UserDirectorySearchResult());
    return p.execute();
}
Also used : UserDirectorySearchRequest(io.kamax.mxisd.http.io.UserDirectorySearchRequest) UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult)

Example 2 with UserDirectorySearchResult

use of io.kamax.mxisd.http.io.UserDirectorySearchResult in project mxisd by kamax-io.

the class LdapDirectoryProvider method search.

protected UserDirectorySearchResult search(String query, List<String> attributes) {
    UserDirectorySearchResult result = new UserDirectorySearchResult();
    result.setLimited(false);
    try (LdapConnection conn = getConn()) {
        bind(conn);
        LdapConfig.Attribute atCfg = getCfg().getAttribute();
        attributes = new ArrayList<>(attributes);
        attributes.add(getUidAtt());
        String[] attArray = new String[attributes.size()];
        attributes.toArray(attArray);
        String searchQuery = buildOrQueryWithFilter(getCfg().getDirectory().getFilter(), "*" + query + "*", attArray);
        log.debug("Query: {}", searchQuery);
        log.debug("Attributes: {}", GsonUtil.build().toJson(attArray));
        for (String baseDN : getBaseDNs()) {
            log.debug("Base DN: {}", baseDN);
            try (EntryCursor cursor = conn.search(baseDN, searchQuery, SearchScope.SUBTREE, attArray)) {
                while (cursor.next()) {
                    Entry entry = cursor.get();
                    log.info("Found possible match, DN: {}", entry.getDn().getName());
                    getAttribute(entry, getUidAtt()).ifPresent(uid -> {
                        log.info("DN {} is a valid match", entry.getDn().getName());
                        try {
                            UserDirectorySearchResult.Result entryResult = new UserDirectorySearchResult.Result();
                            entryResult.setUserId(buildMatrixIdFromUid(uid));
                            getAttribute(entry, atCfg.getName()).ifPresent(entryResult::setDisplayName);
                            result.addResult(entryResult);
                        } catch (IllegalArgumentException e) {
                            log.warn("Bind was found but type {} is not supported", atCfg.getUid().getType());
                        }
                    });
                }
            }
        }
    } catch (CursorLdapReferralException e) {
        log.warn("An entry is only available via referral, skipping");
    } catch (IOException | LdapException | CursorException e) {
        throw new InternalServerError(e);
    }
    return result;
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) IOException(java.io.IOException) InternalServerError(io.kamax.mxisd.exception.InternalServerError) UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult) LdapConfig(io.kamax.mxisd.config.ldap.LdapConfig) 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) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 3 with UserDirectorySearchResult

use of io.kamax.mxisd.http.io.UserDirectorySearchResult in project mxisd by kamax-io.

the class ExecDirectoryStoreTest method byNameNoCommandDefined.

@Test
public void byNameNoCommandDefined() {
    ExecConfig cfg = getCfg();
    assertTrue(StringUtils.isEmpty(cfg.getDirectory().getSearch().getByName().getCommand()));
    ExecDirectoryStore store = getStore(cfg);
    UserDirectorySearchResult result = store.searchByDisplayName("user");
    assertFalse(result.isLimited());
    assertTrue(result.getResults().isEmpty());
}
Also used : ExecConfig(io.kamax.mxisd.config.ExecConfig) UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult) ExecDirectoryStore(io.kamax.mxisd.backend.exec.ExecDirectoryStore) Test(org.junit.Test)

Example 4 with UserDirectorySearchResult

use of io.kamax.mxisd.http.io.UserDirectorySearchResult in project mxisd by kamax-io.

the class ExecDirectoryStoreTest method byNameFailureNoOutput.

@Test
public void byNameFailureNoOutput() {
    UserDirectorySearchResult result = getStore(fno).searchByDisplayName("user");
    assertFalse(result.isLimited());
    assertTrue(result.getResults().isEmpty());
}
Also used : UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult) Test(org.junit.Test)

Example 5 with UserDirectorySearchResult

use of io.kamax.mxisd.http.io.UserDirectorySearchResult in project mxisd by kamax-io.

the class ExecDirectoryStoreTest method byNameSuccessSingleResult.

@Test
public void byNameSuccessSingleResult() {
    UserDirectorySearchResult output = getStore("byNameSuccessSingleResult").searchByDisplayName("user");
    assertFalse(output.isLimited());
    assertEquals(1, output.getResults().size());
    UserDirectorySearchResult.Result result = output.getResults().iterator().next();
    assertEquals(MatrixID.from(user1Localpart, domain).acceptable().getId(), result.getUserId());
    assertEquals(user1Name, result.getDisplayName());
}
Also used : UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult) Test(org.junit.Test)

Aggregations

UserDirectorySearchResult (io.kamax.mxisd.http.io.UserDirectorySearchResult)17 Test (org.junit.Test)9 InternalServerError (io.kamax.mxisd.exception.InternalServerError)5 UserDirectorySearchRequest (io.kamax.mxisd.http.io.UserDirectorySearchRequest)4 IOException (java.io.IOException)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 MatrixErrorInfo (io.kamax.matrix.MatrixErrorInfo)1 ExecDirectoryStore (io.kamax.mxisd.backend.exec.ExecDirectoryStore)1 ExecConfig (io.kamax.mxisd.config.ExecConfig)1 LdapConfig (io.kamax.mxisd.config.ldap.LdapConfig)1 HttpMatrixException (io.kamax.mxisd.exception.HttpMatrixException)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)1 CursorLdapReferralException (org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException)1