11import uuid
22import flask
33import requests
4- from flask import Flask , render_template , session , request
4+ from flask import Flask , render_template , session , request , url_for
55from flask_session import Session
66import msal
77import app_config
1010app = Flask (__name__ )
1111app .config .from_object ('config.Config' )
1212sess .init_app (app )
13-
1413cache = msal .SerializableTokenCache ()
1514application = 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 ('/' )
2750def 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" )
0 commit comments