
    ----------
    NTLM support
    ----------
    ----------
    ----------

NTLM support in HtmlUnit

    As {{{http://hc.apache.org/httpcomponents-client/ntlm.html}HttpClient does not provide support for NTLM authentication scheme out of the box}}, so does not HtmlUnit.

* {Using Samba JCIFS as an NTLM engine}

    Follow these instructions to build an NTLMEngine implementation using JCIFS library

    <<!!!!DISCLAIMER !!!! HttpComponents and HtmlUnit projects DO _NOT_ SUPPORT the code provided below. 
    Use it as is at your own discretion>>.

    * Download the latest release of the JCIFS library from the 
    {{{http://jcifs.samba.org/}Samba}} web site

    * Implement NTLMEngine interface

----------------------------------------
import jcifs.ntlmssp.Type1Message;
import jcifs.ntlmssp.Type2Message;
import jcifs.ntlmssp.Type3Message;
import jcifs.util.Base64;

import org.apache.http.impl.auth.NTLMEngine;
import org.apache.http.impl.auth.NTLMEngineException;

public class JCIFSEngine implements NTLMEngine {

    public String generateType1Msg(
            String domain, 
            String workstation) throws NTLMEngineException {

        Type1Message t1m = new Type1Message(Type1Message.getDefaultFlags(), domain, workstation);
        return Base64.encode(t1m.toByteArray());
    }

    public String generateType3Msg(
            String username, 
            String password, 
            String domain,
            String workstation, 
            String challenge) throws NTLMEngineException {
        Type2Message t2m;
        try {
            t2m = new Type2Message(Base64.decode(challenge));
        } catch (IOException ex) {
            throw new NTLMEngineException("Invalid Type2 message", ex);
        }
        Type3Message t3m = new Type3Message(t2m, password, domain, username, workstation, 0);
        return Base64.encode(t3m.toByteArray());
    }
}
----------------------------------------

    * Implement AuthSchemeFactory interface

----------------------------------------
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeFactory;
import org.apache.http.impl.auth.NTLMScheme;
import org.apache.http.params.HttpParams;

public class NTLMSchemeFactory implements AuthSchemeFactory {

    public AuthScheme newInstance(final HttpParams params) {
        return new NTLMScheme(new JCIFSEngine());
    }
}
----------------------------------------

    * Register NTLMSchemeFactory with the HttpClient instance.

----------------------------------------
webClient.setWebConnection(new HttpWebConnection(webClient) {

    protected synchronized AbstractHttpClient getHttpClient() {
        AbstractHttpClient httpClient = super.getHttpClient();
        httpClient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
        return httpClient;
    }
});
----------------------------------------

    * Set NTLM Credentials for the web server you are going to access.

----------------------------------------
DefaultCredentialsProvider credentialProvider =
    (DefaultCredentialsProvider) webClient.getCredentialsProvider();
credentialProvider.addNTLMCredentials("username", "password", "myServer", -1, "myStation", "myDomain");
-----------------------------------------------------------

    * You are done.


* {Why this code is not distributed with HttpClient} 

    JCIFS is licensed under the Lesser General Public License (LGPL). This license 
    is not compatible with the Apache Licenses under which all Apache Software is 
    released.
