@@ -25,25 +25,18 @@ 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
+ if ("flow" in session and ("code" in request .args or "error" in request .args )
36
+ and request .args .get ('state' ) == session ["flow" ].get ("state" )):
41
37
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 ))
46
- if "error" in result :
38
+ result = _build_msal_app (cache = cache ).acquire_token_by_auth_code_flow (session ["flow" ], request .args )
39
+ if "error" in result : # Authentication/Authorization failure
47
40
return render_template ("auth_error.html" , result = result )
48
41
session ["user" ] = result .get ("id_token_claims" )
49
42
_save_cache (cache )
@@ -83,10 +76,9 @@ def _build_msal_app(cache=None, authority=None):
83
76
app_config .CLIENT_ID , authority = authority or app_config .AUTHORITY ,
84
77
client_credential = app_config .CLIENT_SECRET , token_cache = cache )
85
78
86
- def _build_auth_url (authority = None , scopes = None , state = None ):
87
- return _build_msal_app (authority = authority ).get_authorization_request_url (
79
+ def _build_auth_code_flow (authority = None , scopes = None ):
80
+ return _build_msal_app (authority = authority ).initiate_auth_code_flow (
88
81
scopes or [],
89
- state = state or str (uuid .uuid4 ()),
90
82
redirect_uri = url_for ("authorized" , _external = True ))
91
83
92
84
def _get_token_from_cache (scope = None ):
@@ -98,7 +90,7 @@ def _get_token_from_cache(scope=None):
98
90
_save_cache (cache )
99
91
return result
100
92
101
- app .jinja_env .globals .update (_build_auth_url = _build_auth_url ) # Used in template
93
+ app .jinja_env .globals .update (_build_auth_code_flow = _build_auth_code_flow ) # Used in template
102
94
103
95
if __name__ == "__main__" :
104
96
app .run ()
0 commit comments