# A Node.js & Express web app authenticating users against Microsoft Entra ID with MSAL Node 1. [Overview](#overview) 1. [Scenario](#scenario) 1. [Contents](#contents) 1. [Prerequisites](#prerequisites) 1. [Setup](#setup) 1. [Registration](#registration) 1. [Running the sample](#running-the-sample) 1. [Explore the sample](#explore-the-sample) 1. [More information](#more-information) 1. [Community Help and Support](#community-help-and-support) 1. [Contributing](#contributing) ## Overview This sample demonstrates a Node.js & Express web application that authenticates users against Microsoft Entra ID, with the help of [Microsoft Authentication Library for Node.js](https://aka.ms/msalnode) (MSAL Node). In doing so, it illustrates authentication concepts such as [OpenID scopes](https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#openid-connect-scopes), [ID Tokens](https://docs.microsoft.com/azure/active-directory/develop/id-tokens), securing routes and more. ## Scenario 1. The client application uses **MSAL Node** (via [msal-node-wrapper](https://github.com/Azure-Samples/ms-identity-javascript-nodejs-tutorial/tree/main/Common/msal-node-wrapper)) to obtain an ID Token from **Microsoft Entra ID**. 2. The **ID Token** proves that the user has successfully authenticated against **Microsoft Entra ID**. ![Overview](./ReadmeFiles/topology.png) ## Contents | File/folder | Description | |-----------------------------|---------------------------------------------------------------| | `AppCreationScripts/` | Contains Powershell scripts to automate app registration. | | `ReadmeFiles/` | Contains illustrations and screenshots. | | `App/authConfig.js` | Authentication parameters reside here. | | `App/app.js` | Application entry point. | ## Prerequisites - [Node.js](https://nodejs.org/en/download/) must be installed to run this sample. - [Visual Studio Code](https://code.visualstudio.com/download) is recommended for running and editing this sample. - A modern web browser. This sample uses **ES6** conventions and will not run on **Internet Explorer**. - An **Microsoft Entra ID** tenant. For more information, see: [How to get a Microsoft Entra tenant](https://docs.microsoft.com/azure/active-directory/develop/quickstart-create-new-tenant) - A user account in your **Microsoft Entra ID** tenant. ## Setup ### Step 1: Clone or download this repository From your shell or command line: ```console git clone https://github.com/Azure-Samples/ms-identity-javascript-nodejs-tutorial.git ``` or download and extract the repository .zip file. > :warning: To avoid path length limitations on Windows, we recommend cloning into a directory near the root of your drive. ### Step 2: Install project dependencies Locate the root of the sample folder. Then: ```console cd 1-Authentication\1-sign-in\App npm install ``` ## Registration There is one project in this sample. To register it, you can: - follow the steps below for manually register your apps - or use PowerShell scripts that: - **automatically** creates the Microsoft Entra applications and related objects (passwords, permissions, dependencies) for you. - modify the projects' configuration files.
Expand this section if you want to use this automation: > :warning: If you have never used **Microsoft Graph PowerShell** before, we recommend you go through the [App Creation Scripts Guide](./AppCreationScripts/AppCreationScripts.md) once to ensure that your environment is prepared correctly for this step. 1. Ensure that you have [PowerShell 7](https://learn.microsoft.com/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.3) or later. 1. Run the script to create your Microsoft Entra application and configure the code of the sample application accordingly. 1. For interactive process -in PowerShell, run: ```PowerShell cd .\AppCreationScripts\ .\Configure.ps1 -TenantId "[Optional] - your tenant id" -AzureEnvironmentName "[Optional] - Azure environment, defaults to 'Global'" ``` > Other ways of running the scripts are described in [App Creation Scripts guide](./AppCreationScripts/AppCreationScripts.md). The scripts also provide a guide to automated application registration, configuration and removal which can help in your CI/CD scenarios. > :information_source: This sample can make use of client certificates. You can use **AppCreationScripts** to register a Microsoft Entra application with certificates. See: [How to use certificates instead of client secrets](./README-use-certificate.md)
### Choose the Microsoft Entra tenant where you want to create your applications As a first step you'll need to: 1. Sign in to the [Microsoft Entra admin center](https://entra.microsoft.com). 1. If your account is present in more than one Microsoft Entra 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 Microsoft Entra tenant. ### Register the client app (msal-node-webapp) 1. Navigate to the [Microsoft Entra admin center](https://entra.microsoft.com) and select the **Microsoft Entra ID** service. 1. Select the **App Registrations** blade on the left, then select **New registration**. 1. 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-webapp`. - Under **Supported account types**, select **Accounts in this organizational directory only**. - In the **Redirect URI (optional)** section, select **Web** in the combo-box and enter the following redirect URI: `http://localhost:4000/redirect`. 1. Select **Register** to create the application. 1. 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. 1. Select **Save** to save your changes. 1. In the app's registration screen, select the **Certificates & secrets** blade in the left to open the page where you can generate secrets and upload certificates. 1. In the **Client secrets** section, select **New client secret**: - Type a key description (for instance `app secret`), - Select one of the available key durations (**6 months**, **12 months** or **Custom**) as per your security posture. - The generated key value will be displayed when you select the **Add** button. Copy and save the generated value for use in later steps. - 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 Microsoft Entra admin center before navigating to any other screen or blade. > :warning: For enhanced security, consider using **certificates** instead of client secrets. See: [How to use certificates instead of secrets](./README-use-certificate.md). #### Configure Optional Claims 1. Still on the same app registration, select the **Token configuration** blade to the left. 1. Select **Add optional claim**: 1. Select **optional claim type**, then choose **Access**. 1. Select the optional claim **acct**. > Provides user's account status in tenant. If the user is a **member** of the tenant, the value is *0*. If they're a **guest**, the value is *1*. 1. Select **Add** to save your changes. #### Configure the client app (msal-node-webapp) 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 `App/authConfig.js` file. 1. Find the key `clientId` and replace the existing value with the application ID (clientId) of `msal-node-webapp` app copied from the Microsoft Entra admin center. 1. Find the key `tenantId` and replace the existing value with your Microsoft Entra tenant ID. 1. Find the key `clientSecret` and replace the existing value with the key you saved during the creation of `msal-node-webapp` copied from the Microsoft Entra admin center. 1. Find the key `redirectUri` and replace the existing value with the Redirect URI for `msal-node-webapp`. (by default `http://localhost:4000/redirect`). > :information_source: For `redirectUri`, you can simply enter the path component of the URI instead of the full URI. For example, instead of `http://localhost:4000/redirect`, you can simply enter `/redirect`. This may come in handy in deployment scenarios. 1. Open the `App/app.js` file. 1. Find the string `ENTER_YOUR_SECRET_HERE` and replace it with a secret that will be used when encrypting your app's session using the [express-session](https://www.npmjs.com/package/express-session) package. ## Running the sample Locate the root of the sample folder. Then: ```console npm start ``` ## Explore the sample 1. Open your browser and navigate to `http://localhost:4000`. 1. Select the **sign-in** button on the top right corner. 1. Once signed in, select the **ID** button to see some of the claims in your ID token. ![Screenshot](./ReadmeFiles/screenshot.png) > :information_source: Did the sample not work for you as expected? Then please reach out to us using the [GitHub Issues](../../../../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](https://forms.office.com/Pages/ResponsePage.aspx?id=v4j5cvGGr0GRqy180BHbR73pcsbpbxNJuZCMKN0lURpUQkRCSVdRSk8wUjdZSkg2NEZGOFFaTkxQVyQlQCN0PWcu). ## More information Configure your application: - [Initialize client applications using MSAL.js](https://docs.microsoft.com/azure/active-directory/develop/msal-js-initializing-client-applications) - [Single sign-on with MSAL.js](https://docs.microsoft.com/azure/active-directory/develop/msal-js-sso) - [Handle MSAL.js exceptions and errors](https://docs.microsoft.com/azure/active-directory/develop/msal-handling-exceptions?tabs=javascript) - [Logging in MSAL.js applications](https://docs.microsoft.com/azure/active-directory/develop/msal-logging?tabs=javascript) - [Pass custom state in authentication requests using MSAL.js](https://docs.microsoft.com/azure/active-directory/develop/msal-js-pass-custom-state-authentication-request) - [Prompt behavior in MSAL.js interactive requests](https://docs.microsoft.com/azure/active-directory/develop/msal-js-prompt-behavior) Learn more about the Microsoft identity platform: - [Microsoft identity platform (Microsoft Entra ID for developers)](https://docs.microsoft.com/azure/active-directory/develop/) - [Overview of Microsoft Authentication Library (MSAL)](https://docs.microsoft.com/azure/active-directory/develop/msal-overview) - [Understanding Microsoft Entra application consent experiences](https://docs.microsoft.com/azure/active-directory/develop/application-consent-experience) - [Understand user and admin consent](https://docs.microsoft.com/azure/active-directory/develop/howto-convert-app-to-be-multi-tenant#understand-user-and-admin-consent) - [Microsoft identity platform and OpenID Connect protocol](https://docs.microsoft.com/azure/active-directory/develop/v2-protocols-oidc) - [Microsoft Identity Platform ID Tokens](https://docs.microsoft.com/azure/active-directory/develop/id-tokens) For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see [Authentication Scenarios for Microsoft Entra ID](https://docs.microsoft.com/azure/active-directory/develop/authentication-flows-app-scenarios). ## Community Help and Support Use [Stack Overflow](http://stackoverflow.com/questions/tagged/msal) 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](../../../../issues). To provide feedback on or suggest features for Microsoft Entra ID, visit [User Voice page](https://feedback.azure.com/forums/169401-azure-active-directory). ## Contributing If you'd like to contribute to this sample, see [CONTRIBUTING.MD](/CONTRIBUTING.md). This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information, see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.