#Azure Active Directory B2C Sample REST API Service for Node.js using MongoDB and Restify
This Node.js server will give you with a quick and easy way to set up a REST API Service using the OAuth2 protocol. Then this service is integrated with Azure Active Directory for API protection. The sample server included in the download are designed to run on any platform.
This REST API server is built using Restify and MongoDB with the following features:
- A node.js server running an REST API interface with JSON using MongoDB as persistent storage
- REST APIs leveraging OAuth2 API protection for endpoints using Microsoft Azure Active Directory
We've released all of the source code for this example in GitHub under an Apache 2.0 license, so feel free to clone (or even better, fork!) and provide feedback on the forums.
Getting started with the sample is easy. It is configured to run out of the box with minimal setup.
If you don't have an Azure AD B2C Tenant yet, please create one.
-
In the main page of your tenant, click
Manage B2C settings
, and you will be redirected to the settings page. -
Click
Applications
, then clickAdd
. Enter a name like 'my_b2c_webapi', and switch theWeb App / Web API
option to yes. After that, enter 'https://localhost:3000' into theReply URL
field, and click 'Create' to create the application. Click the application you just created, copy theApplication ID
field and save it somewhere. This value is the clientID of your web api.
To successfully use this sample, you need a working installation of Node.js.
Install Node.js from https://nodejs.org.
To successfully use this sample, you must have a working installation of MongoDB. We will use MongoDB to make our REST API persistent across server instances.
Install MongoDB from https://mongodb.org.
NOTE: This walkthrough assumes that you use the default installation and server endpoints for MongoDB, which at the time of this writing is: mongodb://localhost. This should work locally without any configuration changes if you run this sample on the same machine as you've installed and ran mongodb.
Next, clone the sample repo and install the NPM.
From your shell or command line:
$ git clone git@github.com:AzureADQuickStarts/B2C-WebApi-Nodejs.git
$ cd node-server
$ npm install
Please update the exports.creds
and exports.mongoose_auth_local
in config.js as instructed.
$ cd node-server
$ node app.js
Is the server output hard to understand?: We use bunyan
for logging in this sample. The console won't make much sense to you unless you also install bunyan and run the server like above but pipe it through the bunyan binary:
$ node server.js | bunyan
You will have a server successfully running on https://localhost:3000
. Your REST / JSON API Endpoint will be https://localhost:3000/api/tasks
We would like to acknowledge the folks who own/contribute to the following projects for their support of Microsoft Azure Active Directory and their libraries that were used to build this sample. In places where we forked these libraries to add additional functionality, we ensured that the chain of forking remains intact so you can navigate back to the original package. Working with such great partners in the open source community clearly illustrates what open collaboration can accomplish. Thank you!
- MongoDB - MongoDB (from "humongous") is an open-source document database, and the leading NoSQL database. Written in C++
- Restify - Restify is a node.js module built specifically to enable you to build correct REST web services.
node-restify
- Restify-OAuth2 - This package provides a very simple OAuth 2.0 endpoint for the Restify framework.
restify-oauth2
- node-jwt-simple - Library for parsing JSON Web Tokens (JWT)
node-jwt-simple
- http-bearer-strategy - HTTP Bearer authentication strategy for Passport and Node.js.
Code hosted on GitHub under Apache 2.0 license