React single-page application calling Node.js web API using Security Groups to implement Role-Based Access Control
- Overview
- Scenario
- Contents
- Prerequisites
- Setup
- Registration
- Running the sample
- Explore the sample
- About the code
- More information
- Community Help and Support
- Contributing
This sample demonstrates a cross-platform application suite involving an React single-page application (TodoListSPA) calling an Node.js web API (TodoListAPI) secured with the Microsoft identity platform. In doing so, it implements Role-based Access Control (RBAC) by using Azure AD Security Groups.
Access control in Azure AD can also be done with App Roles, as shown in the previous tutorial. Groups and App Roles in Azure AD are by no means mutually exclusive - they can be used in tandem to provide even finer grained access control.
In the sample, a dashboard component allows signed-in users to see the tasks assigned to them or other users based on their memberships to one of the two security groups, GroupAdmin and GroupMember.
- The TodoListSPA uses MSAL React to authenticate a user with the Microsoft identity platform.
- The app then obtains an access token from Azure Active Directory (Azure AD) on behalf of the authenticated user for the TodoListAPI.
- TodoListAPI uses passport-azure-ad to protect its endpoint and accept only authorized calls.
File/folder | Description |
---|---|
SPA/src/authConfig.js |
Authentication parameters for SPA project reside here. |
SPA/src/components/RouteGuard.jsx |
This component protects other components that require a user to be in a group. |
SPA/src/pages/Overage.jsx |
This component handles overage scenarios. |
SPA/src/index.js |
MSAL React is initialized here. |
API/authConfig.json |
Authentication parameters for web API project. |
API/utils/guard.js |
Custom middleware protecting app routes that require a user to be in a group. |
API/utils/overage.js |
Custom middleware for handling overage scenarios. |
API/app.js |
passport-azure-ad is initialized here. |
- An Azure AD tenant. For more information see: How to get an Azure AD tenant
- At least two user accounts in your Azure AD tenant.
Using a command line interface such as VS Code integrated terminal, follow the steps below:
git clone https://github.com/Azure-Samples/ms-identity-javascript-react-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-react-tutorial
cd 5-AccessControl/2-call-api-groups/API
npm install
cd ../
cd SPA
npm install
There are two projects in this sample. Each needs to be registered separately in your Azure AD tenant. To register these projects, you can:
- either follow the steps below for manual registration,
- or use PowerShell scripts that:
- automatically creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you.
- modify the configuration files.
Expand this section if you want to use this automation:
-
On Windows, run PowerShell as Administrator and navigate to the root of the cloned directory
-
In PowerShell run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
-
Run the script to create your Azure AD application and configure the code of the sample application accordingly.
-
In PowerShell run:
cd .\AppCreationScripts\ .\Configure.ps1
Other ways of running the scripts are described in App Creation Scripts The scripts also provide a guide to automated application registration, configuration and removal which can help in your CI/CD scenarios.
- Navigate to the Azure portal and select the Azure AD 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 this organizational directory only.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- Select Register to create the application.
- In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
- Select Save to save your changes.
- In the app's registration screen, select the Certificates & secrets blade in the left to open the page where we can generate secrets and upload certificates.
- In the Client secrets section, select New client secret:
- Type a key description (for instance
app secret
), - Select one of the available key durations (In 1 year, In 2 years, or Never Expires) as per your security posture.
- The generated key value will be displayed when you select the Add button. Copy the generated value for use in the steps later.
- You'll need this key later in your code's configuration files. This key value will not be displayed again, and is not retrievable by any other means, so make sure to note it from the Azure portal before navigating to any other screen or blade.
- Type a key description (for instance
- 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 Microsoft APIs tab is selected.
- In the Commonly used Microsoft APIs section, select Microsoft Graph
- In the Delegated permissions section, select the User.Read, GroupMember.Read.All in the list. Use the search box if necessary.
- Select the Add permissions button at the bottom.
- GroupMember.Read.All requires admin to consent. Select the Grant/revoke 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 account in the tenant. You need to be an Azure AD tenant admin to do this.
- Select the Add a permission button and then,
- In the app's registration screen, select the Expose an API blade to the left to open the page where you can declare the parameters to expose this app 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, 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 (
api://{clientId}
) by selecting Save.
- Select
- All APIs have to publish a minimum of one scope for the client's to obtain an access token successfully. To publish a scope, follow the following steps:
- Select Add a scope button open the Add a scope screen and Enter the values as indicated below:
- For Scope name, use
access_as_user
. - Select Admins and users options for Who can consent?.
- For Admin consent display name type
Access msal-node-api
. - For Admin consent description type
Allows the app to access msal-node-api as the signed-in user.
- For User consent display name type
Access msal-node-api
. - For User consent description type
Allow the application to access msal-node-api on your behalf.
- Keep State as Enabled.
- Select the Add scope button on the bottom to save this scope.
- For Scope name, use
- Select Add a scope button open the Add a scope screen and Enter the values as indicated below:
- On the right side menu, select the
Manifest
blade.- Set
accessTokenAcceptedVersion
property to 2. - Click on Save.
- Set
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 app key
ClientId
and replace the existing value with the application ID (clientId) of themsal-node-api
application copied from the Azure portal. - Find the app key
TenantId
and replace the existing value with your Azure AD tenant ID. - Find the app key
ClientSecret
and replace the existing value with the key you saved during the creation of themsal-node-api
app, in the Azure portal.
- Navigate to the Azure portal and select the Azure AD 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-react-spa
. - Under Supported account types, select Accounts in this organizational directory only.
- In the Redirect URI (optional) section, select Single-page application in the combo-box and enter the following redirect URI:
http://localhost:3000/
.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- Select Register to create the application.
- In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
- Select Save to save your changes.
- 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 Microsoft APIs tab is selected.
- In the Commonly used Microsoft APIs section, select Microsoft Graph
- In the Delegated permissions section, select the User.Read, GroupMember.Read.All in the list. Use the search box if necessary.
- Select the Add permissions button at the bottom.
- GroupMember.Read.All requires admin to consent. Select the Grant/revoke 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 account in the tenant. You need to be an Azure AD tenant admin to do this.
- Select the Add a permission button and then:
⚠️ The next step requires you to go back to your msal-node-api registration.
- Now you need to leave the registration for msal-react-spa and go back to your app registration for msal-node-api.
- From the app's Overview page, select the Manifest section.
- Find the entry for
KnownClientApplications
, and add the Application (client) ID of themsal-react-spa
application copied from the Azure portal. i.e.KnownClientApplications: [ "your_client_id_for_TodoListSPA" ]
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\src\authConfig.js
file. - Find the string
Enter_the_Application_Id_Here
and replace the existing value with the application ID (clientId) of the msal-react-spa application copied from the Azure portal. - Find the string
Enter_the_Tenant_Info_Here
and replace the existing value with your Azure AD tenant ID. - Find the string
Enter_the_Web_Api_Scope_here
and replace the existing value with the scope you created earlierapi://{clientId-of-service}/access_as_user
.
You have two different options available to you on how you can further configure your application(s) to receive the groups
claim.
- Receive all the groups that the signed-in user is assigned to in an Azure AD tenant, included nested groups.
- Receive the groups claim values from a filtered set of groups that your application is programmed to work with (Not available in the Azure AD Free edition).
To get the on-premise group's
samAccountName
orOn Premises Group Security Identifier
instead of Group ID, please refer to the document Configure group claims for applications with Azure Active Directory.
Configure your application to receive all the groups the signed-in user is assigned to, including nested groups
- In the app's registration screen, select the Token Configuration blade in the left to open the page where you can configure the claims provided tokens issued to your application.
- Select the Add groups claim button on top to open the Edit Groups Claim screen.
- Select
Security groups
or theAll groups (includes distribution lists but not groups assigned to the application)
option. Choosing both negates the effect ofSecurity Groups
option. - Under the ID section, select
Group ID
. This will result in Azure AD sending the Object ID of the groups the user is assigned to in the groups claim of the ID Token that your app receives after signing-in a user.
Configure your application to receive the groups
claim values from a filtered set of groups a user may be assigned to
- This option is useful when your application is interested in a selected set of groups that a signing-in user may be assigned to and not every security group this user is assigned to in the tenant. This option also saves your application from running into the overage issue.
- This feature is not available in the Azure AD Free edition.
- Nested group assignments are not available when this option is utilized.
- In the app's registration screen, select the Token Configuration blade in the left to open the page where you can configure the claims provided tokens issued to your application.
- Select the Add groups claim button on top to open the Edit Groups Claim screen.
- Select
Groups assigned to the application
.- Choosing additional options like
Security Groups
orAll groups (includes distribution lists but not groups assigned to the application)
will negate the benefits your app derives from choosing to use this option.
- Choosing additional options like
- Under the ID section, select
Group ID
. This will result in Azure AD sending the Object ID of the groups the user is assigned to in thegroups
claim of the ID Token that your app receives after signing-in a user. - If you are exposing a web API using the Expose an API option, then you can also choose the
Group ID
option under the Access section. This will result in Azure AD sending the Object ID of the groups the user is assigned to in thegroups
claim of the Access Token issued to the client applications of your API. - In the app's registration screen, select on the Overview blade in the left to open the Application overview screen. Select the hyperlink with the name of your application in Managed application in local directory (note this field title can be truncated for instance
Managed application in ...
). When you select this link you will navigate to the Enterprise Application Overview page associated with the service principal for your application in the tenant where you created it. You can navigate back to the app registration page by using the back button of your browser. - Select the Users and groups blade in the left to open the page where you can assign users and groups to your application.
- Select the Add user button on the top row.
- Select User and Groups from the resultant screen.
- Choose the groups that you want to assign to this application.
- Click Select in the bottom to finish selecting the groups.
- Select Assign to finish the group assignment process.
- Your application will now receive these selected groups in the
groups
claim when a user signing in to your app is a member of one or more these assigned groups.
- Select the Properties blade in the left to open the page that lists the basic properties of your application.Set the User assignment required? flag to Yes.
💡 Important security tip
You can configure Azure AD to make sure that only users assigned to your application in the Users and groups blade are able to sign-in to your app. To enable this, follow the instructions here to set User assignment required? to Yes. You can assign users directly or by assigning security groups they belong to.
⚠️ During Token Configuration, if you have chosen any other option except groupID (e.g. like DNSDomain\sAMAccountName) you should enter the group name (for examplecontoso.com\Test Group
) instead of the object ID below:
- Open the
SPA\src\authConfig.js
file. - Find the string
Enter_the_Object_Id_of_GroupAdmin_Group_Here
and replace the existing value with the object ID of the GroupAdmin group copied from the Azure portal. - Find the string
Enter_the_Object_Id_of_GroupMember_Here
and replace the existing value with the object ID of the GroupMember group copied from the Azure portal.
⚠️ During Token Configuration, if you have chosen any other option except groupID (e.g. like DNSDomain\sAMAccountName) you should enter the group name (for examplecontoso.com\Test Group
) instead of the object ID below:
- Open the
API\authConfig.json
file. - Find the string
Enter_the_Object_Id_of_GroupAdmin_Group_Here
and replace the existing value with the object ID of the GroupAdmin group copied from the Azure portal. - Find the string
Enter_the_Object_Id_of_GroupMember_Here
and replace the existing value with the object ID of the GroupMember group copied from the Azure portal.
Using a command line interface such as VS Code integrated terminal, locate the application directory. Then:
cd ../
cd msal-react-spa
npm start
In a separate console window, execute the following commands:
cd msal-node-api
npm start
- Open your browser and navigate to
http://localhost:3000
. - Sign-in using the button on top-right:
- Click on the TodoList button to access your (the signed-in user's) todo list:
- If the signed-in user has the right privileges (i.e. in the right "group"), click on the Dashboard button to access every users' todo list:
- If the signed-in user does not have the right privileges, clicking on the Dashboard will give an error:
ℹ️ Consider taking a moment to share your experience with us
Much of the specifics of implementing RBAC with Security Groups is the same with implementing RBAC with App Roles discussed in the previous tutorial. In order to avoid redundancy, here we discuss particular issues that might arise with using groups claim.
To ensure that the token size doesn’t exceed HTTP header size limits, the Microsoft Identity Platform limits the number of object Ids that it includes in the groups claim.
If a user is member of more groups than the overage limit (150 for SAML tokens, 200 for JWT tokens, 6 for single-page applications using implicit flow), then the Microsoft Identity Platform does not emit the group IDs in the groups
claim in the token. Instead, it includes an overage claim in the token that indicates to the application to query the MS Graph API to retrieve the user’s group membership.
We strongly advise you use the group filtering feature (if possible) to avoid running into group overages.
- You can use the
BulkCreateGroups.ps1
provided in the App Creation Scripts folder to create a large number of groups and assign users to them. This will help test overage scenarios during development.⚠️ Remember to change the user's objectId provided in theBulkCreateGroups.ps1
script. - When you run this sample and an overage occurred, then you'd see the
_claim_names
in the home page after the user signs-in. - We strongly advise you use the group filtering feature (if possible) to avoid running into group overages.
- In case you cannot avoid running into group overage, we suggest you use the following logic to process groups claim in your token.
- Check for the claim
_claim_names
with one of the values beinggroups
. This indicates overage. - If found, make a call to the endpoint specified in
_claim_sources
to fetch user’s groups. - If none found, look into the
groups
claim for user’s groups.
- Check for the claim
When attending to overage scenarios, which requires a call to Microsoft Graph to read the signed-in user's group memberships, your app will need to have the GroupMember.Read.All for the getMemberObjects function to execute successfully.
Developers who wish to gain good familiarity of programming for Microsoft Graph are advised to go through the An introduction to Microsoft Graph for developers recorded session.
The client application React SPA has a RouteGuard component that checks whether a user has the right privileges to access a protected route. In case of overage, we are checking whether the token for the user has the _claim_names
claim, which indicates that the user has too many group memberships.
export const RouteGuard = ({ Component, ...props }) => {
const { instance } = useMsal();
const [isAuthorized, setIsAuthorized] = useState(false);
const [isOveraged, setIsOveraged] = useState(false);
const onLoad = async () => {
if (props.location.state) {
let intersection = props.groups
.filter(group => props.location.state.groupsData.includes(group));
if (intersection.length > 0) {
setIsAuthorized(true);
}
} else {
const currentAccount = instance.getActiveAccount();
if (currentAccount && currentAccount.idTokenClaims['groups']) {
let intersection = props.groups
.filter(group => currentAccount.idTokenClaims['groups'].includes(group));
if (intersection.length > 0) {
setIsAuthorized(true);
}
} else if (currentAccount && (currentAccount.idTokenClaims['_claim_names'] || currentAccount.idTokenClaims['_claim_sources'])) {
window.alert('You have too many group memberships. The application will now query Microsoft Graph to get the full list of groups that you are a member of.');
setIsOveraged(true);
}
}
}
useEffect(() => {
onLoad();
}, [instance]);
return (
<>
{
isAuthorized
?
<Route {...props} render={routeProps => <Component {...routeProps} />} />
:
isOveraged
?
<Redirect
to={{
pathname: "/overage",
state: { origin: props.location.pathname }
}}
/>
:
<div className="data-area-div">
<h3>You are unauthorized to view this content.</h3>
</div>
}
</>
);
};
If the overage occurs, we redirect the user to the /overage
page. There, we initiate a call to MS Graph API's https://graph.microsoft.com/v1.0/me/memberOf
endpoint to query the full list of groups that the user belongs to. Finally we check for the designated groupID
among this list.
const OverageContent = () => {
const { inProgress } = useMsal();
const [groupsData, setGroupsData] = useState([]);
const handleNextPage = (nextPage) => {
getNextPage(nextPage).then((response) => {
response.value.map((v) => {
if (!groupsData.includes(v.id)) {
setGroupsData((gr) => [...gr, v.id]);
}
});
if (response['@odata.nextLink']) {
handleNextPage(response['@odata.nextLink'])
}
})
}
useEffect(() => {
if (groupsData.length === 0 && inProgress === InteractionStatus.None) {
getGroups().then(response => {
response.value.map((v) => {
if (!groupsData.includes(v.id)) {
setGroupsData((gr) => [...gr, v.id]);
}
});
/**
* Some queries against Microsoft Graph return multiple pages of data either due to server-side paging
* or due to the use of the $top query parameter to specifically limit the page size in a request.
* When a result set spans multiple pages, Microsoft Graph returns an @odata.nextLink property in
* the response that contains a URL to the next page of results. Learn more at https://docs.microsoft.com/graph/paging
*/
if (response['@odata.nextLink']) {
handleNextPage(response['@odata.nextLink']);
}
});
}
}, [inProgress]);
return (
<>
{ groupsData ? <GraphQuery groupsData={groupsData} /> : null}
</>
);
};
export const Overage = () => {
const authRequest = {
...loginRequest
};
return (
<MsalAuthenticationTemplate
interactionType={InteractionType.Redirect}
authenticationRequest={authRequest}
>
<OverageContent />
</MsalAuthenticationTemplate>
)
};
Similar to the React RouteGuard component above, in Node.js routeGuard middleware we are checking whether the token for the user has the _claim_names
claim, which indicates that the user has too many group memberships and thus overage occurs.
const routeGuard = (accessMatrix) => {
return async (req, res, next) => {
if (req.authInfo.groups === undefined) {
if (req.authInfo['_claim_names'] || req.authInfo['_claim_sources']) {
return handleOverage(req, res, next);
} else {
return res.status(403).json({ error: 'No group claim found!' });
}
} else {
// check for group IDs
}
next();
}
}
module.exports = routeGuard;
If overage occurs, we initiate a call to MS Graph API's https://graph.microsoft.com/v1.0/me/memberOf
endpoint to query the full list of groups that the user belongs to. Finally we check for the designated groupID
among this list.
To do this, we are using MSAL Node's acquireTokenOnBehalf
API, as we are querying MS Graph on-behalf-of the user that is trying to access our web API here:
const msalConfig = {
auth: {
clientId: config.credentials.clientID,
authority: `https://${config.metadata.authority}/${config.credentials.tenantID}`,
clientSecret: config.credentials.clientSecret
},
system: {
loggerOptions: {
loggerCallback(loglevel, message, containsPii) {
console.log(message);
},
piiLoggingEnabled: false,
logLevel: msal.LogLevel.Info,
}
}
};
// Create msal application object
const cca = new msal.ConfidentialClientApplication(msalConfig);
const getOboToken = async (oboAssertion) => {
const oboRequest = {
oboAssertion: oboAssertion,
scopes: config.protectedResources.graphAPI.scopes,
}
try {
const response = await cca.acquireTokenOnBehalfOf(oboRequest);
return response.accessToken;
} catch (error) {
console.log(error);
return error;
}
}
const callGraph = async (oboToken, endpoint) => {
const options = {
headers: {
Authorization: `Bearer ${oboToken}`
}
};
console.log('request made to web API at: ' + new Date().toString());
try {
const response = await axios.default.get(endpoint, options);
return response.data;
} catch (error) {
console.log(error)
return error;
}
}
const handlePagination = async (oboToken, nextPage, userGroups) => {
try {
const graphResponse = await callGraph(oboToken, nextPage);
graphResponse.value.map((v) => userGroups.push(v.id));
if (graphResponse['@odata.nextLink']) {
return await handlePagination(accessToken, graphResponse['@odata.nextLink'], userGroups)
} else {
return userGroups
}
} catch (error) {
console.log(error);
}
}
const handleOverage = async (req, res, next) => {
console.log('going through overage');
const authHeader = req.headers.authorization;
const accessToken = authHeader.split(' ')[1]
const userGroups = [];
try {
const oboToken = await getOboToken(accessToken);
const graphResponse = await callGraph(oboToken, config.protectedResources.graphAPI.endpoint);
/**
* Some queries against Microsoft Graph return multiple pages of data either due to server-side paging
* or due to the use of the $top query parameter to specifically limit the page size in a request.
* When a result set spans multiple pages, Microsoft Graph returns an @odata.nextLink property in
* the response that contains a URL to the next page of results. Learn more at https://docs.microsoft.com/graph/paging
*/
if (graphResponse['@odata.nextLink']) {
graphResponse.value.map((v) => userGroups.push(v.id));
try {
res.locals.groups = await handlePagination(oboToken, graphResponse['@odata.nextLink'], userGroups);
return checkAccess(req, res, next);
} catch (error) {
console.log(error);
}
} else {
res.locals.groups = graphResponse.value.map((v) => v.id);
return checkAccess(req, res, next);
}
} catch (error) {
console.log(error);
}
}
module.exports = handleOverage;
ℹ️ Did the sample not work for you as expected? Did you encounter issues trying this sample? Then please reach out to us using the GitHub Issues page.
- Microsoft identity platform (Azure Active Directory for developers)
- Overview of Microsoft Authentication Library (MSAL)
- Quickstart: Register an application with the Microsoft identity platform
- Quickstart: Configure a client application to access web APIs
- Understanding Azure AD application consent experiences
- Understand user and admin consent
- Initialize client applications using MSAL.js
- Single sign-on with MSAL.js
- Handle MSAL.js exceptions and errors
- Logging in MSAL.js applications
- Pass custom state in authentication requests using MSAL.js
- Prompt behavior in MSAL.js interactive requests
- Use MSAL.js to work with Azure AD B2C
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-active-directory
node
ms-identity
adal
msal
].
If you find a bug in the sample, raise the issue on GitHub Issues.
To provide feedback on or suggest features for Azure Active Directory, visit 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.