Search in sources :

Example 1 with InternalServerError

use of io.kamax.mxisd.exception.InternalServerError 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("Base DN: {}", getBaseDn());
        log.debug("Query: {}", searchQuery);
        log.debug("Attributes: {}", GsonUtil.build().toJson(attArray));
        try (EntryCursor cursor = conn.search(getBaseDn(), 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.controller.directory.v1.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.controller.directory.v1.io.UserDirectorySearchResult) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 2 with InternalServerError

use of io.kamax.mxisd.exception.InternalServerError 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 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(new MatrixID(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) MatrixID(io.kamax.matrix.MatrixID) UserDirectorySearchResult(io.kamax.mxisd.controller.directory.v1.io.UserDirectorySearchResult) InternalServerError(io.kamax.mxisd.exception.InternalServerError)

Example 4 with InternalServerError

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

the class ClientDnsOverwrite method transform.

public URIBuilder transform(URI initial) {
    URIBuilder builder = new URIBuilder(initial);
    Entry mapping = mappings.get(initial.getHost());
    if (mapping == null) {
        throw new InternalServerError("No DNS client override for " + initial.getHost());
    }
    try {
        URL target = new URL(mapping.getValue());
        builder.setScheme(target.getProtocol());
        builder.setHost(target.getHost());
        if (target.getPort() != -1) {
            builder.setPort(target.getPort());
        }
        return builder;
    } catch (MalformedURLException e) {
        log.warn("Skipping DNS overwrite entry {} due to invalid value [{}]: {}", mapping.getName(), mapping.getValue(), e.getMessage());
        throw new ConfigurationException("Invalid DNS overwrite entry in homeserver client: " + mapping.getName(), e.getMessage());
    }
}
Also used : Entry(io.kamax.mxisd.config.DnsOverwriteConfig.Entry) MalformedURLException(java.net.MalformedURLException) ConfigurationException(io.kamax.mxisd.exception.ConfigurationException) InternalServerError(io.kamax.mxisd.exception.InternalServerError) URL(java.net.URL) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 5 with InternalServerError

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

the class EmailSmtpConnector method send.

@Override
public void send(String senderAddress, String senderName, String recipient, String content) {
    if (StringUtils.isBlank(senderAddress)) {
        throw new FeatureNotAvailable("3PID Email identity: sender address is empty - " + "You must set a value for notifications to work");
    }
    if (StringUtils.isBlank(content)) {
        throw new InternalServerError("Notification content is empty");
    }
    try {
        InternetAddress sender = new InternetAddress(senderAddress, senderName);
        MimeMessage msg = new MimeMessage(session, IOUtils.toInputStream(content, StandardCharsets.UTF_8));
        // FIXME set version
        msg.setHeader("X-Mailer", "mxisd");
        msg.setSentDate(new Date());
        msg.setFrom(sender);
        msg.setRecipients(Message.RecipientType.TO, recipient);
        msg.saveChanges();
        log.info("Sending invite to {} via SMTP using {}:{}", recipient, cfg.getHost(), cfg.getPort());
        SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
        transport.setStartTLS(cfg.getTls() > 0);
        transport.setRequireStartTLS(cfg.getTls() > 1);
        log.info("Connecting to {}:{}", cfg.getHost(), cfg.getPort());
        transport.connect(cfg.getHost(), cfg.getPort(), cfg.getLogin(), cfg.getPassword());
        try {
            transport.sendMessage(msg, InternetAddress.parse(recipient));
            log.info("Invite to {} was sent", recipient);
        } finally {
            transport.close();
        }
    } catch (UnsupportedEncodingException | MessagingException e) {
        throw new RuntimeException("Unable to send e-mail invite to " + recipient, e);
    }
}
Also used : FeatureNotAvailable(io.kamax.mxisd.exception.FeatureNotAvailable) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InternalServerError(io.kamax.mxisd.exception.InternalServerError) Date(java.util.Date) SMTPTransport(com.sun.mail.smtp.SMTPTransport)

Aggregations

InternalServerError (io.kamax.mxisd.exception.InternalServerError)10 IOException (java.io.IOException)6 UserDirectorySearchResult (io.kamax.mxisd.controller.directory.v1.io.UserDirectorySearchResult)5 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)3 MatrixID (io.kamax.matrix.MatrixID)2 UserDirectorySearchRequest (io.kamax.mxisd.controller.directory.v1.io.UserDirectorySearchRequest)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)2 CursorLdapReferralException (org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException)2 EntryCursor (org.apache.directory.api.ldap.model.cursor.EntryCursor)2 Entry (org.apache.directory.api.ldap.model.entry.Entry)2 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 URIBuilder (org.apache.http.client.utils.URIBuilder)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 SMTPTransport (com.sun.mail.smtp.SMTPTransport)1