awacke1 commited on
Commit
b19be65
Β·
verified Β·
1 Parent(s): c97ecbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -14
app.py CHANGED
@@ -164,7 +164,6 @@ def clear_query_params():
164
 
165
  def process_query_params():
166
  query_params = st.query_params
167
- st.write("Debug: All query parameters:", query_params)
168
 
169
  if 'error' in query_params:
170
  error = query_params.get('error')
@@ -174,23 +173,25 @@ def process_query_params():
174
  st.session_state.clear()
175
  st.rerun()
176
 
177
- if 'code' in query_params:
 
 
 
 
 
 
178
  code = query_params.get('code')
179
- st.write('πŸ”‘ Authorization Code Obtained:', code[:10] + '...')
180
-
181
  try:
182
- access_token = get_access_token(code)
183
- st.session_state['access_token'] = access_token
184
- st.success("Access token acquired successfully!")
 
 
185
  st.rerun()
186
  except Exception as e:
187
- if "AADSTS70000" in str(e) and "code has expired" in str(e):
188
- st.error("The authorization code has expired. Please log in again.")
189
- st.session_state.clear()
190
- st.rerun()
191
- else:
192
- st.error(f"Error acquiring access token: {str(e)}")
193
- st.stop()
194
 
195
  def process_query_params3():
196
  query_params = st.query_params
@@ -303,6 +304,30 @@ def get_user_info(access_token):
303
  else:
304
  raise Exception(f"Failed to fetch user info: {response.status_code} - {response.text}")
305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
 
307
  # πŸƒβ€β™‚οΈ Main application function
308
  def main():
@@ -310,6 +335,17 @@ def main():
310
 
311
  process_query_params()
312
 
 
 
 
 
 
 
 
 
 
 
 
313
  #added
314
  if 'access_token' not in st.session_state:
315
  client_instance = get_msal_app()
 
164
 
165
  def process_query_params():
166
  query_params = st.query_params
 
167
 
168
  if 'error' in query_params:
169
  error = query_params.get('error')
 
173
  st.session_state.clear()
174
  st.rerun()
175
 
176
+ if 'code' in query_params and 'state' in query_params:
177
+ received_state = query_params.get('state')
178
+ if received_state != st.session_state.get('auth_state'):
179
+ st.error("Invalid state parameter. Please try logging in again.")
180
+ st.session_state.clear()
181
+ st.rerun()
182
+
183
  code = query_params.get('code')
 
 
184
  try:
185
+ token = get_access_token(code)
186
+ st.session_state['access_token'] = token
187
+ st.success("Successfully authenticated!")
188
+ # Clear the URL parameters
189
+ st.experimental_set_query_params()
190
  st.rerun()
191
  except Exception as e:
192
+ st.error(f"Error acquiring access token: {str(e)}")
193
+ st.session_state.clear()
194
+ st.rerun()
 
 
 
 
195
 
196
  def process_query_params3():
197
  query_params = st.query_params
 
304
  else:
305
  raise Exception(f"Failed to fetch user info: {response.status_code} - {response.text}")
306
 
307
+ def initiate_auth_flow():
308
+ client_instance = get_msal_app()
309
+ auth_url = client_instance.get_authorization_request_url(
310
+ scopes=SCOPES,
311
+ redirect_uri=REDIRECT_URI,
312
+ state=generate_state()
313
+ )
314
+ st.write('πŸ‘‹ Please [click here]({}) to log in and authorize the app.'.format(auth_url))
315
+
316
+ def generate_state():
317
+ state = secrets.token_urlsafe(32)
318
+ st.session_state['auth_state'] = state
319
+ return state
320
+
321
+ def is_token_valid(token):
322
+ if not token:
323
+ return False
324
+ try:
325
+ # Make a simple API call to check if the token is still valid
326
+ headers = {'Authorization': f'Bearer {token}'}
327
+ response = requests.get('https://graph.microsoft.com/v1.0/me', headers=headers)
328
+ return response.status_code == 200
329
+ except:
330
+ return False
331
 
332
  # πŸƒβ€β™‚οΈ Main application function
333
  def main():
 
335
 
336
  process_query_params()
337
 
338
+
339
+ # Check if we have a valid access token
340
+ if 'access_token' not in st.session_state or not is_token_valid(st.session_state.get('access_token')):
341
+ # If not, initiate the login process
342
+ initiate_auth_flow()
343
+ st.stop()
344
+
345
+ # If we have a valid token, proceed with the app
346
+ access_token = st.session_state['access_token']
347
+
348
+
349
  #added
350
  if 'access_token' not in st.session_state:
351
  client_instance = get_msal_app()