Search in sources :

Example 41 with NTCredentials

use of org.apache.http.auth.NTCredentials in project webdrivermanager by bonigarcia.

the class HttpClient method createBasicCredentialsProvider.

private final Optional<BasicCredentialsProvider> createBasicCredentialsProvider(String proxy, String proxyUser, String proxyPass, HttpHost proxyHost) throws MalformedURLException, UnsupportedEncodingException {
    Optional<URL> proxyUrl = determineProxyUrl(proxy);
    if (!proxyUrl.isPresent()) {
        return empty();
    }
    String username = null;
    String password = null;
    // apply env value
    String userInfo = proxyUrl.get().getUserInfo();
    if (userInfo != null) {
        StringTokenizer st = new StringTokenizer(userInfo, ":");
        username = st.hasMoreTokens() ? decode(st.nextToken(), UTF_8.name()) : null;
        password = st.hasMoreTokens() ? decode(st.nextToken(), UTF_8.name()) : null;
    }
    String envProxyUser = getenv("HTTPS_PROXY_USER");
    String envProxyPass = getenv("HTTPS_PROXY_PASS");
    username = (envProxyUser != null) ? envProxyUser : username;
    password = (envProxyPass != null) ? envProxyPass : password;
    // apply option value
    username = (proxyUser != null) ? proxyUser : username;
    password = (proxyPass != null) ? proxyPass : password;
    if (username == null) {
        return empty();
    }
    String ntlmUsername = username;
    String ntlmDomain = null;
    int index = username.indexOf('\\');
    if (index > 0) {
        ntlmDomain = username.substring(0, index);
        ntlmUsername = username.substring(index + 1);
    }
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    AuthScope authScope = new AuthScope(proxyHost.getHostName(), proxyHost.getPort(), ANY_REALM, NTLM);
    Credentials creds = new NTCredentials(ntlmUsername, password, getWorkstation(), ntlmDomain);
    credentialsProvider.setCredentials(authScope, creds);
    authScope = new AuthScope(proxyHost.getHostName(), proxyHost.getPort());
    creds = new UsernamePasswordCredentials(username, password);
    credentialsProvider.setCredentials(authScope, creds);
    return Optional.of(credentialsProvider);
}
Also used : StringTokenizer(java.util.StringTokenizer) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthScope(org.apache.http.auth.AuthScope) URL(java.net.URL) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 42 with NTCredentials

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

the class UrlDownloaderUsingNtlmCredentials method init.

@PostConstruct
public void init(Map<String, String> props) throws UnknownHostException {
    String user = null;
    String ntDomain = null;
    final String userProp = PREFIX + "user";
    final String fullyQualifiedUser = props.get(userProp);
    if (fullyQualifiedUser != null) {
        final List<String> userParts = Splitter.on(slash()).splitToList(fullyQualifiedUser);
        ntDomain = userParts.get(0);
        user = userParts.get(1);
    }
    String password = props.get(PREFIX + "password");
    host = props.get(PREFIX + "host");
    if (user != null && password != null && ntDomain != null && host != null) {
        workstation = InetAddress.getLocalHost().getHostName();
        // thread-safe according to HTTP Client
        httpclient = HttpClientBuilder.create().build();
        // implementations are required to be thread-safe, apparently
        credsProvider = new BasicCredentialsProvider();
        // immutable, so okay to reuse
        final NTCredentials credentials = new NTCredentials(user, password, workstation, ntDomain);
        credsProvider.setCredentials(AuthScope.ANY, credentials);
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) NTCredentials(org.apache.http.auth.NTCredentials) PostConstruct(javax.annotation.PostConstruct)

Example 43 with NTCredentials

use of org.apache.http.auth.NTCredentials in project iaf by ibissource.

the class WebServiceNtlmSender method configure.

@Override
public void configure() throws ConfigurationException {
    super.configure();
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, getTimeout());
    HttpConnectionParams.setSoTimeout(httpParameters, getTimeout());
    httpClient = new DefaultHttpClient(connectionManager, httpParameters);
    httpClient.getAuthSchemes().register("NTLM", new NTLMSchemeFactory());
    CredentialFactory cf = new CredentialFactory(getAuthAlias(), getUsername(), getPassword());
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new NTCredentials(cf.getUsername(), cf.getPassword(), Misc.getHostname(), getAuthDomain()));
    if (StringUtils.isNotEmpty(getProxyHost())) {
        HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) NTCredentials(org.apache.http.auth.NTCredentials)

Example 44 with NTCredentials

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

the class NTLMProxyHandle method applyProxy.

@Override
public void applyProxy(AbstractAjaxCallback<?, ?> cb, HttpRequest request, DefaultHttpClient client) {
    if (!isIntranet(cb.getUrl())) {
        if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(user)) {
            AQUtility.debug("ntlm token");
            client.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
            client.getCredentialsProvider().setCredentials(new AuthScope(host, port), new NTCredentials(user, password, host, domain));
            cb.proxy(host, port);
        }
    }
}
Also used : AuthScope(org.apache.http.auth.AuthScope) NTCredentials(org.apache.http.auth.NTCredentials)

Example 45 with NTCredentials

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

the class NTLMScheme method authenticate.

public Header authenticate(final Credentials credentials, final HttpRequest request) throws AuthenticationException {
    NTCredentials ntcredentials = null;
    try {
        ntcredentials = (NTCredentials) credentials;
    } catch (ClassCastException e) {
        throw new InvalidCredentialsException("Credentials cannot be used for NTLM authentication: " + credentials.getClass().getName());
    }
    String response = null;
    if (this.state == State.CHALLENGE_RECEIVED || this.state == State.FAILED) {
        response = this.engine.generateType1Msg(ntcredentials.getDomain(), ntcredentials.getWorkstation());
        this.state = State.MSG_TYPE1_GENERATED;
    } else if (this.state == State.MSG_TYPE2_RECEVIED) {
        response = this.engine.generateType3Msg(ntcredentials.getUserName(), ntcredentials.getPassword(), ntcredentials.getDomain(), ntcredentials.getWorkstation(), this.challenge);
        this.state = State.MSG_TYPE3_GENERATED;
    } else {
        throw new AuthenticationException("Unexpected state: " + this.state);
    }
    CharArrayBuffer buffer = new CharArrayBuffer(32);
    if (isProxy()) {
        buffer.append(AUTH.PROXY_AUTH_RESP);
    } else {
        buffer.append(AUTH.WWW_AUTH_RESP);
    }
    buffer.append(": NTLM ");
    buffer.append(response);
    return new BufferedHeader(buffer);
}
Also used : InvalidCredentialsException(org.apache.http.auth.InvalidCredentialsException) AuthenticationException(org.apache.http.auth.AuthenticationException) BufferedHeader(org.apache.http.message.BufferedHeader) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) NTCredentials(org.apache.http.auth.NTCredentials)

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