Reputation: 3742
I would like to authenticate against Azure to claim an access token and use it for the Graph API. So I've done my homework and started with this guide
https://learn.microsoft.com/en-us/graph/auth-v2-service
I was able to authenticate successfully. But the amount of code was huge. This is my own code that I wrote for the authentication:
If a scripts needs to access the current session token it just needs to call the getAuthenticationInfo
function. Due to the huge amount of code I was wondering if my code is redundant or maybe not correct.
A little bit of research brought me to MSAL.js which (I think) handles the authentication stuff for me. MSAL uses a different approach than the guide from the link I posted above. I started with this plain JS demo code (I only have a demo account so the credentials shouldn't matter)
const msal = require('msal');
async function start() {
const clientId = 'bec52b71-dc94-4577-9f8d-b8536ed0e73d';
const clientSecret = 'OV/NkBIWH7d3G/BGyJQN3vxQA]fT6qK@';
const tenant = '2c1714e1-1030-4da9-af5e-59630d7fe05f';
const scope = 'https://graph.microsoft.com/.default';
const grantType = 'client_credentials';
const msalConfig = {
auth: {
clientId
}
};
const msalInstance = new msal.UserAgentApplication(msalConfig);
const tokenRequest = {
scope,
};
try {
const tokenResponse = await msalInstance.acquireTokenSilent(tokenRequest);
const { accessToken } = tokenResponse;
console.log(accessToken);
} catch (error) {
throw error;
}
}
start();
I was wondering where I should put my login credentials here. But when I run the code I get the following error
ReferenceError: window is not defined
It seems that the code tries to access the browser storage. But my code is running in a Node environment. So would someone be so nice to tell me how I can transfer my code from the snippet which is based on the first link to the MSAL approach?
As far as I understood MSAL should do the trick for me, I don't need to write the authentication code myself and can shorten my own code.
Thanks in advance!
Update
Based on Allen Wus answer I tried out ADAL.js and this code seems to work fine
const { AuthenticationContext } = require('adal-node');
const clientId = 'bec52b71-dc94-4577-9f8d-b8536ed0e73d';
const clientSecret = 'OV/NkBIWH7d3G/BGyJQN3vxQA]fT6qK@';
const tenant = '2c1714e1-1030-4da9-af5e-59630d7fe05f';
const authorityHostUrl = 'https://login.windows.net';
const authorityUrl = `${authorityHostUrl}/${tenant}`;
const resource = 'https://graph.microsoft.com';
const context = new AuthenticationContext(authorityUrl);
context.acquireTokenWithClientCredentials(resource, clientId, clientSecret, (error, tokenResponse) => {
if (error) {
throw error;
}
console.log(tokenResponse);
});
The only thing I didn't get is what resource
is. Feel free to add a comment explaining it :)
Upvotes: 2
Views: 4102
Reputation: 16438
You get this error "ReferenceError: window is not defined" because browser thing doesn't exist on Node.
MSAL.js is designed to be used in client side JavaScript running in a web browser such as in case of single page apps. In another word, it does not support for nodejs currently.
For your console application, you may need to take a look at MSAL .Net.
Or you may be interested in ADAL for nodejs.
Upvotes: 3