Skip to content

Commit 366ea2f

Browse files
authored
Merge pull request #55 from Azure-Samples/auth-code-flow
Switch to new MSAL auth_code_flow API, because its dependency, MSAL Python 1.7.0, is released minutes ago!
2 parents f354954 + 1528e73 commit 366ea2f

File tree

5 files changed

+15
-21
lines changed

5 files changed

+15
-21
lines changed

app.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,23 @@ def index():
2525

2626
@app.route("/login")
2727
def login():
28-
session["state"] = str(uuid.uuid4())
2928
# Technically we could use empty list [] as scopes to do just sign in,
3029
# here we choose to also collect end user consent upfront
31-
auth_url = _build_auth_url(scopes=app_config.SCOPE, state=session["state"])
32-
return render_template("login.html", auth_url=auth_url, version=msal.__version__)
30+
session["flow"] = _build_auth_code_flow(scopes=app_config.SCOPE)
31+
return render_template("login.html", auth_url=session["flow"]["auth_uri"], version=msal.__version__)
3332

3433
@app.route(app_config.REDIRECT_PATH) # Its absolute URL must match your app's redirect_uri set in AAD
3534
def authorized():
36-
if request.args.get('state') != session.get("state"):
37-
return redirect(url_for("index")) # No-OP. Goes back to Index page
38-
if "error" in request.args: # Authentication/Authorization failure
39-
return render_template("auth_error.html", result=request.args)
40-
if request.args.get('code'):
35+
try:
4136
cache = _load_cache()
42-
result = _build_msal_app(cache=cache).acquire_token_by_authorization_code(
43-
request.args['code'],
44-
scopes=app_config.SCOPE, # Misspelled scope would cause an HTTP 400 error here
45-
redirect_uri=url_for("authorized", _external=True))
37+
result = _build_msal_app(cache=cache).acquire_token_by_auth_code_flow(
38+
session.get("flow", {}), request.args)
4639
if "error" in result:
47-
return render_template("auth_error.html", result=result)
40+
return render_template("error.html", result)
4841
session["user"] = result.get("id_token_claims")
4942
_save_cache(cache)
43+
except ValueError: # Usually caused by CSRF
44+
pass # Simply ignore them
5045
return redirect(url_for("index"))
5146

5247
@app.route("/logout")
@@ -83,10 +78,9 @@ def _build_msal_app(cache=None, authority=None):
8378
app_config.CLIENT_ID, authority=authority or app_config.AUTHORITY,
8479
client_credential=app_config.CLIENT_SECRET, token_cache=cache)
8580

86-
def _build_auth_url(authority=None, scopes=None, state=None):
87-
return _build_msal_app(authority=authority).get_authorization_request_url(
81+
def _build_auth_code_flow(authority=None, scopes=None):
82+
return _build_msal_app(authority=authority).initiate_auth_code_flow(
8883
scopes or [],
89-
state=state or str(uuid.uuid4()),
9084
redirect_uri=url_for("authorized", _external=True))
9185

9286
def _get_token_from_cache(scope=None):
@@ -98,7 +92,7 @@ def _get_token_from_cache(scope=None):
9892
_save_cache(cache)
9993
return result
10094

101-
app.jinja_env.globals.update(_build_auth_url=_build_auth_url) # Used in template
95+
app.jinja_env.globals.update(_build_auth_code_flow=_build_auth_code_flow) # Used in template
10296

10397
if __name__ == "__main__":
10498
app.run()

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Flask>=1,<2
22
werkzeug>=1,<2
33
flask-session~=0.3.2
44
requests>=2,<3
5-
msal>=0.6.1,<2
5+
msal>=1.7,<2
66

77
# cachelib==0.1 # Only need this if you are running Python 2
88
# Note: This sample does NOT directly depend on cachelib.

templates/auth_error.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
{% if config.get("B2C_RESET_PASSWORD_AUTHORITY") and "AADB2C90118" in result.get("error_description") %}
77
<!-- See also https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-reference-policies#linking-user-flows -->
8-
<meta http-equiv="refresh" content='0;{{_build_auth_url(authority=config["B2C_RESET_PASSWORD_AUTHORITY"])}}'>
8+
<meta http-equiv="refresh" content='0;{{_build_auth_code_flow(authority=config["B2C_RESET_PASSWORD_AUTHORITY"])["auth_uri"]}}'>
99
{% endif %}
1010
</head>
1111
<body>

templates/index.html

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

1414
{% if config.get("B2C_PROFILE_AUTHORITY") %}
15-
<li><a href='{{_build_auth_url(authority=config["B2C_PROFILE_AUTHORITY"])}}'>Edit Profile</a></li>
15+
<li><a href='{{_build_auth_code_flow(authority=config["B2C_PROFILE_AUTHORITY"])["auth_uri"]}}'>Edit Profile</a></li>
1616
{% endif %}
1717

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

templates/login.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ <h1>Microsoft Identity Python Web App</h1>
99
<li><a href='{{ auth_url }}'>Sign In</a></li>
1010

1111
{% if config.get("B2C_RESET_PASSWORD_AUTHORITY") %}
12-
<li><a href='{{_build_auth_url(authority=config["B2C_RESET_PASSWORD_AUTHORITY"])}}'>Reset Password</a></li>
12+
<li><a href='{{_build_auth_code_flow(authority=config["B2C_RESET_PASSWORD_AUTHORITY"])["auth_uri"]}}'>Reset Password</a></li>
1313
{% endif %}
1414

1515
<hr>

0 commit comments

Comments
 (0)