Skip to content

Commit 537ab24

Browse files
committed
Merge branch 'b2c-fix'
2 parents 67e4a47 + 5ac7288 commit 537ab24

File tree

8 files changed

+58
-51
lines changed

8 files changed

+58
-51
lines changed

.env.sample

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
# Note: If you are using Azure App Service, go to your app's Configuration,
22
# and then set the following values into your app's "Application settings".
33

4+
# The following variables are required for the app to run.
45
CLIENT_ID=<client id>
56
CLIENT_SECRET=<client secret>
67

7-
# The AUTHORITY variable expects a full authority URL.
8+
# This sample can be configured as a Microsoft Entra ID app,
9+
# a Microsoft Entra External ID app, or a B2C app.
10+
11+
# 1. If you are using a Microsoft Entra ID tenent,
12+
# configure the AUTHORITY variable as
13+
# "https://login.microsoftonline.com/TENANT_GUID"
14+
# or "https://login.microsoftonline.com/subdomain.onmicrosoft.com".
815
#
9-
# If you are using an AAD tenent, configure it as
10-
# "https://login.microsoftonline.com/TENANT_GUID"
11-
# or "https://login.microsoftonline.com/subdomain.onmicrosoft.com".
16+
# Alternatively, leave it undefined if you are building a multi-tenant AAD app
17+
# in world-wide cloud
18+
#AUTHORITY=<authority url>
1219
#
13-
# If you are using a CIAM tenant, configure it as "https://subdomain.ciamlogin.com"
1420
#
15-
# Alternatively, leave it undefined if you are building a multi-tenant app in world-wide cloud
21+
# 2. If you are using a Microsoft Entra External ID for customers (CIAM) tenant,
22+
# configure AUTHORITY as "https://subdomain.ciamlogin.com"
1623
#AUTHORITY=<authority url>
24+
#
25+
#
26+
# 3. If you are using a B2C tenant, configure the following variables:
27+
# Note the B2C_TENANT_NAME shall be the display name such as "contoso"
28+
#
29+
#B2C_TENANT_NAME=<tenant name>
30+
SIGNUPSIGNIN_USER_FLOW=B2C_1_signupsignin1
31+
EDITPROFILE_USER_FLOW=B2C_1_profile_editing
32+
RESETPASSWORD_USER_FLOW=B2C_1_reset_password

.env.sample.b2c

Lines changed: 0 additions & 9 deletions
This file was deleted.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@ urlFragment: ms-identity-python-webapp
1111

1212
This is a Python web application that uses the Flask framework and the Microsoft identity platform to sign in users and make authenticated calls to the Microsoft Graph API.
1313

14+
# Configuration
15+
16+
## If you are configuring your Microsoft Entra ID app or Microsoft Entra External ID app
17+
1418
To get started with this sample, you have two options:
1519

1620
* Use the Azure portal to create the Azure AD applications and related objects. Follow the steps in
1721
[Quickstart: Add sign-in with Microsoft to a Python web app](https://docs.microsoft.com/azure/active-directory/develop/web-app-quickstart?pivots=devlang-python).
1822
* Use PowerShell scripts that automatically create the Azure AD applications and related objects (passwords, permissions, dependencies) for you, and then modify the configuration files. Follow the steps in the [App Creation Scripts README](./AppCreationScripts/AppCreationScripts.md).
1923

24+
## If you are configuring your B2C app
25+
26+
This sample can also work as a B2C app. If you are using a B2C tenant, follow
27+
[Configure authentication in a sample Python web app by using Azure AD B2C](https://learn.microsoft.com/azure/active-directory-b2c/configure-authentication-sample-python-web-app).
28+
29+
2030
# Deployment
2131

2232
Once you finish testing this web app locally, you can deploy it to your production.

README_B2C.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from werkzeug.middleware.proxy_fix import ProxyFix
2020
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
2121

22+
app.jinja_env.globals.update(Auth=identity.web.Auth) # Useful in template for B2C
2223
auth = identity.web.Auth(
2324
session=session,
2425
authority=app.config["AUTHORITY"],

app_config.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
import os
22

3+
4+
if (os.getenv('B2C_TENANT_NAME')
5+
and os.getenv('SIGNUPSIGNIN_USER_FLOW') and os.getenv('EDITPROFILE_USER_FLOW')):
6+
# This branch is for B2C. You can delete this branch if you are not using B2C.
7+
b2c_tenant = os.getenv('B2C_TENANT_NAME')
8+
authority_template = "https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/{user_flow}"
9+
AUTHORITY = authority_template.format(
10+
tenant=b2c_tenant,
11+
user_flow=os.getenv('SIGNUPSIGNIN_USER_FLOW'))
12+
B2C_PROFILE_AUTHORITY = authority_template.format(
13+
tenant=b2c_tenant,
14+
user_flow=os.getenv('EDITPROFILE_USER_FLOW'))
15+
B2C_RESET_PASSWORD_AUTHORITY = authority_template.format(
16+
# If you are using the new "Recommended user flow"
17+
# (https://docs.microsoft.com/en-us/azure/active-directory-b2c/user-flow-versions),
18+
# you can remove the B2C_RESET_PASSWORD_AUTHORITY settings from this file.
19+
tenant=b2c_tenant,
20+
user_flow=os.getenv('RESETPASSWORD_USER_FLOW'))
21+
else: # This branch is for AAD or CIAM
22+
# You can configure your authority via environment variable
23+
# Defaults to a multi-tenant app in world-wide cloud
24+
AUTHORITY = os.getenv("AUTHORITY") or "https://login.microsoftonline.com/common"
25+
326
# Application (client) ID of app registration
427
CLIENT_ID = os.getenv("CLIENT_ID")
528
# Application's generated client secret: never check this into source control!
629
CLIENT_SECRET = os.getenv("CLIENT_SECRET")
730

8-
# You can configure your authority via environment variable
9-
# Defaults to a multi-tenant app in world-wide cloud
10-
AUTHORITY = os.getenv("AUTHORITY", "https://login.microsoftonline.com/common")
11-
1231
REDIRECT_PATH = "/getAToken" # Used for forming an absolute URL to your redirect URI.
1332
# The absolute URL must match the redirect URI you set
1433
# in the app's registration in the Azure portal.

templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h2>Welcome {{ user.get("name") }}!</h2>
1414
{% endif %}
1515

1616
{% if config.get("B2C_PROFILE_AUTHORITY") %}
17-
<li><a href='{{config.get("B2C_PROFILE_AUTHORITY")}}?client_id={{config.get("CLIENT_ID")}}'>Edit Profile</a></li>
17+
<li><a href='{{Auth(session={}, authority=config["B2C_PROFILE_AUTHORITY"], client_id=config["CLIENT_ID"]).log_in(redirect_uri=url_for("auth_response", _external=True))["auth_uri"]}}'>Edit Profile</a></li>
1818
{% endif %}
1919

2020
<li><a href="/logout">Logout</a></li>

templates/login.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h1>Microsoft Identity Python Web App</h1>
2020
{% endif %}
2121

2222
{% if config.get("B2C_RESET_PASSWORD_AUTHORITY") %}
23-
<a href="{{config.get('B2C_RESET_PASSWORD_AUTHORITY')}}?client_id={{config.get('CLIENT_ID')}}">Reset Password</a>
23+
<a href='{{Auth(session={}, authority=config["B2C_RESET_PASSWORD_AUTHORITY"], client_id=config["CLIENT_ID"]).log_in(redirect_uri=url_for("auth_response", _external=True))["auth_uri"]}}'>Reset Password</a>
2424
{% endif %}
2525

2626
<hr>

0 commit comments

Comments
 (0)