The Problem

There are a couple of ways that we can authenticate with a CRM deployment: Active Directory (AD), Claims, and OAuth. Finding a single library that can easily handle all three in Node.js was a bit of a challenge. Installing multiple libraries, with different methods of usage, is too much hassle for such a straightforward purpose, in my opinion.

The research

To connect to CRM using AD, I tried httpntlm library, and it worked right away without any issues. However, i faced a lot of issues with OAuth; mainly caused by outdated guides on the Internet.
Previously, I worked mainly with Google’s APIs when it came to OAuth, so it was new territory for me working with Azure. So the first step I did was to try the well-known ADAL.js library. It worked fine in a web app with a proper front end, but in my case, I wanted to connect server-to-server. Try as I might, it never worked, as expected as nearly all guides assume there is an interface for the user.
Stumbling upon this article by Lucas: link, I went back to the basics by opting to do it by hand using basic Node built-in functions.

The Solutions

I followed his steps for the most part, but I wanted to simplify things even more by automatically acquiring the tokenendpoint. The endpoint requires the tenant ID, which can be found by accessing https://login.windows.net/{xyz.onmicrosoft.com}/.well-known/openid-configuration, after replacing xyz.onmicrosoft.com with the domain of the users.
I will add the option to specify the tenant ID in the configuration, for when the above causes an issue I didn’t foresee.
Using the knowledge acquired above, I created a Node.js library that can do the intended job, for AD and OAuth at least.

Installation

Run the following command in your app’s CLI:

Copy to Clipboard

Leave A Comment

Categories
Table of Contents

Share this article