Search in sources :

Example 36 with NTCredentials

use of org.apache.http.auth.NTCredentials in project fess by codelibs.

the class DataConfig method initializeClientFactory.

@Override
public Map<String, Object> initializeClientFactory(final CrawlerClientFactory crawlerClientFactory) {
    final Map<String, String> paramMap = getHandlerParameterMap();
    final Map<String, Object> factoryParamMap = new HashMap<>();
    crawlerClientFactory.setInitParameterMap(factoryParamMap);
    // parameters
    for (final Map.Entry<String, String> entry : paramMap.entrySet()) {
        final String key = entry.getKey();
        if (key.startsWith(CRAWLER_PARAM_PREFIX)) {
            factoryParamMap.put(key.substring(CRAWLER_PARAM_PREFIX.length()), entry.getValue());
        }
    }
    // user agent
    final String userAgent = paramMap.get(CRAWLER_USERAGENT);
    if (StringUtil.isNotBlank(userAgent)) {
        factoryParamMap.put(HcHttpClient.USER_AGENT_PROPERTY, userAgent);
    }
    // web auth
    final String webAuthStr = paramMap.get(CRAWLER_WEB_AUTH);
    if (StringUtil.isNotBlank(webAuthStr)) {
        final String[] webAuthNames = webAuthStr.split(",");
        final List<Authentication> basicAuthList = new ArrayList<>();
        for (final String webAuthName : webAuthNames) {
            final String scheme = paramMap.get(CRAWLER_WEB_AUTH + "." + webAuthName + ".scheme");
            final String hostname = paramMap.get(CRAWLER_WEB_AUTH + "." + webAuthName + ".host");
            final String port = paramMap.get(CRAWLER_WEB_AUTH + "." + webAuthName + ".port");
            final String realm = paramMap.get(CRAWLER_WEB_AUTH + "." + webAuthName + ".realm");
            final String username = paramMap.get(CRAWLER_WEB_AUTH + "." + webAuthName + ".username");
            final String password = paramMap.get(CRAWLER_WEB_AUTH + "." + webAuthName + ".password");
            if (StringUtil.isEmpty(username)) {
                logger.warn("username is empty. webAuth:" + webAuthName);
                continue;
            }
            AuthScheme authScheme = null;
            if (Constants.BASIC.equals(scheme)) {
                authScheme = new BasicScheme();
            } else if (Constants.DIGEST.equals(scheme)) {
                authScheme = new DigestScheme();
            } else if (Constants.NTLM.equals(scheme)) {
                authScheme = new NTLMScheme(new JcifsEngine());
            }
            // TODO FORM
            AuthScope authScope;
            if (StringUtil.isBlank(hostname)) {
                authScope = AuthScope.ANY;
            } else {
                int p = AuthScope.ANY_PORT;
                if (StringUtil.isNotBlank(port)) {
                    try {
                        p = Integer.parseInt(port);
                    } catch (final NumberFormatException e) {
                        logger.warn("Failed to parse " + port, e);
                    }
                }
                String r = realm;
                if (StringUtil.isBlank(realm)) {
                    r = AuthScope.ANY_REALM;
                }
                String s = scheme;
                if (StringUtil.isBlank(scheme) || Constants.NTLM.equals(scheme)) {
                    s = AuthScope.ANY_SCHEME;
                }
                authScope = new AuthScope(hostname, p, r, s);
            }
            Credentials credentials;
            if (Constants.NTLM.equals(scheme)) {
                final String workstation = paramMap.get(CRAWLER_WEB_AUTH + "." + webAuthName + ".workstation");
                final String domain = paramMap.get(CRAWLER_WEB_AUTH + "." + webAuthName + ".domain");
                credentials = new NTCredentials(username, password == null ? StringUtil.EMPTY : password, workstation == null ? StringUtil.EMPTY : workstation, domain == null ? StringUtil.EMPTY : domain);
            } else {
                credentials = new UsernamePasswordCredentials(username, password == null ? StringUtil.EMPTY : password);
            }
            basicAuthList.add(new AuthenticationImpl(authScope, credentials, authScheme));
        }
        factoryParamMap.put(HcHttpClient.BASIC_AUTHENTICATIONS_PROPERTY, basicAuthList.toArray(new Authentication[basicAuthList.size()]));
    }
    // request header
    final List<org.codelibs.fess.crawler.client.http.RequestHeader> rhList = new ArrayList<>();
    int count = 1;
    String headerName = paramMap.get(CRAWLER_WEB_HEADER_PREFIX + count + ".name");
    while (StringUtil.isNotBlank(headerName)) {
        final String headerValue = paramMap.get(CRAWLER_WEB_HEADER_PREFIX + count + ".value");
        rhList.add(new org.codelibs.fess.crawler.client.http.RequestHeader(headerName, headerValue));
        count++;
        headerName = paramMap.get(CRAWLER_WEB_HEADER_PREFIX + count + ".name");
    }
    if (!rhList.isEmpty()) {
        factoryParamMap.put(HcHttpClient.REQUERT_HEADERS_PROPERTY, rhList.toArray(new org.codelibs.fess.crawler.client.http.RequestHeader[rhList.size()]));
    }
    // file auth
    final String fileAuthStr = paramMap.get(CRAWLER_FILE_AUTH);
    if (StringUtil.isNotBlank(fileAuthStr)) {
        final String[] fileAuthNames = fileAuthStr.split(",");
        final List<SmbAuthentication> smbAuthList = new ArrayList<>();
        final List<FtpAuthentication> ftpAuthList = new ArrayList<>();
        for (final String fileAuthName : fileAuthNames) {
            final String scheme = paramMap.get(CRAWLER_FILE_AUTH + "." + fileAuthName + ".scheme");
            if (Constants.SAMBA.equals(scheme)) {
                final String domain = paramMap.get(CRAWLER_FILE_AUTH + "." + fileAuthName + ".domain");
                final String hostname = paramMap.get(CRAWLER_FILE_AUTH + "." + fileAuthName + ".host");
                final String port = paramMap.get(CRAWLER_FILE_AUTH + "." + fileAuthName + ".port");
                final String username = paramMap.get(CRAWLER_FILE_AUTH + "." + fileAuthName + ".username");
                final String password = paramMap.get(CRAWLER_FILE_AUTH + "." + fileAuthName + ".password");
                if (StringUtil.isEmpty(username)) {
                    logger.warn("username is empty. fileAuth:" + fileAuthName);
                    continue;
                }
                final SmbAuthentication smbAuth = new SmbAuthentication();
                smbAuth.setDomain(domain == null ? StringUtil.EMPTY : domain);
                smbAuth.setServer(hostname);
                if (StringUtil.isNotBlank(port)) {
                    try {
                        smbAuth.setPort(Integer.parseInt(port));
                    } catch (final NumberFormatException e) {
                        logger.warn("Failed to parse " + port, e);
                    }
                }
                smbAuth.setUsername(username);
                smbAuth.setPassword(password == null ? StringUtil.EMPTY : password);
                smbAuthList.add(smbAuth);
            } else if (Constants.FTP.equals(scheme)) {
                final String hostname = paramMap.get(CRAWLER_FILE_AUTH + "." + fileAuthName + ".host");
                final String port = paramMap.get(CRAWLER_FILE_AUTH + "." + fileAuthName + ".port");
                final String username = paramMap.get(CRAWLER_FILE_AUTH + "." + fileAuthName + ".username");
                final String password = paramMap.get(CRAWLER_FILE_AUTH + "." + fileAuthName + ".password");
                if (StringUtil.isEmpty(username)) {
                    logger.warn("username is empty. fileAuth:" + fileAuthName);
                    continue;
                }
                final FtpAuthentication ftpAuth = new FtpAuthentication();
                ftpAuth.setServer(hostname);
                if (StringUtil.isNotBlank(port)) {
                    try {
                        ftpAuth.setPort(Integer.parseInt(port));
                    } catch (final NumberFormatException e) {
                        logger.warn("Failed to parse " + port, e);
                    }
                }
                ftpAuth.setUsername(username);
                ftpAuth.setPassword(password == null ? StringUtil.EMPTY : password);
                ftpAuthList.add(ftpAuth);
            }
        }
        if (!smbAuthList.isEmpty()) {
            factoryParamMap.put(SmbClient.SMB_AUTHENTICATIONS_PROPERTY, smbAuthList.toArray(new SmbAuthentication[smbAuthList.size()]));
        }
        if (!ftpAuthList.isEmpty()) {
            factoryParamMap.put(FtpClient.FTP_AUTHENTICATIONS_PROPERTY, ftpAuthList.toArray(new FtpAuthentication[ftpAuthList.size()]));
        }
    }
    return factoryParamMap;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AuthScheme(org.apache.http.auth.AuthScheme) NTCredentials(org.apache.http.auth.NTCredentials) SmbAuthentication(org.codelibs.fess.crawler.client.smb.SmbAuthentication) DigestScheme(org.apache.http.impl.auth.DigestScheme) BasicScheme(org.apache.http.impl.auth.BasicScheme) FtpAuthentication(org.codelibs.fess.crawler.client.ftp.FtpAuthentication) JcifsEngine(org.codelibs.fess.crawler.client.http.ntlm.JcifsEngine) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) AuthenticationImpl(org.codelibs.fess.crawler.client.http.impl.AuthenticationImpl) Authentication(org.codelibs.fess.crawler.client.http.Authentication) SmbAuthentication(org.codelibs.fess.crawler.client.smb.SmbAuthentication) FtpAuthentication(org.codelibs.fess.crawler.client.ftp.FtpAuthentication) NTLMScheme(org.apache.http.impl.auth.NTLMScheme) AuthScope(org.apache.http.auth.AuthScope) HashMap(java.util.HashMap) Map(java.util.Map) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 37 with NTCredentials

