Vanilla JavaScript single-page application (SPA) using MSAL.js to authorize users for calling a protected web API on Azure AD B2C
- Overview
- Scenario
- Contents
- Prerequisites
- Setup
- Registration
- Running the sample
- Explore the sample
- About the code
- More information
- Community Help and Support
- Contributing
- Code of Conduct
This sample demonstrates a Vanilla JavaScript single-page application that lets users authenticate against Azure Active Directory B2C using the Microsoft Authentication Library for JavaScript (MSAL.js), then acquires an Access Token for a web API that is also protected by Azure AD B2C. In doing so, it also illustrates various authorization and B2C concepts, such as Access Tokens, Refresh Tokens, Token Lifetimes and Configuration, silent requests and more.
- The client application uses the MSAL.js to obtain an Access Token from Azure AD B2C.
- The Access Token is used as a bearer to authorize the user to call a protected web API.
- The protected web API responds with the claims in the Access Token.
File/folder | Description |
---|---|
SPA/public/authPopup.js |
Main authentication logic resides here (using Popup flow). |
SPA/public/authRedirect.js |
Use this instead of authPopup.js for authentication with redirect flow. |
SPA/public/authConfig.js |
Contains configuration parameters for the sample. |
SPA/public/policies.js |
Contains B2C custom policies and user-flows. |
API/index.js |
Main application logic resides here. |
API/authConfig.js |
Contains authentication parameters for the sample. |
- Node.js must be installed to run this sample.
- A modern web browser. This sample uses ES6 conventions and will not run on Internet Explorer.
- Visual Studio Code is recommended for running and editing this sample.
- VS Code Azure Tools extension is recommended for interacting with Azure through VS Code Interface.
- An Azure Active Directory B2C (Azure AD B2C) tenant. For more information on how to get an Azure AD tenant, see: Create an Azure Active Directory B2C tenant
- A user account in your Azure AD B2C tenant.
From your shell or command line:
git clone https://github.com/Azure-Samples/ms-identity-javascript-tutorial.git
or download and extract the repository .zip file.
⚠️ To avoid path length limitations on Windows, we recommend cloning into a directory near the root of your drive.
cd ms-identity-javascript-tutorial
cd 3-Authorization-II/2-call-api-b2c
cd API
npm install
cd..
cd SPA
npm install
ℹ️ This sample comes with a pre-registered application for testing purposes. If you would like to use your own Azure AD B2C tenant and application, follow the steps below to register and configure the applications in the Azure Portal. Otherwise, continue with the steps for Running the sample.
As a first step you'll need to:
-
Sign in to the Azure portal.
-
If your account is present in more than one Azure AD B2C tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory to change your portal session to the desired Azure AD B2C tenant.
Please refer to: Tutorial: Create userflows in Azure Active Directory B2C
⚠️ This sample requires B2C user-flows to emit the emails claim in the ID token, which is used as username by MSAL. To do so, navigate to the Azure portal and locate the Azure AD B2C service. Then, navigate to the User flows blade. Select the User Attributes tab and make sure Email Address is checked. Then select the Application Claims tab and make sure Email Addresses is checked.You may want additional claims (such as object ID (oid) and etc.) to appear in the ID tokens obtained from Azure AD B2C user-flows. In that case, please refer to User profile attributes to learn about how to configure your user-flows to emit those claims.
Please refer to: Tutorial: Add identity providers to your applications in Azure Active Directory B2C
- Navigate to the Azure portal and select the Azure Active Directory B2C service.
- Select the App Registrations blade on the left, then select New registration.
- In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
msal-node-api
. - Under Supported account types, select Accounts in any identity provider or organizational directory (for authenticating users with user flows)
- Select Register to create the application.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- In the Overview blade, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
- In the app's registration screen, select the Expose an API blade to the left to open the page where you can publish the permission as an API for which client applications can obtain access tokens for. The first thing that we need to do is to declare the unique resource URI that the clients will be using to obtain access tokens for this API. To declare an resource URI(Application ID URI), follow the following steps:
- Select Set next to the Application ID URI to generate a URI that is unique for this app.
- For this sample, accept the proposed Application ID URI (
https://{tenantName}.onmicrosoft.com/{clientId}
) by selecting Save.
- All APIs must publish a minimum of one scope, also called Delegated Permission, for the client apps to obtain an access token for a user successfully. To publish a scope, follow these steps:
- Select Add a scope button open the Add a scope screen and Enter the values as indicated below:
- For Scope name, use
ToDoList.Read
. - For Admin consent display name type in Read users ToDo list using the 'msal-node-api'.
- For Admin consent description type in Allow the app to read the user's ToDo list using the 'msal-node-api'.
- Keep State as Enabled.
- Select the Add scope button on the bottom to save this scope.
Repeat the steps above for another scope named ToDoList.ReadWrite
- For Scope name, use
- Select the Manifest blade on the left.
- Set
accessTokenAcceptedVersion
property to 2. - Select on Save.
- Set
ℹ️ Follow the principle of least privilege when publishing permissions for a web API.
Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
- Open the
API\authConfig.json
file. - Find the key
clientID
and replace the existing value with the application ID (clientId) ofmsal-node-api
app copied from the Azure portal. - Find the key
tenantID
and replace the existing value with your Azure AD tenant/directory ID. - Find the key
policyName
and replace the existing value with Enter_The_Your_policy_Name.
- Navigate to the Azure portal and select the Azure Active Directory B2C service.
- Select the App Registrations blade on the left, then select New registration.
- In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
msal-javascript-spa
. - Under Supported account types, select Accounts in any identity provider or organizational directory (for authenticating users with user flows)
- Select Register to create the application.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- In the Overview blade, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
- In the app's registration screen, select the Authentication blade to the left.
- If you don't have a platform added, select Add a platform and select the Single-page application option.
- In the Redirect URI section enter the following redirect URIs:
http://localhost:3000
http://localhost:3000/redirect
- Click Save to save your changes.
- In the Redirect URI section enter the following redirect URIs:
- Since this app signs-in users, we will now proceed to select delegated permissions, which is is required by apps signing-in users.
- In the app's registration screen, select the API permissions blade in the left to open the page where we add access to the APIs that your application needs:
- Select the Add a permission button and then:
- Ensure that the My APIs tab is selected.
- In the list of APIs, select the API
msal-node-api
. - In the Delegated permissions section, select ToDoList.Read, ToDoList.ReadWrite in the list. Use the search box if necessary.
- Select the Add permissions button at the bottom.
- At this stage, the permissions are assigned correctly, but since it's a B2C tenant, the users themselves cannot consent to these permissions. To get around this problem, we'd let the tenant administrator consent on behalf of all users in the tenant. Select the Grant admin consent for {tenant} button, and then select Yes when you are asked if you want to grant consent for the requested permissions for all accounts in the tenant. You need to be a tenant admin to be able to carry out this operation.
Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
- Open the
SPA\public\authConfig.js
file. - Find the key
Enter_the_Application_Id_Here
and replace the existing value with the application ID (clientId) ofmsal-javascript-spa
app copied from the Azure portal. - Find the key
Enter_the_Tenant_Info_Here
and replace the existing value with your Azure AD tenant/directory ID. - Find the key
Enter_the_Web_Api_Scope_Here
and replace the existing value with Scope. - Find the key
policyName
and replace the existing value with Enter_The_Your_policy_Name. - Find the key
b2cDomain
and replace the existing value with Enter_The_Tenant_Domain_name. - Find the key
b2cPolicies.names
and replace it with the names (IDs) of your policies/user-flows e.g.b2c_1_susi
. - Find the key
b2cPolicies.authorities
abd replace it with the authority strings of your policies/user-flows e.g.https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_susi
. - Find the key
b2cPolicies.authorityDomain
abd replace it with the domain of your authority e.g.fabrikamb2c.b2clogin.com
.
cd ms-identity-javascript-tutorial
cd 3-Authorization-II/2-call-api-b2c
cd API
npm start
cd..
cd SPA
npm start
- Open your browser and navigate to
http://localhost:6420
. - Click on the sign-in button on the top right corner.
- Once you authenticate, click the Call API button at the center.
Were we successful in addressing your learning objective? Consider taking a moment to share your experience with us..
Access Token requests in MSAL.js are meant to be per-resource-per-scope(s). This means that an Access Token requested for resource A with scope scp1
:
- cannot be used for accessing resource A with scope
scp2
, and, - cannot be used for accessing resource B of any scope.
The intended recipient of an Access Token is represented by the aud
claim; in case the value for the aud
claim does not mach the resource APP ID URI, the token should be considered invalid. Likewise, the permissions that an Access Token grants is represented by the scp
claim. See Access Token claims for more information.
MSAL.js exposes 3 APIs for acquiring a token: acquireTokenPopup()
, acquireTokenRedirect()
and acquireTokenSilent()
:
myMSALObj.acquireTokenPopup(request)
.then(response => {
// do something with response
})
.catch(error => {
console.log(error)
});
For acquireTokenRedirect()
, you must register a redirect promise handler:
myMSALObj.handleRedirectPromise()
.then(response => {
// do something with response
})
.catch(error => {
console.log(error);
});
myMSALObj.acquireTokenRedirect(request);
For the purpose of the sample, cross-origin resource sharing (CORS) is enabled for all domains and methods, using the Express.js cors middleware. This is insecure and only used for demonstration purposes here. In production, you should modify this as to allow only the domains that you designate. If your web API is going to be hosted on Azure App Service, we recommend configuring CORS on the App Service itself. This is illustrated in app.js:
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
On the web API side, passport-azure-ad verifies the incoming access token's signature and validates it's payload against the issuer
and audience
claims (defined in BearerStrategy
constructor) using the passport.authenticate()
API. In the BearerStrategy
callback, you can add further validation steps as shown below (see app.js):
const options = {
identityMetadata: `https://${authConfig.metadata.b2cDomain}/${authConfig.credentials.tenantName}/${authConfig.policies.policyName}/${authConfig.metadata.version}/${authConfig.metadata.discovery}`,
clientID: authConfig.credentials.clientID,
audience: authConfig.credentials.clientID,
policyName: authConfig.policies.policyName,
isB2C: authConfig.settings.isB2C,
validateIssuer: authConfig.settings.validateIssuer,
loggingLevel: authConfig.settings.loggingLevel,
passReqToCallback: authConfig.settings.passReqToCallback,
};
const bearerStrategy = new passportAzureAd.BearerStrategy(options, (token, done) => {
/**
* Below you can do extended token validation and check for additional claims, such as:
* - check if the delegated permissions in the 'scp' are the same as the ones declared in the application registration.
*
* Bear in mind that you can do any of the above checks within the individual routes and/or controllers as well.
* For more information, visit: https://learn.microsoft.com/en-us/azure/active-directory-b2c/tokens-overview
*/
/**
* Lines below verifies if the caller's client ID is in the list of allowed clients.
* This ensures only the applications with the right client ID can access this API.
* To do so, we use "azp" claim in the access token. Uncomment the lines below to enable this check.
*/
// const myAllowedClientsList = [
// /* add here the client IDs of the applications that are allowed to call this API */
// ]
// if (!myAllowedClientsList.includes(token.azp)) {
// return done(new Error('Unauthorized'), {}, "Client not allowed");
// }
/**
* Access tokens that have no 'scp' (for delegated permissions).
*/
if (!token.hasOwnProperty('scp')) {
return done(new Error('Unauthorized'), null, 'No delegated permissions found');
}
done(null, {}, token);
});
app.use(passport.initialize());
passport.use(bearerStrategy);
Clients should treat access tokens as opaque strings, as the contents of the token are intended for the resource only (such as a web API or Microsoft Graph). For validation and debugging purposes, developers can decode JWTs (JSON Web Tokens) using a site like jwt.ms.
Controllers should check if the presented access token has the necessary permissions to access the data, depending on the type of permission. This is illustrated in todolist.js:
exports.getTodo = (req, res, next) => {
if (hasRequiredDelegatedPermissions(req.authInfo, authConfig.protectedRoutes.todolist.delegatedPermissions.read)) {
try {
const id = req.params.id;
const todo = db.get('todos').find({ id: id }).value();
res.status(200).send(todo);
} catch (error) {
next(error);
}
} else {
next(new Error('User does not have the required permissions'));
}
};
When granting access to data based on scopes, be sure to follow the principle of least privilege.
Configure your application:
- Use Microsoft Authentication Library for JavaScript to work with Azure AD B2C
- Tutorial: Create an Azure Active Directory B2C tenant
- Single sign-on with MSAL.js
- Handle MSAL.js exceptions and errors
- Logging in MSAL.js applications
Learn more about Microsoft identity platform and Azure AD B2C:
- Microsoft identity platform (Azure Active Directory for developers)
- Overview of Microsoft Authentication Library (MSAL)
- What is Azure Active Directory B2C?
- Azure AD B2C User Flows
- Azure AD B2C Custom Policies
For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
Use Stack Overflow to get support from the community.
Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.
Make sure that your questions or comments are tagged with [azure-ad
azure-ad-b2c
ms-identity
msal
].
If you find a bug in the sample, please raise the issue on GitHub Issues.
To provide a recommendation, visit the following User Voice page.
If you'd like to contribute to this sample, see CONTRIBUTING.MD.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.