Search in sources :

Example 31 with NTCredentials

use of org.apache.http.auth.NTCredentials in project hale by halestudio.

the class ClientProxyUtil method createCredentials.

/**
 * Create a credentials object.
 *
 * @param user the user name
 * @param password the password
 * @return the created credentials
 */
public static Credentials createCredentials(String user, String password) {
    Credentials credentials;
    int sepIndex = user.indexOf('\\');
    if (sepIndex > 0 && sepIndex + 1 < user.length()) {
        // assume this is DOMAIN \ user for NTLM authentication
        String userName = user.substring(sepIndex + 1);
        String domain = user.substring(0, sepIndex);
        String workstation = null;
        credentials = new NTCredentials(userName, password, workstation, domain);
    } else {
        credentials = new UsernamePasswordCredentials(user, password);
    }
    return credentials;
}
Also used : 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 32 with NTCredentials

use of org.apache.http.auth.NTCredentials in project coprhd-controller by CoprHD.

the class WinRMTarget method createHttpClientContext.

protected HttpClientContext createHttpClientContext() {
    HttpClientContext httpClientContext = HttpClientContext.create();
    // Build the credential provider. Note that the credentials are using NTCredentials class which is a derived class of UserPasswordCredentials
    // This is specifically needed for NTLM authentication.
    // NTCredentials requires user name in the format "user" and NOT "domain\\user"
    String[] tokens = StringUtils.split(getUsername(), "\\", 2);
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(tokens.length > 1 ? tokens[1] : getUsername(), getPassword(), System.getProperty("hostname"), tokens.length > 1 ? tokens[0] : null));
    httpClientContext.setCredentialsProvider(credsProvider);
    httpClientContext.setTargetHost(new HttpHost(getHost()));
    return httpClientContext;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) NTCredentials(org.apache.http.auth.NTCredentials)

Example 33 with NTCredentials

use of org.apache.http.auth.NTCredentials in project crawler4j by yasserg.

the class PageFetcher method addNtCredentials.

/**
 * Do NT auth for Microsoft AD sites.
 */
private void addNtCredentials(NtAuthInfo authInfo, Map<AuthScope, Credentials> credentialsMap) {
    logger.info("NT authentication for: {}", authInfo.getLoginTarget());
    try {
        Credentials credentials = new NTCredentials(authInfo.getUsername(), authInfo.getPassword(), InetAddress.getLocalHost().getHostName(), authInfo.getDomain());
        credentialsMap.put(new AuthScope(authInfo.getHost(), authInfo.getPort()), credentials);
    } catch (UnknownHostException e) {
        logger.error("Error creating NT credentials", e);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) AuthScope(org.apache.http.auth.AuthScope) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.http.auth.NTCredentials)

Example 34 with NTCredentials

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

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)

Example 35 with NTCredentials

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

the class AuthManager method setupCredentials.

/**
     * Configure credentials and auth scheme on client if an authorization is 
     * available for url
     * @param client {@link HttpClient}
     * @param url URL to test 
     * @param credentialsProvider {@link CredentialsProvider}
     * @param localHost host running JMeter
     */
public void setupCredentials(HttpClient client, URL url, CredentialsProvider credentialsProvider, String localHost) {
    Authorization auth = getAuthForURL(url);
    if (auth != null) {
        String username = auth.getUser();
        String realm = auth.getRealm();
        String domain = auth.getDomain();
        if (log.isDebugEnabled()) {
            log.debug(username + " > D=" + domain + " R=" + realm + " M=" + auth.getMechanism());
        }
        if (Mechanism.KERBEROS.equals(auth.getMechanism())) {
            ((AbstractHttpClient) client).getAuthSchemes().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(isStripPort(url)));
            credentialsProvider.setCredentials(new AuthScope(null, -1, null), USE_JAAS_CREDENTIALS);
        } else {
            credentialsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort(), realm.length() == 0 ? null : realm), new NTCredentials(username, auth.getPass(), localHost, domain));
        }
    }
}
Also used : AuthScope(org.apache.http.auth.AuthScope) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) 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