use of org.apache.http.auth.NTCredentials in project camel by apache.

the class ProxyHttpClientConfigurer method configureHttpClient.

public void configureHttpClient(HttpClientBuilder clientBuilder) {
    clientBuilder.setProxy(new HttpHost(host, port, scheme));
    if (username != null && password != null) {
        Credentials defaultcreds;
        if (domain != null) {
            defaultcreds = new NTCredentials(username, password, ntHost, domain);
        } else {
            defaultcreds = new UsernamePasswordCredentials(username, password);
        }
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, defaultcreds);
        clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) NTCredentials(org.apache.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 38 with NTCredentials

use of org.apache.http.auth.NTCredentials in project camel by apache.

the class BasicAuthenticationHttpClientConfigurer method configureHttpClient.

public void configureHttpClient(HttpClientBuilder clientBuilder) {
    Credentials defaultcreds;
    if (domain != null) {
        defaultcreds = new NTCredentials(username, password, host, domain);
    } else {
        defaultcreds = new UsernamePasswordCredentials(username, password);
    }
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, defaultcreds);
    clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) NTCredentials(org.apache.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 39 with NTCredentials

use of org.apache.http.auth.NTCredentials in project epp.mpc by eclipse.

the class SystemCredentialsProvider method getCredentials.

@Override
public Credentials getCredentials(AuthScope authscope) {
    Credentials credentials = super.getCredentials(authscope);
    if (credentials instanceof NTCredentials) {
        NTCredentials ntCredentials = (NTCredentials) credentials;
        if (ntCredentials.getDomain() == null || ntCredentials.getWorkstation() == null) {
            String domain = ntCredentials.getDomain();
            String userName = ntCredentials.getUserName();
            String workstation = ntCredentials.getWorkstation();
            String strippedUserName = HttpClientProxyUtil.getNTLMUserName(userName);
            if (domain == null || !strippedUserName.equals(userName)) {
                domain = HttpClientProxyUtil.getNTLMUserDomain(userName);
                if (domain != null) {
                    userName = strippedUserName;
                }
            }
            if (workstation == null) {
                workstation = HttpClientProxyUtil.getNTLMWorkstation();
            }
            credentials = new NTCredentials(userName, ntCredentials.getPassword(), workstation, domain);
        }
    }
    return credentials;
}
Also used : NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) NTCredentials(org.apache.http.auth.NTCredentials)

