question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Authenticate to Azure SDK for JavaScript using oAuth token from Azure AD

See original GitHub issue
  1. I’m writing a vanilla JavaScript application using Express.js and Node.js version 14.10.
  2. I’m using the Microsoft Authentication Library (MSAL) for JS to log into Azure Active Directory

Question

How do I authenticate to Azure Resource Management APIs using an oAuth token that I received from Azure Active Directory (AAD)? I can’t find any constructor to feed the oAuth token into, so I can obtain a ServiceCredentials object.

All of the examples are useless, because they show how to perform an “interactive login,” which isn’t what I want.

I already have a valid oAuth token. How do I use it in the Azure SDK for JS?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
daviwilcommented, Sep 16, 2020

Hey Trevor, long time no see!

@ramya-rao-a’s most recent suggestion is the simplest way to go, you’ll want to create a simple class or object that implements ServiceClientCredentials to wrap the token you get back from MSAL. Here’s a full example of how you’d do that in JavaScript:

const { HttpHeaders } = require("@azure/ms-rest-js");

function getCredentialForToken(accessToken) {
  return {
    signRequest: (request) => {
      if (!request.headers) request.headers = new HttpHeaders();
      request.headers.set("Authorization", `Bearer ${accessToken}`);
      return Promise.resolve(request);
    }
  };
}

const creds = getCredentialForToken("your token from MSAL");

You can then pass creds into the constructor of the Azure SDK client library you’re using.

0reactions
ramya-rao-acommented, Sep 21, 2020

Looks like that solution worked for you @pcgeek86, thanks for reporting the issue.

@daviwil, As discussed offline, can you update the readme for @azure/ms-rest-nodeauth to include a note on the above solution if the one already has an access token? Logged https://github.com/Azure/ms-rest-nodeauth/issues/103

Read more comments on GitHub >

github_iconTop Results From Across the Web

Azure Identity client library for JavaScript | Microsoft Learn
The Azure Identity library provides Azure Active Directory (Azure AD) token authentication through a set of convenient TokenCredential ...
Read more >
Authenticate Using Microsoft with JavaScript | Firebase - Google
You can let your users authenticate with Firebase using OAuth providers like Microsoft Azure Active Directory by integrating generic OAuth Login into your ......
Read more >
Azure Identity SDK (JS) How to Authenticate to User's Azure ...
Using access token, you can retrieve user's info or access any other resource in Azure tenant. The InteractiveBrowserCredential uses ...
Read more >
Authenticating to Azure services with Azure AD and MSAL.js
As more and more services in Azure support modern & managed authentication using Azure Active Directory, you may begin to wonder, ...
Read more >
Microsoft Authentication Library for JavaScript (MSAL.js)
through Azure AD B2C service. It also enables your app to get tokens to access Microsoft Cloud services such as Microsoft Graph. npm...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found