Skip to content

Commit 0ecbd97

Browse files
committed
Catch and render potential auth failure
1 parent e448a22 commit 0ecbd97

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

app.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@ def login():
2929

3030
@app.route(app_config.REDIRECT_PATH) # Its absolute URL must match your app's redirect_uri set in AAD
3131
def authorized():
32-
if request.args.get('state') == session.get("state"):
32+
if request.args.get('state') != session.get("state"):
33+
return redirect(url_for("index")) # No-OP. Goes back to Index page
34+
if "error" in request.args: # Authentication/Authorization failure
35+
return render_template("auth_error.html", result=request.args)
36+
if request.args.get('code'):
3337
cache = _load_cache()
3438
result = _build_msal_app(cache=cache).acquire_token_by_authorization_code(
3539
request.args['code'],
3640
scopes=app_config.SCOPE, # Misspelled scope would cause an HTTP 400 error here
3741
redirect_uri=url_for("authorized", _external=True))
3842
if "error" in result:
39-
return "Login failure: %s, %s" % (
40-
result["error"], result.get("error_description"))
43+
return render_template("auth_error.html", result=result)
4144
session["user"] = result.get("id_token_claims")
4245
_save_cache(cache)
4346
return redirect(url_for("index"))

templates/auth_error.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
</head>
6+
<body>
7+
<h2>Login Failure</h2>
8+
<dl>
9+
<dt>{{ result.get("error") }}</dt>
10+
<dd>{{ result.get("error_description") }}</dd>
11+
</dl>
12+
<hr>
13+
<a href="{{ url_for('index') }}">Homepage</a>
14+
</body>
15+
</html>
16+

0 commit comments

Comments
 (0)