title: Authenticating with on-premise (IFD) CRM using NTLM authentication from Web App (Express.js)
body: <p>I'm asking this question on the back of a <a href="https://stackoverflow.com/questions/49763453/how-to-query-an-on-premise-dynamics-crm-from-a-web-app-node-express">previous question</a> I raised, as the scope of the question has changed somewhat but that might be worth reading first for background info.</p>
  
  <p>I'm trying to programatically obtain data out of our Dynamics CRM instance, using a single set of admin credentials within a Node powered Express app. This Express app is hosted on a separate server outside of our network where CRM is hosted. The app will then request, process and serve CRM data back to any logged in user who has access (controlled by roles/permissions within the app), meaning an end user only has to login into the Express app.</p>
  
  <p>From my web browser, if I visit our on-premise CRM endpoint: <code>https://my.crm.endpoint</code>, I get prompted for a username and password.</p>
  
  <p>If I provide correct credentials, I am authenticated and have full access to the CRM, allowing me to query the API. </p>
  
  <p><strong>Example</strong>
  <a href="https://my.crm.endpoint/api/data/v8.2/contacts" rel="nofollow noreferrer">https://my.crm.endpoint/api/data/v8.2/contacts</a>?$select=fullname,contactid</p>
  
  <p>This returns a lovely JSON object containing all the data I want :)</p>
  
  <p>NOW! Under the covers, I can see that it is using NTLM to authenticate, of which I have little knowledge :/ Having read up a little and watched a few YouTube videos, I have a <strong><em>basic</em></strong> understanding of the challenge/response mechanism but I'm still unsure as to how to proceed.</p>
  
  <p>NB: I have read <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa378749(v=vs.85).aspx" rel="nofollow noreferrer">this from Microsoft</a> which describes the mechanism, but doesn't give any specific examples. I don't even know what hashing algorithm should be used, or what headers to set etc. </p>
  
  <p><strong>Question</strong> Can anyone provide any sort of detail as to how I can authenticate with our CRM using NTLM from a Web App (Express in my case)?</p>
  
  <p>Steps I can see the browser making...</p>
  
  <ol>
  <li>Visit <code>https://my.crm.endpoint</code></li>
  <li>302 Redirected to: <code>https://fs.our.domain/adfs/ls/?wa=wsignin1.0&amp;wtrealm=https%3a%2f%2fmy.crm.endpoint%2f&amp;wctx=rm%3d1%26id%3dfaf0791c-6a3a-4c4e-9e69-9dfa8fd4c2e8%26ru%3d%252fdefault.aspx&amp;wct=2018-04-20T10%3a12%3a37Z&amp;wauth=urn%3afederation%3aauthentication%3awindows</code></li>
  <li>Prompted for user credentials</li>
  <li>** enter credentials**</li>
  <li><em>A whole bunch of stuff happens here and I get a little lost but looks like it gets a couple of 401's then a POST is made to the <code>https://my.crm.endpoint</code>. Another 302 is shown, then finally a GET to the actual <code>default.aspx</code> page.</em></li>
  <li>I then have access to CRM.</li>
  </ol>
  
  <p><strong><em>NB: Once authenticated, I can see three cookies that have been set and which are sent when querying the api example above. These cookies are MSISAuth, MSISAuth1 and ReClientId.</em></strong></p>
  
  <p>If I'm missing any crucial info, please let me know and I'll provide what I can!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have just installed httpntlm module and attempted to authenticate using this...</p>
  
  <pre><code>let httpntlm = require('httpntlm');
  httpntlm.get({
      url: 'https://my.crm.endpoint',
      username: '&lt;my.email@address.com&gt;',
      password: '&lt;mypassword&gt;',
      workstation: '',  // unsure what to put here if anything?
      domain: ''        // unsure what to put here if anything?
  }, function (err, res){
      if(err) return err;
  
      console.log(res.headers);
      console.log(res.body);
  });
  </code></pre>
  
  <p>The response I get is this...</p>
  
  <pre><code>{ location: 'https://fs.our.domain/adfs/ls/?wa=wsignin1.0&amp;wtrealm=https%3a%2f%2fmy.crm.endpoint%2f&amp;wctx=rm%3d1%26id%3d93a4c6fd-5b17-4a2b-965f-07af5e96b08f%26ru%3d%252fdefault.aspx&amp;wct=2018-04-20T14%3a08%3a00Z&amp;wauth=urn%3afederation%3aauthentication%3awindows',
    server: 'Microsoft-IIS/8.5',
    req_id: '298acefc-53aa-46fa-96c4-e5d8762b1fd2',
    'x-powered-by': 'ASP.NET',
    date: 'Fri, 20 Apr 2018 14:08:00 GMT',
    connection: 'close',
    'content-length': '397' }
  &lt;html&gt;&lt;head&gt;&lt;title&gt;Object moved&lt;/title&gt;&lt;/head&gt;&lt;body&gt;
  &lt;h2&gt;Object moved to &lt;a href="https://fs.our.domain/adfs/ls/?wa=wsignin1.0&amp;wtrealm=https%3a%2f%2fmy.crm.endpoint%2f&amp;wctx=rm%3d1%26id%3d93a4c6fd-5b17-4a2b-965f-07af5e96b08f%26ru%3d%252fdefault.aspx&amp;wct=2018-04-20T14%3a08%3a00Z&amp;wauth=urn%3afederation%3aauthentication%3awindows"&gt;here&lt;/a&gt;.&lt;/h2&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>Anyone able to shed any light on what I actually need to be doing?! :-/</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Following @markgamache comment, and having read the suggested docs, we are indeed using WS-Fed! As the <code>Wa=signin1.0</code> parameter informs the browser to pop up a login box, does this make it impossible to achieve this programmatically, without additional user interaction?</p>