Skip to content

Commit 7b31de6

Browse files
committed
Simplified based on new APIs from identity v0.6
1 parent 6839e50 commit 7b31de6

4 files changed

Lines changed: 13 additions & 20 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ to sign in users and make authenticated calls to the Microsoft Graph API.
2727
how to build this from scratch, or how to add auth to your existing project.)
2828
2. `cd project_name`
2929
3. Run `pip install -r requirements.txt` to install dependencies
30-
4. Run `python manage.py migrate` to initialize your Django project
31-
5. Copy [`.env.sample`](https://github.com/Azure-Samples/ms-identity-python-webapp-django/blob/main/.env.sample) as `.env`,
30+
4. Copy [`.env.sample`](https://github.com/Azure-Samples/ms-identity-python-webapp-django/blob/main/.env.sample) as `.env`,
3231
and then modify its content based on your application's registration.
32+
5. Run `python manage.py migrate` to initialize your Django project
3333

3434
### Quickstart
3535

mysite/settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
os.getenv('CLIENT_ID'),
2020
client_credential=os.getenv('CLIENT_SECRET'),
2121
redirect_uri=os.getenv('REDIRECT_URI'),
22-
scopes=os.getenv('SCOPE', "").split(),
2322
authority=os.getenv('AUTHORITY'),
2423
)
2524

mysite/views.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,29 @@
22
import json
33

44
from django.conf import settings
5-
from django.shortcuts import redirect, render
5+
from django.shortcuts import render
66
import requests
77

88

9-
__version__ = "0.3.0"
9+
__version__ = "0.4.0"
1010

1111

1212
@settings.AUTH.login_required
13-
def index(request):
14-
user = settings.AUTH.get_user(request)
15-
assert user # User would not be None since we decorated this view with @login_required
13+
def index(request, *, context):
1614
return render(request, 'index.html', dict(
17-
user=user,
15+
user=context['user'],
1816
version=__version__,
19-
edit_profile_url=settings.AUTH.get_edit_profile_url(request),
17+
edit_profile_url=settings.AUTH.get_edit_profile_url(),
2018
downstream_api=os.getenv("ENDPOINT"),
2119
))
2220

23-
# Instead of using the login_required decorator,
24-
# here we demonstrate how to handle the error explicitly.
25-
def call_downstream_api(request):
26-
token = settings.AUTH.get_token_for_user(request, os.getenv("SCOPE", "").split())
27-
if "error" in token:
28-
return redirect(settings.AUTH.login)
29-
api_result = requests.get( # Use access token to call downstream api
21+
@settings.AUTH.login_required(scopes=os.getenv("SCOPE", "").split())
22+
def call_downstream_api(request, *, context):
23+
api_result = requests.get( # Use access token to call a web api
3024
os.getenv("ENDPOINT"),
31-
headers={'Authorization': 'Bearer ' + token['access_token']},
25+
headers={'Authorization': 'Bearer ' + context['access_token']},
3226
timeout=30,
33-
).json() # Here we assume the response format is json
27+
).json() if context.get('access_token') else "Did you forget to set the SCOPE environment variable?"
3428
return render(request, 'display.html', {
3529
"title": "Result of downstream API call",
3630
"content": json.dumps(api_result, indent=4),

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
# See https://docs.djangoproject.com/en/5.0/faq/install/#what-python-version-can-i-use-with-django
33
django>=3.2,<6
44

5-
identity>=0.5.2,<0.6
5+
identity>=0.6,<0.7
66
python-dotenv<0.22
77
requests>=2,<3

0 commit comments

Comments
 (0)