Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

A Node.js Web API secured by Azure AD and calling Microsoft Graph on behalf of a signed-in user

  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
  13. Code of Conduct

Overview

This sample demonstrates a Vanilla JavaScript single-page application (SPA) which lets a user authenticate against Azure Active Directory (Azure AD) using the Microsoft Authentication Library for JavaScript (MSAL.js) and then calls a protected Node.js web API. The web API then calls the Microsoft Graph API (MS Graph) on behalf of the user signed-in to the client app using the on-behalf-of flow.

Scenario

  1. The client JavaScript SPA uses MSAL.js to sign-in a user and obtain an Access Token from Azure AD.
  2. The Access Token is used as a bearer token to authorize the user to call a Node.js web API protected by Azure AD.
  3. The web API exchanges the user's Access Token with an Access Token for itself to call MS Graph.

Overview

Contents

File/folder Description
AppCreationScripts/ Contains Powershell scripts to automate app registration.
App/authPopup.js Main authentication logic resides here (using Popup flow).
App/authRedirect.js Use this instead of authPopup.js for authentication with redirect flow.
App/authConfig.js Contains configuration parameters for the sample.
SPA/server.js Simple Node server for index.html.
API/process.json Contains configuration parameters for logging via Morgan.
API/index.js Main application logic resides here.
API/config.json Contains authentication parameters for the sample.

Prerequisites

  • 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 AD tenant. For more information see: How to get an Azure AD tenant
  • A user account in your Azure AD. 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-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

    cd ms-identity-javascript-tutorial
    cd 4-AdvancedGrants/1-call-api-graph/API
    npm install
    cd ..
    cd SPA
    npm install

Register the sample application(s) with your Azure Active Directory tenant

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. In PowerShell run:

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

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

  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 ms-identity-javascript-tutorial-c4s1-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, click on the Certificates & secrets blade in the left to open the page where we can generate secrets and upload certificates.
  8. In the Client secrets section, click on 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 click 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.
  9. In the app's registration screen, click on the API permissions blade in the left to open the page where we add access to the APIs that your application needs.
    • Click the Add a permission button and then,
    • Ensure that the Microsoft APIs tab is selected.
    • In the Commonly used Microsoft APIs section, click on Microsoft Graph
    • In the Delegated permissions section, select the offline_access, user.read in the list. Use the search box if necessary.
    • Click on the Add permissions button at the bottom.
  10. 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:
    • Click 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.
  11. 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 ms-identity-javascript-tutorial-c4s1-api
      • For Admin consent description type Allows the app to access ms-identity-javascript-tutorial-c4s1-api as the signed-in user.
      • For User consent display name type Access ms-identity-javascript-tutorial-c4s1-api
      • For User consent description type Allow the application to access ms-identity-javascript-tutorial-c4s1-api on your behalf.
      • Keep State as Enabled
      • Click on the Add scope button on the bottom to save this scope.
  12. On the right side menu, select the Manifest blade.
    • Set accessTokenAcceptedVersion property to 2.
    • Click on Save.

Configure the service app 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 the ms-identity-javascript-tutorial-c4s1-api application copied from the Azure portal.
  3. Find the key tenantID and replace the existing value with your Azure AD tenant ID.
  4. Find the key clientSecret and replace the existing value with the key you saved during the creation of the ms-identity-javascript-tutorial-c4s1-api app in the Azure portal.

Register the client app

  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 ms-identity-javascript-tutorial-c4s1-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/.
  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, click on the API permissions blade in the left to open the page where we add access to the APIs that your application needs.
    • Click the Add a permission button and then:

    • Ensure that the Microsoft APIs tab is selected.

    • In the Commonly used Microsoft APIs section, click on Microsoft Graph

    • In the Delegated permissions section, select the User.Read in the list. Use the search box if necessary.

    • Click on the Add permissions button at the bottom.

    • Click the Add a permission button and then:

    • Ensure that the My APIs tab is selected.

    • In the list of APIs, select the API ms-identity-javascript-tutorial-c4s1-api.

    • In the Delegated permissions section, select the access_as_user in the list. Use the search box if necessary.

    • Click on the Add permissions button at the bottom.

Configure the client app 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".

Open the SPA\App\authConfig.js file. Then:

  1. Find the key Enter_the_Application_Id_Here and replace the existing value with the application ID (clientId) of the ms-identity-javascript-tutorial-c4s1-spa application copied from the Azure portal.
  2. Find the key Enter_the_Tenant_Info_Here and replace the existing value with your tenant ID copied from the Azure portal.
  3. Find the key Enter_the_Redirect_Uri_Here and replace the existing value with the Redirect URI for ms-identity-javascript-tutorial-c4s1-spa app. For example, http://localhost:3000/.
  4. Find the key Enter_the_Web_Api_Uri_Here and replace the existing value with the URL of the exposed web api endpoint, e.g. http://localhost:5000/api
  5. Find the key Enter_the_Web_Api_Scope_Here and replace the existing value with the application ID (clientId) of the, e.g. api://cd96451f-9709-4a95-b1f5-79da05cf8502/.default

Configure knownClientApplications for service app

For a middle tier Web API to be able to call a downstream Web API, the middle tier app needs to be granted the required permissions as well. However, since the middle tier cannot interact with the signed-in user, it needs to be explicitly bound to the client app in its Azure AD registration. This binding merges the permissions required by both the client and the middle tier Web Api and presents it to the end user in a single consent dialog. The user then consent to this combined set of permissions.

To achieve this, you need to add the Application Id of the client app, in the Manifest of the Web API in the knownClientApplications property. Here's how:

  1. In the Azure portal, navigate to your ms-identity-javascript-tutorial-c4s1-api app registration, and select Manifest section.

  2. In the manifest editor, change the "knownClientApplications": [] line so that the array contains the Client ID of the client application (ms-identity-javascript-tutorial-c4s1-spa) as an element of the array.

    For instance:

        "knownClientApplications": ["ca8dca8d-f828-4f08-82f5-325e1a1c6428"],
  3. Save the changes to the manifest.

Running the sample

    cd 4-AdvancedGrants/1-call-api-graph/API
    npm start
    cd ..
    cd SPA
    npm start

Explore the sample

  1. Open your browser and navigate to http://localhost:3000.
  2. Click the sign-in button on the top right corner.
  3. Once you authenticate, click the Call API button at the center.

Screenshot

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

/.default scope and combined consent

Notice that we have set the scope in the client app as api://cd96451f-9709-4a95-b1f5-79da05cf8502/.default, instead of api://cd96451f-9709-4a95-b1f5-79da05cf8502/access_as_user. The /.default scope is a built-in scope for every application that refers to the static list of permissions configured on the application registration in Azure Portal. Basically, it bundles all the permissions from the web API and MS Graph in one call, thus allowing you to grant combined consent to both the client app and the web API.

Furthermore, we had configured the knownClientApplications attribute in application manifest. This attribute is used for bundling consent if you have a solution that contains two (or more) parts: a client app and a custom web API. If you enter the appID (clientID) of the client app into this array, the user will only have to consent once to the client app. Azure AD will know that consenting to the client means implicitly consenting to the web API.

Token validation

The middle-tier web API uses the passport-azure-ad to validate 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, on the other hand, 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.

More information

Configure your application:

Learn more about Microsoft identity platform:

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

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.

Code of Conduct

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.