Office 365 Authentication using Open ID Connect in Node.js

Office 365 Development

Users of our custom app authenticate with their Office 365 identity. Our application does not need to record separate passwords.

Users are re-directed to an Office 365 authentication page during login. They return to our custom application after successfully authentication in Office 365.

This also employs any two-factor authentication configured for Office 365 authentication.

Office 365 Authentication

In the Architecture Description section of this blog, we explain how to recruit Microsoft Graph API for access to Office 365 resources. The API requests are authenticated by tokens. There are two different types of authentication tokens: Delegated Permissions and Application Permissions.

Delegated Permissions

Office 365 Delegated Permissions Authentication

Users are re-directed to the Microsoft Azure login page to provider their Office 365 credentials. After providing the correct credentials, Azure creates an access token which is returned to the application.

The token authorizes access to the Graph API. The access impersonates the authenticated user. Any access outside the privileges of the user, will be un-authorized.

Application Permissions

Office 365 Delegated Permissions Authentication

Authentication is independent of any Azure Active Directory users. The credentials and scope of access is configured by the application administrator.

Configure Azure Active Directory

In our tutorial we focus on the delegated permissions.

The Office 365 authentication needs to be enabled before it can be used in custom applications.

Step 1
Log on to Azure Active Directory Admin Center

Log on to the Azure Active Directory Admin Center.

Select Enterprise Applications from the Identity category of the All Services page.

Select Create your own application in the next step.

Step 2
Register a New Application

Select New Application from the top of the list.

Select Create you own application in the subsequent page. Make sure you select "Register an application to integrate with Azure AD (App you're developing)" from the radio buttons below the input to name your app.

Step 3
Select Account Types and Define Redirect URL

In the final step for registration, we need to confirm the name, select the type of user accounts and define redirect url.

The account type defines who can use this application. We chose "Accounts in this organizational directory only" to limit the users to our Office 365 plan.

The other account types are used to open your custom application to a wider audience. But this is outside the scope of this tutorial.

At last, we specify the Redirect URL. This is the URL to which the native Office 365 authentication page will redirect after successful user authentication. Later, we will configure this route in our http server to accept the result of the successful user authentication.

Step 4
Complete Application Registration

You can view the information of the newly created application in its detail page. This is also the starting point to make any changes in the future.

Azure Active Directory Admin Center - All Services
Create New Enterprise Application in Azure Active Directory Admin Center
Create and name your own application in Azure Active Directory Admin Center
Chose user type for Azure Active Directory Admin Center
Summary of application in Azure Active Directory Admin Center
Step 1
Log on to Azure Active Directory Admin Center

Log on to the Azure Active Directory Admin Center.

Select Enterprise Applications from the Identity category of the All Services page.

Select Create your own application in the next step.

Azure Active Directory Admin Center - All Services
Step 2
Register a New Application

Select New Application from the top of the list.

Select Create you own application in the subsequent page. Make sure you select "Register an application to integrate with Azure AD (App you're developing)" from the radio buttons below the input to name your app.

Create New Enterprise Application in Azure Active Directory Admin Center
Create and name your own application in Azure Active Directory Admin Center
Step 3
Select Account Types and Define Redirect URL

In the final step for registration, we need to confirm the name, select the type of user accounts and define redirect url.

The account type defines who can use this application. We chose "Accounts in this organizational directory only" to limit the users to our Office 365 plan.

The other account types are used to open your custom application to a wider audience. But this is outside the scope of this tutorial.

At last, we specify the Redirect URL. This is the URL to which the native Office 365 authentication page will redirect after successful user authentication. Later, we will configure this route in our http server to accept the result of the successful user authentication.

Chose user type for Azure Active Directory Admin Center
Step 4
Complete Application Registration

You can view the information of the newly created application in its detail page. This is also the starting point to make any changes in the future.

Summary of application in Azure Active Directory Admin Center

Programming Interface to Accept Office 365 Authentication Result

After the application registration is completed, we can start implementing the Office 365 Authentication on top of our Node, Express and Passport Stack.

We start by installing the Azure Active Directory Authentication library for Passport.

Passport provides the authentication layer on top of Express. It features many plugins for different authentication channels. This includes plain old username and password as well as OAuth with varies social media and also Microsoft Azure Active Directory.

Step 1
Install Microsoft Azure Authentication Library

Add the authentication library for authentication with Azure Active Directory. The library runs on top of Passport.js. It lets your Office 365 users authenticate through Microsoft.

Step 2
Register a New Application

Select New Application from the top of the list.

Select Create you own application in the subsequent page. Make sure you select "Register an application to integrate with Azure AD (App you're developing)" from the radio buttons below the input to name your app.

Step 3
Select Account Types and Define Redirect URL

In the final step for registration, we need to confirm the name, select the type of user accounts and define redirect url.

The account type defines who can use this application. We chose "Accounts in this organizational directory only" to limit the users to our Office 365 plan.

The other account types are used to open your custom application to a wider audience. But this is outside the scope of this tutorial.

At last, we specify the Redirect URL. This is the URL to which the native Office 365 authentication page will redirect after successful user authentication. Later, we will configure this route in our http server to accept the result of the successful user authentication.

npm install --save passport-azure-ad
Create New Enterprise Application in Azure Active Directory Admin Center
Create and name your own application in Azure Active Directory Admin Center
Chose user type for Azure Active Directory Admin Center
Step 1
Log on to Azure Active Directory Admin Center

Log on to the Azure Active Directory Admin Center.

Select Enterprise Applications from the Identity category of the All Services page.

Select Create your own application in the next step.

Azure Active Directory Admin Center - All Services
Step 2
Register a New Application

Select New Application from the top of the list.

Select Create you own application in the subsequent page. Make sure you select "Register an application to integrate with Azure AD (App you're developing)" from the radio buttons below the input to name your app.

Create New Enterprise Application in Azure Active Directory Admin Center
Create and name your own application in Azure Active Directory Admin Center
Step 3
Select Account Types and Define Redirect URL

In the final step for registration, we need to confirm the name, select the type of user accounts and define redirect url.

The account type defines who can use this application. We chose "Accounts in this organizational directory only" to limit the users to our Office 365 plan.

The other account types are used to open your custom application to a wider audience. But this is outside the scope of this tutorial.

At last, we specify the Redirect URL. This is the URL to which the native Office 365 authentication page will redirect after successful user authentication. Later, we will configure this route in our http server to accept the result of the successful user authentication.

Chose user type for Azure Active Directory Admin Center
Step 4
Complete Application Registration

You can view the information of the newly created application in its detail page. This is also the starting point to make any changes in the future.

Summary of application in Azure Active Directory Admin Center