Skip to content

Commit a12bf09

Browse files
committed
Updating ReadME and cleanup
1 parent e91f4d7 commit a12bf09

2 files changed

Lines changed: 155 additions & 43 deletions

File tree

README.md

Lines changed: 155 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,178 @@
11
---
2-
page_type: sample
3-
languages:
4-
- csharp
5-
products:
6-
- dotnet
7-
description: "Add 150 character max description"
8-
urlFragment: "update-this-to-unique-url-stub"
2+
services: active-directory
3+
platforms: python
4+
author: abpati
5+
level: 300
6+
client: Python Web Application
7+
service: Microsoft Graph
8+
endpoint: Microsoft Identity platform (formerly Azure AD v2.0)
99
---
10+
# Integrating Microsoft Identity Platform with a Python web application
1011

11-
# Official Microsoft Sample
12+
## About this sample
1213

13-
<!--
14-
Guidelines on README format: https://review.docs.microsoft.com/help/onboard/admin/samples/concepts/readme-template?branch=master
14+
> This sample is also available as a quickstart for the Microsoft identity platform: [Quickstart: Add sign-in with Microsoft to a Java web app]("insert_url_here")
1515
16-
Guidance on onboarding samples to docs.microsoft.com/samples: https://review.docs.microsoft.com/help/onboard/admin/samples/process/onboarding?branch=master
16+
### Overview
1717

18-
Taxonomies for products and languages: https://review.docs.microsoft.com/new-hope/information-architecture/metadata/taxonomies?branch=master
19-
-->
18+
This sample demonstrates a Python Web Application application calling The Microsoft Graph.
2019

21-
Give a short description for your sample here. What does it do and why is it important?
20+
1. The python web application uses the MicroSoft Authentication Library (MSAL) to obtain a JWT access token from Azure Active Directory (Azure AD):
21+
2. The access token is used as a bearer token to authenticate the user when calling the Microsoft Graph.
2222

23-
## Contents
23+
![Overview](./ReadmeFiles/topology.png)
2424

25-
Outline the file contents of the repository. It helps users navigate the codebase, build configuration and any related assets.
25+
### Scenario
2626

27-
| File/folder | Description |
28-
|-------------------|--------------------------------------------|
29-
| `src` | Sample source code. |
30-
| `.gitignore` | Define what to ignore at commit time. |
31-
| `CHANGELOG.md` | List of changes to the sample. |
32-
| `CONTRIBUTING.md` | Guidelines for contributing to the sample. |
33-
| `README.md` | This README file. |
34-
| `LICENSE` | The license for the sample. |
27+
This sample shows how to build a Python web app that uses OAuth2 to get access to Microsoft Graph using MSAL Python.For more information about how th eprotocols work in this scenario and other scenarios, see [Authentication Scenarios for Azure AD](https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-authentication-scenarios).
3528

36-
## Prerequisites
29+
## How to run this sample
3730

38-
Outline the required components and tools that a user might need to have on their machine in order to run the sample. This can be anything from frameworks, SDKs, OS versions or IDE releases.
31+
To run this sample, you'll need:
3932

