@@ -25,28 +25,23 @@ def index():
2525
2626@app .route ("/login" )
2727def 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
3534def 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
9286def _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
10397if __name__ == "__main__" :
10498 app .run ()
0 commit comments