@@ -25,28 +25,23 @@ def index():
25
25
26
26
@app .route ("/login" )
27
27
def login ():
28
- session ["state" ] = str (uuid .uuid4 ())
29
28
# Technically we could use empty list [] as scopes to do just sign in,
30
29
# 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__ )
33
32
34
33
@app .route (app_config .REDIRECT_PATH ) # Its absolute URL must match your app's redirect_uri set in AAD
35
34
def 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 :
41
36
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 )
46
39
if "error" in result :
47
- return render_template ("auth_error .html" , result = result )
40
+ return render_template ("error .html" , result )
48
41
session ["user" ] = result .get ("id_token_claims" )
49
42
_save_cache (cache )
43
+ except ValueError : # Usually caused by CSRF
44
+ pass # Simply ignore them
50
45
return redirect (url_for ("index" ))
51
46
52
47
@app .route ("/logout" )
@@ -83,10 +78,9 @@ def _build_msal_app(cache=None, authority=None):
83
78
app_config .CLIENT_ID , authority = authority or app_config .AUTHORITY ,
84
79
client_credential = app_config .CLIENT_SECRET , token_cache = cache )
85
80
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 (
88
83
scopes or [],
89
- state = state or str (uuid .uuid4 ()),
90
84
redirect_uri = url_for ("authorized" , _external = True ))
91
85
92
86
def _get_token_from_cache (scope = None ):
@@ -98,7 +92,7 @@ def _get_token_from_cache(scope=None):
98
92
_save_cache (cache )
99
93
return result
100
94
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
102
96
103
97
if __name__ == "__main__" :
104
98
app .run ()
0 commit comments