40-
## Setup
33+
> To run this sample you will need:
34+
> - An internet connection.
35+
> - [Python 2.7+](https://www.python.org/downloads/release/python-2713/) or [Python 3+](https://www.python.org/downloads/release/python-364/)
36+
> - [Flask](http://flask.pocoo.org/), [Flask-Session](https://pythonhosted.org/Flask-Session/), [requests](https://2.python-requests.org/en/master/)
37+
> - [MSAL Python](https://github.com/AzureAD/microsoft-authentication-library-for-python)
38+
> - An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see [how to get an Azure AD tenant.](https://docs.microsoft.com/azure/active-directory/develop/quickstart-create-new-tenant)
4139
42-
Explain how to prepare the sample once the user clones or downloads the repository. The section should outline every step necessary to install dependencies and set up any settings (for example, API keys and output folders).
4340

44-
## Runnning the sample
41+
### Step 1: Clone or download this repository
4542

46-
Outline step-by-step instructions to execute the sample and see its output. Include steps for executing the sample from the IDE, starting specific services in the Azure portal or anything related to the overall launch of the code.
43+
From your shell or command line:
4744

48-
## Key concepts
45+
```Shell
46+
git clone https://github.com/Azure-Samples/https://github.com/Azure-Samples/ms-identity-python-webapp.git
47+
```
4948

50-
Provide users with more context on the tools and services used in the sample. Explain some of the code that is being used and how services interact with each other.
49+
or download and extract the repository .zip file.
50+
51+
> Given that the name of the sample is quiet long, and so are the names of the referenced NuGet packages, you might want to clone it in a folder close to the root of your hard drive, to avoid file size limitations on Windows.
52+
53+
### Step 2: Register the sample application with your Azure Active Directory tenant
54+
55+
There is one project in this sample. To register it, you can:
56+
57+
- either follow the steps [Step 2: Register the sample with your Azure Active Directory tenant](#step-2-register-the-sample-with-your-azure-active-directory-tenant) and [Step 3: Configure the sample to use your Azure AD tenant](#choose-the-azure-ad-tenant-where-you-want-to-create-your-applications)
58+
- or use PowerShell scripts that:
59+
- **automatically** creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you
60+
- modify the applications' configuration files.
61+
62+
If you want to use this automation:
63+
64+
1. On Windows, run PowerShell and navigate to the root of the cloned directory
65+
1. In PowerShell run:
66+
67+
```PowerShell
68+
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
69+
```
70+
71+
1. Run the script to create your Azure AD application and configure the code of the sample application accordingly.
72+
1. In PowerShell run:
73+
74+
```PowerShell
75+
.\AppCreationScripts\Configure.ps1
76+
```
77+
78+
> Other ways of running the scripts are described in [App Creation Scripts](./AppCreationScripts/AppCreationScripts.md)
79+
80+
If you don't want to use this automation, follow the steps below.
81+
82+
#### Choose the Azure AD tenant where you want to create your applications
83+
84+
As a first step you'll need to:
85+
86+
1. Sign in to the [Azure portal](https://portal.azure.com) using either a work or school account or a personal Microsoft account.
87+
1. If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then **switch directory**.
88+
Change your portal session to the desired Azure AD tenant.
89+
90+
#### Register the Python Webapp (python-webapp)
91+
92+
1. Navigate to the Microsoft identity platform for developers [App registrations](https://go.microsoft.com/fwlink/?linkid=2083908) page.
93+
1. Select **New registration**.
94+
1. When the **Register an application page** appears, enter your application's registration information:
95+
- In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `python-webapp`.
96+
- Change **Supported account types** to **Accounts in any organizational directory and personal Microsoft accounts (e.g. Skype, Xbox, Outlook.com)**.
97+
- In the Redirect URI (optional) section, select **Web** in the combo-box and enter the following redirect URIs: `http://localhost:5000/getAToken`.
98+
1. Select **Register** to create the application.
99+
1. On the app **Overview** page, find the **Application (client) ID** value and record it for later. You'll need it to configure the Visual Studio configuration file for this project.
100+
1. From the app's Overview page, select the **Authentication** section.
101+
- In the **Advanced settings** section set **Logout URL** to `http://localhost:5000/logout`
102+
1. Select **Save**.
103+
1. From the **Certificates & secrets** page, in the **Client secrets** section, choose **New client secret**:
104+
105+
- Type a key description (of instance `app secret`),
106+
- Select a key duration of either **In 1 year**, **In 2 years**, or **Never Expires**.
107+
- When you press the **Add** button, the key value will be displayed, copy, and save the value in a safe location.
108+
- You'll need this key later to configure the project in Visual Studio. This key value will not be displayed again, nor retrievable by any other means,
109+
so record it as soon as it is visible from the Azure portal.
110+
1. Select the **API permissions** section
111+
- Click the **Add a permission** button and then,
112+
- Ensure that the **Microsoft APIs** tab is selected
113+
- In the *Commonly used Microsoft APIs* section, click on **Microsoft Graph**
114+
- In the **Delegated permissions** section, ensure that the right permissions are checked: **User.Read**. Use the search box if necessary.
115+
- Select the **Add permissions** button
116+
117+
### Step 3: Configure the sample to use your Azure AD tenant
118+
119+
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
120+
121+
#### Configure the pythonwebapp project
122+
123+
> Note: if you used the setup scripts, the changes below will have been applied for you
124+
125+
1. Open the `app_config.py` file
126+
1. Find the app key `tenant-name` and replace the existing value with your Azure AD tenant name.
127+
1. Find the app key `client-secret-obtained-during-app-registration` and replace the existing value with the key you saved during the creation of the `python-webapp` app, in the Azure portal.
128+
1. Find the app key `client-id-as-obtained-during-app-registration` and replace the existing value with the application ID (clientId) of the `python-webapp` application copied from the Azure portal.
129+
130+
131+
### Step 4: Run the sample
132+
133+
- You will need to install MSAL Python library, Flask framework, Flask-Sessions for server side session management and requests using pip as follows:
134+
```Shell
135+
$ pip install msal
136+
$ pip install flask
137+
$ pip install Flask-Session
138+
$ pip install requests
139+
```
140+
- If the environment variable for Flask is already set:
141+
142+
Run app.py from shell or command line:
143+
```Shell
144+
$ python app.py
145+
```
146+
- If the environment variable for Flask is not set:
147+
148+
Type the following commands on shell or command line by navigating to the project directory:
149+
```Shell
150+
$ export FLASK_APP=app.py
151+
$ export FLASK_DEBUG=1
152+
$ flask run
153+
```
154+
155+
## Community Help and Support
156+
157+
Use [Stack Overflow](http://stackoverflow.com/questions/tagged/msal) to get support from the community.
158+
Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.
159+
Make sure that your questions or comments are tagged with [`azure-active-directory` `adal` `msal` `python`].
160+
161+
If you find a bug in the sample, please raise the issue on [GitHub Issues](../../issues).
162+
163+
To provide a recommendation, visit the following [User Voice page](https://feedback.azure.com/forums/169401-azure-active-directory).
51164

52165
## Contributing
53166

54-
This project welcomes contributions and suggestions. Most contributions require you to agree to a
55-
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
56-
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
167+
If you'd like to contribute to this sample, see [CONTRIBUTING.MD](/CONTRIBUTING.md).
168+
169+
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.
170+
171+
## More information
172+
173+
For more information, see MSAL.Python's [conceptual documentation]("https://msal-python.readthedocs.io/en/latest/"):
174+
57175

58-
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
59-
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
60-
provided by the bot. You will only need to do this once across all repos using our CLA.
176+
For more information about web apps scenarios on the Microsoft identity platform see [Scenario: Web app that calls web APIs](https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-web-app-call-api-overview)
61177

62-
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
63-
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
64-
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
178+
For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see [Authentication Scenarios for Azure AD](http://go.microsoft.com/fwlink/?LinkId=394414).

app.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import requests
44
from flask import Flask, render_template, session, request
55
from flask_session import Session
6-
import json
76
import msal
8-
import sys
97
import app_config
108

119
sess = Session()

0 commit comments

Comments
 (0)