I am building a node app with a express backend. One of the requirements is using Azure AD for authentication. I've installed the passport-azure-ad
module and have set it up as the following:
import * as passportAD from "passport-azure-ad";
// ... <snip> ....
const tenantName = "<MY_TENANT_NAME>"";
const clientID = "<MY_CLIENT_ID>";
app.use(passport.initialize());
app.use(passport.session());
const bearerStrategy = new passportAD.BearerStrategy(
{
identityMetadata: `https://login.microsoftonline.com/${tenantName}.onmicrosoft.com/.well-known/openid-configuration`,
clientID
},
(token: any, done: any) => {
console.log(token);
return done(null, {}, token);
}
);
passport.use(bearerStrategy);
Then I have added authorization to a route like this:
const myHandler = () => (req, res) => return res.json({});
app.get('/my/route',
passport.authenticate("oauth-bearer", { session: false }),
myHandler()
);
This is returning a 401 status as expected however, I haven't been able to find documentation on how to issue a token to a client from Azure AD. I'd like to accept a POST to a login endpoint with a username and password in the body and return a Azure AD token. Is this possible?
You can also do the following. I have recently implemented one with my react application with nodejs backend
You can find the key values for BearerStrategyOptions at https://github.com/AzureADQuickStarts/AppModelv2-WebAPI-nodejs/blob/master/node-server/config.js
Allow FYI I used the following common endpoint 'https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration for identityMetadata
You can find the key values for OIDCStrategyOptions at https://github.com/AzureADQuickStarts/AppModelv2-WebApp-OpenIDConnect-nodejs/blob/master/config.js
For Authentication:
For Authorization:
And to authorize the routes use the following code in your api
Done! Hope this helps :) for someone looking to use
passport-azure-ad
The only issuer of an Azure AD token is Azure AD. You should not collect username/password in your clients, and you should not accept them in your service.
Your client applications simply needs to use MSAL (or ADAL, or any OpenID Connect client library) to send the user to Azure AD, have them sign in, and in response get an access token for your API.
For example, if you client were a JavaScript single-page app, with MSAL for JavaScript you could do the following:
(Of course, you can do this for various other platforms.)
To
passport-azure-ad
module, about how the azure ad issue a token, you could refer to doc1 and doc2.Yes, it is possible. If you want to do this way, you could refer to here.