Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import streamlit as st
|
|
| 3 |
import requests
|
| 4 |
import msal
|
| 5 |
import secrets
|
|
|
|
| 6 |
|
| 7 |
# Configuration
|
| 8 |
APPLICATION_ID = os.getenv('APPLICATION_ID_KEY')
|
|
@@ -11,6 +12,12 @@ AUTHORITY = 'https://login.microsoftonline.com/common'
|
|
| 11 |
REDIRECT_URI = 'https://huggingface.co/spaces/awacke1/MSGraphAPI'
|
| 12 |
SCOPES = ['User.Read', 'Calendars.ReadWrite', 'Mail.ReadWrite']
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# MSAL setup
|
| 15 |
def get_msal_app():
|
| 16 |
return msal.ConfidentialClientApplication(
|
|
@@ -23,7 +30,8 @@ def get_msal_app():
|
|
| 23 |
def generate_auth_url():
|
| 24 |
msal_app = get_msal_app()
|
| 25 |
state = secrets.token_urlsafe(32)
|
| 26 |
-
st.session_state
|
|
|
|
| 27 |
return msal_app.get_authorization_request_url(
|
| 28 |
scopes=SCOPES,
|
| 29 |
redirect_uri=REDIRECT_URI,
|
|
@@ -66,13 +74,20 @@ def make_api_call(endpoint, token):
|
|
| 66 |
def main():
|
| 67 |
st.title("🦄 MS Graph API Integration")
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Check for authentication
|
| 70 |
token = get_token_from_cache()
|
| 71 |
|
| 72 |
if 'code' in st.query_params:
|
| 73 |
-
|
| 74 |
-
if
|
| 75 |
-
st.error("Invalid state parameter.
|
|
|
|
| 76 |
st.session_state.clear()
|
| 77 |
st.rerun()
|
| 78 |
|
|
|
|
| 3 |
import requests
|
| 4 |
import msal
|
| 5 |
import secrets
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
# Configuration
|
| 9 |
APPLICATION_ID = os.getenv('APPLICATION_ID_KEY')
|
|
|
|
| 12 |
REDIRECT_URI = 'https://huggingface.co/spaces/awacke1/MSGraphAPI'
|
| 13 |
SCOPES = ['User.Read', 'Calendars.ReadWrite', 'Mail.ReadWrite']
|
| 14 |
|
| 15 |
+
# Initialize session state
|
| 16 |
+
if 'auth_state' not in st.session_state:
|
| 17 |
+
st.session_state.auth_state = None
|
| 18 |
+
if 'auth_state_time' not in st.session_state:
|
| 19 |
+
st.session_state.auth_state_time = None
|
| 20 |
+
|
| 21 |
# MSAL setup
|
| 22 |
def get_msal_app():
|
| 23 |
return msal.ConfidentialClientApplication(
|
|
|
|
| 30 |
def generate_auth_url():
|
| 31 |
msal_app = get_msal_app()
|
| 32 |
state = secrets.token_urlsafe(32)
|
| 33 |
+
st.session_state.auth_state = state
|
| 34 |
+
st.session_state.auth_state_time = time.time()
|
| 35 |
return msal_app.get_authorization_request_url(
|
| 36 |
scopes=SCOPES,
|
| 37 |
redirect_uri=REDIRECT_URI,
|
|
|
|
| 74 |
def main():
|
| 75 |
st.title("🦄 MS Graph API Integration")
|
| 76 |
|
| 77 |
+
# Debug information
|
| 78 |
+
st.sidebar.write("Debug Info:")
|
| 79 |
+
st.sidebar.write(f"Auth State: {st.session_state.auth_state}")
|
| 80 |
+
st.sidebar.write(f"Auth State Time: {st.session_state.auth_state_time}")
|
| 81 |
+
st.sidebar.write(f"Query Params: {st.query_params}")
|
| 82 |
+
|
| 83 |
# Check for authentication
|
| 84 |
token = get_token_from_cache()
|
| 85 |
|
| 86 |
if 'code' in st.query_params:
|
| 87 |
+
received_state = st.query_params.get('state')
|
| 88 |
+
if received_state != st.session_state.auth_state:
|
| 89 |
+
st.error(f"Invalid state parameter. Expected {st.session_state.auth_state}, got {received_state}")
|
| 90 |
+
st.error("Please try logging in again.")
|
| 91 |
st.session_state.clear()
|
| 92 |
st.rerun()
|
| 93 |
|