Skip to content

Commit 9ee34f5

Browse files
committed
Refactoring the code
1 parent dbf55ae commit 9ee34f5

File tree

3 files changed

+68
-40
lines changed

3 files changed

+68
-40
lines changed

app.py

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import uuid
22
import flask
33
import requests
4-
from flask import Flask, render_template, session, request
4+
from flask import Flask, render_template, session, request, url_for
55
from flask_session import Session
66
import msal
77
import app_config
@@ -10,7 +10,6 @@
1010
app = Flask(__name__)
1111
app.config.from_object('config.Config')
1212
sess.init_app(app)
13-
1413
cache = msal.SerializableTokenCache()
1514
application = msal.ConfidentialClientApplication(
1615
app_config.CLIENT_ID, authority=app_config.AUTHORITY,
@@ -23,13 +22,53 @@ def set_cache():
2322
session[request.cookies.get("session")] = cache.serialize()
2423

2524

25+
def check_cache():
26+
# Checking token cache for accounts
27+
result = None
28+
accounts = application.get_accounts()
29+
30+
# Trying to acquire token silently
31+
if accounts:
32+
result = application.acquire_token_silent(app_config.SCOPE, account=accounts[0])
33+
return result
34+
35+
36+
def get_graph_info(result):
37+
if 'access_token' not in result:
38+
return flask.redirect(flask.url_for('index'))
39+
endpoint = 'https://graph.microsoft.com/v1.0/me/'
40+
http_headers = {'Authorization': 'Bearer ' + result['access_token'],
41+
'User-Agent': 'msal-python-sample',
42+
'Accept': 'application/json',
43+
'Content-Type': 'application/json',
44+
'client-request-id': str(uuid.uuid4())}
45+
graph_data = requests.get(endpoint, headers=http_headers, stream=False).json()
46+
return graph_data
47+
48+
2649
@app.route('/')
2750
def index():
51+
return render_template("index.html")
52+
53+
54+
@app.route('/processing')
55+
def processing():
2856
# Initializing
29-
if (session.get(request.cookies.get("session"), '')) == '':
57+
is_session = session.get(request.cookies.get("session"))
58+
if is_session is None:
3059
session[request.cookies.get("session")] = ''
3160
cache.deserialize(session.get(request.cookies.get("session")))
32-
return render_template("index.html")
61+
return flask.redirect(url_for('my_info'))
62+
63+
64+
@app.route('/my_info')
65+
def my_info():
66+
result = check_cache()
67+
if result:
68+
graph_result = get_graph_info(result)
69+
return flask.render_template('display.html', auth_result=graph_result, cond="logout")
70+
else:
71+
return flask.render_template('display.html', auth_result="You are not signed in", cond="")
3372

3473

3574
@app.route('/authenticate')
@@ -51,32 +90,14 @@ def main_logic():
5190
# Raising error if state does not match
5291
if state != session[(request.cookies.get("session")+'state')]:
5392
raise ValueError("State does not match")
54-
result = None
55-
# Checking token cache for accounts
56-
accounts = application.get_accounts()
57-
58-
# Trying to acquire token silently
59-
if accounts:
60-
result = application.acquire_token_silent(app_config.SCOPE, account=accounts[0])
61-
62-
# If silent call fails, fallback to acquireToken interactive call
63-
if not result:
64-
result = application.acquire_token_by_authorization_code(code, scopes=app_config.SCOPE,
65-
redirect_uri=app_config.REDIRECT_URI)
93+
result = application.acquire_token_by_authorization_code(code, scopes=app_config.SCOPE,
94+
redirect_uri=app_config.REDIRECT_URI)
6695
# Updating cache
6796
set_cache()
6897

6998
# Using access token from result to call Microsoft Graph
70-
if 'access_token' not in result:
71-
return flask.redirect(flask.url_for('index'))
72-
endpoint = 'https://graph.microsoft.com/v1.0/me/'
73-
http_headers = {'Authorization': 'Bearer ' + result['access_token'],
74-
'User-Agent': 'msal-python-sample',
75-
'Accept': 'application/json',
76-
'Content-Type': 'application/json',
77-
'client-request-id': str(uuid.uuid4())}
78-
graph_data = requests.get(endpoint, headers=http_headers, stream=False).json()
79-
return flask.render_template('display.html', auth_result=graph_data)
99+
graph_data = get_graph_info(result)
100+
return flask.render_template('display.html', auth_result=graph_data, cond="logout")
80101

81102

82103
@app.route("/logout")

templates/display.html

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@
55
<title>Acquire Token Result </title>
66
</head>
77
<body>
8-
<p1><b>Your information</b> </p1>
9-
<table>
10-
{% for key, value in auth_result.items() %}
11-
<tr>
12-
<th> {{ key }} </th>
13-
<td> {{ value }} </td>
14-
</tr>
15-
{% endfor %}
16-
</table>
17-
<form action="/logout" >
18-
<input type="submit" value=" Logout"/>
19-
</form>
8+
{% if cond %}
9+
<p1><b>Your information</b> </p1>
10+
<table>
11+
{% for key, value in auth_result.items() %}
12+
<tr>
13+
<th> {{ key }} </th>
14+
<td> {{ value }} </td>
15+
</tr>
16+
{% endfor %}
17+
</table>
18+
<form action="/logout" >
19+
<input type="submit" value=" Logout"/>
20+
</form>
21+
{% else %}
22+
<p1><b> {{auth_result}} </b> </p1>
23+
<form action="/authenticate" >
24+
<input type="submit" value=" Sign-in"/>
25+
</form>
26+
{% endif %}
2027
</body>
2128
</html>

templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<title>Title</title>
66
</head>
77
<body>
8-
<form action="/authenticate" >
9-
<input type="submit" value="Get my info from graph"/>
8+
<form action="/processing" >
9+
<input type="submit" value="Get my information from graph"/>
1010
</form>
1111
</body>
1212
</html>

0 commit comments

Comments
 (0)