Skip to content

Commit 2ba4489

Browse files
committed
Apply enhancements from upstream helper library
1 parent ecc9610 commit 2ba4489

File tree

7 files changed

+14
-22
lines changed

7 files changed

+14
-22
lines changed

.env.sample

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ AUTHORITY=https://login.microsoftonline.com/common
1919
# configure AUTHORITY as "https://subdomain.ciamlogin.com"
2020
#AUTHORITY=<authority url>
2121

22-
REDIRECT_VIEW=getAToken # Used for forming an absolute URL to your redirect URI.
23-
# The absolute URL must match the redirect URI you set
24-
# in the app's registration in the Azure portal.
22+
# Your project's redirect URI. For example: http://localhost:5000/redirect
23+
REDIRECT_URI=<your redirect uri>
2524

2625
# You can use your own API's scope. Here we use a Microsoft Graph API as an example
2726
SCOPE=User.ReadBasic.All

README.md

+4-10
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,9 @@ as a reference. What we need are these steps:
7777

7878
```python
7979
from identity.django import Auth
80-
AUTH = Auth("your_client_id", client_credential=..., authority=..., redirect_view="xyz")
80+
AUTH = Auth("your_client_id", client_credential=..., authority=..., redirect_uri=...)
8181
```
8282

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-
8783
2. Inside the same `mysite/settings.py` file,
8884
add `"identity",` into the `INSTALLED_APPS` list,
8985
to enable the default templates came with the `identity` package.
@@ -99,11 +95,10 @@ as a reference. What we need are these steps:
9995

10096
```python
10197
...
102-
from django.urls import path, include
10398
from django.conf import settings
10499

105100
urlpatterns = [
106-
path("", include(settings.AUTH.urlpatterns)),
101+
settings.AUTH.urlpattern,
107102
...
108103
]
109104
```
@@ -119,7 +114,7 @@ as a reference. What we need are these steps:
119114

120115
@settings.AUTH.login_required
121116
def index(request):
122-
return HttpResponse("Hello, if you can read this, you're signed in.")
117+
return HttpResponse("Hello, only signed-in user can read this.")
123118
```
124119

125120
That is it. Now visit `http://localhost:5000` again, you will see the sign-in experience.
@@ -137,6 +132,7 @@ import requests
137132

138133
...
139134

135+
# here we demonstrate how to handle the error explicitly.
140136
def call_downstream_api(request):
141137
token = settings.AUTH.get_token_for_user(["your_scope1", "your_scope2"])
142138
if "error" in token:
@@ -152,8 +148,6 @@ def call_downstream_api(request):
152148
})
153149
```
154150

155-
The `settings.AUTH.get_token_for_user(...)` will also implicitly enforce sign-in.
156-
157151
You can refer to our
158152
[full sample here](https://github.com/Azure-Samples/ms-identity-python-webapp-django)
159153
to pick up other minor details, such as how to modify `urls.py` accordingly,

mysite/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
AUTH = Auth(
1919
os.getenv('CLIENT_ID'),
2020
client_credential=os.getenv('CLIENT_SECRET'),
21-
redirect_view=os.getenv('REDIRECT_VIEW'),
21+
redirect_uri=os.getenv('REDIRECT_URI'),
2222
scopes=os.getenv('SCOPE', "").split(),
2323
authority=os.getenv('AUTHORITY'),
2424
)

mysite/urls.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1616
"""
1717
from django.contrib import admin
18-
from django.urls import path, include
18+
from django.urls import path
1919
from django.conf import settings
2020

2121
from . import views
2222

2323

2424
urlpatterns = [
25-
path("", include(settings.AUTH.urlpatterns)),
25+
settings.AUTH.urlpattern,
2626
path('', views.index, name="index"),
2727
path("call_downstream_api", views.call_downstream_api),
2828
path('admin/', admin.site.urls),

mysite/views.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import requests
77

88

9-
__version__ = "0.1.0"
9+
__version__ = "0.2.0"
1010

1111

1212
@settings.AUTH.login_required
@@ -20,9 +20,8 @@ def index(request):
2020
downstream_api=os.getenv("ENDPOINT"),
2121
))
2222

23-
# We choose to not decorate this view with @login_required,
24-
# because its get_token_for_user() could ask for more scopes than initial login,
25-
# so we want to handle the error separately.
23+
# Instead of using the login_required decorator,
24+
# here we demonstrate how to handle the error explicitly.
2625
def call_downstream_api(request):
2726
token = settings.AUTH.get_token_for_user(request, os.getenv("SCOPE", "").split())
2827
if "error" in token:

requirements.txt

+1-1
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.4,<0.5
5+
identity>=0.5,<0.6
66
python-dotenv<0.22
77
requests>=2,<3

templates/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h2>Welcome {{ user.name }}!</h2>
1717
<li><a href='{{ edit_profile_url }}'>Edit Profile</a></li>
1818
{% endif %}
1919

20-
<li><a href="/logout">Logout</a></li>
20+
<li><a href="{% url 'identity.django.logout' %}">Logout</a></li>
2121
</ul>
2222
</body>
2323
</html>

0 commit comments

Comments
 (0)