Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import base64
|
|
| 7 |
import hashlib
|
| 8 |
import secrets
|
| 9 |
|
| 10 |
-
#
|
| 11 |
APPLICATION_ID_KEY = os.getenv('APPLICATION_ID_KEY')
|
| 12 |
CLIENT_SECRET_KEY = os.getenv('CLIENT_SECRET_KEY')
|
| 13 |
AUTHORITY_URL = 'https://login.microsoftonline.com/common'
|
|
@@ -33,29 +33,28 @@ PRODUCT_SCOPES = {
|
|
| 33 |
"π Azure OpenAI Service": ['AzureAIServices.ReadWrite.All']
|
| 34 |
}
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
# Function to generate PKCE code verifier and challenge
|
| 40 |
def generate_pkce_codes():
|
| 41 |
code_verifier = secrets.token_urlsafe(128)[:128]
|
| 42 |
code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode()).digest()).decode().rstrip('=')
|
| 43 |
return code_verifier, code_challenge
|
| 44 |
|
| 45 |
-
def get_msal_app(
|
| 46 |
return msal.PublicClientApplication(
|
| 47 |
client_id=APPLICATION_ID_KEY,
|
| 48 |
authority=AUTHORITY_URL
|
| 49 |
)
|
| 50 |
|
| 51 |
-
# Function to get access token
|
| 52 |
def get_access_token(code, code_verifier):
|
| 53 |
client_instance = get_msal_app()
|
| 54 |
|
| 55 |
try:
|
| 56 |
result = client_instance.acquire_token_by_authorization_code(
|
| 57 |
code=code,
|
| 58 |
-
scopes=st.session_state.get('
|
| 59 |
redirect_uri=REDIRECT_URI,
|
| 60 |
code_verifier=code_verifier
|
| 61 |
)
|
|
@@ -69,11 +68,9 @@ def get_access_token(code, code_verifier):
|
|
| 69 |
st.error(f"Exception in get_access_token: {str(e)}")
|
| 70 |
raise
|
| 71 |
|
| 72 |
-
# Main application function
|
| 73 |
def main():
|
| 74 |
st.title("π¦ MS Graph API with AI & Cloud Integration for M365")
|
| 75 |
|
| 76 |
-
# Sidebar for product selection
|
| 77 |
st.sidebar.title("π M365 Products")
|
| 78 |
st.sidebar.write("Select products to integrate:")
|
| 79 |
|
|
@@ -84,13 +81,14 @@ def main():
|
|
| 84 |
selected_products[product] = True
|
| 85 |
|
| 86 |
# Dynamically build scopes based on selected products
|
| 87 |
-
|
| 88 |
for product in selected_products:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
# Authentication flow
|
| 94 |
if 'access_token' not in st.session_state:
|
| 95 |
if 'code_verifier' not in st.session_state:
|
| 96 |
code_verifier, code_challenge = generate_pkce_codes()
|
|
@@ -101,14 +99,13 @@ def main():
|
|
| 101 |
|
| 102 |
client_instance = get_msal_app()
|
| 103 |
auth_url = client_instance.get_authorization_request_url(
|
| 104 |
-
scopes=scopes
|
| 105 |
redirect_uri=REDIRECT_URI,
|
| 106 |
code_challenge=code_challenge,
|
| 107 |
code_challenge_method="S256"
|
| 108 |
)
|
| 109 |
st.write('π Please [click here]({}) to log in and authorize the app.'.format(auth_url))
|
| 110 |
|
| 111 |
-
# Check for authorization code in query parameters
|
| 112 |
query_params = st.query_params
|
| 113 |
if 'code' in query_params:
|
| 114 |
code = query_params.get('code')
|
|
@@ -123,15 +120,12 @@ def main():
|
|
| 123 |
st.error(f"Error acquiring access token: {str(e)}")
|
| 124 |
st.stop()
|
| 125 |
else:
|
| 126 |
-
# User is authenticated, proceed with the app
|
| 127 |
access_token = st.session_state['access_token']
|
| 128 |
|
| 129 |
-
# Greet the user
|
| 130 |
user_info = get_user_info(access_token)
|
| 131 |
if user_info:
|
| 132 |
st.sidebar.write(f"π Hello, {user_info.get('displayName', 'User')}!")
|
| 133 |
|
| 134 |
-
# Handle selected products
|
| 135 |
if selected_products:
|
| 136 |
st.header("π§© M365 Product Integrations")
|
| 137 |
for product in selected_products:
|
|
@@ -140,7 +134,6 @@ def main():
|
|
| 140 |
else:
|
| 141 |
st.write("No products selected. Please select products from the sidebar.")
|
| 142 |
|
| 143 |
-
# Function to get user info
|
| 144 |
def get_user_info(access_token):
|
| 145 |
headers = {'Authorization': f'Bearer {access_token}'}
|
| 146 |
response = requests.get('https://graph.microsoft.com/v1.0/me', headers=headers)
|
|
@@ -150,13 +143,9 @@ def get_user_info(access_token):
|
|
| 150 |
st.error('Failed to fetch user info.')
|
| 151 |
return None
|
| 152 |
|
| 153 |
-
# Function to handle product integration
|
| 154 |
def handle_product_integration(access_token, product):
|
| 155 |
-
# Implement the integration logic for each product here
|
| 156 |
-
# This is a placeholder - you'll need to implement the actual API calls
|
| 157 |
st.write(f"Integrating {product}...")
|
| 158 |
-
#
|
| 159 |
|
| 160 |
-
# Run the main function
|
| 161 |
if __name__ == "__main__":
|
| 162 |
main()
|
|
|
|
| 7 |
import hashlib
|
| 8 |
import secrets
|
| 9 |
|
| 10 |
+
# Configuration
|
| 11 |
APPLICATION_ID_KEY = os.getenv('APPLICATION_ID_KEY')
|
| 12 |
CLIENT_SECRET_KEY = os.getenv('CLIENT_SECRET_KEY')
|
| 13 |
AUTHORITY_URL = 'https://login.microsoftonline.com/common'
|
|
|
|
| 33 |
"π Azure OpenAI Service": ['AzureAIServices.ReadWrite.All']
|
| 34 |
}
|
| 35 |
|
| 36 |
+
# Separate reserved scopes
|
| 37 |
+
RESERVED_SCOPES = ['openid', 'profile', 'offline_access']
|
| 38 |
+
BASE_SCOPES = ['User.Read'] # Non-reserved base scopes
|
| 39 |
|
|
|
|
| 40 |
def generate_pkce_codes():
|
| 41 |
code_verifier = secrets.token_urlsafe(128)[:128]
|
| 42 |
code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode()).digest()).decode().rstrip('=')
|
| 43 |
return code_verifier, code_challenge
|
| 44 |
|
| 45 |
+
def get_msal_app():
|
| 46 |
return msal.PublicClientApplication(
|
| 47 |
client_id=APPLICATION_ID_KEY,
|
| 48 |
authority=AUTHORITY_URL
|
| 49 |
)
|
| 50 |
|
|
|
|
| 51 |
def get_access_token(code, code_verifier):
|
| 52 |
client_instance = get_msal_app()
|
| 53 |
|
| 54 |
try:
|
| 55 |
result = client_instance.acquire_token_by_authorization_code(
|
| 56 |
code=code,
|
| 57 |
+
scopes=st.session_state.get('all_scopes', BASE_SCOPES + RESERVED_SCOPES),
|
| 58 |
redirect_uri=REDIRECT_URI,
|
| 59 |
code_verifier=code_verifier
|
| 60 |
)
|
|
|
|
| 68 |
st.error(f"Exception in get_access_token: {str(e)}")
|
| 69 |
raise
|
| 70 |
|
|
|
|
| 71 |
def main():
|
| 72 |
st.title("π¦ MS Graph API with AI & Cloud Integration for M365")
|
| 73 |
|
|
|
|
| 74 |
st.sidebar.title("π M365 Products")
|
| 75 |
st.sidebar.write("Select products to integrate:")
|
| 76 |
|
|
|
|
| 81 |
selected_products[product] = True
|
| 82 |
|
| 83 |
# Dynamically build scopes based on selected products
|
| 84 |
+
request_scopes = BASE_SCOPES.copy()
|
| 85 |
for product in selected_products:
|
| 86 |
+
request_scopes.extend(PRODUCT_SCOPES[product])
|
| 87 |
+
request_scopes = list(set(request_scopes)) # Remove duplicates
|
| 88 |
+
|
| 89 |
+
# Store all scopes (including reserved) for token acquisition
|
| 90 |
+
st.session_state['all_scopes'] = request_scopes + RESERVED_SCOPES
|
| 91 |
|
|
|
|
| 92 |
if 'access_token' not in st.session_state:
|
| 93 |
if 'code_verifier' not in st.session_state:
|
| 94 |
code_verifier, code_challenge = generate_pkce_codes()
|
|
|
|
| 99 |
|
| 100 |
client_instance = get_msal_app()
|
| 101 |
auth_url = client_instance.get_authorization_request_url(
|
| 102 |
+
scopes=request_scopes, # Use only non-reserved scopes for the auth request
|
| 103 |
redirect_uri=REDIRECT_URI,
|
| 104 |
code_challenge=code_challenge,
|
| 105 |
code_challenge_method="S256"
|
| 106 |
)
|
| 107 |
st.write('π Please [click here]({}) to log in and authorize the app.'.format(auth_url))
|
| 108 |
|
|
|
|
| 109 |
query_params = st.query_params
|
| 110 |
if 'code' in query_params:
|
| 111 |
code = query_params.get('code')
|
|
|
|
| 120 |
st.error(f"Error acquiring access token: {str(e)}")
|
| 121 |
st.stop()
|
| 122 |
else:
|
|
|
|
| 123 |
access_token = st.session_state['access_token']
|
| 124 |
|
|
|
|
| 125 |
user_info = get_user_info(access_token)
|
| 126 |
if user_info:
|
| 127 |
st.sidebar.write(f"π Hello, {user_info.get('displayName', 'User')}!")
|
| 128 |
|
|
|
|
| 129 |
if selected_products:
|
| 130 |
st.header("π§© M365 Product Integrations")
|
| 131 |
for product in selected_products:
|
|
|
|
| 134 |
else:
|
| 135 |
st.write("No products selected. Please select products from the sidebar.")
|
| 136 |
|
|
|
|
| 137 |
def get_user_info(access_token):
|
| 138 |
headers = {'Authorization': f'Bearer {access_token}'}
|
| 139 |
response = requests.get('https://graph.microsoft.com/v1.0/me', headers=headers)
|
|
|
|
| 143 |
st.error('Failed to fetch user info.')
|
| 144 |
return None
|
| 145 |
|
|
|
|
| 146 |
def handle_product_integration(access_token, product):
|
|
|
|
|
|
|
| 147 |
st.write(f"Integrating {product}...")
|
| 148 |
+
# Implement product-specific API calls here
|
| 149 |
|
|
|
|
| 150 |
if __name__ == "__main__":
|
| 151 |
main()
|