|
1 |
| -# Project Name |
| 1 | +# Sample: A Python Django web project to sign in users and call APIs with the Microsoft Entra ID |
2 | 2 |
|
3 |
| -(short, 1-3 sentenced, description of the project) |
| 3 | +This is a Python web application that uses the |
| 4 | +[Django framework](https://www.djangoproject.com/) |
| 5 | +and the |
| 6 | +[Microsoft Entra ID](https://www.microsoft.com/security/business/microsoft-entra) |
| 7 | +to sign in users and make authenticated calls to the Microsoft Graph API. |
4 | 8 |
|
5 |
| -## Features |
6 |
| - |
7 |
| -This project framework provides the following features: |
8 |
| - |
9 |
| -* Feature 1 |
10 |
| -* Feature 2 |
11 |
| -* ... |
12 | 9 |
|
13 | 10 | ## Getting Started
|
14 | 11 |
|
15 | 12 | ### Prerequisites
|
16 | 13 |
|
17 |
| -(ideally very short, if any) |
18 |
| - |
19 |
| -- OS |
20 |
| -- Library version |
21 |
| -- ... |
| 14 | +- Register your web application in the Microsoft Entra admin center, |
| 15 | + by following step 1, 2 and 3 of this |
| 16 | + [Quickstart: Add sign-in with Microsoft to a Python web app](https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-web-app-python-sign-in?tabs=windows) |
| 17 | +- Have [Python](https://python.org) 3.8+ installed |
22 | 18 |
|
23 | 19 | ### Installation
|
24 | 20 |
|
25 |
| -(ideally very short) |
26 |
| - |
27 |
| -- npm install [package name] |
28 |
| -- mvn install |
29 |
| -- ... |
| 21 | +1. This sample already implements sign-in and calling downstream API. |
| 22 | + You can clone |
| 23 | + [its repo](https://github.com/Azure-Samples/ms-identity-python-webapp-django) |
| 24 | + or download its zip package, and then start using it or build on top of it. |
| 25 | + (Alternatively, you can follow our [tutorial](#tutorial) to learn |
| 26 | + how to build this from scratch, or how to add auth to your existing project. |
| 27 | +2. `cd project_name` |
| 28 | +3. Run `pip install -r requirements.txt` to install dependencies |
| 29 | +4. Run `python manage.py migrate` to initialize your Django project |
| 30 | +5. Copy [`.env.sample`](https://github.com/Azure-Samples/ms-identity-python-webapp-django/blob/main/.env.sample) as `.env`, |
| 31 | + and then modify its content based on your application's registration. |
30 | 32 |
|
31 | 33 | ### Quickstart
|
32 |
| -(Add steps to get up and running quickly) |
33 | 34 |
|
34 |
| -1. git clone [repository clone url] |
35 |
| -2. cd [repository name] |
36 |
| -3. ... |
| 35 | +1. After finishing installation, now you can run |
| 36 | + `python manage.py runserver localhost:5000` to start your development server. |
| 37 | + You may need to change to a different port to match your redirect_uri setup. |
| 38 | +2. Now visit http://localhost:5000 |
| 39 | + |
| 40 | + |
| 41 | +## Tutorial |
| 42 | + |
| 43 | +> Note: You do not have to read this tutorial. |
| 44 | +> |
| 45 | +> * If you are starting a new project, you can begin with |
| 46 | +> [our sample](https://github.com/Azure-Samples/ms-identity-python-webapp-django) |
| 47 | +> and build on top of it. |
| 48 | +
|
| 49 | +The following chapters teach you |
| 50 | +how to build a Django project with Microsoft Entra ID from scratch. |
| 51 | +After that, you will also know how to modify an existing Python Django project |
| 52 | +to sign in users and call APIs with the Microsoft Entra ID. |
| 53 | + |
| 54 | +### Preface: Have a Django web project as a starting point |
| 55 | + |
| 56 | +You can use |
| 57 | +[Django's own tutorial, part 1](https://docs.djangoproject.com/en/5.0/intro/tutorial01/) |
| 58 | +as a reference. What we need are these steps: |
| 59 | + |
| 60 | +1. `django-admin startproject mysite` |
| 61 | +2. `python manage.py migrate` |
| 62 | +3. `python manage.py runserver localhost:5000` |
| 63 | + You may need to change to a different port to match your redirect_uri setup. |
| 64 | + |
| 65 | +4. Now, add an `index` view to your project. |
| 66 | + For now, it can simply return a "hello world" page to any visitor. |
| 67 | + |
| 68 | + ```python |
| 69 | + from django.http import HttpResponse |
| 70 | + def index(request): |
| 71 | + return HttpResponse("Hello, world. Everyone can read this line.") |
| 72 | + ``` |
| 73 | + |
| 74 | +### Chapter 1: Enable your Python Django web app to sign in users |
| 75 | + |
| 76 | +1. At the beginning of `mysite/settings.py` file, create a global Auth helper like this: |
| 77 | + |
| 78 | + ```python |
| 79 | + from identity.django import Auth |
| 80 | + AUTH = Auth("your_client_id", client_credential=..., authority=..., redirect_view="xyz") |
| 81 | + ``` |
| 82 | + |
| 83 | + Generally speaking, your redirect_uri shall contain a top-level path such as |
| 84 | + `http://localhost:5000/redirect`, |
| 85 | + then your setting here shall be `..., redirect_view="redirect")`. |
| 86 | + |
| 87 | +2. Inside the same `mysite/settings.py` file, |
| 88 | + add `"identity",` into the `INSTALLED_APPS` list, |
| 89 | + to enable the default templates came with the `identity` package. |
| 90 | + |
| 91 | + ```python |
| 92 | + INSTALLED_APPS = [ |
| 93 | + ..., |
| 94 | + "identity", |
| 95 | + ] |
| 96 | + ``` |
| 97 | + |
| 98 | +3. Modify the `mysite/urls.py` to add these content: |
| 99 | + |
| 100 | + ```python |
| 101 | + ... |
| 102 | + from django.urls import path, include |
| 103 | + from django.conf import settings |
| 104 | + |
| 105 | + urlpatterns = [ |
| 106 | + path("", include(settings.AUTH.urlpatterns)), |
| 107 | + ... |
| 108 | + ] |
| 109 | + ``` |
| 110 | + |
| 111 | +4. Now, inside the `mysite/views.py`, |
| 112 | + for each view that you would like to enforce user login, |
| 113 | + simply add a one-liner `@settings.AUTH.login_required` before each view. |
| 114 | + For example, |
| 115 | + |
| 116 | + ```python |
| 117 | + ... |
| 118 | + from django.conf import settings |
| 119 | + |
| 120 | + @settings.AUTH.login_required |
| 121 | + def index(request): |
| 122 | + return HttpResponse("Hello, if you can read this, you're signed in.") |
| 123 | + ``` |
| 124 | + |
| 125 | + That is it. Now visit `http://localhost:5000` again, you will see the sign-in experience. |
| 126 | + |
| 127 | + |
| 128 | +### Chapter 2: Get an Access Token and call Microsoft Graph |
| 129 | + |
| 130 | +This chapter begins where chapter 1 left off. |
| 131 | +We will add the following new view which will call a downstream API. |
| 132 | + |
| 133 | +```python |
| 134 | +import json |
| 135 | +from django.shortcuts import redirect, render |
| 136 | +import requests |
| 137 | + |
| 138 | +... |
| 139 | + |
| 140 | +def call_downstream_api(request): |
| 141 | + token = settings.AUTH.get_token_for_user(["your_scope1", "your_scope2"]) |
| 142 | + if "error" in token: |
| 143 | + return redirect(settings.AUTH.login) |
| 144 | + api_result = requests.get( # Use access token to call downstream api |
| 145 | + "https://example.com/your/api", |
| 146 | + headers={'Authorization': 'Bearer ' + token['access_token']}, |
| 147 | + timeout=30, |
| 148 | + ).json() # Here we assume the response format is json |
| 149 | + return render(request, 'display.html', { |
| 150 | + "title": "Result of downstream API call", |
| 151 | + "content": json.dumps(api_result, indent=4), |
| 152 | + }) |
| 153 | +``` |
| 154 | + |
| 155 | +The `settings.AUTH.get_token_for_user(...)` will also implicitly enforce sign-in. |
| 156 | + |
| 157 | +You can refer to our |
| 158 | +[full sample here](https://github.com/Azure-Samples/ms-identity-python-webapp-django) |
| 159 | +to pick up other minor details, such as how to modify `urls.py` accordingly, |
| 160 | +and how to add templates for this new view (and for the existing `index()` view). |
37 | 161 |
|
38 | 162 |
|
39 |
| -## Demo |
| 163 | +### What is next? |
40 | 164 |
|
41 |
| -A demo app is included to show how to use the project. |
| 165 | +You may notice that, this sample's code base contains no templates used by sign-in. |
| 166 | +That is because the upstream helper library provides built-in minimalist templates. |
| 167 | +That is convenient, but at some point you may want to customize the look-and-feel. |
| 168 | +You can do so by copying |
| 169 | +[the upstream templates](https://github.com/rayluo/identity/tree/dev/identity/templates/identity) |
| 170 | +into your own project's `templates/identity` sub-directory, and then start hacking. |
42 | 171 |
|
43 |
| -To run the demo, follow these steps: |
44 | 172 |
|
45 |
| -(Add steps to start up the demo) |
| 173 | +## Contributing |
46 | 174 |
|
47 |
| -1. |
48 |
| -2. |
49 |
| -3. |
| 175 | +If you find a bug in the sample, please raise the issue on [GitHub Issues](../../issues). |
50 | 176 |
|
51 |
| -## Resources |
| 177 | +If you'd like to contribute to this sample, see [CONTRIBUTING.MD](/CONTRIBUTING.md). |
52 | 178 |
|
53 |
| -(Any additional resources or related projects) |
| 179 | +This project has adopted the |
| 180 | +[Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). |
| 181 | +For more information, see the |
| 182 | +[Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) |
| 183 | + |
| 184 | +with any additional questions or comments. |
54 | 185 |
|
55 |
| -- Link to supporting information |
56 |
| -- Link to similar sample |
57 |
| -- ... |
|
0 commit comments