In this tutorial, I will present the procedure to employ Microsoft Graph API to authenticate users with Microsoft OAuth2 authentication mechanism in order to implement the single sign-on (SSO), aka. sign-in with Microsoft. This article is organized as follows:
If you just want to run the demo program, please visit this GitHub repository to clone the sample program: https://github.com/duonghuuphuc/msgraph-sso-python.
To complete this tutorial, you will need to have the following things:
In this section, I will show you how to create an application on Microsoft Azure. First, you sign in to Microsoft Azure with either your personal account (Skype, Xbox, Live, and Hotmail) or work account (Microsoft 365 Business Plan). Then, you perform the following steps:
Credentials are used by confidential client applications that access a web API. In this example, we will add client secrets (a string) as credentials to our confidential client app registration.
By default, the User.Read.All has been already added to the application. This default permission is enough to implement the SSO functionality. You can visit this article to try other Microsoft Graph APIs.
In this section, I will show the implementation of a Flask application (client program) that authenticates users having Microsoft accounts. You need to perform the following step to run the demo program:
For ease of executing the demo program, I have prepared an env.sh file that contains a list of commands that run sequentially to start the demo Flask application. There are two parameters that you must configure before running the demo program, i.e., CLIENT_ID and CLIENT_SECRET. The CLIENT_ID is a string that you did obtain as presented in Section 3. And the CLIENT_SECRET is the recorded secret's value, as presented in Section 4.
After inserting the CLIENT_ID and CLIENT_SECRET into the env.sh file, you can now run the demo program by executing the following commands in Terminal window:
work <env_name>pip install requirements.txtsource env.shYou should notice that the second command is a one-time executed command. The first and third commands will be manually executed each time you run the demo program. Fig. 1 shows the result when visiting the http://localhost:5000 in a web browser.
The demo program allows you to sign in with both a personal account (Skype, Xbox, Live, and Hotmail) and a work account (Microsoft 365 Business plan). In Fig. 2, I demonstrate the demo program with a personal account.
After users click on the Yes button, they will be redirected to the demo application that will render a list of available functionalities, as shown in Fig. 3, since the users have been authenticated.
In this article, I have presented the procedure to implement the single sign-on functionality for users having either personal Microsoft accounts (Skype, Xbox, Live, and Hotmail) or work accounts. The procedure consists of four main steps, i.e., (1) create an application on Microsoft Azure, (2) issue credentials, (3) add API permissions to the application, and (4) run the demo program.