Skip to content

Commit 1c8a88b

Browse files
committed
Merge branch 'refactor'
2 parents 4863747 + 75f4f23 commit 1c8a88b

4 files changed

Lines changed: 28 additions & 21 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ In the steps below, "ClientID" is the same as "Application ID" or "AppId".
138138
$ pip install -r requirements.txt
139139
```
140140

141-
Run app.py from shell or command line:
141+
Run app.py from shell or command line. Note that the port needs to match what you've set up in your redirect_uri:
142142
```Shell
143-
$ python app.py
143+
$ flask run --port 5000
144144
```
145145

146146
## Community Help and Support

app.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,26 @@ def login():
2727
redirect_uri=url_for("authorized", _external=True))
2828
return "<a href='%s'>Login with Microsoft Identity</a>" % auth_url
2929

30-
@app.route("/getAToken") # Its absolute URL must match your app's redirect_uri set in AAD
30+
@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['state'] != session.get("state"):
33-
return redirect(url_for("login"))
34-
cache = _load_cache()
35-
result = _build_msal_app(cache).acquire_token_by_authorization_code(
36-
request.args['code'],
37-
scopes=app_config.SCOPE, # Misspelled scope would cause an HTTP 400 error here
38-
redirect_uri=url_for("authorized", _external=True))
39-
if "error" in result:
40-
return "Login failure: %s, %s" % (
41-
result["error"], result.get("error_description"))
42-
session["user"] = result.get("id_token_claims")
43-
_save_cache(cache)
32+
if request.args.get('state') == session.get("state"):
33+
cache = _load_cache()
34+
result = _build_msal_app(cache=cache).acquire_token_by_authorization_code(
35+
request.args['code'],
36+
scopes=app_config.SCOPE, # Misspelled scope would cause an HTTP 400 error here
37+
redirect_uri=url_for("authorized", _external=True))
38+
if "error" in result:
39+
return "Login failure: %s, %s" % (
40+
result["error"], result.get("error_description"))
41+
session["user"] = result.get("id_token_claims")
42+
_save_cache(cache)
4443
return redirect(url_for("index"))
4544

4645
@app.route("/logout")
4746
def logout():
4847
session.clear() # Wipe out user and its token cache from session
49-
return redirect( # Also need to logout from Microsoft Identity platform
50-
"https://login.microsoftonline.com/common/oauth2/v2.0/logout"
48+
return redirect( # Also logout from your tenant's web session
49+
app_config.AUTHORITY + "/oauth2/v2.0/logout" +
5150
"?post_logout_redirect_uri=" + url_for("index", _external=True))
5251

5352
@app.route("/graphcall")
@@ -79,7 +78,7 @@ def _build_msal_app(cache=None):
7978

8079
def _get_token_from_cache(scope=None):
8180
cache = _load_cache() # This web app maintains one cache per session
82-
cca = _build_msal_app(cache)
81+
cca = _build_msal_app(cache=cache)
8382
accounts = cca.get_accounts()
8483
if accounts: # So all account(s) belong to the current signed-in user
8584
result = cca.acquire_token_silent(scope, account=accounts[0])

app_config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
CLIENT_ID = "Enter_the_Application_Id_here"
1515

16+
REDIRECT_PATH = "/getAToken" # It will be used to form an absolute URL
17+
# And that absolute URL must match your app's redirect_uri set in AAD
18+
1619
# You can find more Microsoft Graph API endpoints from Graph Explorer
1720
# https://developer.microsoft.com/en-us/graph/graph-explorer
1821
ENDPOINT = 'https://graph.microsoft.com/v1.0/users' # This resource requires no admin consent

templates/index.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
</head>
66
<body>
77
<h1>Microsoft Identity Python Web App</h1>
8-
<h2>Powered by MSAL Python {{ version }}</h2>
9-
Welcome {{ user.get("name") }}!
10-
<li><a href='/graphcall'>Call Microsoft Graph API</a></li>
8+
<h2>Welcome {{ user.get("name") }}!</h2>
9+
10+
{% if config.get("ENDPOINT") %}
11+
<li><a href='/graphcall'>Call Microsoft Graph API</a></li>
12+
{% endif %}
13+
1114
<li><a href="/logout">Logout</a></li>
15+
<hr>
16+
<footer style="text-align: right">Powered by MSAL Python {{ version }}</footer>
1217
</body>
1318
</html>
1419

0 commit comments

Comments
 (0)