Friday, February 19, 2016

Working with npm/bower/git behind a NTLM authenticating proxy server.

Working with development tools like maven, npm, and bower on Windows behind a proxy server, which only supports NTLM authentication has always been a cause of head aches for me. For the solution I am about to describe, I can only thank the creators of cntlm at source forge (http://cntlm.sourceforge.net/), who must surely really know what they are doing.

In essence, the solution is to install cntlm locally and divert the development tools' requests through it rather than sending directly to the proxy server. 

The quickest way to get started is to unzip cntlm and configure the cntlm.ini file in the application's home directry in the appropriate places with the following

/-----------------------------------------------------------------------------------------------------/
Username YOUR_USERNAME
Domain YOUR_DOMAIN
Password

PassNTLMv2 01213D7806A61B78FB3BA894CAA4BA9D

Proxy t-web-gw.farnet.net:8080
Listen 8082

Auth NTLMv2
/-----------------------------------------------------------------------------------------------------/
Be sure to leave  the "Password" setting blank.

To generate the hashed credentials and get the appropriate Auth value, highlighted in yellow above and below, run the program as follows and enter in your normal login password when prompted:
>cntlm -c cntlm.ini  -I -M http://www.google.de
Password:
Config profile  1/4... OK (HTTP code: 200)
----------------------------[ Profile  0 ]------
Auth            NTLMv2
PassNTLMv2      01213D7806A61B78FB3BA894CAA4BA9D
------------------------------------------------

Now your settings, with lots of logging to the console, can be tested by running the server in the forground as follows:
>cntlm -c cntlm.ini  -f -v

If it's npm that you would like to use, then look for a file in your home directory called: ".npmrc" .  Here you should add
proxy = http://localhost:8082/
https-proxy = http://localhost:8082/

You can also use nplm itself:
npm config set proxy http://localhost:8082:8080
npm config set https-proxy localhost:8082:8080

For bower, add them to the .bowerrc file in the project's folder or in whichever folder you ran bower init
{
"proxy":"http://localhost:8082",
"https-proxy":"http://localhost:8082"
}

If you still get errors like below, when trying to execute "bower -update":
bower ECMDERR Failed to execute "git ls-remote --tags --heads git://github
.com/angular/bower-angular.git", exit code of #128 fatal: unable to access 'http
s://github.com/angular/bower-angular.git/': Received HTTP code 407 from proxy after CONNECT

Then check your credentials again by using cmtlm as previously shown above:
>cntlm -c cntlm.ini  -I -M http://www.google.de

Adding the "url.https://.insteadof" to the git settings, as suggested in many places, with
git config --add url.https://.insteadof git:// 
will probably not help.