|
1 | 1 | --- |
2 | 2 | page_type: sample |
3 | | -description: "This sample demonstrates how to protect a Node.js web API with Azure AD B2C using the Passport.js library." |
4 | 3 | languages: |
5 | 4 | - javascript |
6 | | -- nodejs |
| 5 | +- node.js |
7 | 6 | products: |
8 | | -- azure |
9 | | -- azure-active-directory |
10 | | -urlFragment: nodejs-web-api-azure-ad |
| 7 | +- microsoft-identity-platform |
| 8 | +- azure-active-directory-b2c |
| 9 | +description: "A sample demonstrating how to protect a Node.js web API with Azure AD B2C using the Passport.js library." |
| 10 | +urlFragment: "active-directory-b2c-javascript-nodejs-webapi" |
11 | 11 | --- |
12 | 12 |
|
13 | | - |
14 | 13 | # Node.js Web API with Azure AD B2C |
15 | 14 |
|
16 | 15 | This sample demonstrates how to protect a Node.js web API with Azure AD B2C using the Passport.js library. The code here is pre-configured with a registered client ID. If you register your own app, you will need to replace the client ID. |
17 | 16 |
|
18 | | -We have deployed this API to Azure to allow testing without running it locally. Checkout one of the apps in [Next Steps](https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi/blob/master/README.md#next-steps) to use it! |
| 17 | +To see how to call this web API from a client application, refer to this [B2C Single Page Application sample](https://github.com/Azure-Samples/active-directory-b2c-javascript-msal-singlepageapp). |
| 18 | + |
| 19 | +## Contents |
| 20 | + |
| 21 | +| File/folder | Description | |
| 22 | +|-------------------|--------------------------------------------| |
| 23 | +| `process.json` | Contains configuration parameters for logging via Morgan. | |
| 24 | +| `index.js` | Main application logic resides here. | |
| 25 | +| `apiConfig.js` | Contains configuration parameters for the sample. | |
| 26 | +| `.gitignore` | Defines what to ignore at commit time. | |
| 27 | +| `CHANGELOG.md` | List of changes to the sample. | |
| 28 | +| `CODE_OF_CONDUCT.md` | Code of Conduct information. | |
| 29 | +| `CONTRIBUTING.md` | Guidelines for contributing to the sample. | |
| 30 | +| `LICENSE` | The license for the sample. | |
| 31 | +| `package.json` | Package manifest for npm. | |
| 32 | +| `README.md` | This README file. | |
| 33 | +| `SECURITY.md` | Security disclosures. | |
| 34 | +| `server.js` | Implements a simple Node server to api endpoint(s). | |
19 | 35 |
|
20 | 36 | ## Steps to Run |
21 | 37 |
|
22 | 38 | 1. Clone the code. |
23 | 39 |
|
24 | | - ```bash |
25 | | - git clone https://github.com/Azure-Samples/active-directory-b2c-javascript-nodejs-webapi |
26 | | - ``` |
| 40 | +```console |
| 41 | +git clone https://github.com/Azure-Samples/active-directory-javascript-nodejs-webapi-v2 |
| 42 | +``` |
| 43 | + |
| 44 | +2. Make sure you've installed [Node.js](https://nodejs.org/en/download/). |
| 45 | + |
| 46 | +3. Install the node dependencies: |
27 | 47 |
|
28 | | -2. Make sure you've [installed Node](https://nodejs.org/en/download/). |
| 48 | +```console |
| 49 | +npm install && npm update |
| 50 | +``` |
29 | 51 |
|
30 | | -4. Install the node dependencies: |
| 52 | +4. Run the Web API! By default it will run on `http://localhost:5000` |
31 | 53 |
|
32 | | - ```bash |
33 | | - npm install && npm update |
34 | | - ``` |
35 | | -5. Run the Web API! By default it will run on `http://localhost:5000`. |
36 | | - ```bash |
37 | | - node index.js |
38 | | - ``` |
| 54 | +```console |
| 55 | +npm start |
| 56 | +``` |
39 | 57 |
|
40 | 58 | ## Next Steps |
41 | | -The `/hello` endpoint in this sample is protected so an authorized request to it requires an access token in the header. |
42 | | -You can make authorized requests to this web API using an [iOS App](https://github.com/Azure-Samples/active-directory-b2c-ios-swift-native-msal) or [Android App](https://github.com/Azure-Samples/active-directory-b2c-android-native-msal). Make sure to update the app configs if you want it to point to your local hello api. |
43 | 59 |
|
44 | | -Alternatively, you can [register your own app](https://apps.dev.microsoft.com) and point to this web API. |
| 60 | +### Using your own Azure AD B2C Tenant |
| 61 | + |
| 62 | +To have a proper understanding of Azure AD B2C as a developer, follow the tutorials on Azure [AD B2C documentation](https://docs.microsoft.com/en-us/azure/active-directory-b2c/). In the rest of this guide, we summarize the steps you need to go through. |
| 63 | + |
| 64 | +#### Step 1: Get your own Azure AD B2C Tenant |
| 65 | + |
| 66 | +First, you'll need an Azure AD B2C tenant. If you don't have an existing Azure AD B2C tenant that you can use for testing purposes, you can create your own by following [these instructions](https://azure.microsoft.com/documentation/articles/active-directory-b2c-get-started). |
| 67 | + |
| 68 | +#### Step 2: Create your own policies |
| 69 | + |
| 70 | +This sample uses a unified sign-up/sign-in policy. You can create [your own unified sign-up/sign-in policy](https://azure.microsoft.com/documentation/articles/active-directory-b2c-reference-policies). You may choose to include as many or as few identity providers as you wish. |
| 71 | + |
| 72 | +If you already have existing policies in your Azure AD B2C tenant, feel free to re-use those policies in this sample. |
| 73 | + |
| 74 | +#### Step 3: Register your own web API with Azure AD B2C |
45 | 75 |
|
46 | | -Customize your user experience further by supporting more identity providers. Checkout the docs belows to learn how to add additional providers: |
| 76 | +Follow the instructions at [register a Web API with Azure AD B2C](https://docs.microsoft.com/en-us/azure/active-directory-b2c/add-web-application?tabs=applications) to register the Node.js Web API sample with your tenant. Registering your Web API allows you to define the scopes that your single page application will request access tokens for. |
47 | 77 |
|
48 | | -- [Microsoft](https://docs.microsoft.com/azure/active-directory-b2c/active-directory-b2c-setup-msa-app) |
49 | | -- [Facebook](https://docs.microsoft.com/azure/active-directory-b2c/active-directory-b2c-setup-fb-app) |
50 | | -- [Google](https://docs.microsoft.com/azure/active-directory-b2c/active-directory-b2c-setup-goog-app) |
51 | | -- [Amazon](https://docs.microsoft.com/azure/active-directory-b2c/active-directory-b2c-setup-amzn-app) |
52 | | -- [LinkedIn](https://docs.microsoft.com/azure/active-directory-b2c/active-directory-b2c-setup-li-app) |
| 78 | +#### Step 4: Configure your application source code |
53 | 79 |
|
| 80 | +You can now fill in the variables in the `apiConfig.js` file of the Node.js Web API sample with the parameters you've obtained from the Azure Portal during the steps above. |
| 81 | + |
| 82 | +Configure the following variables: |
| 83 | + |
| 84 | +```javascript |
| 85 | +const clientID = "<Application ID for your Node.js Web API - found on Properties page in Azure portal e.g. 93733604-cc77-4a3c-a604-87084dd55348>"; |
| 86 | +const b2cDomainHost = "<Domain of your B2C host eg. fabrikamb2c.b2clogin.com>"; |
| 87 | +const tenantIdGuid = "<Application ID for your Node.js Web API - found on Properties page in Azure portal e.g. 775527ff-9a37-4307-8b3d-cc311f58d925>"; |
| 88 | +const policyName = "<Name of your sign in / sign up policy, e.g. B2C_1_SUSI>"; |
| 89 | +``` |
| 90 | + |
| 91 | +> **NOTE** |
| 92 | +> |
| 93 | +>Developers using the [Azure China Environment](https://docs.microsoft.com/en-us/azure/active-directory/develop/authentication-national-cloud), MUST use <your-tenant-name>.b2clogin.cn authority, instead of `login.chinacloudapi.cn`. |
| 94 | +> |
| 95 | +> In order to use <your-tenant-name>.b2clogin.*, you will need to configure you application and set `validateAuthority: false`. Learn more about using [b2clogin](https://docs.microsoft.com/en-us/azure/active-directory-b2c/b2clogin#set-the-validateauthority-property). |
| 96 | +
|
| 97 | +Lastly, to run your Node.js Web API, run the following command from your shell or command line |
| 98 | + |
| 99 | +```bash |
| 100 | +npm install && npm update |
| 101 | +npm start |
| 102 | +``` |
| 103 | + |
| 104 | +Your Node.js Web API sample is now running on Port 5000. |
54 | 105 |
|
55 | 106 | ## Questions & Issues |
56 | 107 |
|
57 | | -Please file any questions or problems with the sample as a GitHub issue. You can also post on Stack Overflow with the tag `azure-ad-b2c`. For OAuth2.0 library issues, please see note below. |
| 108 | +Please file any questions or problems with the sample as a GitHub issue. You can also post on Stack Overflow with the tag `azure-ad-b2c`. For OAuth2.0 library issues, please see note below. |
| 109 | + |
| 110 | +## Contributing |
| 111 | + |
| 112 | +If you'd like to contribute to this sample, see [CONTRIBUTING.MD](./CONTRIBUTING.md). |
| 113 | + |
| 114 | +## Code of Conduct |
| 115 | + |
| 116 | +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). |
| 117 | +For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or |
| 118 | +contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. |
0 commit comments