Example 40 with NTCredentials

use of org.apache.http.auth.NTCredentials in project epp.mpc by eclipse.

the class HttpClientProxyUtil method proxyAuthentication.

public static Executor proxyAuthentication(Executor executor, URI uri) throws IOException {
    IProxyData proxy = ProxyHelper.getProxyData(uri);
    if (proxy != null) {
        HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort());
        String proxyUserID = proxy.getUserId();
        if (proxyUserID != null) {
            String domainUserID = getNTLMUserName(proxyUserID);
            String password = proxy.getPassword();
            String domain = getNTLMUserDomain(proxyUserID);
            if (domain != null || !proxyUserID.equals(domainUserID)) {
                String workstation = getNTLMWorkstation();
                executor.auth(new AuthScope(proxyHost, AuthScope.ANY_REALM, "ntlm"), new NTCredentials(domainUserID, password, workstation, domain));
            }
            return executor.auth(new AuthScope(proxyHost, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME), new UsernamePasswordCredentials(proxyUserID, password));
        }
    }
    return executor;
}
Also used : IProxyData(org.eclipse.core.net.proxy.IProxyData) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) NTCredentials(org.apache.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

NTCredentials (org.apache.http.auth.NTCredentials)63 AuthScope (org.apache.http.auth.AuthScope)41 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)33 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)30 CredentialsProvider (org.apache.http.client.CredentialsProvider)27 Credentials (org.apache.http.auth.Credentials)23 HttpHost (org.apache.http.HttpHost)22 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)11 RequestConfig (org.apache.http.client.config.RequestConfig)9 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)9 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)9 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)9 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 LaxRedirectStrategy (org.apache.http.impl.client.LaxRedirectStrategy)5 HttpRequestExecutor (org.apache.http.protocol.HttpRequestExecutor)5 AuthSchemeProvider (org.apache.http.auth.AuthSchemeProvider)4 AuthenticationException (org.apache.http.auth.AuthenticationException)4 InvalidCredentialsException (org.apache.http.auth.InvalidCredentialsException)4 BufferedHeader (org.apache.http.message.BufferedHeader)4