Skip to content

Latest commit

 

History

History
279 lines (202 loc) · 16.2 KB

File metadata and controls

279 lines (202 loc) · 16.2 KB

A React single-page application using MSAL React to authorize users for calling a protected web API on Azure Active Directory

  1. Overview
  2. Scenario
  3. Contents
  4. Prerequisites
  5. Setup
  6. Registration
  7. Running the sample
  8. Explore the sample
  9. About the code
  10. More information
  11. Community Help and Support
  12. Contributing

Overview

This sample demonstrates a React single-page application (SPA) calling a protected Node.js web API using the Microsoft Authentication Library for React (MSAL React). The Node.js web API itself is protected using the passport-azure-ad plug-in for Passport.js.

Here you'll learn how to register a protected web API, accept authorized calls and validate access tokens.

Scenario

  1. The client React SPA uses MSAL React to sign-in and obtain a JWT access token from Azure AD.
  2. The access token is used as a bearer token to authorize the user to call the Node.js web API protected by Azure AD.
  3. The protected web API responds with the claims in the Access Token.

Overview

Contents

File/folder Description
SPA/App.jsx Main application logic resides here.
SPA/fetch.jsx Provides a helper method for making fetch calls.
SPA/authConfig.js Contains authentication parameters for SPA project.
API/config.js Contains authentication parameters for API project.
API/index.js Main application logic of custom web API.

Prerequisites

  • An Azure AD tenant. For more information see: How to get an Azure AD tenant
  • A user account in your Azure AD tenant. This sample will not work with a personal Microsoft account. Therefore, if you signed in to the Azure portal with a personal account and have never created a user account in your directory before, you need to do that now.

Setup

Step 1: Clone or download this repository

From your shell or command line:

    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.

Step 2: Install project dependencies

  • Setup the service app:
    cd ms-identity-javascript-react-tutorial
    cd 3-Authorization-II/1-call-api
    cd API
    npm install
  • Setup the client app:
    cd ..
    cd SPA
    npm install

Registration

There are two projects in this sample. Each needs to be separately registered in your Azure AD tenant. To register these projects, you can:

  • follow the steps below for manually register your apps
  • or use PowerShell scripts that:
    • automatically creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you.
    • modify the projects' configuration files.
Expand this section if you want to use this automation:

⚠️ If you have never used Azure AD Powershell before, we recommend you go through the App Creation Scripts once to ensure that your environment is prepared correctly for this step.

  1. On Windows, run PowerShell as Administrator and navigate to the root of the cloned directory

  2. If you have never used Azure AD Powershell before, we recommend you go through the App Creation Scripts once to ensure that your environment is prepared correctly for this step.

  3. In PowerShell run:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
  4. Run the script to create your Azure AD application and configure the code of the sample application accordingly.

  5. 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.

Choose the Azure AD tenant where you want to create your applications

As a first step you'll need to:

  1. Sign in to the Azure portal.
  2. If your account is present in more than one Azure AD 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 tenant.

Register the service app (msal-react-api)

  1. Navigate to the Azure portal and select the Azure AD service.
  2. Select the App Registrations blade on the left, then select New registration.
  3. 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-api.
    • Under Supported account types, select Accounts in this organizational directory only.
  4. Select Register to create the application.
  5. 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.
  6. Select Save to save your changes.
  7. 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.
  8. 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-react-api.
      • For Admin consent description type Allows the app to access msal-react-api as the signed-in user.
      • For User consent display name type Access msal-react-api.
      • For User consent description type Allow the application to access msal-react-api on your behalf.
      • Keep State as Enabled.
      • Select the Add scope button on the bottom to save this scope.
  9. Select the Manifest blade on the left.
    • Set accessTokenAcceptedVersion property to 2.
    • Click on Save.

Configure the service app (msal-react-api) to use your app registration

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".

  1. Open the API\config.json file.
  2. Find the key clientID and replace the existing value with the application ID (clientId) of msal-react-api app copied from the Azure portal.
  3. Find the key tenantID and replace the existing value with your Azure AD tenant ID.

Register the client app (msal-react-spa)

  1. Navigate to the Azure portal and select the Azure AD service.
  2. Select the App Registrations blade on the left, then select New registration.
  3. 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 section, select Single-page application in the combo-box and enter the following redirect URI: http://localhost:3000/.
  4. Select Register to create the application.
  5. 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.
  6. Select Save to save your changes.
  7. 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 in the list. Use the search box if necessary.
      • Select the Add permissions button at the bottom.
    • 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-react-api.
      • In the Delegated permissions section, select the Access 'msal-react-api' in the list. Use the search box if necessary.
      • Select the Add permissions button at the bottom.

Configure the client app (msal-react-spa) to use your app registration

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".

  1. Open the SPA\src\authConfig.js file.
  2. Find the key Enter_the_Application_Id_Here and replace the existing value with the application ID (clientId) of msal-react-spa app copied from the Azure portal.
  3. Find the key Enter_the_Tenant_Info_Here and replace the existing value with your Azure AD tenant ID.
  4. Find the key Enter_the_Web_Api_Scope_Here and replace the existing value with APP ID URI of the web API project that you've registered earlier, e.g. api://****-****-********-********/access_as_user

Running the sample

  • Run the service app:
    cd 3-Authorization-II/1-call-api/API
    npm start
  • In a separate terminal, run the client app:
    cd 3-Authorization-II/1-call-api/SPA
    npm start

Explore the sample

  1. Open your browser and navigate to http://localhost:3000.
  2. Select the Sign In button on the top right corner. Choose either Popup or Redirect flows.
  3. Select the Profile button on the navigation bar. This will make a call to the Microsoft Graph API.
  4. Select the HelloAPI button on the navigation bar. This will make a call to your web API.

Screenshot

ℹ️ Did the sample not work for you as expected? Then please reach out to us using the GitHub Issues page.

We'd love your feedback!

Were we successful in addressing your learning objective? Consider taking a moment to share your experience with us.

About the code

Token Validation

On the web API side, passport-azure-ad validates the token against the issuer, scope and audience claims (defined in BearerStrategy constructor) using the passport.authenticate() API:

    app.get('/api', passport.authenticate('oauth-bearer', { session: false }),
        (req, res) => {
            console.log('Validated claims: ', req.authInfo);
    );

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.

CORS Settings

For the purpose of the sample, cross-origin resource sharing is enabled for all domains. This is insecure and not recommended. In production, you should modify this as to allow only the domains that you designate.

    app.use((req, res, next) => {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept");
        next();
    });

More information

For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.

Community Help and Support

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 azure-ad-b2c 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.

Contributing

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.